Skip to content

Commit

Permalink
add adap_go tests
Browse files Browse the repository at this point in the history
  • Loading branch information
cpunion committed Nov 3, 2024
1 parent 581c896 commit 124d41b
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 10 deletions.
10 changes: 0 additions & 10 deletions adap_go.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,6 @@ func AllocCStrDontFree(s string) *C.char {
return C.CString(s)
}

func AllocWCStr(s string) *C.wchar_t {
runes := []rune(s)
wchars := make([]uint16, len(runes)+1)
for i, r := range runes {
wchars[i] = uint16(r)
}
wchars[len(runes)] = 0
return (*C.wchar_t)(unsafe.Pointer(&wchars[0]))
}

func GoString(s *C.char) string {
return C.GoString((*C.char)(s))
}
Expand Down
61 changes: 61 additions & 0 deletions adap_go_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package gp

import (
"testing"
)

func TestAllocCStr(t *testing.T) {
tests := []struct {
name string
input string
want string
}{
{"empty string", "", ""},
{"ascii string", "hello", "hello"},
{"unicode string", "hello 世界", "hello 世界"},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cstr := AllocCStr(tt.input)
got := GoString(cstr)
if got != tt.want {
t.Errorf("AllocCStr() = %v, want %v", got, tt.want)
}
})
}
}

func TestGoStringN(t *testing.T) {
tests := []struct {
name string
input string
n int
want string
}{
{"empty string", "", 0, ""},
{"partial string", "hello", 3, "hel"},
{"full string", "hello", 5, "hello"},
{"unicode partial", "hello 世界", 6, "hello "},
{"unicode full", "hello 世界", 12, "hello 世界"},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cstr := AllocCStr(tt.input)
got := GoStringN(cstr, tt.n)
if got != tt.want {
t.Errorf("GoStringN() = %v, want %v", got, tt.want)
}
})
}
}

func TestAllocCStrDontFree(t *testing.T) {
input := "test string"
cstr := AllocCStrDontFree(input)
got := GoString(cstr)
if got != input {
t.Errorf("AllocCStrDontFree() = %v, want %v", got, input)
}
}

0 comments on commit 124d41b

Please sign in to comment.