]> git.scottworley.com Git - slidingtile/blobdiff - sliding_tile_test.go
Adjacent spaces
[slidingtile] / sliding_tile_test.go
index 41be233cb203728499723359393edd1f4a889282..34f3fcc5ba46af1b78dd61b262c293b4efaf2189 100644 (file)
@@ -1,28 +1,70 @@
 package main
 
 package main
 
+import "reflect"
+import "sort"
 import "testing"
 
 func Test_read_board_from_strings(t *testing.T) {
 import "testing"
 
 func Test_read_board_from_strings(t *testing.T) {
-  b, err := read_board_from_strings([]string{"1","2","9","12","6","8","13","14","0","11","15","3","7","4","10","5"})
-  if err != nil || *b != Board([BOARD_SIZE]Space{1,2,9,12,6,8,13,14,0,11,15,3,7,4,10,5}) {
-    t.Fail()
-  }
-
-  // Not enough
-  b, err = read_board_from_strings([]string{"1","2","9","12","6","8","13","14","0","11","15","3","7","4","10"})
-  if err == nil {
-    t.Fail()
-  }
-
-  // Not an integer
-  b, err = read_board_from_strings([]string{"1","foo","9","12","6","8","13","14","0","11","15","3","7","4","10","5"})
-  if err == nil {
-    t.Fail()
-  }
-
-  // Empty string
-  b, err = read_board_from_strings([]string{"1","","9","12","6","8","13","14","0","11","15","3","7","4","10","5"})
-  if err == nil {
-    t.Fail()
-  }
+       b, err := read_board_from_strings([]string{"1", "2", "9", "12", "6", "8", "13", "14", "0", "11", "15", "3", "7", "4", "10", "5"})
+       if err != nil || *b != Board([BOARD_SIZE]Space{1, 2, 9, 12, 6, 8, 13, 14, 0, 11, 15, 3, 7, 4, 10, 5}) {
+               t.Fail()
+       }
+
+       // Not enough
+       b, err = read_board_from_strings([]string{"1", "2", "9", "12", "6", "8", "13", "14", "0", "11", "15", "3", "7", "4", "10"})
+       if err == nil {
+               t.Fail()
+       }
+
+       // Not an integer
+       b, err = read_board_from_strings([]string{"1", "foo", "9", "12", "6", "8", "13", "14", "0", "11", "15", "3", "7", "4", "10", "5"})
+       if err == nil {
+               t.Fail()
+       }
+
+       // Empty string
+       b, err = read_board_from_strings([]string{"1", "", "9", "12", "6", "8", "13", "14", "0", "11", "15", "3", "7", "4", "10", "5"})
+       if err == nil {
+               t.Fail()
+       }
+}
+
+// For sorting, for unordered equality testing
+type Spaces []Space
+
+func (s Spaces) Len() int           { return len(s) }
+func (s Spaces) Swap(i, j int)      { s[i], s[j] = s[j], s[i] }
+func (s Spaces) Less(i, j int) bool { return s[i] < s[j] }
+
+func Test_adjacent_spaces(t *testing.T) {
+       type TestCase struct {
+               s        Space
+               expected Spaces
+       }
+       cases := []TestCase{
+               {0, []Space{1, 4}},
+               {1, []Space{0, 2, 5}},
+               {2, []Space{1, 3, 6}},
+               {3, []Space{2, 7}},
+               {4, []Space{0, 5, 8}},
+               {5, []Space{1, 4, 6, 9}},
+               {6, []Space{2, 5, 7, 10}},
+               {7, []Space{3, 6, 11}},
+               {8, []Space{4, 9, 12}},
+               {9, []Space{5, 8, 10, 13}},
+               {10, []Space{6, 9, 11, 14}},
+               {11, []Space{7, 10, 15}},
+               {12, []Space{8, 13}},
+               {13, []Space{9, 12, 14}},
+               {14, []Space{10, 13, 15}},
+               {15, []Space{11, 14}},
+       }
+       for i, c := range cases {
+               var actual Spaces = adjacent_spaces(c.s)
+               sort.Sort(c.expected)
+               sort.Sort(actual)
+               if !reflect.DeepEqual(c.expected, actual) {
+                       t.Error(i, c.expected, actual)
+               }
+       }
 }
 }