main_test.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. package main
  2. import (
  3. "testing"
  4. )
  5. func TestRobotIsImmobilized(t *testing.T) {
  6. world := &World{
  7. width: 3, height: 3,
  8. wmap: []byte(
  9. "" +
  10. "###" +
  11. "#.#" +
  12. "###",
  13. ),
  14. }
  15. world.robot = indexOf(1, 1, world.width)
  16. cases := []byte("^v<>")
  17. for _, c := range cases {
  18. if world.Robot(c) {
  19. t.Errorf("Expected nothing to move, but robot moved %q", c)
  20. }
  21. }
  22. }
  23. func TestRobotMovesInAllDirections(t *testing.T) {
  24. world := &World{
  25. width: 3, height: 3,
  26. wmap: []byte(
  27. "" +
  28. "..." +
  29. "..." +
  30. "...",
  31. ),
  32. }
  33. center := indexOf(1, 1, world.width)
  34. cases := []byte("^v<>")
  35. for _, c := range cases {
  36. world.robot = center
  37. if !world.Robot(c) {
  38. t.Errorf("Expected robot to move %q, but robot did not move", c)
  39. }
  40. expected := -1
  41. switch c {
  42. case '<':
  43. expected = center - 1
  44. case '>':
  45. expected = center + 1
  46. case '^':
  47. expected = center - world.width
  48. case 'v':
  49. expected = center + world.width
  50. }
  51. actual := world.robot
  52. if actual != expected {
  53. actualX, actualY := coordOf(actual, world.width)
  54. expectedX, expectedY := coordOf(expected, world.width)
  55. t.Errorf("Expected robot at (%d, %d), but robot was at (%d, %d)",
  56. expectedX, expectedY,
  57. actualX, actualY,
  58. )
  59. }
  60. }
  61. }
  62. func TestRobotMovesUntilStoppedByWall(t *testing.T) {
  63. world := &World{
  64. width: 6, height: 3,
  65. wmap: []byte(
  66. "" +
  67. "######" +
  68. "#...##" +
  69. "######",
  70. ),
  71. }
  72. world.robot = indexOf(1, 1, world.width)
  73. moves := []byte("^v>^v>^v>^v>")
  74. for _, c := range moves {
  75. world.Robot(c)
  76. }
  77. expected := indexOf(3, 1, 6)
  78. actual := world.robot
  79. if actual != expected {
  80. actualX, actualY := coordOf(actual, world.width)
  81. expectedX, expectedY := coordOf(expected, world.width)
  82. t.Errorf("Expected robot at (%d, %d), but robot was at (%d, %d)",
  83. expectedX, expectedY,
  84. actualX, actualY,
  85. )
  86. }
  87. }