| 1 |
package slices |
| 2 |
|
| 3 |
import ( |
| 4 |
"strconv" |
| 5 |
"testing" |
| 6 |
|
| 7 |
"github.com/jesseduffield/generics/internal/testutils" |
| 8 |
"golang.org/x/exp/slices" |
| 9 |
) |
| 10 |
|
| 11 |
func TestInsert(t *testing.T) { |
| 12 |
tests := []struct { |
| 13 |
startSlice []int |
| 14 |
index int |
| 15 |
values []int |
| 16 |
endSlice []int |
| 17 |
}{ |
| 18 |
{[]int{}, 0, []int{1}, []int{1}}, |
| 19 |
{[]int{1}, 0, []int{2}, []int{2, 1}}, |
| 20 |
{[]int{1, 2}, 1, []int{3}, []int{1, 3, 2}}, |
| 21 |
{[]int{1, 2}, 2, []int{3}, []int{1, 2, 3}}, |
| 22 |
{[]int{1, 2}, 2, []int{3, 4}, []int{1, 2, 3, 4}}, |
| 23 |
{[]int{1, 2}, 1, []int{3, 4}, []int{1, 3, 4, 2}}, |
| 24 |
} |
| 25 |
for _, test := range tests { |
| 26 |
result := Insert(test.startSlice, test.index, test.values...) |
| 27 |
testutils.ExpectSlice(t, test.endSlice, result) |
| 28 |
} |
| 29 |
|
| 30 |
panicTests := []struct { |
| 31 |
startSlice []int |
| 32 |
index int |
| 33 |
value int |
| 34 |
}{ |
| 35 |
{[]int{}, 1, 1}, |
| 36 |
{[]int{}, 2, 1}, |
| 37 |
{[]int{1}, 2, 1}, |
| 38 |
{[]int{1}, -1, 1}, |
| 39 |
} |
| 40 |
for _, test := range panicTests { |
| 41 |
func() { |
| 42 |
defer testutils.ExpectPanic(t) |
| 43 |
Insert(test.startSlice, test.index, test.value) |
| 44 |
}() |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
func TestRemove(t *testing.T) { |
| 49 |
tests := []struct { |
| 50 |
startSlice []int |
| 51 |
index int |
| 52 |
endSlice []int |
| 53 |
}{ |
| 54 |
{[]int{1}, 0, []int{}}, |
| 55 |
{[]int{1, 2}, 0, []int{2}}, |
| 56 |
{[]int{1, 2}, 1, []int{1}}, |
| 57 |
} |
| 58 |
for _, test := range tests { |
| 59 |
result := Remove(test.startSlice, test.index) |
| 60 |
testutils.ExpectSlice(t, test.endSlice, result) |
| 61 |
} |
| 62 |
|
| 63 |
panicTests := []struct { |
| 64 |
startSlice []int |
| 65 |
index int |
| 66 |
}{ |
| 67 |
{[]int{}, 0}, |
| 68 |
{[]int{}, 1}, |
| 69 |
{[]int{1}, 1}, |
| 70 |
{[]int{1}, -1}, |
| 71 |
} |
| 72 |
for _, test := range panicTests { |
| 73 |
func() { |
| 74 |
defer testutils.ExpectPanic(t) |
| 75 |
Remove(test.startSlice, test.index) |
| 76 |
}() |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
func TestPop(t *testing.T) { |
| 81 |
tests := []struct { |
| 82 |
startSlice []int |
| 83 |
value int |
| 84 |
endSlice []int |
| 85 |
}{ |
| 86 |
{[]int{1}, 1, []int{}}, |
| 87 |
{[]int{1, 2}, 2, []int{1}}, |
| 88 |
{[]int{1, 2, 3}, 3, []int{1, 2}}, |
| 89 |
} |
| 90 |
for _, test := range tests { |
| 91 |
value, slice := Pop(test.startSlice) |
| 92 |
if value != test.value { |
| 93 |
t.Errorf("expected %d, got %d", test.value, value) |
| 94 |
} |
| 95 |
testutils.ExpectSlice(t, test.endSlice, slice) |
| 96 |
} |
| 97 |
|
| 98 |
panicTests := []struct { |
| 99 |
startSlice []int |
| 100 |
}{ |
| 101 |
{[]int{}}, |
| 102 |
} |
| 103 |
for _, test := range panicTests { |
| 104 |
func() { |
| 105 |
defer testutils.ExpectPanic(t) |
| 106 |
Pop(test.startSlice) |
| 107 |
}() |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
func TestFilter(t *testing.T) { |
| 112 |
even := func(value int) bool { return value%2 == 0 } |
| 113 |
tests := []struct { |
| 114 |
startSlice []int |
| 115 |
testFunc func(value int) bool |
| 116 |
endSlice []int |
| 117 |
}{ |
| 118 |
{[]int{1}, even, []int{}}, |
| 119 |
{[]int{1, 2}, even, []int{2}}, |
| 120 |
{[]int{1, 2, 3}, even, []int{2}}, |
| 121 |
{[]int{1, 2, 3, 4}, even, []int{2, 4}}, |
| 122 |
} |
| 123 |
for _, test := range tests { |
| 124 |
testSlice := slices.Clone(test.startSlice) |
| 125 |
testutils.ExpectSlice(t, test.endSlice, Filter(testSlice, test.testFunc)) |
| 126 |
testutils.ExpectSlice(t, testSlice, test.startSlice) |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
func TestFilterInPlace(t *testing.T) { |
| 131 |
even := func(value int) bool { return value%2 == 0 } |
| 132 |
tests := []struct { |
| 133 |
startSlice []int |
| 134 |
testFunc func(value int) bool |
| 135 |
endSlice []int |
| 136 |
}{ |
| 137 |
{[]int{1}, even, []int{}}, |
| 138 |
{[]int{1, 2}, even, []int{2}}, |
| 139 |
{[]int{1, 2, 3}, even, []int{2}}, |
| 140 |
{[]int{1, 2, 3, 4}, even, []int{2, 4}}, |
| 141 |
} |
| 142 |
for _, test := range tests { |
| 143 |
result := FilterInPlace(test.startSlice, test.testFunc) |
| 144 |
testutils.ExpectSlice(t, test.endSlice, result) |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
func TestFilterMap(t *testing.T) { |
| 149 |
slice := []int{1, 2, 3, 4} |
| 150 |
result := FilterMap(slice, |
| 151 |
func(value int) (string, bool) { |
| 152 |
if value%2 != 0 { |
| 153 |
return "", false |
| 154 |
} |
| 155 |
|
| 156 |
return strconv.Itoa(value * 2), true |
| 157 |
}, |
| 158 |
) |
| 159 |
|
| 160 |
testutils.ExpectSlice(t, []string{"4", "8"}, result) |
| 161 |
} |
| 162 |
|
| 163 |
func TestMap(t *testing.T) { |
| 164 |
double := func(value int) int { return value * 2 } |
| 165 |
tests := []struct { |
| 166 |
startSlice []int |
| 167 |
mapFunc func(value int) int |
| 168 |
endSlice []int |
| 169 |
}{ |
| 170 |
{[]int{}, double, []int{}}, |
| 171 |
{[]int{1}, double, []int{2}}, |
| 172 |
{[]int{1, 2}, double, []int{2, 4}}, |
| 173 |
{[]int{1, 2, 3}, double, []int{2, 4, 6}}, |
| 174 |
{[]int{1, 2, 3, 4}, double, []int{2, 4, 6, 8}}, |
| 175 |
} |
| 176 |
|
| 177 |
for _, test := range tests { |
| 178 |
testSlice := slices.Clone(test.startSlice) |
| 179 |
testutils.ExpectSlice(t, test.endSlice, Map(testSlice, test.mapFunc)) |
| 180 |
testutils.ExpectSlice(t, test.startSlice, testSlice) |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
func TestMapWithIndex(t *testing.T) { |
| 185 |
double := func(value int, i int) int { return value*2 + i } |
| 186 |
tests := []struct { |
| 187 |
startSlice []int |
| 188 |
mapFunc func(value int, i int) int |
| 189 |
endSlice []int |
| 190 |
}{ |
| 191 |
{[]int{}, double, []int{}}, |
| 192 |
{[]int{1}, double, []int{2}}, |
| 193 |
{[]int{1, 2}, double, []int{2, 5}}, |
| 194 |
{[]int{1, 2, 3}, double, []int{2, 5, 8}}, |
| 195 |
{[]int{1, 2, 3, 4}, double, []int{2, 5, 8, 11}}, |
| 196 |
} |
| 197 |
|
| 198 |
for _, test := range tests { |
| 199 |
testSlice := slices.Clone(test.startSlice) |
| 200 |
testutils.ExpectSlice(t, test.endSlice, MapWithIndex(testSlice, test.mapFunc)) |
| 201 |
testutils.ExpectSlice(t, test.startSlice, testSlice) |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
func TestFilterWithIndex(t *testing.T) { |
| 206 |
double := func(value int, i int) bool { return (value*2+i)%2 == 0 } |
| 207 |
tests := []struct { |
| 208 |
startSlice []int |
| 209 |
mapFunc func(value int, i int) bool |
| 210 |
endSlice []int |
| 211 |
}{ |
| 212 |
{[]int{}, double, []int{}}, |
| 213 |
{[]int{1}, double, []int{1}}, |
| 214 |
{[]int{1, 2}, double, []int{1}}, |
| 215 |
{[]int{1, 2, 3}, double, []int{1, 3}}, |
| 216 |
{[]int{1, 2, 3, 4}, double, []int{1, 3}}, |
| 217 |
} |
| 218 |
|
| 219 |
for _, test := range tests { |
| 220 |
testSlice := slices.Clone(test.startSlice) |
| 221 |
testutils.ExpectSlice(t, test.endSlice, FilterWithIndex(testSlice, test.mapFunc)) |
| 222 |
testutils.ExpectSlice(t, test.startSlice, testSlice) |
| 223 |
} |
| 224 |
} |
| 225 |
|
| 226 |
func TestFlatMap(t *testing.T) { |
| 227 |
f := func(value int) []int { return []int{value * 2, value * 4} } |
| 228 |
tests := []struct { |
| 229 |
startSlice []int |
| 230 |
mapFunc func(value int) []int |
| 231 |
endSlice []int |
| 232 |
}{ |
| 233 |
{[]int{}, f, []int{}}, |
| 234 |
{[]int{1}, f, []int{2, 4}}, |
| 235 |
{[]int{1, 2}, f, []int{2, 4, 4, 8}}, |
| 236 |
{[]int{1, 2, 3}, f, []int{2, 4, 4, 8, 6, 12}}, |
| 237 |
{[]int{1, 2, 3, 4}, f, []int{2, 4, 4, 8, 6, 12, 8, 16}}, |
| 238 |
} |
| 239 |
|
| 240 |
for _, test := range tests { |
| 241 |
testSlice := slices.Clone(test.startSlice) |
| 242 |
testutils.ExpectSlice(t, test.endSlice, FlatMap(testSlice, test.mapFunc)) |
| 243 |
testutils.ExpectSlice(t, test.startSlice, testSlice) |
| 244 |
} |
| 245 |
} |
| 246 |
|
| 247 |
func TestFlatten(t *testing.T) { |
| 248 |
tests := []struct { |
| 249 |
startSlice [][]int |
| 250 |
endSlice []int |
| 251 |
}{ |
| 252 |
{[][]int{}, []int{}}, |
| 253 |
{[][]int{{1}}, []int{1}}, |
| 254 |
{[][]int{{1, 2}, {3}}, []int{1, 2, 3}}, |
| 255 |
{[][]int{{1, 2}, {}, {3, 4}}, []int{1, 2, 3, 4}}, |
| 256 |
} |
| 257 |
|
| 258 |
for _, test := range tests { |
| 259 |
testutils.ExpectSlice(t, test.endSlice, Flatten(test.startSlice)) |
| 260 |
} |
| 261 |
} |
| 262 |
|
| 263 |
func TestMapInPlace(t *testing.T) { |
| 264 |
double := func(value int) int { return value * 2 } |
| 265 |
tests := []struct { |
| 266 |
startSlice []int |
| 267 |
mapFunc func(value int) int |
| 268 |
endSlice []int |
| 269 |
}{ |
| 270 |
{[]int{}, double, []int{}}, |
| 271 |
{[]int{1}, double, []int{2}}, |
| 272 |
{[]int{1, 2}, double, []int{2, 4}}, |
| 273 |
{[]int{1, 2, 3}, double, []int{2, 4, 6}}, |
| 274 |
{[]int{1, 2, 3, 4}, double, []int{2, 4, 6, 8}}, |
| 275 |
} |
| 276 |
for _, test := range tests { |
| 277 |
testSlice := slices.Clone(test.startSlice) |
| 278 |
MapInPlace(testSlice, test.mapFunc) |
| 279 |
testutils.ExpectSlice(t, test.endSlice, testSlice) |
| 280 |
} |
| 281 |
} |
| 282 |
|
| 283 |
func TestPrepend(t *testing.T) { |
| 284 |
tests := []struct { |
| 285 |
slice []int |
| 286 |
values []int |
| 287 |
expectedSlice []int |
| 288 |
}{ |
| 289 |
{[]int{}, []int{}, []int{}}, |
| 290 |
{[]int{}, []int{1}, []int{1}}, |
| 291 |
{[]int{1}, []int{2}, []int{2, 1}}, |
| 292 |
{[]int{1, 2}, []int{3}, []int{3, 1, 2}}, |
| 293 |
{[]int{1, 2}, []int{3, 4}, []int{3, 4, 1, 2}}, |
| 294 |
} |
| 295 |
for _, test := range tests { |
| 296 |
slice := Prepend(test.slice, test.values...) |
| 297 |
testutils.ExpectSlice(t, test.expectedSlice, slice) |
| 298 |
} |
| 299 |
} |
| 300 |
|
| 301 |
func TestConcat(t *testing.T) { |
| 302 |
tests := []struct { |
| 303 |
startSlice []int |
| 304 |
values []int |
| 305 |
endSlice []int |
| 306 |
}{ |
| 307 |
{[]int{}, []int{}, []int{}}, |
| 308 |
{[]int{1}, []int{2}, []int{1, 2}}, |
| 309 |
{[]int{1, 2}, []int{3, 4}, []int{1, 2, 3, 4}}, |
| 310 |
} |
| 311 |
for _, test := range tests { |
| 312 |
testSlice := slices.Clone(test.startSlice) |
| 313 |
result := Concat(testSlice, test.values...) |
| 314 |
testutils.ExpectSlice(t, test.endSlice, result) |
| 315 |
testutils.ExpectSlice(t, test.startSlice, testSlice) |
| 316 |
} |
| 317 |
} |
| 318 |
|
| 319 |
func TestSome(t *testing.T) { |
| 320 |
even := func(value int) bool { return value%2 == 0 } |
| 321 |
tests := []struct { |
| 322 |
startSlice []int |
| 323 |
testFunc func(value int) bool |
| 324 |
expected bool |
| 325 |
}{ |
| 326 |
{[]int{}, even, false}, |
| 327 |
{[]int{1}, even, false}, |
| 328 |
{[]int{2}, even, true}, |
| 329 |
{[]int{1, 2}, even, true}, |
| 330 |
} |
| 331 |
for _, test := range tests { |
| 332 |
if Some(test.startSlice, test.testFunc) != test.expected { |
| 333 |
t.Errorf("Some(%v) = %v, expected %v", |
| 334 |
test.startSlice, Some(test.startSlice, test.testFunc), test.expected, |
| 335 |
) |
| 336 |
} |
| 337 |
} |
| 338 |
} |
| 339 |
|
| 340 |
func TestEvery(t *testing.T) { |
| 341 |
even := func(value int) bool { return value%2 == 0 } |
| 342 |
tests := []struct { |
| 343 |
startSlice []int |
| 344 |
testFunc func(value int) bool |
| 345 |
expected bool |
| 346 |
}{ |
| 347 |
{[]int{}, even, true}, |
| 348 |
{[]int{1}, even, false}, |
| 349 |
{[]int{2}, even, true}, |
| 350 |
{[]int{1, 2}, even, false}, |
| 351 |
{[]int{2, 2}, even, true}, |
| 352 |
} |
| 353 |
for _, test := range tests { |
| 354 |
if Every(test.startSlice, test.testFunc) != test.expected { |
| 355 |
t.Errorf("Every(%v) = %v, expected %v", |
| 356 |
test.startSlice, Every(test.startSlice, test.testFunc), test.expected, |
| 357 |
) |
| 358 |
} |
| 359 |
} |
| 360 |
} |
| 361 |
|
| 362 |
func TestReverse(t *testing.T) { |
| 363 |
tests := []struct { |
| 364 |
startSlice []int |
| 365 |
expected []int |
| 366 |
}{ |
| 367 |
{[]int{}, []int{}}, |
| 368 |
{[]int{1}, []int{1}}, |
| 369 |
{[]int{1, 2}, []int{2, 1}}, |
| 370 |
{[]int{1, 2, 3}, []int{3, 2, 1}}, |
| 371 |
} |
| 372 |
for _, test := range tests { |
| 373 |
testSlice := slices.Clone(test.startSlice) |
| 374 |
result := Reverse(testSlice) |
| 375 |
testutils.ExpectSlice(t, test.expected, result) |
| 376 |
testutils.ExpectSlice(t, test.startSlice, testSlice) |
| 377 |
} |
| 378 |
} |
| 379 |
|
| 380 |
func TestReverseInPlace(t *testing.T) { |
| 381 |
tests := []struct { |
| 382 |
startSlice []int |
| 383 |
expected []int |
| 384 |
}{ |
| 385 |
{[]int{}, []int{}}, |
| 386 |
{[]int{1}, []int{1}}, |
| 387 |
{[]int{1, 2}, []int{2, 1}}, |
| 388 |
{[]int{1, 2, 3}, []int{3, 2, 1}}, |
| 389 |
} |
| 390 |
for _, test := range tests { |
| 391 |
testSlice := slices.Clone(test.startSlice) |
| 392 |
ReverseInPlace(testSlice) |
| 393 |
testutils.ExpectSlice(t, test.expected, testSlice) |
| 394 |
} |
| 395 |
} |
| 396 |
|
| 397 |
func TestClone(t *testing.T) { |
| 398 |
slice := []int{1, 2, 3} |
| 399 |
clone := Clone(slice) |
| 400 |
testutils.ExpectSlice(t, []int{1, 2, 3}, clone) |
| 401 |
|
| 402 |
|
| 403 |
clone = Prepend(clone, 0) |
| 404 |
testutils.ExpectSlice(t, []int{0, 1, 2, 3}, clone) |
| 405 |
testutils.ExpectSlice(t, []int{1, 2, 3}, slice) |
| 406 |
} |
| 407 |
|
| 408 |
func TestMove(t *testing.T) { |
| 409 |
tests := []struct { |
| 410 |
startSlice []int |
| 411 |
from int |
| 412 |
to int |
| 413 |
endSlice []int |
| 414 |
}{ |
| 415 |
{ |
| 416 |
startSlice: []int{1, 2, 3}, |
| 417 |
from: 0, |
| 418 |
to: 1, |
| 419 |
endSlice: []int{2, 1, 3}, |
| 420 |
}, |
| 421 |
{ |
| 422 |
startSlice: []int{1, 2, 3, 4}, |
| 423 |
from: 2, |
| 424 |
to: 1, |
| 425 |
endSlice: []int{1, 3, 2, 4}, |
| 426 |
}, |
| 427 |
{ |
| 428 |
startSlice: []int{1, 2, 3}, |
| 429 |
from: 1, |
| 430 |
to: 0, |
| 431 |
endSlice: []int{2, 1, 3}, |
| 432 |
}, |
| 433 |
{ |
| 434 |
startSlice: []int{1, 2, 3, 4}, |
| 435 |
from: 0, |
| 436 |
to: 3, |
| 437 |
endSlice: []int{2, 3, 4, 1}, |
| 438 |
}, |
| 439 |
} |
| 440 |
|
| 441 |
for _, test := range tests { |
| 442 |
result := Move(test.startSlice, test.from, test.to) |
| 443 |
testutils.ExpectSlice(t, test.endSlice, result) |
| 444 |
} |
| 445 |
} |
| 446 |
|
| 447 |
func TestSwap(t *testing.T) { |
| 448 |
tests := []struct { |
| 449 |
startSlice []int |
| 450 |
from int |
| 451 |
to int |
| 452 |
endSlice []int |
| 453 |
}{ |
| 454 |
{ |
| 455 |
startSlice: []int{1, 2, 3}, |
| 456 |
from: 0, |
| 457 |
to: 1, |
| 458 |
endSlice: []int{2, 1, 3}, |
| 459 |
}, |
| 460 |
{ |
| 461 |
startSlice: []int{1, 2, 3, 4}, |
| 462 |
from: 2, |
| 463 |
to: 1, |
| 464 |
endSlice: []int{1, 3, 2, 4}, |
| 465 |
}, |
| 466 |
{ |
| 467 |
startSlice: []int{1, 2, 3}, |
| 468 |
from: 1, |
| 469 |
to: 0, |
| 470 |
endSlice: []int{2, 1, 3}, |
| 471 |
}, |
| 472 |
{ |
| 473 |
startSlice: []int{1, 2, 3, 4}, |
| 474 |
from: 0, |
| 475 |
to: 3, |
| 476 |
endSlice: []int{4, 2, 3, 1}, |
| 477 |
}, |
| 478 |
{ |
| 479 |
startSlice: []int{1, 2, 3, 4}, |
| 480 |
from: 3, |
| 481 |
to: 0, |
| 482 |
endSlice: []int{4, 2, 3, 1}, |
| 483 |
}, |
| 484 |
} |
| 485 |
|
| 486 |
for _, test := range tests { |
| 487 |
Swap(test.startSlice, test.from, test.to) |
| 488 |
testutils.ExpectSlice(t, test.endSlice, test.startSlice) |
| 489 |
} |
| 490 |
} |
| 491 |
|
| 492 |
func TestEqual(t *testing.T) { |
| 493 |
tests := []struct { |
| 494 |
first []int |
| 495 |
second []int |
| 496 |
expected bool |
| 497 |
}{ |
| 498 |
{[]int{}, []int{}, true}, |
| 499 |
{[]int{1}, []int{1}, true}, |
| 500 |
{[]int{}, []int{1}, false}, |
| 501 |
{[]int{1, 2}, []int{1, 2}, true}, |
| 502 |
{[]int{1, 2}, []int{2, 1}, false}, |
| 503 |
{[]int{1, 2, 3}, []int{1, 2}, false}, |
| 504 |
} |
| 505 |
for _, test := range tests { |
| 506 |
if Equal(test.first, test.second) != test.expected { |
| 507 |
t.Errorf("Equal(%v, %v) = %v, expected %v", |
| 508 |
test.first, test.second, Equal(test.first, test.second), test.expected, |
| 509 |
) |
| 510 |
} |
| 511 |
} |
| 512 |
} |
| 513 |
|
| 514 |
func TestCompact(t *testing.T) { |
| 515 |
tests := []struct { |
| 516 |
slice []int |
| 517 |
expected []int |
| 518 |
}{ |
| 519 |
{[]int{}, []int{}}, |
| 520 |
{[]int{1}, []int{1}}, |
| 521 |
{[]int{1, 2}, []int{1, 2}}, |
| 522 |
{[]int{1, 1, 2}, []int{1, 2}}, |
| 523 |
{[]int{1, 2, 1}, []int{1, 2, 1}}, |
| 524 |
{[]int{1, 1, 1}, []int{1}}, |
| 525 |
} |
| 526 |
for _, test := range tests { |
| 527 |
result := Compact(test.slice) |
| 528 |
testutils.ExpectSlice(t, test.expected, result) |
| 529 |
} |
| 530 |
} |
| 531 |
|
| 532 |
func TestIndex(t *testing.T) { |
| 533 |
tests := []struct { |
| 534 |
slice []int |
| 535 |
value int |
| 536 |
expected int |
| 537 |
}{ |
| 538 |
{[]int{}, 1, -1}, |
| 539 |
{[]int{1}, 1, 0}, |
| 540 |
{[]int{1, 1}, 1, 0}, |
| 541 |
{[]int{1, 1}, 2, -1}, |
| 542 |
{[]int{1, 2, 3}, 2, 1}, |
| 543 |
{[]int{1, 2, 3}, 3, 2}, |
| 544 |
} |
| 545 |
for _, test := range tests { |
| 546 |
if Index(test.slice, test.value) != test.expected { |
| 547 |
t.Errorf("Index(%v, %v) = %v, expected %v", test.slice, test.value, Index(test.slice, test.value), test.expected) |
| 548 |
} |
| 549 |
} |
| 550 |
} |
| 551 |
|
| 552 |
func TestIndexFunc(t *testing.T) { |
| 553 |
tests := []struct { |
| 554 |
slice []int |
| 555 |
f func(value int) bool |
| 556 |
expected int |
| 557 |
}{ |
| 558 |
{[]int{}, func(value int) bool { return true }, -1}, |
| 559 |
{[]int{1}, func(value int) bool { return true }, 0}, |
| 560 |
{[]int{1, 1}, func(value int) bool { return true }, 0}, |
| 561 |
{[]int{1, 1}, func(value int) bool { return false }, -1}, |
| 562 |
{[]int{1, 2, 3}, func(value int) bool { return value == 2 }, 1}, |
| 563 |
{[]int{1, 2, 3}, func(value int) bool { return value == 3 }, 2}, |
| 564 |
} |
| 565 |
for _, test := range tests { |
| 566 |
if IndexFunc(test.slice, test.f) != test.expected { |
| 567 |
t.Errorf("IndexFunc(%v, func) = %v, expected %v", test.slice, IndexFunc(test.slice, test.f), test.expected) |
| 568 |
} |
| 569 |
} |
| 570 |
} |
| 571 |
|
| 572 |
func TestContains(t *testing.T) { |
| 573 |
tests := []struct { |
| 574 |
slice []int |
| 575 |
value int |
| 576 |
expected bool |
| 577 |
}{ |
| 578 |
{[]int{}, 1, false}, |
| 579 |
{[]int{1}, 1, true}, |
| 580 |
{[]int{1, 1}, 1, true}, |
| 581 |
{[]int{1, 1}, 2, false}, |
| 582 |
{[]int{1, 2, 3}, 2, true}, |
| 583 |
{[]int{1, 2, 3}, 3, true}, |
| 584 |
} |
| 585 |
for _, test := range tests { |
| 586 |
if Contains(test.slice, test.value) != test.expected { |
| 587 |
t.Errorf("Contains(%v, %v) = %v, expected %v", test.slice, test.value, Contains(test.slice, test.value), test.expected) |
| 588 |
} |
| 589 |
} |
| 590 |
} |
| 591 |
|
| 592 |
func TestContainsFunc(t *testing.T) { |
| 593 |
tests := []struct { |
| 594 |
slice []int |
| 595 |
f func(value int) bool |
| 596 |
expected bool |
| 597 |
}{ |
| 598 |
{[]int{}, func(value int) bool { return true }, false}, |
| 599 |
{[]int{1}, func(value int) bool { return true }, true}, |
| 600 |
{[]int{1, 1}, func(value int) bool { return true }, true}, |
| 601 |
{[]int{1, 1}, func(value int) bool { return false }, false}, |
| 602 |
{[]int{1, 2, 3}, func(value int) bool { return value == 2 }, true}, |
| 603 |
{[]int{1, 2, 3}, func(value int) bool { return value == 3 }, true}, |
| 604 |
} |
| 605 |
for _, test := range tests { |
| 606 |
if ContainsFunc(test.slice, test.f) != test.expected { |
| 607 |
t.Errorf("ContainsFunc(%v, func) = %v, expected %v", test.slice, ContainsFunc(test.slice, test.f), test.expected) |
| 608 |
} |
| 609 |
} |
| 610 |
} |
| 611 |
|
| 612 |
func TestShift(t *testing.T) { |
| 613 |
tests := []struct { |
| 614 |
slice []int |
| 615 |
expectedValue int |
| 616 |
expectedSlice []int |
| 617 |
}{ |
| 618 |
{[]int{1}, 1, []int{}}, |
| 619 |
{[]int{1, 2}, 1, []int{2}}, |
| 620 |
} |
| 621 |
for _, test := range tests { |
| 622 |
value, slice := Shift(test.slice) |
| 623 |
if value != test.expectedValue { |
| 624 |
t.Errorf("Shift(%v) = %v, expected %v", test.slice, value, test.expectedValue) |
| 625 |
} |
| 626 |
testutils.ExpectSlice(t, test.expectedSlice, slice) |
| 627 |
} |
| 628 |
} |
| 629 |
|
| 630 |
func TestPartition(t *testing.T) { |
| 631 |
even := func(value int) bool { return value%2 == 0 } |
| 632 |
tests := []struct { |
| 633 |
startSlice []int |
| 634 |
testFunc func(value int) bool |
| 635 |
endSliceLeft []int |
| 636 |
endSliceRight []int |
| 637 |
}{ |
| 638 |
{[]int{1}, even, []int{}, []int{1}}, |
| 639 |
{[]int{1, 2}, even, []int{2}, []int{1}}, |
| 640 |
{[]int{1, 2, 3}, even, []int{2}, []int{1, 3}}, |
| 641 |
{[]int{1, 2, 3, 4}, even, []int{2, 4}, []int{1, 3}}, |
| 642 |
} |
| 643 |
for _, test := range tests { |
| 644 |
testSlice := slices.Clone(test.startSlice) |
| 645 |
left, right := Partition(testSlice, test.testFunc) |
| 646 |
testutils.ExpectSlice(t, test.endSliceLeft, left) |
| 647 |
testutils.ExpectSlice(t, test.endSliceRight, right) |
| 648 |
testutils.ExpectSlice(t, testSlice, test.startSlice) |
| 649 |
} |
| 650 |
} |
| 651 |
|
| 652 |
func TestMaxBy(t *testing.T) { |
| 653 |
tests := []struct { |
| 654 |
slice []int |
| 655 |
f func(value int) int |
| 656 |
expected int |
| 657 |
}{ |
| 658 |
{[]int{}, func(value int) int { return value }, 0}, |
| 659 |
{[]int{1}, func(value int) int { return value }, 1}, |
| 660 |
{[]int{-1}, func(value int) int { return value }, -1}, |
| 661 |
{[]int{1, 2}, func(value int) int { return value }, 2}, |
| 662 |
{[]int{3, 1, 2, 3}, func(value int) int { return value }, 3}, |
| 663 |
{[]int{1, 2, 2, 3, 4}, func(value int) int { return value }, 4}, |
| 664 |
} |
| 665 |
for _, test := range tests { |
| 666 |
if MaxBy(test.slice, test.f) != test.expected { |
| 667 |
t.Errorf("MaxBy(%v, func) = %v, expected %v", test.slice, MaxBy(test.slice, test.f), test.expected) |
| 668 |
} |
| 669 |
} |
| 670 |
} |
| 671 |
|
| 672 |
func TestMinBy(t *testing.T) { |
| 673 |
tests := []struct { |
| 674 |
slice []int |
| 675 |
f func(value int) int |
| 676 |
expected int |
| 677 |
}{ |
| 678 |
{[]int{}, func(value int) int { return value }, 0}, |
| 679 |
{[]int{1}, func(value int) int { return value }, 1}, |
| 680 |
{[]int{-1}, func(value int) int { return value }, -1}, |
| 681 |
{[]int{3, 1, 2}, func(value int) int { return value }, 1}, |
| 682 |
{[]int{1, 2, 3}, func(value int) int { return value }, 1}, |
| 683 |
{[]int{1, 2, 3, 4}, func(value int) int { return value }, 1}, |
| 684 |
} |
| 685 |
for _, test := range tests { |
| 686 |
if MinBy(test.slice, test.f) != test.expected { |
| 687 |
t.Errorf("MinBy(%v, func) = %v, expected %v", test.slice, MinBy(test.slice, test.f), test.expected) |
| 688 |
} |
| 689 |
} |
| 690 |
} |
| 691 |
|
| 692 |
func TestSumInt(t *testing.T) { |
| 693 |
tests := []struct { |
| 694 |
slice []int |
| 695 |
expected int |
| 696 |
}{ |
| 697 |
{[]int{}, 0}, |
| 698 |
{[]int{1}, 1}, |
| 699 |
{[]int{1, 2}, 3}, |
| 700 |
{[]int{1, 2, 3}, 6}, |
| 701 |
} |
| 702 |
for _, test := range tests { |
| 703 |
if Sum(test.slice) != test.expected { |
| 704 |
t.Errorf("Sum(%v) = %v, expected %v", test.slice, Sum(test.slice), test.expected) |
| 705 |
} |
| 706 |
} |
| 707 |
} |
| 708 |
|
| 709 |
func TestSumString(t *testing.T) { |
| 710 |
tests := []struct { |
| 711 |
slice []string |
| 712 |
expected string |
| 713 |
}{ |
| 714 |
{[]string{}, ""}, |
| 715 |
{[]string{"a"}, "a"}, |
| 716 |
{[]string{"a", "b"}, "ab"}, |
| 717 |
} |
| 718 |
for _, test := range tests { |
| 719 |
if Sum(test.slice) != test.expected { |
| 720 |
t.Errorf("Sum(%v) = %v, expected %v", test.slice, Sum(test.slice), test.expected) |
| 721 |
} |
| 722 |
} |
| 723 |
} |