Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tests #10

Merged
merged 6 commits into from
Nov 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
60 changes: 60 additions & 0 deletions adap_go_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package gp

import (
"testing"
)

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

for _, tt := range tests {
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) {
setupTest(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 {
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) {
setupTest(t)
input := "test string"
cstr := AllocCStrDontFree(input)
got := GoString(cstr)
if got != input {
t.Errorf("AllocCStrDontFree() = %v, want %v", got, input)
}
}
1 change: 1 addition & 0 deletions bool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
)

func TestBool(t *testing.T) {
setupTest(t)
// Test MakeBool
b1 := MakeBool(true)
if !b1.Bool() {
Expand Down
3 changes: 3 additions & 0 deletions bytes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
)

func TestBytesCreation(t *testing.T) {
setupTest(t)
// Test BytesFromStr
b1 := BytesFromStr("hello")
if string(b1.Bytes()) != "hello" {
Expand All @@ -21,6 +22,7 @@ func TestBytesCreation(t *testing.T) {
}

func TestBytesDecode(t *testing.T) {
setupTest(t)
// Test UTF-8 decode
b := BytesFromStr("你好")
if !bytes.Equal(b.Bytes(), []byte("你好")) {
Expand All @@ -40,6 +42,7 @@ func TestBytesDecode(t *testing.T) {
}

func TestBytesConversion(t *testing.T) {
setupTest(t)
original := []byte{0x48, 0x65, 0x6c, 0x6c, 0x6f} // "Hello" in hex
b := MakeBytes(original)

Expand Down
31 changes: 16 additions & 15 deletions complex_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
)

func TestComplex(t *testing.T) {
setupTest(t)
tests := []struct {
name string
input complex128
Expand Down Expand Up @@ -38,28 +39,27 @@ func TestComplex(t *testing.T) {
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := MakeComplex(tt.input)
c := MakeComplex(tt.input)

// Test Real() method
if got := c.Real(); got != tt.wantReal {
t.Errorf("Complex.Real() = %v, want %v", got, tt.wantReal)
}
// Test Real() method
if got := c.Real(); got != tt.wantReal {
t.Errorf("Complex.Real() = %v, want %v", got, tt.wantReal)
}

// Test Imag() method
if got := c.Imag(); got != tt.wantImag {
t.Errorf("Complex.Imag() = %v, want %v", got, tt.wantImag)
}
// Test Imag() method
if got := c.Imag(); got != tt.wantImag {
t.Errorf("Complex.Imag() = %v, want %v", got, tt.wantImag)
}

// Test Complex128() method
if got := c.Complex128(); got != tt.input {
t.Errorf("Complex.Complex128() = %v, want %v", got, tt.input)
}
})
// Test Complex128() method
if got := c.Complex128(); got != tt.input {
t.Errorf("Complex.Complex128() = %v, want %v", got, tt.input)
}
}
}

func TestComplexZeroValue(t *testing.T) {
setupTest(t)
// Create a proper zero complex number instead of using zero-value struct
c := MakeComplex(complex(0, 0))

Expand All @@ -76,6 +76,7 @@ func TestComplexZeroValue(t *testing.T) {
}

func TestComplexNilHandling(t *testing.T) {
setupTest(t)
var c Complex // zero-value struct with nil pointer
defer func() {
if r := recover(); r == nil {
Expand Down
5 changes: 5 additions & 0 deletions dict.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ func MakeDict(m map[any]any) Dict {
return dict
}

func (d Dict) Has(key any) bool {
keyObj := From(key)
return C.PyDict_Contains(d.obj, keyObj.obj) != 0
}

func (d Dict) Get(key Objecter) Object {
v := C.PyDict_GetItem(d.obj, key.Obj())
C.Py_IncRef(v)
Expand Down
52 changes: 27 additions & 25 deletions dict_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ import (
)

func TestDictFromPairs(t *testing.T) {
setupTest(t)
// Add panic test case
t.Run("odd number of arguments", func(t *testing.T) {
func() {
defer func() {
if r := recover(); r == nil {
t.Errorf("DictFromPairs() with odd number of arguments should panic")
Expand All @@ -16,7 +17,7 @@ func TestDictFromPairs(t *testing.T) {
}()

DictFromPairs("key1", "value1", "key2") // Should panic
})
}()

tests := []struct {
name string
Expand All @@ -39,23 +40,22 @@ func TestDictFromPairs(t *testing.T) {
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
dict := DictFromPairs(tt.pairs...)

// Verify each key-value pair
for i := 0; i < len(tt.wantKeys); i++ {
key := From(tt.wantKeys[i])
val := dict.Get(key)
if !ObjectsAreEqual(val, From(tt.wantVals[i])) {
t.Errorf("DictFromPairs() got value %v for key %v, want %v",
val, tt.wantKeys[i], tt.wantVals[i])
}
dict := DictFromPairs(tt.pairs...)

// Verify each key-value pair
for i := 0; i < len(tt.wantKeys); i++ {
key := From(tt.wantKeys[i])
val := dict.Get(key)
if !ObjectsAreEqual(val, From(tt.wantVals[i])) {
t.Errorf("DictFromPairs() got value %v for key %v, want %v",
val, tt.wantKeys[i], tt.wantVals[i])
}
})
}
}
}

func TestMakeDict(t *testing.T) {
setupTest(t)
tests := []struct {
name string
m map[any]any
Expand All @@ -78,22 +78,21 @@ func TestMakeDict(t *testing.T) {
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
dict := MakeDict(tt.m)

// Verify each key-value pair
for k, v := range tt.m {
key := From(k)
got := dict.Get(key)
if !ObjectsAreEqual(got, From(v)) {
t.Errorf("MakeDict() got value %v for key %v, want %v", got, k, v)
}
dict := MakeDict(tt.m)

// Verify each key-value pair
for k, v := range tt.m {
key := From(k)
got := dict.Get(key)
if !ObjectsAreEqual(got, From(v)) {
t.Errorf("MakeDict() got value %v for key %v, want %v", got, k, v)
}
})
}
}
}

func TestDictSetGet(t *testing.T) {
setupTest(t)
dict := DictFromPairs()

// Test Set and Get
Expand All @@ -108,6 +107,7 @@ func TestDictSetGet(t *testing.T) {
}

func TestDictSetGetString(t *testing.T) {
setupTest(t)
dict := DictFromPairs()

// Test SetString and GetString
Expand All @@ -121,6 +121,7 @@ func TestDictSetGetString(t *testing.T) {
}

func TestDictDel(t *testing.T) {
setupTest(t)
dict := DictFromPairs("test_key", "test_value")
key := From("test_key")

Expand All @@ -141,6 +142,7 @@ func TestDictDel(t *testing.T) {
}

func TestDictForEach(t *testing.T) {
setupTest(t)
dict := DictFromPairs(
"key1", "value1",
"key2", "value2",
Expand Down
13 changes: 7 additions & 6 deletions float_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import (
)

func TestFloat(t *testing.T) {
t.Run("MakeFloat and conversions", func(t *testing.T) {
setupTest(t)
func() {
// Test creating float and converting back
f := MakeFloat(3.14159)

Expand All @@ -18,9 +19,9 @@ func TestFloat(t *testing.T) {
if got := f.Float32(); float64(got) != float64(float32(3.14159)) {
t.Errorf("Float32() = %v, want %v", got, float32(3.14159))
}
})
}()

t.Run("IsInteger", func(t *testing.T) {
func() {
// Test integer float
intFloat := MakeFloat(5.0)

Expand All @@ -34,9 +35,9 @@ func TestFloat(t *testing.T) {
if fracFloat.IsInteger().Bool() {
t.Errorf("IsInteger() for 5.5 = true, want false")
}
})
}()

t.Run("Zero and special values", func(t *testing.T) {
func() {
// Test zero
zero := MakeFloat(0.0)

Expand All @@ -50,5 +51,5 @@ func TestFloat(t *testing.T) {
if got := large.Float64(); got != 1e308 {
t.Errorf("Float64() = %v, want 1e308", got)
}
})
}()
}
2 changes: 2 additions & 0 deletions function_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ func (t *TestStruct) TestMethod() int {
}

func TestAddType(t *testing.T) {
setupTest(t)
m := MainModule()

// test add type
Expand Down Expand Up @@ -121,6 +122,7 @@ func (i *InitTestStruct) Init(val int) {
}

func TestAddTypeWithInit(t *testing.T) {
setupTest(t)
m := MainModule()

typ := AddType[InitTestStruct](m, (*InitTestStruct).Init, "InitTestStruct", "Test init struct")
Expand Down
18 changes: 9 additions & 9 deletions kw_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
)

func TestSplitArgs(t *testing.T) {
setupTest(t)
tests := []struct {
name string
args []any
Expand Down Expand Up @@ -39,21 +40,20 @@ func TestSplitArgs(t *testing.T) {
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gotTup, gotKw := splitArgs(tt.args...)
gotTup, gotKw := splitArgs(tt.args...)

if !reflect.DeepEqual(gotTup, tt.wantTup) {
t.Errorf("splitArgs() tuple = %v, want %v", gotTup, tt.wantTup)
}
if !reflect.DeepEqual(gotTup, tt.wantTup) {
t.Errorf("splitArgs() tuple = %v, want %v", gotTup, tt.wantTup)
}

if !reflect.DeepEqual(gotKw, tt.wantKw) {
t.Errorf("splitArgs() kwargs = %v, want %v", gotKw, tt.wantKw)
}
})
if !reflect.DeepEqual(gotKw, tt.wantKw) {
t.Errorf("splitArgs() kwargs = %v, want %v", gotKw, tt.wantKw)
}
}
}

func TestKwArgs(t *testing.T) {
setupTest(t)
kw := KwArgs{
"name": "test",
"age": 42,
Expand Down
Loading
Loading