|
1 | 1 | package de.ronny_h.aoc.extensions.grids |
2 | 2 |
|
| 3 | +import de.ronny_h.aoc.extensions.grids.Direction.* |
| 4 | +import de.ronny_h.aoc.extensions.grids.Turn.* |
3 | 5 | import io.kotest.core.spec.style.StringSpec |
4 | 6 | import io.kotest.data.forAll |
5 | 7 | import io.kotest.data.row |
@@ -46,10 +48,10 @@ class CoordinatesTest : StringSpec({ |
46 | 48 |
|
47 | 49 | "Add a direction" { |
48 | 50 | forAll( |
49 | | - row(Coordinates(5, 5), Direction.NORTH, Coordinates(4, 5)), |
50 | | - row(Coordinates(5, 5), Direction.SOUTH, Coordinates(6, 5)), |
51 | | - row(Coordinates(5, 5), Direction.EAST, Coordinates(5, 6)), |
52 | | - row(Coordinates(5, 5), Direction.WEST, Coordinates(5, 4)), |
| 51 | + row(Coordinates(5, 5), NORTH, Coordinates(4, 5)), |
| 52 | + row(Coordinates(5, 5), SOUTH, Coordinates(6, 5)), |
| 53 | + row(Coordinates(5, 5), EAST, Coordinates(5, 6)), |
| 54 | + row(Coordinates(5, 5), WEST, Coordinates(5, 4)), |
53 | 55 | ) { coordinates, direction, result -> |
54 | 56 | coordinates + direction shouldBe result |
55 | 57 | } |
@@ -79,76 +81,108 @@ class CoordinatesTest : StringSpec({ |
79 | 81 |
|
80 | 82 | "Directed neighbours" { |
81 | 83 | Coordinates(5, 5).directedNeighbours() shouldContainAll listOf( |
82 | | - Direction.NORTH to Coordinates(4, 5), |
83 | | - Direction.SOUTH to Coordinates(6, 5), |
84 | | - Direction.EAST to Coordinates(5, 6), |
85 | | - Direction.WEST to Coordinates(5, 4), |
| 84 | + NORTH to Coordinates(4, 5), |
| 85 | + SOUTH to Coordinates(6, 5), |
| 86 | + EAST to Coordinates(5, 6), |
| 87 | + WEST to Coordinates(5, 4), |
86 | 88 | ) |
87 | 89 | } |
88 | 90 |
|
| 91 | + "Coordinates are comparable and can be sorted" { |
| 92 | + forAll( |
| 93 | + row(listOf(Coordinates.ZERO), listOf(Coordinates.ZERO)), |
| 94 | + row(listOf(Coordinates(1, 0), Coordinates.ZERO), listOf(Coordinates.ZERO, Coordinates(1, 0))), |
| 95 | + row( |
| 96 | + listOf(Coordinates(1, 1), Coordinates(1, 0), Coordinates.ZERO), |
| 97 | + listOf(Coordinates.ZERO, Coordinates(1, 0), Coordinates(1, 1)) |
| 98 | + ), |
| 99 | + ) { list, sorted -> |
| 100 | + list.sorted() shouldBe sorted |
| 101 | + } |
| 102 | + } |
| 103 | + |
89 | 104 | "Direction turnRight() turns right" { |
90 | | - Direction.NORTH.turnRight() shouldBe Direction.EAST |
91 | | - Direction.EAST.turnRight() shouldBe Direction.SOUTH |
92 | | - Direction.SOUTH.turnRight() shouldBe Direction.WEST |
93 | | - Direction.WEST.turnRight() shouldBe Direction.NORTH |
| 105 | + NORTH.turnRight() shouldBe EAST |
| 106 | + EAST.turnRight() shouldBe SOUTH |
| 107 | + SOUTH.turnRight() shouldBe WEST |
| 108 | + WEST.turnRight() shouldBe NORTH |
94 | 109 | } |
95 | 110 |
|
96 | 111 | "Direction turnLeft() turns left" { |
97 | | - Direction.NORTH.turnLeft() shouldBe Direction.WEST |
98 | | - Direction.EAST.turnLeft() shouldBe Direction.NORTH |
99 | | - Direction.SOUTH.turnLeft() shouldBe Direction.EAST |
100 | | - Direction.WEST.turnLeft() shouldBe Direction.SOUTH |
| 112 | + NORTH.turnLeft() shouldBe WEST |
| 113 | + EAST.turnLeft() shouldBe NORTH |
| 114 | + SOUTH.turnLeft() shouldBe EAST |
| 115 | + WEST.turnLeft() shouldBe SOUTH |
101 | 116 | } |
102 | 117 |
|
103 | 118 | "Direction reverse() does a u-turn" { |
104 | | - Direction.NORTH.reverse() shouldBe Direction.SOUTH |
105 | | - Direction.EAST.reverse() shouldBe Direction.WEST |
106 | | - Direction.SOUTH.reverse() shouldBe Direction.NORTH |
107 | | - Direction.WEST.reverse() shouldBe Direction.EAST |
| 119 | + NORTH.reverse() shouldBe SOUTH |
| 120 | + EAST.reverse() shouldBe WEST |
| 121 | + SOUTH.reverse() shouldBe NORTH |
| 122 | + WEST.reverse() shouldBe EAST |
108 | 123 | } |
109 | 124 |
|
110 | 125 | "asChar gives a graphical representation" { |
111 | | - Direction.NORTH.asChar() shouldBe '↑' |
112 | | - Direction.EAST.asChar() shouldBe '→' |
113 | | - Direction.SOUTH.asChar() shouldBe '↓' |
114 | | - Direction.WEST.asChar() shouldBe '←' |
| 126 | + NORTH.asChar() shouldBe '↑' |
| 127 | + EAST.asChar() shouldBe '→' |
| 128 | + SOUTH.asChar() shouldBe '↓' |
| 129 | + WEST.asChar() shouldBe '←' |
115 | 130 | } |
116 | 131 |
|
117 | 132 | "A Direction's orientation is checked right" { |
118 | | - Direction.NORTH.isVertical() shouldBe true |
119 | | - Direction.NORTH.isHorizontal() shouldBe false |
120 | | - Direction.SOUTH.isVertical() shouldBe true |
121 | | - Direction.SOUTH.isHorizontal() shouldBe false |
| 133 | + NORTH.isVertical() shouldBe true |
| 134 | + NORTH.isHorizontal() shouldBe false |
| 135 | + SOUTH.isVertical() shouldBe true |
| 136 | + SOUTH.isHorizontal() shouldBe false |
122 | 137 |
|
123 | | - Direction.EAST.isVertical() shouldBe false |
124 | | - Direction.EAST.isHorizontal() shouldBe true |
125 | | - Direction.WEST.isVertical() shouldBe false |
126 | | - Direction.WEST.isHorizontal() shouldBe true |
| 138 | + EAST.isVertical() shouldBe false |
| 139 | + EAST.isHorizontal() shouldBe true |
| 140 | + WEST.isVertical() shouldBe false |
| 141 | + WEST.isHorizontal() shouldBe true |
127 | 142 | } |
128 | 143 |
|
129 | 144 | "Opposite directions" { |
130 | | - Direction.NORTH.isOpposite(Direction.SOUTH) shouldBe true |
131 | | - Direction.SOUTH.isOpposite(Direction.NORTH) shouldBe true |
132 | | - Direction.EAST.isOpposite(Direction.WEST) shouldBe true |
133 | | - Direction.WEST.isOpposite(Direction.EAST) shouldBe true |
| 145 | + NORTH.isOpposite(SOUTH) shouldBe true |
| 146 | + SOUTH.isOpposite(NORTH) shouldBe true |
| 147 | + EAST.isOpposite(WEST) shouldBe true |
| 148 | + WEST.isOpposite(EAST) shouldBe true |
134 | 149 |
|
135 | | - Direction.NORTH.isOpposite(Direction.EAST) shouldBe false |
136 | | - Direction.SOUTH.isOpposite(Direction.EAST) shouldBe false |
137 | | - Direction.EAST.isOpposite(Direction.SOUTH) shouldBe false |
138 | | - Direction.WEST.isOpposite(Direction.SOUTH) shouldBe false |
| 150 | + NORTH.isOpposite(EAST) shouldBe false |
| 151 | + SOUTH.isOpposite(EAST) shouldBe false |
| 152 | + EAST.isOpposite(SOUTH) shouldBe false |
| 153 | + WEST.isOpposite(SOUTH) shouldBe false |
139 | 154 | } |
140 | 155 |
|
141 | 156 | "Difference between directions" { |
142 | | - Direction.NORTH - Direction.NORTH shouldBe 0 |
143 | | - Direction.NORTH - Direction.EAST shouldBe 1 |
144 | | - Direction.NORTH - Direction.SOUTH shouldBe 2 |
145 | | - Direction.NORTH - Direction.WEST shouldBe 1 |
| 157 | + NORTH - NORTH shouldBe 0 |
| 158 | + NORTH - EAST shouldBe 1 |
| 159 | + NORTH - SOUTH shouldBe 2 |
| 160 | + NORTH - WEST shouldBe 1 |
146 | 161 | } |
147 | 162 |
|
148 | 163 | "toString returns the abbreviation letter" { |
149 | | - Direction.NORTH.toString() shouldBe "N" |
150 | | - Direction.SOUTH.toString() shouldBe "S" |
151 | | - Direction.EAST.toString() shouldBe "E" |
152 | | - Direction.WEST.toString() shouldBe "W" |
| 164 | + NORTH.toString() shouldBe "N" |
| 165 | + SOUTH.toString() shouldBe "S" |
| 166 | + EAST.toString() shouldBe "E" |
| 167 | + WEST.toString() shouldBe "W" |
| 168 | + } |
| 169 | + |
| 170 | + "turning unsing Turns" { |
| 171 | + forAll( |
| 172 | + row(NORTH, STRAIGHT, NORTH), |
| 173 | + row(NORTH, LEFT, WEST), |
| 174 | + row(NORTH, RIGHT, EAST), |
| 175 | + row(SOUTH, STRAIGHT, SOUTH), |
| 176 | + row(SOUTH, LEFT, EAST), |
| 177 | + row(SOUTH, RIGHT, WEST), |
| 178 | + row(EAST, STRAIGHT, EAST), |
| 179 | + row(EAST, LEFT, NORTH), |
| 180 | + row(EAST, RIGHT, SOUTH), |
| 181 | + row(WEST, STRAIGHT, WEST), |
| 182 | + row(WEST, LEFT, SOUTH), |
| 183 | + row(WEST, RIGHT, NORTH), |
| 184 | + ) { direction, turn, newDirection -> |
| 185 | + direction.turn(turn) shouldBe newDirection |
| 186 | + } |
153 | 187 | } |
154 | 188 | }) |
0 commit comments