@@ -8,41 +8,60 @@ import (
88)
99
1010func TestDirection_String (t * testing.T ) {
11- assert .Equal (t , E .String (), "E" )
12- assert .Equal (t , NE .String (), "NE" )
13- assert .Equal (t , N .String (), "N" )
14- assert .Equal (t , NW .String (), "NW" )
15- assert .Equal (t , W .String (), "W" )
16- assert .Equal (t , SW .String (), "SW" )
17- assert .Equal (t , S .String (), "S" )
18- assert .Equal (t , SE .String (), "SE" )
19- // values beyond 7 wrap via %8
20- assert .Equal (t , Direction (8 ).String (), "E" )
21- assert .Equal (t , Direction (10 ).String (), "N" )
11+ cases := []struct {
12+ name string
13+ dir Direction
14+ want string
15+ }{
16+ {"E" , E , "E" },
17+ {"NE" , NE , "NE" },
18+ {"N" , N , "N" },
19+ {"NW" , NW , "NW" },
20+ {"W" , W , "W" },
21+ {"SW" , SW , "SW" },
22+ {"S" , S , "S" },
23+ {"SE" , SE , "SE" },
24+ {"wrap_8_to_E" , Direction (8 ), "E" },
25+ {"wrap_10_to_N" , Direction (10 ), "N" },
26+ }
27+ for _ , tc := range cases {
28+ t .Run (tc .name , func (t * testing.T ) {
29+ assert .Equal (t , tc .dir .String (), tc .want )
30+ })
31+ }
2232}
2333
2434func TestDirection_Opposite (t * testing.T ) {
25- assert .Equal (t , E .Opposite (), W )
26- assert .Equal (t , NE .Opposite (), SW )
27- assert .Equal (t , N .Opposite (), S )
28- assert .Equal (t , NW .Opposite (), SE )
29- assert .Equal (t , W .Opposite (), E )
30- assert .Equal (t , SW .Opposite (), NE )
31- assert .Equal (t , S .Opposite (), N )
32- assert .Equal (t , SE .Opposite (), NW )
33-
34- // double opposite returns the original direction
35- for _ , d := range []Direction {E , NE , N , NW , W , SW , S , SE } {
36- assert .Equal (t , d .Opposite ().Opposite (), d )
37- }
38-
39- // neighbor vectors of opposite directions sum to zero
40- for _ , d := range []Direction {E , NE , N , NW } {
41- v := Directions [d ]
42- opp := Directions [d .Opposite ()]
43- assert .Equal (t , v .X + opp .X , 0 )
44- assert .Equal (t , v .Y + opp .Y , 0 )
45- }
35+ t .Run ("pairs" , func (t * testing.T ) {
36+ cases := []struct { dir , want Direction }{
37+ {E , W },
38+ {NE , SW },
39+ {N , S },
40+ {NW , SE },
41+ {W , E },
42+ {SW , NE },
43+ {S , N },
44+ {SE , NW },
45+ }
46+ for _ , tc := range cases {
47+ t .Run (tc .dir .String (), func (t * testing.T ) {
48+ assert .Equal (t , tc .dir .Opposite (), tc .want )
49+ })
50+ }
51+ })
52+ t .Run ("double opposite" , func (t * testing.T ) {
53+ for _ , d := range []Direction {E , NE , N , NW , W , SW , S , SE } {
54+ assert .Equal (t , d .Opposite ().Opposite (), d )
55+ }
56+ })
57+ t .Run ("vectors sum to zero" , func (t * testing.T ) {
58+ for _ , d := range []Direction {E , NE , N , NW } {
59+ v := Directions [d ]
60+ opp := Directions [d .Opposite ()]
61+ assert .Equal (t , v .X + opp .X , 0 )
62+ assert .Equal (t , v .Y + opp .Y , 0 )
63+ }
64+ })
4665}
4766
4867func TestAllDirections_Order (t * testing.T ) {
@@ -75,16 +94,19 @@ func TestNeighborOffsets_Panic(t *testing.T) {
7594}
7695
7796func TestNeighborOffset (t * testing.T ) {
78- // Cardinal: all four cardinal directions return the correct vector.
79- assert .Equal (t , NeighborOffset (Cardinal , E ), geom .Vec (1 , 0 ))
80- assert .Equal (t , NeighborOffset (Cardinal , N ), geom .Vec (0 , - 1 ))
81- assert .Equal (t , NeighborOffset (Cardinal , W ), geom .Vec (- 1 , 0 ))
82- assert .Equal (t , NeighborOffset (Cardinal , S ), geom .Vec (0 , 1 ))
83-
84- // Diagonal: all eight directions return the correct vector.
85- for _ , d := range []Direction {E , NE , N , NW , W , SW , S , SE } {
86- assert .Equal (t , NeighborOffset (Diagonal , d ), Directions [d ])
87- }
97+ t .Run ("cardinal" , func (t * testing.T ) {
98+ assert .Equal (t , NeighborOffset (Cardinal , E ), geom .Vec (1 , 0 ))
99+ assert .Equal (t , NeighborOffset (Cardinal , N ), geom .Vec (0 , - 1 ))
100+ assert .Equal (t , NeighborOffset (Cardinal , W ), geom .Vec (- 1 , 0 ))
101+ assert .Equal (t , NeighborOffset (Cardinal , S ), geom .Vec (0 , 1 ))
102+ })
103+ t .Run ("diagonal" , func (t * testing.T ) {
104+ for _ , d := range []Direction {E , NE , N , NW , W , SW , S , SE } {
105+ t .Run (d .String (), func (t * testing.T ) {
106+ assert .Equal (t , NeighborOffset (Diagonal , d ), Directions [d ])
107+ })
108+ }
109+ })
88110}
89111
90112func TestNeighborOffset_Panic (t * testing.T ) {
@@ -99,17 +121,18 @@ func TestDistanceTo(t *testing.T) {
99121 origin := geom .Pt (0 , 0 )
100122 target := geom .Pt (3 , 4 )
101123
102- // Cardinal: Manhattan distance
103- assert .Equal (t , DistanceTo (origin , target , Cardinal ), 7 )
104- assert .Equal (t , DistanceTo (origin , geom .Pt (- 2 , 3 ), Cardinal ), 5 )
105-
106- // Diagonal: Chebyshev distance
107- assert .Equal (t , DistanceTo (origin , target , Diagonal ), 4 )
108- assert .Equal (t , DistanceTo (origin , geom .Pt (2 , 2 ), Diagonal ), 2 )
109-
110- // symmetric
111- assert .Equal (t , DistanceTo (origin , target , Cardinal ), DistanceTo (target , origin , Cardinal ))
112- assert .Equal (t , DistanceTo (origin , target , Diagonal ), DistanceTo (target , origin , Diagonal ))
124+ t .Run ("cardinal" , func (t * testing.T ) {
125+ assert .Equal (t , DistanceTo (origin , target , Cardinal ), 7 )
126+ assert .Equal (t , DistanceTo (origin , geom .Pt (- 2 , 3 ), Cardinal ), 5 )
127+ })
128+ t .Run ("diagonal" , func (t * testing.T ) {
129+ assert .Equal (t , DistanceTo (origin , target , Diagonal ), 4 )
130+ assert .Equal (t , DistanceTo (origin , geom .Pt (2 , 2 ), Diagonal ), 2 )
131+ })
132+ t .Run ("symmetric" , func (t * testing.T ) {
133+ assert .Equal (t , DistanceTo (origin , target , Cardinal ), DistanceTo (target , origin , Cardinal ))
134+ assert .Equal (t , DistanceTo (origin , target , Diagonal ), DistanceTo (target , origin , Diagonal ))
135+ })
113136}
114137
115138func TestDistanceTo_Panic (t * testing.T ) {
0 commit comments