forked from gotray/go-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
function_test.go
162 lines (136 loc) · 3.53 KB
/
function_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package gp
import (
"testing"
)
// TestStruct contains various types of fields for testing
type TestStruct struct {
// C-compatible basic types
BoolField bool
Int8Field int8
Int16Field int16
Int32Field int32
Int64Field int64
IntField int
Uint8Field uint8
Uint16Field uint16
Uint32Field uint32
Uint64Field uint64
UintField uint
Float32Field float32
Float64Field float64
Complex64Field complex64
Complex128Field complex128
// Non-C-compatible types
StringField string
SliceField []int
MapField map[string]int
StructField struct{ X int }
}
func (t *TestStruct) TestMethod() int {
return 42
}
func TestAddType(t *testing.T) {
Initialize()
defer Finalize()
m := MainModule()
// test add type
typ := AddType[TestStruct](m, nil, "TestStruct", "Test struct documentation")
if typ.Nil() {
t.Fatal("Failed to create type")
}
// test type by Python code
code := `
# create instance
obj = TestStruct()
# test C-compatible types
obj.bool_field = True
obj.int8_field = 127
obj.int16_field = 32767
obj.int32_field = 2147483647
obj.int64_field = 9223372036854775807
obj.int_field = 1234567890
obj.uint8_field = 255
obj.uint16_field = 65535
obj.uint32_field = 4294967295
obj.uint64_field = 18446744073709551615
obj.uint_field = 4294967295
obj.float32_field = 3.14
obj.float64_field = 3.14159265359
obj.complex64_field = 1.5 + 2.5j
obj.complex128_field = 3.14 + 2.718j
# test non-C-compatible types
obj.string_field = "test string"
assert obj.string_field == "test string"
obj.slice_field = [1, 2, 3]
assert obj.slice_field == [1, 2, 3]
obj.map_field = {"key": 42}
assert obj.map_field["key"] == 42
obj.struct_field = {"x": 100}
assert obj.struct_field["x"] == 100
# test method call
result = obj.test_method()
assert result == 42
# verify C-compatible types
assert obj.bool_field == True
assert obj.int8_field == 127
assert obj.int16_field == 32767
assert obj.int32_field == 2147483647
assert obj.int64_field == 9223372036854775807
assert obj.int_field == 1234567890
assert obj.uint8_field == 255
assert obj.uint16_field == 65535
assert obj.uint32_field == 4294967295
assert obj.uint64_field == 18446744073709551615
assert obj.uint_field == 4294967295
assert abs(obj.float32_field - 3.14) < 0.0001
assert abs(obj.float64_field - 3.14159265359) < 0.0000001
assert abs(obj.complex64_field - (1.5 + 2.5j)) < 0.0001
assert abs(obj.complex128_field - (3.14 + 2.718j)) < 0.0000001
# verify non-C-compatible types
assert obj.string_field == "test string"
assert obj.slice_field == [1, 2, 3]
assert obj.map_field["key"] == 42
assert obj.struct_field["x"] == 100
`
err := RunString(code)
if err != nil {
t.Fatalf("Test failed: %v", err)
}
}
type InitTestStruct struct {
Value int
}
func (i *InitTestStruct) Init(val int) {
i.Value = val
}
func TestAddTypeWithInit(t *testing.T) {
Initialize()
defer Finalize()
m := MainModule()
typ := AddType[InitTestStruct](m, (*InitTestStruct).Init, "InitTestStruct", "Test init struct")
if typ.Nil() {
t.Fatal("Failed to create type with init")
}
// test init function
code := `
# test init function
obj = InitTestStruct(42)
assert obj.value == 42
# test error handling without arguments
try:
obj2 = InitTestStruct()
assert False, "Should fail without arguments"
except TypeError as e:
pass
# test error handling with wrong argument type
try:
obj3 = InitTestStruct("wrong type")
assert False, "Should fail with wrong argument type"
except TypeError:
pass
`
err := RunString(code)
if err != nil {
t.Fatalf("Test failed: %v", err)
}
}