Repository navigation
Expand file tree
/
Copy pathsortedmap_test.go
More file actions
218 lines (193 loc) · 4.37 KB
/
Copy pathsortedmap_test.go
File metadata and controls
218 lines (193 loc) · 4.37 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
// Copyright 2018 the u-root Authors. All rights reserved
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package sortedmap
import (
"testing"
)
func TestInsert(t *testing.T) {
cases := []struct {
data map[int64]int64
keys []int64
}{
{data: map[int64]int64{1: 2, 3: 4}, keys: []int64{1, 3}},
{data: map[int64]int64{4: 0, 1: 0}, keys: []int64{1, 4}},
}
for _, c := range cases {
m := NewMap()
for k, v := range c.data {
m.Insert(k, v)
}
// All expected entries exist
for k, e := range c.data {
v, ok := m.m[k]
if !ok {
t.Errorf("%d not found in %v", k, m.m)
}
if v != e {
t.Errorf("got %d want %d in %v", v, e, m.m)
}
}
if len(m.k) != len(c.keys) {
t.Errorf("Bad length, got %d, expected %d. %v vs %v", len(m.k), len(c.keys), m.k, c.keys)
}
// Key slice is correct
for i, e := range c.keys {
if m.k[i] != e {
t.Errorf("Got %v, expected %v", m.k, c.keys)
break
}
}
}
}
func TestDelete(t *testing.T) {
cases := []struct {
before Map
del []int64
after Map
}{
{
before: Map{
m: map[int64]int64{1: 0, 2: 0},
k: sortedSlice{1, 2},
},
del: []int64{1},
after: Map{
m: map[int64]int64{2: 0},
k: sortedSlice{2},
},
},
{
before: Map{
m: map[int64]int64{1: 0, 2: 0},
k: sortedSlice{1, 2},
},
del: []int64{1, 2},
after: Map{
m: map[int64]int64{},
k: sortedSlice{},
},
},
}
for i := range cases {
for _, k := range cases[i].del {
cases[i].before.Delete(k)
}
// All expected entries exist
for k, e := range cases[i].after.m {
v, ok := cases[i].before.m[k]
if !ok {
t.Errorf("%d not found in %v", k, cases[i].before.m)
}
if v != e {
t.Errorf("got %d want %d in %v", v, e, cases[i].before.m)
}
}
if len(cases[i].before.k) != len(cases[i].after.k) {
t.Errorf("Bad length, got %d, expected %d. %v vs %v", len(cases[i].before.k), len(cases[i].after.k), cases[i].before.k, cases[i].after.k)
}
// Key slice is correct
for i, e := range cases[i].after.k {
if cases[i].before.k[i] != e {
t.Errorf("Got %v, expected %v", cases[i].before.k, cases[i].after.k)
break
}
}
}
}
func TestGet(t *testing.T) {
m := Map{
m: map[int64]int64{2: 20, 4: 40},
k: sortedSlice{2, 4},
}
// Simple lookup
var v int64
var ok bool
if v, ok = m.Get(2); !ok {
t.Errorf("want ok got false for Get(2)")
}
if v != 20 {
t.Errorf("want 20 got %d for Get(2)", v)
}
// Non-existant key
if _, ok = m.Get(3); ok {
t.Errorf("want not ok got ok for Get(3)")
}
}
func TestNearestLessEqual(t *testing.T) {
m := Map{
m: map[int64]int64{2: 20, 4: 40},
k: sortedSlice{2, 4},
}
// Nothing less than smallest
_, _, err := m.NearestLessEqual(1)
if err != ErrNoSuchKey {
t.Errorf("want err got nil for NLE(1)")
}
// Exact match
k, v, err := m.NearestLessEqual(2)
if err != nil {
t.Errorf("want nil got err for NLE(2): %v", err)
}
if k != 2 {
t.Errorf("bad key for NLE(2): want 2 got %d", k)
}
if v != 20 {
t.Errorf("bad value for NLE(2): want 20 got %d", k)
}
// One above
k, v, err = m.NearestLessEqual(3)
if err != nil {
t.Errorf("want nil got err for NLE(3): %v", err)
}
if k != 2 {
t.Errorf("bad key for NLE(3): want 2 got %d", k)
}
if v != 20 {
t.Errorf("bad value for NLE(3): want 20 got %d", k)
}
// Way above
k, v, err = m.NearestLessEqual(1000)
if err != nil {
t.Errorf("want nil got err for NLE(1000): %v", err)
}
if k != 4 {
t.Errorf("bad key for NLE(1000): want 4 got %d", k)
}
if v != 40 {
t.Errorf("bad value for NLE(1000): want 40 got %d", k)
}
}
func TestNearestGreaterEqual(t *testing.T) {
m := Map{
m: map[int64]int64{2: 20, 4: 40},
k: sortedSlice{2, 4},
}
// Nothing bigger than biggest
_, _, err := m.NearestGreater(5)
if err != ErrNoSuchKey {
t.Errorf("want ErrNoSuchKey got %v for NG(5)", err)
}
// One below
k, v, err := m.NearestGreater(3)
if err != nil {
t.Errorf("want nil got err for NG(3): %v", err)
}
if k != 4 {
t.Errorf("bad key for NG(3): want 4 got %d", k)
}
if v != 40 {
t.Errorf("bad value for NG(3): want 40 got %d", k)
}
// Way below
k, v, err = m.NearestGreater(-1000)
if err != nil {
t.Errorf("want nil got err for NG(-1000): %v", err)
}
if k != 2 {
t.Errorf("bad key for NG(-1000): want 2 got %d", k)
}
if v != 20 {
t.Errorf("bad value for NG(-1000): want 20 got %d", k)
}
}