|
| 1 | +package gp |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | +) |
| 6 | + |
| 7 | +func TestTupleCreation(t *testing.T) { |
| 8 | + // Test empty tuple |
| 9 | + empty := MakeTupleWithLen(0) |
| 10 | + if empty.Len() != 0 { |
| 11 | + t.Errorf("Expected empty tuple length 0, got %d", empty.Len()) |
| 12 | + } |
| 13 | + |
| 14 | + // Test tuple with values |
| 15 | + tuple := MakeTuple(42, "hello", 3.14) |
| 16 | + if tuple.Len() != 3 { |
| 17 | + t.Errorf("Expected tuple length 3, got %d", tuple.Len()) |
| 18 | + } |
| 19 | +} |
| 20 | + |
| 21 | +func TestTupleGetSet(t *testing.T) { |
| 22 | + tuple := MakeTupleWithLen(2) |
| 23 | + |
| 24 | + // Test setting and getting values |
| 25 | + tuple.Set(0, From(123)) |
| 26 | + tuple.Set(1, From("test")) |
| 27 | + |
| 28 | + if val := tuple.Get(0).AsLong().Int64(); val != 123 { |
| 29 | + t.Errorf("Expected 123, got %d", val) |
| 30 | + } |
| 31 | + if val := tuple.Get(1).AsStr().String(); val != "test" { |
| 32 | + t.Errorf("Expected 'test', got %s", val) |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +func TestTupleSlice(t *testing.T) { |
| 37 | + tuple := MakeTuple(1, 2, 3, 4, 5) |
| 38 | + |
| 39 | + // Test slicing |
| 40 | + slice := tuple.Slice(1, 4) |
| 41 | + if slice.Len() != 3 { |
| 42 | + t.Errorf("Expected slice length 3, got %d", slice.Len()) |
| 43 | + } |
| 44 | + |
| 45 | + expected := []int64{2, 3, 4} |
| 46 | + for i := 0; i < slice.Len(); i++ { |
| 47 | + if val := slice.Get(i).AsLong().Int64(); val != expected[i] { |
| 48 | + t.Errorf("At index %d: expected %d, got %d", i, expected[i], val) |
| 49 | + } |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +func TestTupleParseArgs(t *testing.T) { |
| 54 | + tuple := MakeTuple(42, "hello", 3.14, true) |
| 55 | + |
| 56 | + var ( |
| 57 | + intVal int |
| 58 | + strVal string |
| 59 | + floatVal float64 |
| 60 | + boolVal bool |
| 61 | + extraVal int // This shouldn't get set |
| 62 | + ) |
| 63 | + |
| 64 | + // Test successful parsing |
| 65 | + success := tuple.ParseArgs(&intVal, &strVal, &floatVal, &boolVal) |
| 66 | + if !success { |
| 67 | + t.Error("ParseArgs failed unexpectedly") |
| 68 | + } |
| 69 | + |
| 70 | + if intVal != 42 { |
| 71 | + t.Errorf("Expected int 42, got %d", intVal) |
| 72 | + } |
| 73 | + if strVal != "hello" { |
| 74 | + t.Errorf("Expected string 'hello', got %s", strVal) |
| 75 | + } |
| 76 | + if floatVal != 3.14 { |
| 77 | + t.Errorf("Expected float 3.14, got %f", floatVal) |
| 78 | + } |
| 79 | + if !boolVal { |
| 80 | + t.Errorf("Expected bool true, got false") |
| 81 | + } |
| 82 | + |
| 83 | + // Test parsing with too many arguments |
| 84 | + success = tuple.ParseArgs(&intVal, &strVal, &floatVal, &boolVal, &extraVal) |
| 85 | + if success { |
| 86 | + t.Error("ParseArgs should have failed with too many arguments") |
| 87 | + } |
| 88 | + |
| 89 | + // Test parsing with invalid type |
| 90 | + var invalidPtr *testing.T |
| 91 | + success = tuple.ParseArgs(&invalidPtr) |
| 92 | + if success { |
| 93 | + t.Error("ParseArgs should have failed with invalid type") |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +func TestTupleParseArgsTypes(t *testing.T) { |
| 98 | + // Test all supported numeric types |
| 99 | + tuple := MakeTuple(42, 42, 42, 42, 42, 42, 42, 42, 42, 42) |
| 100 | + |
| 101 | + var ( |
| 102 | + intVal int |
| 103 | + int8Val int8 |
| 104 | + int16Val int16 |
| 105 | + int32Val int32 |
| 106 | + int64Val int64 |
| 107 | + uintVal uint |
| 108 | + uint8Val uint8 |
| 109 | + uint16Val uint16 |
| 110 | + uint32Val uint32 |
| 111 | + uint64Val uint64 |
| 112 | + ) |
| 113 | + |
| 114 | + success := tuple.ParseArgs( |
| 115 | + &intVal, &int8Val, &int16Val, &int32Val, &int64Val, |
| 116 | + &uintVal, &uint8Val, &uint16Val, &uint32Val, &uint64Val, |
| 117 | + ) |
| 118 | + |
| 119 | + if !success { |
| 120 | + t.Error("ParseArgs failed for numeric types") |
| 121 | + } |
| 122 | + |
| 123 | + // Test floating point types |
| 124 | + floatTuple := MakeTuple(3.14, 3.14) |
| 125 | + var float32Val float32 |
| 126 | + var float64Val float64 |
| 127 | + |
| 128 | + success = floatTuple.ParseArgs(&float32Val, &float64Val) |
| 129 | + if !success { |
| 130 | + t.Error("ParseArgs failed for floating point types") |
| 131 | + } |
| 132 | + |
| 133 | + // Test complex types |
| 134 | + complexTuple := MakeTuple(complex(1, 2), complex(3, 4)) |
| 135 | + var complex64Val complex64 |
| 136 | + var complex128Val complex128 |
| 137 | + |
| 138 | + success = complexTuple.ParseArgs(&complex64Val, &complex128Val) |
| 139 | + if !success { |
| 140 | + t.Error("ParseArgs failed for complex types") |
| 141 | + } |
| 142 | + |
| 143 | + // Test string and bytes |
| 144 | + strTuple := MakeTuple("hello") |
| 145 | + var strVal string |
| 146 | + var bytesVal []byte |
| 147 | + var objVal Object |
| 148 | + var pyObj *PyObject |
| 149 | + |
| 150 | + success = strTuple.ParseArgs(&strVal) |
| 151 | + if !success || strVal != "hello" { |
| 152 | + t.Error("ParseArgs failed for string type") |
| 153 | + } |
| 154 | + |
| 155 | + success = strTuple.ParseArgs(&bytesVal) |
| 156 | + if !success || string(bytesVal) != "hello" { |
| 157 | + t.Error("ParseArgs failed for bytes type") |
| 158 | + } |
| 159 | + |
| 160 | + success = strTuple.ParseArgs(&objVal) |
| 161 | + if !success || !objVal.IsStr() { |
| 162 | + t.Error("ParseArgs failed for object type") |
| 163 | + } |
| 164 | + |
| 165 | + success = strTuple.ParseArgs(&pyObj) |
| 166 | + if !success || pyObj == nil { |
| 167 | + t.Error("ParseArgs failed for PyObject type") |
| 168 | + } |
| 169 | + str := FromPy(pyObj) |
| 170 | + if !str.IsStr() || str.String() != "hello" { |
| 171 | + t.Error("FromPy returned non-string object") |
| 172 | + } |
| 173 | +} |
0 commit comments