@@ -7,6 +7,8 @@ package slices
7
7
import (
8
8
"math"
9
9
"math/rand"
10
+ "strconv"
11
+ "strings"
10
12
"testing"
11
13
)
12
14
@@ -151,31 +153,112 @@ func TestStability(t *testing.T) {
151
153
}
152
154
153
155
func TestBinarySearch (t * testing.T ) {
154
- data := []string {"aa" , "ad" , "ca" , "xy" }
156
+ str1 := []string {"foo" }
157
+ str2 := []string {"ab" , "ca" }
158
+ str3 := []string {"mo" , "qo" , "vo" }
159
+ str4 := []string {"ab" , "ad" , "ca" , "xy" }
160
+
161
+ // slice with repeating elements
162
+ strRepeats := []string {"ba" , "ca" , "da" , "da" , "da" , "ka" , "ma" , "ma" , "ta" }
163
+
164
+ // slice with all element equal
165
+ strSame := []string {"xx" , "xx" , "xx" }
166
+
155
167
tests := []struct {
156
- target string
157
- want int
168
+ data []string
169
+ target string
170
+ wantPos int
171
+ wantFound bool
158
172
}{
159
- {"aa" , 0 },
160
- {"ab" , 1 },
161
- {"ad" , 1 },
162
- {"ax" , 2 },
163
- {"ca" , 2 },
164
- {"cc" , 3 },
165
- {"dd" , 3 },
166
- {"xy" , 3 },
167
- {"zz" , 4 },
173
+ {[]string {}, "foo" , 0 , false },
174
+ {[]string {}, "" , 0 , false },
175
+
176
+ {str1 , "foo" , 0 , true },
177
+ {str1 , "bar" , 0 , false },
178
+ {str1 , "zx" , 1 , false },
179
+
180
+ {str2 , "aa" , 0 , false },
181
+ {str2 , "ab" , 0 , true },
182
+ {str2 , "ad" , 1 , false },
183
+ {str2 , "ca" , 1 , true },
184
+ {str2 , "ra" , 2 , false },
185
+
186
+ {str3 , "bb" , 0 , false },
187
+ {str3 , "mo" , 0 , true },
188
+ {str3 , "nb" , 1 , false },
189
+ {str3 , "qo" , 1 , true },
190
+ {str3 , "tr" , 2 , false },
191
+ {str3 , "vo" , 2 , true },
192
+ {str3 , "xr" , 3 , false },
193
+
194
+ {str4 , "aa" , 0 , false },
195
+ {str4 , "ab" , 0 , true },
196
+ {str4 , "ac" , 1 , false },
197
+ {str4 , "ad" , 1 , true },
198
+ {str4 , "ax" , 2 , false },
199
+ {str4 , "ca" , 2 , true },
200
+ {str4 , "cc" , 3 , false },
201
+ {str4 , "dd" , 3 , false },
202
+ {str4 , "xy" , 3 , true },
203
+ {str4 , "zz" , 4 , false },
204
+
205
+ {strRepeats , "da" , 2 , true },
206
+ {strRepeats , "db" , 5 , false },
207
+ {strRepeats , "ma" , 6 , true },
208
+ {strRepeats , "mb" , 8 , false },
209
+
210
+ {strSame , "xx" , 0 , true },
211
+ {strSame , "ab" , 0 , false },
212
+ {strSame , "zz" , 3 , false },
168
213
}
169
214
for _ , tt := range tests {
170
215
t .Run (tt .target , func (t * testing.T ) {
171
- i := BinarySearch (data , tt .target )
172
- if i != tt .want {
173
- t .Errorf ("BinarySearch want %d, got %d" , tt .want , i )
216
+ {
217
+ pos , found := BinarySearch (tt .data , tt .target )
218
+ if pos != tt .wantPos || found != tt .wantFound {
219
+ t .Errorf ("BinarySearch got (%v, %v), want (%v, %v)" , pos , found , tt .wantPos , tt .wantFound )
220
+ }
221
+ }
222
+
223
+ {
224
+ pos , found := BinarySearchFunc (tt .data , tt .target , strings .Compare )
225
+ if pos != tt .wantPos || found != tt .wantFound {
226
+ t .Errorf ("BinarySearchFunc got (%v, %v), want (%v, %v)" , pos , found , tt .wantPos , tt .wantFound )
227
+ }
228
+ }
229
+ })
230
+ }
231
+ }
232
+
233
+ func TestBinarySearchInts (t * testing.T ) {
234
+ data := []int {20 , 30 , 40 , 50 , 60 , 70 , 80 , 90 }
235
+ tests := []struct {
236
+ target int
237
+ wantPos int
238
+ wantFound bool
239
+ }{
240
+ {20 , 0 , true },
241
+ {23 , 1 , false },
242
+ {43 , 3 , false },
243
+ {80 , 6 , true },
244
+ }
245
+ for _ , tt := range tests {
246
+ t .Run (strconv .Itoa (tt .target ), func (t * testing.T ) {
247
+ {
248
+ pos , found := BinarySearch (data , tt .target )
249
+ if pos != tt .wantPos || found != tt .wantFound {
250
+ t .Errorf ("BinarySearch got (%v, %v), want (%v, %v)" , pos , found , tt .wantPos , tt .wantFound )
251
+ }
174
252
}
175
253
176
- j := BinarySearchFunc (data , func (s string ) bool { return s >= tt .target })
177
- if j != tt .want {
178
- t .Errorf ("BinarySearchFunc want %d, got %d" , tt .want , j )
254
+ {
255
+ cmp := func (a , b int ) int {
256
+ return a - b
257
+ }
258
+ pos , found := BinarySearchFunc (data , tt .target , cmp )
259
+ if pos != tt .wantPos || found != tt .wantFound {
260
+ t .Errorf ("BinarySearchFunc got (%v, %v), want (%v, %v)" , pos , found , tt .wantPos , tt .wantFound )
261
+ }
179
262
}
180
263
})
181
264
}
0 commit comments