|
1 | 1 | package util
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "fmt" |
4 | 5 | "testing"
|
5 | 6 | )
|
6 | 7 |
|
@@ -28,3 +29,45 @@ func TestCoordinateDirections(t *testing.T) {
|
28 | 29 | })
|
29 | 30 | }
|
30 | 31 | }
|
| 32 | + |
| 33 | +func TestIn2DArray(t *testing.T) { |
| 34 | + matrix := [][]int{{0, 0}, {0, 0}} |
| 35 | + tests := []struct { |
| 36 | + c Coordinate |
| 37 | + expected bool |
| 38 | + }{ |
| 39 | + // Top left corner |
| 40 | + {Coordinate{X: -1, Y: -1}, false}, |
| 41 | + {Coordinate{X: +0, Y: -1}, false}, |
| 42 | + {Coordinate{X: -1, Y: +0}, false}, |
| 43 | + {Coordinate{X: +0, Y: +0}, true}, |
| 44 | + |
| 45 | + // Top right corner |
| 46 | + {Coordinate{X: +1, Y: -1}, false}, |
| 47 | + {Coordinate{X: +2, Y: -1}, false}, |
| 48 | + {Coordinate{X: +1, Y: +0}, true}, |
| 49 | + {Coordinate{X: +2, Y: +0}, false}, |
| 50 | + |
| 51 | + // Bottom left corner |
| 52 | + {Coordinate{X: -1, Y: +1}, false}, |
| 53 | + {Coordinate{X: +0, Y: +1}, true}, |
| 54 | + {Coordinate{X: -1, Y: +2}, false}, |
| 55 | + {Coordinate{X: +0, Y: +2}, false}, |
| 56 | + |
| 57 | + // Bottom right corner |
| 58 | + {Coordinate{X: +1, Y: +1}, true}, |
| 59 | + {Coordinate{X: +2, Y: +1}, false}, |
| 60 | + {Coordinate{X: +1, Y: +2}, false}, |
| 61 | + {Coordinate{X: +2, Y: +2}, false}, |
| 62 | + } |
| 63 | + |
| 64 | + for _, test := range tests { |
| 65 | + name := fmt.Sprintf("%v in 2D matrix", test.c) |
| 66 | + t.Run(name, func(t *testing.T) { |
| 67 | + actual := In2DArray(test.c, matrix) |
| 68 | + if test.expected != actual { |
| 69 | + t.Errorf("expected In2DArray(test.c, matrix) == %v but got %v", test.expected, actual) |
| 70 | + } |
| 71 | + }) |
| 72 | + } |
| 73 | +} |
0 commit comments