1
2
3
4
5 package main
6
7 import (
8 "./a"
9 "fmt"
10 "math"
11 "sort"
12 )
13
14 var m1 = map[int]int{1: 2, 2: 4, 4: 8, 8: 16}
15 var m2 = map[int]string{1: "2", 2: "4", 4: "8", 8: "16"}
16
17 func TestKeys() {
18 want := []int{1, 2, 4, 8}
19
20 got1 := a.Keys(m1)
21 sort.Ints(got1)
22 if !a.SliceEqual(got1, want) {
23 panic(fmt.Sprintf("a.Keys(%v) = %v, want %v", m1, got1, want))
24 }
25
26 got2 := a.Keys(m2)
27 sort.Ints(got2)
28 if !a.SliceEqual(got2, want) {
29 panic(fmt.Sprintf("a.Keys(%v) = %v, want %v", m2, got2, want))
30 }
31 }
32
33 func TestValues() {
34 got1 := a.Values(m1)
35 want1 := []int{2, 4, 8, 16}
36 sort.Ints(got1)
37 if !a.SliceEqual(got1, want1) {
38 panic(fmt.Sprintf("a.Values(%v) = %v, want %v", m1, got1, want1))
39 }
40
41 got2 := a.Values(m2)
42 want2 := []string{"16", "2", "4", "8"}
43 sort.Strings(got2)
44 if !a.SliceEqual(got2, want2) {
45 panic(fmt.Sprintf("a.Values(%v) = %v, want %v", m2, got2, want2))
46 }
47 }
48
49 func TestEqual() {
50 if !a.Equal(m1, m1) {
51 panic(fmt.Sprintf("a.Equal(%v, %v) = false, want true", m1, m1))
52 }
53 if a.Equal(m1, nil) {
54 panic(fmt.Sprintf("a.Equal(%v, nil) = true, want false", m1))
55 }
56 if a.Equal(nil, m1) {
57 panic(fmt.Sprintf("a.Equal(nil, %v) = true, want false", m1))
58 }
59 if !a.Equal[int, int](nil, nil) {
60 panic("a.Equal(nil, nil) = false, want true")
61 }
62 if ms := map[int]int{1: 2}; a.Equal(m1, ms) {
63 panic(fmt.Sprintf("a.Equal(%v, %v) = true, want false", m1, ms))
64 }
65
66
67 mf := map[int]float64{1: 0, 2: math.NaN()}
68 if a.Equal(mf, mf) {
69 panic(fmt.Sprintf("a.Equal(%v, %v) = true, want false", mf, mf))
70 }
71 }
72
73 func TestCopy() {
74 m2 := a.Copy(m1)
75 if !a.Equal(m1, m2) {
76 panic(fmt.Sprintf("a.Copy(%v) = %v, want %v", m1, m2, m1))
77 }
78 m2[16] = 32
79 if a.Equal(m1, m2) {
80 panic(fmt.Sprintf("a.Equal(%v, %v) = true, want false", m1, m2))
81 }
82 }
83
84 func TestAdd() {
85 mc := a.Copy(m1)
86 a.Add(mc, mc)
87 if !a.Equal(mc, m1) {
88 panic(fmt.Sprintf("a.Add(%v, %v) = %v, want %v", m1, m1, mc, m1))
89 }
90 a.Add(mc, map[int]int{16: 32})
91 want := map[int]int{1: 2, 2: 4, 4: 8, 8: 16, 16: 32}
92 if !a.Equal(mc, want) {
93 panic(fmt.Sprintf("a.Add result = %v, want %v", mc, want))
94 }
95 }
96
97 func TestSub() {
98 mc := a.Copy(m1)
99 a.Sub(mc, mc)
100 if len(mc) > 0 {
101 panic(fmt.Sprintf("a.Sub(%v, %v) = %v, want empty map", m1, m1, mc))
102 }
103 mc = a.Copy(m1)
104 a.Sub(mc, map[int]int{1: 0})
105 want := map[int]int{2: 4, 4: 8, 8: 16}
106 if !a.Equal(mc, want) {
107 panic(fmt.Sprintf("a.Sub result = %v, want %v", mc, want))
108 }
109 }
110
111 func TestIntersect() {
112 mc := a.Copy(m1)
113 a.Intersect(mc, mc)
114 if !a.Equal(mc, m1) {
115 panic(fmt.Sprintf("a.Intersect(%v, %v) = %v, want %v", m1, m1, mc, m1))
116 }
117 a.Intersect(mc, map[int]int{1: 0, 2: 0})
118 want := map[int]int{1: 2, 2: 4}
119 if !a.Equal(mc, want) {
120 panic(fmt.Sprintf("a.Intersect result = %v, want %v", mc, want))
121 }
122 }
123
124 func TestFilter() {
125 mc := a.Copy(m1)
126 a.Filter(mc, func(int, int) bool { return true })
127 if !a.Equal(mc, m1) {
128 panic(fmt.Sprintf("a.Filter(%v, true) = %v, want %v", m1, mc, m1))
129 }
130 a.Filter(mc, func(k, v int) bool { return k < 3 })
131 want := map[int]int{1: 2, 2: 4}
132 if !a.Equal(mc, want) {
133 panic(fmt.Sprintf("a.Filter result = %v, want %v", mc, want))
134 }
135 }
136
137 func TestTransformValues() {
138 mc := a.Copy(m1)
139 a.TransformValues(mc, func(i int) int { return i / 2 })
140 want := map[int]int{1: 1, 2: 2, 4: 4, 8: 8}
141 if !a.Equal(mc, want) {
142 panic(fmt.Sprintf("a.TransformValues result = %v, want %v", mc, want))
143 }
144 }
145
146 func main() {
147 TestKeys()
148 TestValues()
149 TestEqual()
150 TestCopy()
151 TestAdd()
152 TestSub()
153 TestIntersect()
154 TestFilter()
155 TestTransformValues()
156 }
157
View as plain text