orderedmap_test.go 7.35 KB
Newer Older
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
package orderedmap

import (
	"encoding/json"
	"slices"
	"testing"
)

func TestMap_BasicOperations(t *testing.T) {
	m := New[string, int]()

	// Test empty map
	if m.Len() != 0 {
		t.Errorf("expected Len() = 0, got %d", m.Len())
	}
	v, ok := m.Get("a")
	if ok {
		t.Error("expected Get on empty map to return false")
	}
	if v != 0 {
		t.Errorf("expected zero value, got %d", v)
	}

	// Test Set and Get
	m.Set("a", 1)
	m.Set("b", 2)
	m.Set("c", 3)

	if m.Len() != 3 {
		t.Errorf("expected Len() = 3, got %d", m.Len())
	}

	v, ok = m.Get("a")
	if !ok || v != 1 {
		t.Errorf("expected Get(a) = (1, true), got (%d, %v)", v, ok)
	}

	v, ok = m.Get("b")
	if !ok || v != 2 {
		t.Errorf("expected Get(b) = (2, true), got (%d, %v)", v, ok)
	}

	v, ok = m.Get("c")
	if !ok || v != 3 {
		t.Errorf("expected Get(c) = (3, true), got (%d, %v)", v, ok)
	}

	// Test updating existing key preserves position
	m.Set("a", 10)
	v, ok = m.Get("a")
	if !ok || v != 10 {
		t.Errorf("expected Get(a) = (10, true), got (%d, %v)", v, ok)
	}
	if m.Len() != 3 {
		t.Errorf("expected Len() = 3 after update, got %d", m.Len())
	}
}

func TestMap_InsertionOrderPreserved(t *testing.T) {
	m := New[string, int]()

	// Insert in non-alphabetical order
	m.Set("z", 1)
	m.Set("a", 2)
	m.Set("m", 3)
	m.Set("b", 4)

	// Verify iteration order matches insertion order
	var keys []string
	var values []int
	for k, v := range m.All() {
		keys = append(keys, k)
		values = append(values, v)
	}

	expectedKeys := []string{"z", "a", "m", "b"}
	expectedValues := []int{1, 2, 3, 4}

	if !slices.Equal(keys, expectedKeys) {
		t.Errorf("expected keys %v, got %v", expectedKeys, keys)
	}
	if !slices.Equal(values, expectedValues) {
		t.Errorf("expected values %v, got %v", expectedValues, values)
	}
}

func TestMap_UpdatePreservesPosition(t *testing.T) {
	m := New[string, int]()

	m.Set("first", 1)
	m.Set("second", 2)
	m.Set("third", 3)

	// Update middle element
	m.Set("second", 20)

	var keys []string
	for k := range m.All() {
		keys = append(keys, k)
	}

	// Order should still be first, second, third
	expected := []string{"first", "second", "third"}
	if !slices.Equal(keys, expected) {
		t.Errorf("expected keys %v, got %v", expected, keys)
	}
}

func TestMap_MarshalJSON_PreservesOrder(t *testing.T) {
	m := New[string, int]()

	// Insert in non-alphabetical order
	m.Set("z", 1)
	m.Set("a", 2)
	m.Set("m", 3)

	data, err := json.Marshal(m)
	if err != nil {
		t.Fatalf("Marshal failed: %v", err)
	}

	// JSON should preserve insertion order, not alphabetical
	expected := `{"z":1,"a":2,"m":3}`
	if string(data) != expected {
		t.Errorf("expected %s, got %s", expected, string(data))
	}
}

func TestMap_UnmarshalJSON_PreservesOrder(t *testing.T) {
	// JSON with non-alphabetical key order
	jsonData := `{"z":1,"a":2,"m":3}`

	m := New[string, int]()
	if err := json.Unmarshal([]byte(jsonData), m); err != nil {
		t.Fatalf("Unmarshal failed: %v", err)
	}

	// Verify iteration order matches JSON order
	var keys []string
	for k := range m.All() {
		keys = append(keys, k)
	}

	expected := []string{"z", "a", "m"}
	if !slices.Equal(keys, expected) {
		t.Errorf("expected keys %v, got %v", expected, keys)
	}
}

func TestMap_JSONRoundTrip(t *testing.T) {
	// Test that unmarshal -> marshal produces identical JSON
	original := `{"zebra":"z","apple":"a","mango":"m","banana":"b"}`

	m := New[string, string]()
	if err := json.Unmarshal([]byte(original), m); err != nil {
		t.Fatalf("Unmarshal failed: %v", err)
	}

	data, err := json.Marshal(m)
	if err != nil {
		t.Fatalf("Marshal failed: %v", err)
	}

	if string(data) != original {
		t.Errorf("round trip failed: expected %s, got %s", original, string(data))
	}
}

func TestMap_ToMap(t *testing.T) {
	m := New[string, int]()
	m.Set("a", 1)
	m.Set("b", 2)

	regular := m.ToMap()

	if len(regular) != 2 {
		t.Errorf("expected len 2, got %d", len(regular))
	}
	if regular["a"] != 1 {
		t.Errorf("expected regular[a] = 1, got %d", regular["a"])
	}
	if regular["b"] != 2 {
		t.Errorf("expected regular[b] = 2, got %d", regular["b"])
	}
}

func TestMap_NilSafety(t *testing.T) {
	var m *Map[string, int]

	// All operations should be safe on nil
	if m.Len() != 0 {
		t.Errorf("expected Len() = 0 on nil map, got %d", m.Len())
	}

	v, ok := m.Get("a")
	if ok {
		t.Error("expected Get on nil map to return false")
	}
	if v != 0 {
		t.Errorf("expected zero value from nil map, got %d", v)
	}

	// Set on nil is a no-op
	m.Set("a", 1)
	if m.Len() != 0 {
		t.Errorf("expected Len() = 0 after Set on nil, got %d", m.Len())
	}

	// All returns empty iterator
	var keys []string
	for k := range m.All() {
		keys = append(keys, k)
	}
	if len(keys) != 0 {
		t.Errorf("expected empty iteration on nil map, got %v", keys)
	}

	// ToMap returns nil
	if m.ToMap() != nil {
		t.Error("expected ToMap to return nil on nil map")
	}

	// MarshalJSON returns null
	data, err := json.Marshal(m)
	if err != nil {
		t.Fatalf("Marshal failed: %v", err)
	}
	if string(data) != "null" {
		t.Errorf("expected null, got %s", string(data))
	}
}

func TestMap_EmptyMapMarshal(t *testing.T) {
	m := New[string, int]()

	data, err := json.Marshal(m)
	if err != nil {
		t.Fatalf("Marshal failed: %v", err)
	}
	if string(data) != "{}" {
		t.Errorf("expected {}, got %s", string(data))
	}
}

func TestMap_NestedValues(t *testing.T) {
	m := New[string, any]()
	m.Set("string", "hello")
	m.Set("number", 42)
	m.Set("bool", true)
	m.Set("nested", map[string]int{"x": 1})

	data, err := json.Marshal(m)
	if err != nil {
		t.Fatalf("Marshal failed: %v", err)
	}

	expected := `{"string":"hello","number":42,"bool":true,"nested":{"x":1}}`
	if string(data) != expected {
		t.Errorf("expected %s, got %s", expected, string(data))
	}
}

func TestMap_AllIteratorEarlyExit(t *testing.T) {
	m := New[string, int]()
	m.Set("a", 1)
	m.Set("b", 2)
	m.Set("c", 3)
	m.Set("d", 4)

	// Collect only first 2
	var keys []string
	for k := range m.All() {
		keys = append(keys, k)
		if len(keys) == 2 {
			break
		}
	}

	expected := []string{"a", "b"}
	if !slices.Equal(keys, expected) {
		t.Errorf("expected %v, got %v", expected, keys)
	}
}

func TestMap_IntegerKeys(t *testing.T) {
	m := New[int, string]()
	m.Set(3, "three")
	m.Set(1, "one")
	m.Set(2, "two")

	var keys []int
	for k := range m.All() {
		keys = append(keys, k)
	}

	// Should preserve insertion order, not numerical order
	expected := []int{3, 1, 2}
	if !slices.Equal(keys, expected) {
		t.Errorf("expected %v, got %v", expected, keys)
	}
}

func TestMap_UnmarshalIntoExisting(t *testing.T) {
	m := New[string, int]()
	m.Set("existing", 999)

	// Unmarshal should replace contents
	if err := json.Unmarshal([]byte(`{"new":1}`), m); err != nil {
		t.Fatalf("Unmarshal failed: %v", err)
	}

	_, ok := m.Get("existing")
	if ok {
		t.Error("existing key should be gone after unmarshal")
	}

	v, ok := m.Get("new")
	if !ok || v != 1 {
		t.Errorf("expected Get(new) = (1, true), got (%d, %v)", v, ok)
	}
}

func TestMap_LargeOrderPreservation(t *testing.T) {
	m := New[string, int]()

	// Create many keys in specific order
	keys := make([]string, 100)
	for i := range 100 {
		keys[i] = string(rune('a' + (99 - i))) // reverse order: 'd', 'c', 'b', 'a' (extended)
		if i >= 26 {
			keys[i] = string(rune('A'+i-26)) + string(rune('a'+i%26))
		}
	}

	for i, k := range keys {
		m.Set(k, i)
	}

	// Verify order preserved
	var resultKeys []string
	for k := range m.All() {
		resultKeys = append(resultKeys, k)
	}

	if !slices.Equal(keys, resultKeys) {
		t.Error("large map should preserve insertion order")
	}
}