laboratory/experiments/go/shuffle-elements/shuffle_test.go

60 lines
1.4 KiB
Go
Raw Normal View History

2024-01-23 20:51:52 +00:00
package main
import (
"fmt"
"reflect"
"testing"
)
func TestShuffle(t *testing.T) {
cases := []struct {
list []any
oldPosition int
newPosition int
want []any
}{
{
list: []any{1, 2, 3, 4, 5, 6, 7, 8},
oldPosition: 5,
newPosition: 2,
want: []any{1, 2, 6, 3, 4, 5, 7, 8},
},
{
list: []any{1, 2, 3, 4, 5, 6, 7, 8},
oldPosition: 7,
newPosition: 0,
want: []any{8, 1, 2, 3, 4, 5, 6, 7},
},
{
list: []any{"zero", "one", "two", "three", "four", "five"},
oldPosition: 0,
newPosition: 5,
want: []any{"one", "two", "three", "four", "five", "zero"},
},
{
list: []any{"zero", "one", "two", "three", "four", "five"},
oldPosition: 2,
newPosition: 3,
want: []any{"zero", "one", "three", "two", "four", "five"},
},
}
for i := range cases {
t.Run(fmt.Sprintf("Test Case %d", i), testFunc(cases[i].list, cases[i].oldPosition, cases[i].newPosition, cases[i].want))
}
}
func testFunc(list []any, oldPosition, newPosition int, want []any) func(t *testing.T) {
return func(t *testing.T) {
t.Logf("Input list: %v", list)
t.Logf("We want '%v' to move to position %d", list[oldPosition], newPosition)
got := shuffle(list, oldPosition, newPosition)
if !reflect.DeepEqual(want, got) {
t.Errorf("TEST FAILED: want: %v, got %v", want, got)
} else {
t.Logf("TEST PASSED: got %v", got)
}
}
}