-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtype_map_test.go
More file actions
231 lines (193 loc) 路 5.8 KB
/
Copy pathtype_map_test.go
File metadata and controls
231 lines (193 loc) 路 5.8 KB
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
package parco
import (
"bytes"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestMapType_IntString(t *testing.T) {
tests := []struct {
name string
value map[int]string
}{
{"empty map", map[int]string{}},
{"single entry", map[int]string{1: "one"}},
{"small map", map[int]string{1: "one", 2: "two", 3: "three"}},
{"larger map", map[int]string{10: "ten", 20: "twenty", 30: "thirty", 40: "forty"}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
mapType := MapType[int, string](IntLEHeader(), IntLE(), Varchar())
var buf bytes.Buffer
// Test Compile
err := mapType.Compile(tt.value, &buf)
require.NoError(t, err)
// Test Parse
parsed, err := mapType.Parse(&buf)
require.NoError(t, err)
assert.Equal(t, tt.value, parsed)
})
}
}
func TestMapType_StringInt(t *testing.T) {
tests := []struct {
name string
value map[string]int
}{
{"empty map", map[string]int{}},
{"small map", map[string]int{"one": 1, "two": 2}},
{"larger map", map[string]int{"ten": 10, "twenty": 20, "thirty": 30}},
{"keys with spaces", map[string]int{"hello world": 1, "foo bar": 2}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
mapType := MapType[string, int](IntLEHeader(), Varchar(), IntLE())
var buf bytes.Buffer
// Test Compile
err := mapType.Compile(tt.value, &buf)
require.NoError(t, err)
// Test Parse
parsed, err := mapType.Parse(&buf)
require.NoError(t, err)
assert.Equal(t, tt.value, parsed)
})
}
}
func TestMapType_StringString(t *testing.T) {
tests := []struct {
name string
value map[string]string
}{
{"empty map", map[string]string{}},
{"single entry", map[string]string{"key": "value"}},
{"multiple entries", map[string]string{"name": "John", "age": "30", "city": "NYC"}},
{"empty values", map[string]string{"key1": "", "key2": "value"}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
mapType := MapType[string, string](UInt8Header(), SmallVarchar(), SmallVarchar())
var buf bytes.Buffer
err := mapType.Compile(tt.value, &buf)
require.NoError(t, err)
parsed, err := mapType.Parse(&buf)
require.NoError(t, err)
assert.Equal(t, tt.value, parsed)
})
}
}
func TestMapType_UInt16UInt8(t *testing.T) {
tests := []struct {
name string
value map[uint16]uint8
}{
{"empty map", map[uint16]uint8{}},
{"small map", map[uint16]uint8{1: 10, 2: 20, 3: 30}},
{"max values", map[uint16]uint8{65535: 255, 0: 0}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
mapType := MapType[uint16, uint8](UInt8Header(), UInt16LE(), UInt8())
var buf bytes.Buffer
err := mapType.Compile(tt.value, &buf)
require.NoError(t, err)
parsed, err := mapType.Parse(&buf)
require.NoError(t, err)
assert.Equal(t, tt.value, parsed)
})
}
}
func TestMapType_BoolInt(t *testing.T) {
tests := []struct {
name string
value map[bool]int
}{
{"empty map", map[bool]int{}},
{"single entry", map[bool]int{true: 1}},
{"both keys", map[bool]int{true: 1, false: 0}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
mapType := MapType[bool, int](UInt8Header(), Bool(), IntLE())
var buf bytes.Buffer
err := mapType.Compile(tt.value, &buf)
require.NoError(t, err)
parsed, err := mapType.Parse(&buf)
require.NoError(t, err)
assert.Equal(t, tt.value, parsed)
})
}
}
func TestMapType_ParseError_InsufficientBytesForHeader(t *testing.T) {
mapType := MapType[int, string](IntLEHeader(), IntLE(), Varchar())
// Not enough bytes for int header (need at least 4 or 8 bytes depending on platform)
buf := bytes.NewBuffer([]byte{0x01})
_, err := mapType.Parse(buf)
assert.Error(t, err)
}
func TestMapType_ParseError_InsufficientBytesForKey(t *testing.T) {
mapType := MapType[int, string](IntLEHeader(), IntLE(), Varchar())
// Header says 1 element, but not enough bytes for the key
// Assuming IntLE is 8 bytes, provide header but incomplete key
buf := bytes.NewBuffer([]byte{
0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // length = 1
0x01, // incomplete key (need 8 bytes)
})
_, err := mapType.Parse(buf)
assert.Error(t, err)
}
func TestMapType_ParseError_InsufficientBytesForValue(t *testing.T) {
mapType := MapType[uint8, string](UInt8Header(), UInt8(), SmallVarchar())
buf := bytes.NewBuffer([]byte{
0x01, // length = 1
0x05, // key = 5
0x03, // varchar length = 3 (using SmallVarchar with UInt8 header)
0x61, 0x62, // only 2 bytes of "abc" string
})
_, err := mapType.Parse(buf)
assert.Error(t, err)
}
func TestMapType_RoundTrip(t *testing.T) {
t.Run("int32 to float32", func(t *testing.T) {
value := map[int32]float32{
1: 1.5,
-1: -1.5,
0: 0.0,
}
mapType := MapType[int32, float32](UInt8Header(), Int32LE(), Float32LE())
var buf bytes.Buffer
err := mapType.Compile(value, &buf)
require.NoError(t, err)
parsed, err := mapType.Parse(&buf)
require.NoError(t, err)
assert.Equal(t, value, parsed)
})
t.Run("uint64 to bool", func(t *testing.T) {
value := map[uint64]bool{
0: false,
1: true,
1000: true,
}
mapType := MapType[uint64, bool](UInt16HeaderLE(), UInt64LE(), Bool())
var buf bytes.Buffer
err := mapType.Compile(value, &buf)
require.NoError(t, err)
parsed, err := mapType.Parse(&buf)
require.NoError(t, err)
assert.Equal(t, value, parsed)
})
}
func TestMapType_LargeMaps(t *testing.T) {
// Test with a larger map to ensure no issues with scaling
value := make(map[int]int)
for i := range 100 {
value[i] = i * 2
}
mapType := MapType[int, int](IntLEHeader(), IntLE(), IntLE())
var buf bytes.Buffer
err := mapType.Compile(value, &buf)
require.NoError(t, err)
parsed, err := mapType.Parse(&buf)
require.NoError(t, err)
assert.Equal(t, value, parsed)
assert.Equal(t, len(value), len(parsed))
}