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

69 lines
1.7 KiB
Go
Raw Normal View History

2024-01-23 20:51:52 +00:00
package main
import (
"reflect"
"testing"
)
type Cases struct {
list []any
oldPosition int
newPosition int
want []any
}
2024-01-23 20:51:52 +00:00
var cases = []Cases{
{
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"},
},
}
func TestShuffleFuncOne(t *testing.T) {
2024-01-23 20:51:52 +00:00
for i := range cases {
t.Logf("Input list: %v", cases[i].list)
t.Logf("We want '%v' to move to position %d", cases[i].list[cases[i].oldPosition], cases[i].newPosition)
got := shuffleFuncOne(cases[i].list, cases[i].oldPosition, cases[i].newPosition)
if !reflect.DeepEqual(cases[i].want, got) {
t.Errorf("TEST FAILED: want: %v, got %v", cases[i].want, got)
} else {
t.Logf("TEST PASSED: got %v", got)
}
2024-01-23 20:51:52 +00:00
}
}
func TestShuffleFuncTwo(t *testing.T) {
for i := range cases {
t.Logf("Input list: %v", cases[i].list)
t.Logf("We want '%v' to move to position %d", cases[i].list[cases[i].oldPosition], cases[i].newPosition)
2024-01-23 20:51:52 +00:00
got := shuffleFuncTwo(cases[i].list, cases[i].oldPosition, cases[i].newPosition)
if !reflect.DeepEqual(cases[i].want, got) {
t.Errorf("TEST FAILED: want: %v, got %v", cases[i].want, got)
2024-01-23 20:51:52 +00:00
} else {
t.Logf("TEST PASSED: got %v", got)
}
}
}