github.com/Ptt-official-app/go-bbs@v0.12.0/cstr_test.go (about)

     1  package bbs
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  )
     7  
     8  func TestCstrToBytes(t *testing.T) {
     9  
    10  	str1 := [13]byte{}
    11  	str2 := [13]byte{}
    12  	copy(str2[:], []byte("123"))
    13  	str3 := [10]byte{}
    14  	copy(str3[:], []byte("0123456789"))
    15  	str4 := [10]byte{}
    16  	copy(str4[:], []byte("01234\x006789"))
    17  
    18  	type args struct {
    19  		cstr Cstr
    20  	}
    21  	tests := []struct {
    22  		name     string
    23  		args     args
    24  		expected []byte
    25  	}{
    26  		{
    27  			name:     "init",
    28  			args:     args{str1[:]},
    29  			expected: []byte{},
    30  		},
    31  		{
    32  			name:     "with only 3 letters",
    33  			args:     args{str2[:]},
    34  			expected: []byte("123"),
    35  		},
    36  		{
    37  			name:     "with no 0",
    38  			args:     args{str3[:]},
    39  			expected: []byte("0123456789"),
    40  		},
    41  		{
    42  			name:     "cutoff at str4[5]",
    43  			args:     args{str4[:]},
    44  			expected: []byte("01234"),
    45  		},
    46  	}
    47  	for _, tt := range tests {
    48  		t.Run(tt.name, func(t *testing.T) {
    49  			if got := CstrToBytes(tt.args.cstr); !reflect.DeepEqual(got, tt.expected) {
    50  				t.Errorf("CstrToBytes() = %v, expected %v", got, tt.expected)
    51  			}
    52  		})
    53  	}
    54  }
    55  
    56  func TestCstrToString(t *testing.T) {
    57  
    58  	str1 := [13]byte{}
    59  	str2 := [13]byte{}
    60  	copy(str2[:], []byte("123"))
    61  	str3 := [10]byte{}
    62  	copy(str3[:], []byte("0123456789"))
    63  	str4 := [10]byte{}
    64  	copy(str4[:], []byte("01234\x006789"))
    65  
    66  	type args struct {
    67  		cstr Cstr
    68  	}
    69  	tests := []struct {
    70  		name     string
    71  		args     args
    72  		expected string
    73  	}{
    74  		// TODO: Add test cases.
    75  		{
    76  			name:     "init",
    77  			args:     args{str1[:]},
    78  			expected: "",
    79  		},
    80  		{
    81  			name:     "with only 3 letters",
    82  			args:     args{str2[:]},
    83  			expected: "123",
    84  		},
    85  		{
    86  			name:     "with no 0",
    87  			args:     args{str3[:]},
    88  			expected: "0123456789",
    89  		},
    90  		{
    91  			name:     "cutoff at str4[5]",
    92  			args:     args{str4[:]},
    93  			expected: "01234",
    94  		},
    95  	}
    96  	for _, tt := range tests {
    97  		t.Run(tt.name, func(t *testing.T) {
    98  			if got := CstrToString(tt.args.cstr); got != tt.expected {
    99  				t.Errorf("CstrToString() = %v, expected %v", got, tt.expected)
   100  			}
   101  		})
   102  	}
   103  }