-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathordered_map_test.go
166 lines (140 loc) · 4.33 KB
/
ordered_map_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
package mapsutil
import (
"encoding/json"
"fmt"
"reflect"
"strconv"
"testing"
"github.com/stretchr/testify/require"
)
func TestOrderedMapBasic(t *testing.T) {
m := NewOrderedMap[string, string]()
m.Set("test", "test")
if m.IsEmpty() {
t.Fatal("ordered map is empty")
}
if !m.Has("test") {
t.Fatal("ordered map doesn't have test key")
}
if m.Has("test2") {
t.Fatal("ordered map has test2 key")
}
if val, ok := m.Get("test"); !ok || val != "test" {
t.Fatal("ordered map get test key doesn't return test value")
}
if m.GetKeys()[0] != "test" {
t.Fatal("ordered map get keys doesn't return test key")
}
if val, ok := m.GetByIndex(0); !ok || val != "test" {
t.Fatal("ordered map get by index doesn't return test key")
}
m.Delete("test")
if !m.IsEmpty() {
t.Fatal("ordered map is not empty after delete")
}
}
func TestOrderedMap(t *testing.T) {
m := NewOrderedMap[string, string]()
for i := 0; i < 110; i++ {
m.Set(strconv.Itoa(i), fmt.Sprintf("value-%d", i))
}
// iterate and validate order
i := 0
m.Iterate(func(key string, value string) bool {
if key != strconv.Itoa(i) {
t.Fatal("ordered map iterate order is not correct")
}
i++
return true
})
// validate get by index
for i := 0; i < 100; i++ {
if val, ok := m.GetByIndex(i); !ok || val != fmt.Sprintf("value-%d", i) {
t.Fatal("ordered map get by index doesn't return correct value")
}
}
// random delete and validate order
deleteElements := []int{0, 10, 20, 30, 40, 50, 60, 70, 80, 90}
for _, i := range deleteElements {
m.Delete(strconv.Itoa(i))
}
// validate elements after delete
for k, i := range deleteElements {
if val, ok := m.GetByIndex(i); !ok || val != fmt.Sprintf("value-%d", i+k+1) {
t.Logf("order mismatch after delete got: index: %d, value: %s, exists: %v", i, val, ok)
}
}
}
func TestOrderedMapMarshalUnmarshal(t *testing.T) {
t.Run("TestSimpleStringToStringMapping", func(t *testing.T) {
orderedMap1 := NewOrderedMap[string, string]()
orderedMap1.Set("name", "John Doe")
orderedMap1.Set("occupation", "Software Developer")
marshaled1, err := json.Marshal(orderedMap1)
if err != nil {
t.Fatalf("Failed to marshal orderedMap1: %v", err)
}
unmarshaled1 := NewOrderedMap[string, string]()
err = json.Unmarshal(marshaled1, &unmarshaled1)
if err != nil {
t.Fatalf("Failed to unmarshal orderedMap1: %v", err)
}
if !reflect.DeepEqual(orderedMap1, unmarshaled1) {
t.Fatal("Unmarshaled map is not equal to the original map for orderedMap1")
}
})
t.Run("TestIntegerToStructMapping", func(t *testing.T) {
type Employee struct {
ID int `json:"id"`
Name string `json:"name"`
}
orderedMap2 := NewOrderedMap[int, Employee]()
orderedMap2.Set(1, Employee{ID: 1, Name: "Alice"})
orderedMap2.Set(2, Employee{ID: 2, Name: "Bob"})
marshaled2, err := json.Marshal(orderedMap2)
if err != nil {
t.Fatalf("Failed to marshal orderedMap2: %v", err)
}
unmarshaled2 := NewOrderedMap[int, Employee]()
err = json.Unmarshal(marshaled2, &unmarshaled2)
if err != nil {
t.Fatalf("Failed to unmarshal orderedMap2: %v", err)
}
if !reflect.DeepEqual(orderedMap2, unmarshaled2) {
t.Fatal("Unmarshaled map is not equal to the original map for orderedMap2")
}
})
t.Run("TestStringToSliceOfStringsMapping", func(t *testing.T) {
orderedMap3 := NewOrderedMap[string, []string]()
orderedMap3.Set("fruits", []string{"apple", "banana", "cherry"})
orderedMap3.Set("vegetables", []string{"tomato", "potato", "carrot"})
marshaled3, err := json.Marshal(orderedMap3)
if err != nil {
t.Fatalf("Failed to marshal orderedMap3: %v", err)
}
unmarshaled3 := NewOrderedMap[string, []string]()
err = json.Unmarshal(marshaled3, &unmarshaled3)
if err != nil {
t.Fatalf("Failed to unmarshal orderedMap3: %v", err)
}
if !reflect.DeepEqual(orderedMap3, unmarshaled3) {
t.Fatal("Unmarshaled map is not equal to the original map for orderedMap3")
}
})
}
func TestOrderedMapDeleteWhileIterating(t *testing.T) {
om := NewOrderedMap[string, string]()
om.Set("key1", "value1")
om.Set("key2", "value2")
om.Set("key3", "value3")
ignoreKey := "key1"
got := []string{}
om.Iterate(func(key string, value string) bool {
got = append(got, key)
if key == ignoreKey {
om.Delete(key)
}
return true
})
require.ElementsMatchf(t, []string{"key1", "key2", "key3"}, got, "inconsistent iteration order")
}