Source file
src/maps/iter_test.go
Documentation: maps
1
2
3
4
5 package maps
6
7 import (
8 "slices"
9 "testing"
10 )
11
12 func TestAll(t *testing.T) {
13 for size := 0; size < 10; size++ {
14 m := make(map[int]int)
15 for i := range size {
16 m[i] = i
17 }
18 cnt := 0
19 for i, v := range All(m) {
20 v1, ok := m[i]
21 if !ok || v != v1 {
22 t.Errorf("at iteration %d got %d, %d want %d, %d", cnt, i, v, i, v1)
23 }
24 cnt++
25 }
26 if cnt != size {
27 t.Errorf("read %d values expected %d", cnt, size)
28 }
29 }
30 }
31
32 func TestKeys(t *testing.T) {
33 for size := 0; size < 10; size++ {
34 var want []int
35 m := make(map[int]int)
36 for i := range size {
37 m[i] = i
38 want = append(want, i)
39 }
40
41 var got []int
42 for k := range Keys(m) {
43 got = append(got, k)
44 }
45 slices.Sort(got)
46 if !slices.Equal(got, want) {
47 t.Errorf("Keys(%v) = %v, want %v", m, got, want)
48 }
49 }
50 }
51
52 func TestValues(t *testing.T) {
53 for size := 0; size < 10; size++ {
54 var want []int
55 m := make(map[int]int)
56 for i := range size {
57 m[i] = i
58 want = append(want, i)
59 }
60
61 var got []int
62 for v := range Values(m) {
63 got = append(got, v)
64 }
65 slices.Sort(got)
66 if !slices.Equal(got, want) {
67 t.Errorf("Values(%v) = %v, want %v", m, got, want)
68 }
69 }
70 }
71
72 func TestInsert(t *testing.T) {
73 got := map[int]int{
74 1: 1,
75 2: 1,
76 }
77 Insert(got, func(yield func(int, int) bool) {
78 for i := 0; i < 10; i += 2 {
79 if !yield(i, i+1) {
80 return
81 }
82 }
83 })
84
85 want := map[int]int{
86 1: 1,
87 2: 1,
88 }
89 for i, v := range map[int]int{
90 0: 1,
91 2: 3,
92 4: 5,
93 6: 7,
94 8: 9,
95 } {
96 want[i] = v
97 }
98
99 if !Equal(got, want) {
100 t.Errorf("Insert got: %v, want: %v", got, want)
101 }
102 }
103
104 func TestCollect(t *testing.T) {
105 m := map[int]int{
106 0: 1,
107 2: 3,
108 4: 5,
109 6: 7,
110 8: 9,
111 }
112 got := Collect(All(m))
113 if !Equal(got, m) {
114 t.Errorf("Collect got: %v, want: %v", got, m)
115 }
116 }
117
View as plain text