github.com/gotranspile/cxgo@v0.3.7/runtime/libc/string_test.go (about)

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