-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeometry_test.go
96 lines (79 loc) · 2.33 KB
/
geometry_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package main
import (
"reflect"
"testing"
)
func mineRectanglesSlice(mine *Mine) []Rectangle {
rectangles := make([]Rectangle, 0)
mine.RectanglesEach(func(r Rectangle) {
rectangles = append(rectangles, r)
})
return rectangles
}
func TestRightMineRectangles(t *testing.T) {
mine := Mine{position: Position{1, 0}, direction: Right}
rectangles := mineRectanglesSlice(&mine)
validRectangles := []Rectangle{
{Position{1, 0}, 2, 1, nil},
{Position{0, 1}, 4, 1, nil},
}
for i := range validRectangles {
if !rectangles[i].Equals(validRectangles[i]) {
t.Errorf("Rectangle %d is not valid", i)
}
}
}
func TestBottomMineRectangles(t *testing.T) {
mine := Mine{position: Position{0, 1}, direction: Bottom}
rectangles := mineRectanglesSlice(&mine)
validRectangles := []Rectangle{
{Position{0, 0}, 1, 4, nil},
{Position{1, 1}, 1, 2, nil},
}
for i := range validRectangles {
if !rectangles[i].Equals(validRectangles[i]) {
t.Errorf("Rectangle %d is not valid", i)
}
}
}
func TestLeftMineRectangles(t *testing.T) {
mine := Mine{position: Position{1, 0}, direction: Left}
rectangles := mineRectanglesSlice(&mine)
validRectangles := []Rectangle{
{Position{0, 0}, 4, 1, nil},
{Position{1, 1}, 2, 1, nil},
}
for i := range validRectangles {
if !rectangles[i].Equals(validRectangles[i]) {
t.Errorf("Rectangle %d is not valid", i)
}
}
}
func TestTopMineRectangles(t *testing.T) {
mine := Mine{position: Position{0, 1}, direction: Top}
rectangles := mineRectanglesSlice(&mine)
validRectangles := []Rectangle{
{Position{0, 1}, 1, 2, nil},
{Position{1, 0}, 1, 4, nil},
}
for i := range validRectangles {
if !rectangles[i].Equals(validRectangles[i]) {
t.Errorf("Rectangle %d is not valid", i)
}
}
}
func TestRectangle_Positions(t *testing.T) {
rect := Rectangle{position: Position{0, 0}, width: 3, height: 2}
positions := []Position{{0, 0}, {0, 1}, {1, 0}, {1, 1}, {2, 0}, {2, 1}}
if !reflect.DeepEqual(rect.Positions(), positions) {
t.Error("Rectangle should have positions", positions, "received", rect.Positions())
}
}
func TestRectangle_PositionsCaching(t *testing.T) {
rect := Rectangle{position: Position{1, 1}, width: 1, height: 1}
positions := []Position{{1, 1}}
rect.Positions()
if !reflect.DeepEqual(rect.Positions(), positions) {
t.Error("Rectangle should have cached positions", positions)
}
}