-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtuple_test.go
178 lines (150 loc) · 3.99 KB
/
tuple_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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package gp
import (
"testing"
)
func TestTupleCreation(t *testing.T) {
setupTest(t)
// Test empty tuple
empty := MakeTupleWithLen(0)
if empty.Len() != 0 {
t.Errorf("Expected empty tuple length 0, got %d", empty.Len())
}
// Test tuple with values
tuple := MakeTuple(42, "hello", 3.14)
if tuple.Len() != 3 {
t.Errorf("Expected tuple length 3, got %d", tuple.Len())
}
}
func TestTupleGetSet(t *testing.T) {
setupTest(t)
tuple := MakeTupleWithLen(2)
// Test setting and getting values
tuple.Set(0, From(123))
tuple.Set(1, From("test"))
if val := tuple.Get(0).AsLong().Int64(); val != 123 {
t.Errorf("Expected 123, got %d", val)
}
if val := tuple.Get(1).AsStr().String(); val != "test" {
t.Errorf("Expected 'test', got %s", val)
}
}
func TestTupleSlice(t *testing.T) {
setupTest(t)
tuple := MakeTuple(1, 2, 3, 4, 5)
// Test slicing
slice := tuple.Slice(1, 4)
if slice.Len() != 3 {
t.Errorf("Expected slice length 3, got %d", slice.Len())
}
expected := []int64{2, 3, 4}
for i := 0; i < slice.Len(); i++ {
if val := slice.Get(i).AsLong().Int64(); val != expected[i] {
t.Errorf("At index %d: expected %d, got %d", i, expected[i], val)
}
}
}
func TestTupleParseArgs(t *testing.T) {
setupTest(t)
tuple := MakeTuple(42, "hello", 3.14, true)
var (
intVal int
strVal string
floatVal float64
boolVal bool
extraVal int // This shouldn't get set
)
// Test successful parsing
success := tuple.ParseArgs(&intVal, &strVal, &floatVal, &boolVal)
if !success {
t.Error("ParseArgs failed unexpectedly")
}
if intVal != 42 {
t.Errorf("Expected int 42, got %d", intVal)
}
if strVal != "hello" {
t.Errorf("Expected string 'hello', got %s", strVal)
}
if floatVal != 3.14 {
t.Errorf("Expected float 3.14, got %f", floatVal)
}
if !boolVal {
t.Errorf("Expected bool true, got false")
}
// Test parsing with too many arguments
success = tuple.ParseArgs(&intVal, &strVal, &floatVal, &boolVal, &extraVal)
if success {
t.Error("ParseArgs should have failed with too many arguments")
}
// Test parsing with invalid type
var invalidPtr *testing.T
success = tuple.ParseArgs(&invalidPtr)
if success {
t.Error("ParseArgs should have failed with invalid type")
}
}
func TestTupleParseArgsTypes(t *testing.T) {
setupTest(t)
// Test all supported numeric types
tuple := MakeTuple(42, 42, 42, 42, 42, 42, 42, 42, 42, 42)
var (
intVal int
int8Val int8
int16Val int16
int32Val int32
int64Val int64
uintVal uint
uint8Val uint8
uint16Val uint16
uint32Val uint32
uint64Val uint64
)
success := tuple.ParseArgs(
&intVal, &int8Val, &int16Val, &int32Val, &int64Val,
&uintVal, &uint8Val, &uint16Val, &uint32Val, &uint64Val,
)
if !success {
t.Error("ParseArgs failed for numeric types")
}
// Test floating point types
floatTuple := MakeTuple(3.14, 3.14)
var float32Val float32
var float64Val float64
success = floatTuple.ParseArgs(&float32Val, &float64Val)
if !success {
t.Error("ParseArgs failed for floating point types")
}
// Test complex types
complexTuple := MakeTuple(complex(1, 2), complex(3, 4))
var complex64Val complex64
var complex128Val complex128
success = complexTuple.ParseArgs(&complex64Val, &complex128Val)
if !success {
t.Error("ParseArgs failed for complex types")
}
// Test string and bytes
strTuple := MakeTuple("hello")
var strVal string
var bytesVal []byte
var objVal Object
var pyObj *cPyObject
success = strTuple.ParseArgs(&strVal)
if !success || strVal != "hello" {
t.Error("ParseArgs failed for string type")
}
success = strTuple.ParseArgs(&bytesVal)
if !success || string(bytesVal) != "hello" {
t.Error("ParseArgs failed for bytes type")
}
success = strTuple.ParseArgs(&objVal)
if !success || !objVal.IsStr() {
t.Error("ParseArgs failed for object type")
}
success = strTuple.ParseArgs(&pyObj)
if !success || pyObj == nil {
t.Error("ParseArgs failed for PyObject type")
}
str := FromPy(pyObj)
if !str.IsStr() || str.String() != "hello" {
t.Error("FromPy returned non-string object")
}
}