Skip to content

Commit 6172301

Browse files
committed
iterator tests
1 parent 725ed20 commit 6172301

File tree

2 files changed

+54
-1
lines changed

2 files changed

+54
-1
lines changed

e2e_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ func TestGetOrSet(t *testing.T) {
208208
}
209209
}
210210

211-
func TestIterator(t *testing.T) {
211+
func TestForEach(t *testing.T) {
212212
m := New[int, *Animal]()
213213

214214
m.ForEach(func(i int, a *Animal) bool {

iterator_test.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
//go:build go1.23
2+
// +build go1.23
3+
4+
package haxmap
5+
6+
import (
7+
"testing"
8+
)
9+
10+
func TestIterators(t *testing.T) {
11+
type Value = struct {
12+
key int
13+
}
14+
15+
m := New[int, *Value]()
16+
17+
itemCount := 16
18+
for i := itemCount; i > 0; i-- {
19+
m.Set(i, &Value{i})
20+
}
21+
22+
t.Run("iterator", func(t *testing.T) {
23+
counter := 0
24+
for k, v := range m.Iterator() {
25+
if v == nil {
26+
t.Error("Expecting an object.")
27+
} else if k != v.key {
28+
t.Error("Incorrect key/value pairs")
29+
}
30+
31+
counter++
32+
}
33+
34+
if counter != itemCount {
35+
t.Error("Iterated item count did not match.")
36+
}
37+
})
38+
39+
t.Run("keys", func(t *testing.T) {
40+
counter := 0
41+
for k := range m.Keys() {
42+
_, ok := m.Get(k)
43+
if !ok {
44+
t.Error("The key is not is the map")
45+
}
46+
counter++
47+
}
48+
49+
if counter != itemCount {
50+
t.Error("Iterated item count did not match.")
51+
}
52+
})
53+
}

0 commit comments

Comments
 (0)