github.com/gotranspile/cxgo@v0.3.7/runtime/libc/wstring_test.go (about) 1 package libc 2 3 import ( 4 "testing" 5 6 "github.com/stretchr/testify/require" 7 ) 8 9 func TestCWString(t *testing.T) { 10 const s = "abcабв" 11 p := CWString(s) 12 v := GoWString(p) 13 require.Equal(t, s, v) 14 } 15 16 func TestWStrLen(t *testing.T) { 17 n := int(WStrLen(nil)) 18 require.Equal(t, 0, n) 19 20 b := make([]uint16, 10) 21 22 n = int(WStrLen(&b[0])) 23 require.Equal(t, 0, n) 24 25 b[0] = 'a' 26 n = int(WStrLen(&b[0])) 27 require.Equal(t, 1, n) 28 29 b[1] = 'b' 30 b[2] = 'c' 31 n = int(WStrLen(&b[0])) 32 require.Equal(t, 3, n) 33 34 b[0] = 0 35 n = int(WStrLen(&b[0])) 36 require.Equal(t, 0, n) 37 n = int(WStrLen(&b[1])) 38 require.Equal(t, 2, n) 39 40 n = int(WStrLen(CWString("\x00"))) 41 require.Equal(t, 0, n) 42 43 n = int(WStrLen(CWString("abc"))) 44 require.Equal(t, 3, n) 45 } 46 47 /* 48 func TestStrChr(t *testing.T) { 49 b := []byte("abcded\x00") 50 51 p := StrChr(&b[0], 'd') 52 require.Equal(t, &b[3], p) 53 54 p = StrChr(&b[0], 'f') 55 require.Equal(t, (*byte)(nil), p) 56 57 b[3] = 0 58 p = StrChr(&b[0], 'e') 59 require.Equal(t, (*byte)(nil), p) 60 61 p = StrChr(nil, 'e') 62 require.Equal(t, (*byte)(nil), p) 63 } 64 65 func TestStrCpy(t *testing.T) { 66 a := []byte("0000000000\x00") 67 b := []byte("abcded\x00") 68 69 // Copies the C string pointed by source into the array pointed by destination, 70 // including the terminating null character (and stopping at that point). 71 p := StrCpy(&a[0], &b[0]) 72 require.Equal(t, &a[0], p) 73 require.Equal(t, "abcded\x00000\x00", string(a)) 74 } 75 */ 76 77 func wrepeat(c WChar, n int) []WChar { 78 b := make([]WChar, n) 79 for i := range b { 80 b[i] = c 81 } 82 return b 83 } 84 85 func TestWStrNCpy(t *testing.T) { 86 a := wrepeat('0', 10) 87 b := []WChar{'a', 'b', 'c', 'd', 'e', 'd', 0} 88 89 // No null-character is implicitly appended at the end of destination if source is longer than num. 90 p := WStrNCpy(&a[0], &b[0], 3) 91 require.Equal(t, &a[0], p) 92 require.Equal(t, []WChar{'a', 'b', 'c', '0', '0', '0', '0', '0', '0', '0'}, a) 93 94 // If the end of the source C string (which is signaled by a null-character) is found before num characters have been copied, 95 // destination is padded with zeros until a total of num characters have been written to it. 96 p = WStrNCpy(&a[0], &b[0], uint32(len(b)+1)) 97 require.Equal(t, &a[0], p) 98 require.Equal(t, []WChar{'a', 'b', 'c', 'd', 'e', 'd', 0, 0, '0', '0'}, a) 99 } 100 101 func TestWStrCat(t *testing.T) { 102 a := wrepeat('0', 10) 103 a[0] = '1' 104 a[1] = 0 105 a[9] = 0 106 b := []WChar{'a', 'b', 'c', 'd', 'e', 'd', 0} 107 108 p := WStrCat(&a[0], &b[0]) 109 require.Equal(t, &a[0], p) 110 require.Equal(t, []WChar{'1', 'a', 'b', 'c', 'd', 'e', 'd', 0, '0', 0}, a) 111 } 112 113 /* 114 func TestStrNCat(t *testing.T) { 115 a := []byte("0\x00000000000\x00") 116 b := []byte("abcded\x00") 117 118 p := StrNCat(&a[0], &b[0], 3) 119 require.Equal(t, &a[0], p) 120 require.Equal(t, "0abc\x00000000\x00", string(a)) 121 122 a = []byte("0\x00000000000\x00") 123 p = StrNCat(&a[0], &b[0], 10) 124 require.Equal(t, &a[0], p) 125 require.Equal(t, "0abcded\x00000\x00", string(a)) 126 } 127 128 func TestStrTok(t *testing.T) { 129 a := []byte("- This, a sample string.\x00") 130 toks := []byte(" ,.-\x00") 131 132 var ( 133 lines []string 134 ptrs []*byte 135 ) 136 for p := StrTok(&a[0], &toks[0]); p != nil; p = StrTok(nil, &toks[0]) { 137 lines = append(lines, GoString(p)) 138 ptrs = append(ptrs, p) 139 } 140 // should split words without delimiters 141 require.Equal(t, []string{ 142 "This", 143 "a", 144 "sample", 145 "string", 146 }, lines) 147 // should point to the same string 148 require.Equal(t, []*byte{ 149 &a[2], 150 &a[8], 151 &a[10], 152 &a[17], 153 }, ptrs) 154 // ...which means it should insert zero bytes into it 155 require.Equal(t, "- This\x00 a\x00sample\x00string\x00\x00", string(a)) 156 } 157 158 func TestStrSpn(t *testing.T) { 159 a := []byte("123abc\x00") 160 toks := []byte("1234567890\x00") 161 i := StrSpn(&a[0], &toks[0]) 162 require.Equal(t, 3, int(i)) 163 } 164 165 func TestStrStr(t *testing.T) { 166 a := []byte("123abc\x00") 167 b := []byte("3a\x00") 168 p := StrStr(&a[0], &b[0]) 169 require.Equal(t, &a[2], p) 170 } 171 172 func TestStrDup(t *testing.T) { 173 a := []byte("123abc\x00") 174 p := StrDup(&a[0]) 175 require.True(t, &a[0] != p) 176 b := BytesN(p, len(a)) 177 require.Equal(t, string(a), string(b)) 178 } 179 */