|
package simple_search |
|
|
|
import ( |
|
"reflect" |
|
"testing" |
|
) |
|
|
|
// Lower numbers in the towers are larger disks. |
|
func makeWorld() *World { |
|
world := NewWorld(4, 5) |
|
world.Towers[0] = Tower{} |
|
world.Towers[1] = Tower{1, 2} |
|
world.Towers[2] = Tower{} |
|
world.Towers[3] = Tower{4} |
|
world.Towers[4] = Tower{3} |
|
return world |
|
} |
|
|
|
func TestNewWorld(t *testing.T) { |
|
for i := 0; i < 100; i++ { |
|
world := NewWorld(10, 5) |
|
sum := 0 |
|
for _, tower := range world.Towers { |
|
sum += len(tower) |
|
} |
|
if sum != 10 { |
|
t.Errorf("Not all blocks used (got %i, want %i)", sum, 10) |
|
} |
|
} |
|
} |
|
|
|
func TestClone(t *testing.T) { |
|
world := makeWorld() |
|
newWorld := world.Clone() |
|
if world == newWorld { |
|
t.Errorf("Worlds should not be same object") |
|
} |
|
if !reflect.DeepEqual(world, newWorld) { |
|
t.Errorf("Worlds should be the same structure") |
|
} |
|
} |
|
|
|
func TestActiveTowers(t *testing.T) { |
|
world := makeWorld() |
|
if open := world.ActiveTowers(); open != 3 { |
|
t.Errorf("ActiveTowers returned %i, expected %i", open, 3) |
|
} |
|
} |
|
|
|
func TestTopOf(t *testing.T) { |
|
world := makeWorld() |
|
if world.TopOf(0) != -1 { |
|
t.Errorf("TopOf(0) expected %i, got %i", -1, world.TopOf(0)) |
|
} |
|
if world.TopOf(1) != 2 { |
|
t.Errorf("TopOf(1) expected %i, got %i", 2, world.TopOf(1)) |
|
} |
|
if world.TopOf(2) != -1 { |
|
t.Errorf("TopOf(2) expected %i, got %i", -1, world.TopOf(2)) |
|
} |
|
if world.TopOf(3) != 4 { |
|
t.Errorf("TopOf(3) expected %i, got %i", 4, world.TopOf(3)) |
|
} |
|
if world.TopOf(4) != 3 { |
|
t.Errorf("TopOf(4) expected %i, got %i", 3, world.TopOf(4)) |
|
} |
|
} |
|
|
|
func TestMoves(t *testing.T) { |
|
world := makeWorld() |
|
moves := world.Moves() |
|
|
|
table := []Move{ |
|
Move{1, 0}, Move{1, 2}, |
|
Move{3, 0}, Move{3, 1}, Move{3, 2}, Move{3, 4}, |
|
Move{4, 0}, Move{4, 1}, Move{4, 2}, |
|
} |
|
|
|
if !reflect.DeepEqual(table, moves) { |
|
t.Errorf("Moves returned %q, expected %q", moves, table) |
|
} |
|
} |
|
|
|
func TestMoveApplyTo(t *testing.T) { |
|
world := makeWorld() |
|
move := Move{1, 2} |
|
resultingWorld := move.ApplyTo(world) |
|
if resultingWorld == world { |
|
t.Errorf("Worlds should not be same object") |
|
} |
|
|
|
world.Towers[1] = Tower{1} |
|
world.Towers[2] = Tower{2} |
|
|
|
if !reflect.DeepEqual(world.Towers, resultingWorld.Towers) { |
|
t.Errorf("Expected the resulting world to be correct, but got %q", resultingWorld.Towers) |
|
} |
|
|
|
if resultingWorld.Previous != world { |
|
t.Errorf("Expected the resulting world's Previous to point to the original world", resultingWorld) |
|
} |
|
} |
|
|
|
func TestSolve(t *testing.T) { |
|
world := makeWorld() |
|
solved := world.Solve() |
|
if !reflect.DeepEqual(solved.Towers[1], Tower{1, 2, 3, 4}) { |
|
t.Errorf("Expected the solved world to be solved, but got %q", solved) |
|
} |
|
} |
Lazy review from
golint
: