1
2
3
4
5 package sgutil
6
7 import (
8 "slices"
9 "testing"
10 )
11
12 func TestInsertMapBasic(t *testing.T) {
13 im := &InsertMap[string, int]{}
14
15
16 if im.Contains("A") {
17 t.Error("empty map should not contain key A")
18 }
19 if val := im.Get("A"); val != 0 {
20 t.Errorf("empty map Get(A) should return 0, got %d", val)
21 }
22
23
24 im.Put("A", 1)
25 im.Put("B", 2)
26 im.Put("C", 3)
27
28
29 if !im.Contains("A") || !im.Contains("B") || !im.Contains("C") {
30 t.Error("map should contain keys A, B, and C")
31 }
32 if im.Contains("D") {
33 t.Error("map should not contain key D")
34 }
35
36
37 if val := im.Get("A"); val != 1 {
38 t.Errorf("Get(A) expected 1, got %d", val)
39 }
40 if val := im.Get("B"); val != 2 {
41 t.Errorf("Get(B) expected 2, got %d", val)
42 }
43 if val := im.Get("C"); val != 3 {
44 t.Errorf("Get(C) expected 3, got %d", val)
45 }
46 if val := im.Get("D"); val != 0 {
47 t.Errorf("Get(D) expected 0, got %d", val)
48 }
49
50
51 if cmp := im.Compare("A", "B"); cmp != -1 {
52 t.Errorf("Compare(A, B) expected -1, got %d", cmp)
53 }
54 if cmp := im.Compare("B", "A"); cmp != 1 {
55 t.Errorf("Compare(B, A) expected 1, got %d", cmp)
56 }
57 if cmp := im.Compare("A", "A"); cmp != 0 {
58 t.Errorf("Compare(A, A) expected 0, got %d", cmp)
59 }
60 if cmp := im.Compare("A", "D"); cmp != -1 {
61 t.Errorf("Compare(A, D) expected -1, got %d", cmp)
62 }
63 if cmp := im.Compare("D", "A"); cmp != 1 {
64 t.Errorf("Compare(D, A) expected 1, got %d", cmp)
65 }
66 if cmp := im.Compare("D", "E"); cmp != 0 {
67 t.Errorf("Compare(D, E) expected 0, got %d", cmp)
68 }
69 }
70
71 func TestInsertMapUpdate(t *testing.T) {
72 im := &InsertMap[string, int]{}
73
74 im.Put("A", 1)
75 im.Put("B", 2)
76
77
78 im.Put("A", 10)
79
80 if val := im.Get("A"); val != 10 {
81 t.Errorf("Get(A) after update expected 10, got %d", val)
82 }
83
84
85 if cmp := im.Compare("A", "B"); cmp != -1 {
86 t.Errorf("Compare(A, B) after update expected -1, got %d", cmp)
87 }
88
89
90 if len(im.v) != 2 {
91 t.Errorf("expected internal slice length 2, got %d. Slice content: %v", len(im.v), im.v)
92 }
93 }
94
95 func TestInsertMapIterators(t *testing.T) {
96 im := &InsertMap[string, int]{}
97 im.Put("A", 1)
98 im.Put("B", 2)
99 im.Put("C", 3)
100
101
102 var keys []string
103 for k := range im.Keys() {
104 keys = append(keys, k)
105 }
106 expectedKeys := []string{"A", "B", "C"}
107 if !slices.Equal(keys, expectedKeys) {
108 t.Errorf("Keys() got %v, expected %v", keys, expectedKeys)
109 }
110
111
112 var values []int
113 for v := range im.Values() {
114 values = append(values, v)
115 }
116 expectedValues := []int{1, 2, 3}
117 if !slices.Equal(values, expectedValues) {
118 t.Errorf("Values() got %v, expected %v", values, expectedValues)
119 }
120
121
122 var allKeys []string
123 var allValues []int
124 for k, v := range im.All() {
125 allKeys = append(allKeys, k)
126 allValues = append(allValues, v)
127 }
128 if !slices.Equal(allKeys, expectedKeys) {
129 t.Errorf("All() keys got %v, expected %v", allKeys, expectedKeys)
130 }
131 if !slices.Equal(allValues, expectedValues) {
132 t.Errorf("All() values got %v, expected %v", allValues, expectedValues)
133 }
134 }
135
View as plain text