-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpuzzle_test.go
87 lines (85 loc) · 1.6 KB
/
puzzle_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package main
import "testing"
func TestPuzzle_IsSolvable(t *testing.T) {
type fields struct {
StartBoard Board
GoalBoard Board
Size int
}
tests := []struct {
name string
fields fields
want bool
}{
{
name: "49 inversions, solvable",
fields: fields{
StartBoard: Board{Tiles: [][]int{
{12, 1, 10, 2},
{7, 11, 4, 14},
{5, 0, 9, 15},
{8, 13, 6, 3}},
},
GoalBoard: Board{Tiles: [][]int{
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12},
{13, 14, 15, 0}},
},
Size: 4,
},
want: true,
}, {
// inversions:
// 4 + 0 + 4 + 1 +
// 4 + 0 + 4 + 0 +
// 4 + 0 + 4 + 0 +
// 0 + 0 + 1 + 0
name: "26 inversions, solvable",
fields: fields{
StartBoard: Board{Tiles: [][]int{
{5, 1, 7, 3},
{9, 2, 11, 4},
{13, 6, 15, 8},
{0, 10, 14, 12}},
},
GoalBoard: Board{Tiles: [][]int{
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12},
{13, 14, 15, 0}},
},
Size: 4,
},
want: true,
}, {
name: "9-puzzle, solvable",
fields: fields{
StartBoard: Board{Tiles: [][]int{
{1, 6, 2},
{5, 3, 0},
{4, 7, 8}},
},
GoalBoard: Board{Tiles: [][]int{
{1, 2, 3},
{4, 5, 6},
{7, 8, 0}},
},
Size: 4,
},
want: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
p := Puzzle{
StartBoard: tt.fields.StartBoard,
GoalBoard: tt.fields.GoalBoard,
Size: tt.fields.Size,
}
if got := p.IsSolvable(); got != tt.want {
t.Errorf("Puzzle.IsSolvable() = %v, want %v", got, tt.want)
}
})
}
}