]> git.scottworley.com Git - slidingtile/blob - sliding_tile_test.go
34f3fcc5ba46af1b78dd61b262c293b4efaf2189
[slidingtile] / sliding_tile_test.go
1 package main
2
3 import "reflect"
4 import "sort"
5 import "testing"
6
7 func Test_read_board_from_strings(t *testing.T) {
8 b, err := read_board_from_strings([]string{"1", "2", "9", "12", "6", "8", "13", "14", "0", "11", "15", "3", "7", "4", "10", "5"})
9 if err != nil || *b != Board([BOARD_SIZE]Space{1, 2, 9, 12, 6, 8, 13, 14, 0, 11, 15, 3, 7, 4, 10, 5}) {
10 t.Fail()
11 }
12
13 // Not enough
14 b, err = read_board_from_strings([]string{"1", "2", "9", "12", "6", "8", "13", "14", "0", "11", "15", "3", "7", "4", "10"})
15 if err == nil {
16 t.Fail()
17 }
18
19 // Not an integer
20 b, err = read_board_from_strings([]string{"1", "foo", "9", "12", "6", "8", "13", "14", "0", "11", "15", "3", "7", "4", "10", "5"})
21 if err == nil {
22 t.Fail()
23 }
24
25 // Empty string
26 b, err = read_board_from_strings([]string{"1", "", "9", "12", "6", "8", "13", "14", "0", "11", "15", "3", "7", "4", "10", "5"})
27 if err == nil {
28 t.Fail()
29 }
30 }
31
32 // For sorting, for unordered equality testing
33 type Spaces []Space
34
35 func (s Spaces) Len() int { return len(s) }
36 func (s Spaces) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
37 func (s Spaces) Less(i, j int) bool { return s[i] < s[j] }
38
39 func Test_adjacent_spaces(t *testing.T) {
40 type TestCase struct {
41 s Space
42 expected Spaces
43 }
44 cases := []TestCase{
45 {0, []Space{1, 4}},
46 {1, []Space{0, 2, 5}},
47 {2, []Space{1, 3, 6}},
48 {3, []Space{2, 7}},
49 {4, []Space{0, 5, 8}},
50 {5, []Space{1, 4, 6, 9}},
51 {6, []Space{2, 5, 7, 10}},
52 {7, []Space{3, 6, 11}},
53 {8, []Space{4, 9, 12}},
54 {9, []Space{5, 8, 10, 13}},
55 {10, []Space{6, 9, 11, 14}},
56 {11, []Space{7, 10, 15}},
57 {12, []Space{8, 13}},
58 {13, []Space{9, 12, 14}},
59 {14, []Space{10, 13, 15}},
60 {15, []Space{11, 14}},
61 }
62 for i, c := range cases {
63 var actual Spaces = adjacent_spaces(c.s)
64 sort.Sort(c.expected)
65 sort.Sort(actual)
66 if !reflect.DeepEqual(c.expected, actual) {
67 t.Error(i, c.expected, actual)
68 }
69 }
70 }