/
find_test.go
173 lines (160 loc) · 4.29 KB
/
find_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
167
168
169
170
171
172
173
package slicey_test
import (
"testing"
"github.com/micvbang/go-helpy/inty"
"github.com/micvbang/go-helpy/slicey"
)
// TestFindSimpleValue verifies that Find finds the expected values in a slice
// of simple values.
func TestFindSimpleValue(t *testing.T) {
tests := map[string]struct {
haystack []int
needle int
expectedExists bool
}{
"nil haystack": {
haystack: nil,
expectedExists: false,
},
"empty haystack": {
haystack: []int{},
expectedExists: false,
},
"one element exists": {
haystack: []int{42},
needle: 42,
expectedExists: true,
},
"one element, doesn't exist": {
haystack: []int{1337},
needle: 42,
expectedExists: false,
},
"multiple elements, exists first": {
haystack: []int{42, 1337, -1500, 17, 71, 18821390293},
needle: 42,
expectedExists: true,
},
"multiple elements, exists middle": {
haystack: []int{1337, -1500, 42, 17, 71, 18821390293},
needle: 42,
expectedExists: true,
},
"multiple elements, exists last": {
haystack: []int{1337, -1500, 17, 71, 18821390293, 42},
needle: 42,
expectedExists: true,
},
"multiple elements, doesn't exist": {
haystack: []int{-42, 1337, -1500, 17, 71, 18821390293},
needle: 42,
expectedExists: false,
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
got, gotExists := slicey.Find(test.haystack, func(v int) bool {
return test.needle == v
})
if gotExists != test.expectedExists {
t.Errorf("expected exist %v, got %v", test.expectedExists, gotExists)
}
if test.expectedExists && got != test.needle {
t.Errorf("expected to find %v, found %v", test.needle, got)
}
})
}
}
// TestFindStruct verifies that Find finds the expected values in a slice of
// complex types.
func TestFindStruct(t *testing.T) {
type complex struct {
v string
sidecar int
}
n := func(v string) complex {
return complex{
v: v,
sidecar: inty.Random(),
}
}
tests := map[string]struct {
haystack []complex
needle complex
expectedExists bool
}{
"nil haystack": {
haystack: nil,
expectedExists: false,
},
"empty haystack": {
haystack: []complex{},
expectedExists: false,
},
"one element exists": {
haystack: []complex{n("yes")},
needle: n("yes"),
expectedExists: true,
},
"one element, doesn't exist": {
haystack: []complex{n("1337")},
needle: n("42"),
expectedExists: false,
},
"multiple elements, exists": {
haystack: []complex{n("me"), n("nah"), n("me")},
needle: n("me"),
expectedExists: true,
},
"multiple elements, exists first": {
haystack: []complex{n("me"), n("nah"), n("sup")},
needle: n("me"),
expectedExists: true,
},
"multiple elements, exists middle": {
haystack: []complex{n("1337"), n("-1500"), n("me"), n("17"), n("71"), n("18821390293")},
needle: n("me"),
expectedExists: true,
},
"multiple elements, exists last": {
haystack: []complex{n("1337"), n("-1500"), n("17"), n("71"), n("18821390293"), n("me")},
needle: n("me"),
expectedExists: true,
},
"multiple elements, doesn't exist": {
haystack: []complex{n("-42"), n("1337"), n("-1500"), n("17"), n("71"), n("18821390293")},
needle: n("42"),
expectedExists: false,
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
got, gotExists := slicey.Find(test.haystack, func(v complex) bool {
return test.needle.v == v.v
})
if gotExists != test.expectedExists {
t.Errorf("expected exist %v, got %v", test.expectedExists, gotExists)
}
if test.expectedExists && got.v != test.needle.v {
t.Errorf("expected to find %v, found %v", test.needle, got)
}
})
}
}
// TestFindFindsFirst verifies that Find stops the first time f returns true.
func TestFindFindsFirst(t *testing.T) {
calls := 0
got, gotExists := slicey.Find([]int{1, 2, 2, 2, 2, 1}, func(v int) bool {
calls += 1
return v == 1
})
if gotExists != true {
t.Errorf("expected exists true, got %v", gotExists)
}
if got != 1 {
t.Errorf("expected to find 1, got %v", got)
}
if calls != 1 {
t.Errorf("expected 1 calls, got %v", calls)
}
}