package main import ( "testing" ) func TestRobotIsImmobilized(t *testing.T) { world := &World{ width: 3, height: 3, wmap: []byte( "" + "###" + "#.#" + "###", ), } world.robot = indexOf(1, 1, world.width) cases := []byte("^v<>") for _, c := range cases { if world.Robot(c) { t.Errorf("Expected nothing to move, but robot moved %q", c) } } } func TestRobotMovesInAllDirections(t *testing.T) { world := &World{ width: 3, height: 3, wmap: []byte( "" + "..." + "..." + "...", ), } center := indexOf(1, 1, world.width) cases := []byte("^v<>") for _, c := range cases { world.robot = center if !world.Robot(c) { t.Errorf("Expected robot to move %q, but robot did not move", c) } expected := -1 switch c { case '<': expected = center - 1 case '>': expected = center + 1 case '^': expected = center - world.width case 'v': expected = center + world.width } actual := world.robot if actual != expected { actualX, actualY := coordOf(actual, world.width) expectedX, expectedY := coordOf(expected, world.width) t.Errorf("Expected robot at (%d, %d), but robot was at (%d, %d)", expectedX, expectedY, actualX, actualY, ) } } } func TestRobotMovesUntilStoppedByWall(t *testing.T) { world := &World{ width: 6, height: 3, wmap: []byte( "" + "######" + "#...##" + "######", ), } world.robot = indexOf(1, 1, world.width) moves := []byte("^v>^v>^v>^v>") for _, c := range moves { world.Robot(c) } expected := indexOf(3, 1, 6) actual := world.robot if actual != expected { actualX, actualY := coordOf(actual, world.width) expectedX, expectedY := coordOf(expected, world.width) t.Errorf("Expected robot at (%d, %d), but robot was at (%d, %d)", expectedX, expectedY, actualX, actualY, ) } }