package main import ( "reflect" "testing" ) type Cases struct { list []any oldPosition int newPosition int want []any } 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"}, }, } var gout []any func TestShuffleFuncOne(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) 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) } } } 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) 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) } else { t.Logf("TEST PASSED: got %v", got) } } } func TestShuffleFuncThree(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) got := shuffleFuncThree(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) } } } func BenchmarkShuffleFuncThree(b *testing.B) { var out []any var benchmarkCase = []any{ -183, -34, -118, -51, 161, -39, 60, -170, -15, -148, 70, 176, -101, -81, 156, -43, -130, 193, 64, 127, 49, 133, 199, -92, 178, -24, -139, 48, -171, 65, 187, -20, 124, 76, 68, -100, 43, 22, -21, 36, -46, 129, 40, 100, 171, 105, -165, 71, -192, -72, -93, -132, 34, 67, 55, -129, -41, 158, 46, 138, } for i := 0; i < b.N; i++ { out = shuffleFuncThree(benchmarkCase, 47, 16) } gout = out } func BenchmarkShuffleFuncTwo(b *testing.B) { var out []any var benchmarkCase = []any{ -183, -34, -118, -51, 161, -39, 60, -170, -15, -148, 70, 176, -101, -81, 156, -43, -130, 193, 64, 127, 49, 133, 199, -92, 178, -24, -139, 48, -171, 65, 187, -20, 124, 76, 68, -100, 43, 22, -21, 36, -46, 129, 40, 100, 171, 105, -165, 71, -192, -72, -93, -132, 34, 67, 55, -129, -41, 158, 46, 138, } for i := 0; i < b.N; i++ { out = shuffleFuncTwo(benchmarkCase, 47, 16) } gout = out } func BenchmarkShuffleFuncOne(b *testing.B) { var out []any var benchmarkCase = []any{ -183, -34, -118, -51, 161, -39, 60, -170, -15, -148, 70, 176, -101, -81, 156, -43, -130, 193, 64, 127, 49, 133, 199, -92, 178, -24, -139, 48, -171, 65, 187, -20, 124, 76, 68, -100, 43, 22, -21, 36, -46, 129, 40, 100, 171, 105, -165, 71, -192, -72, -93, -132, 34, 67, 55, -129, -41, 158, 46, 138, } for i := 0; i < b.N; i++ { out = shuffleFuncOne(benchmarkCase, 47, 16) } gout = out }