-
Notifications
You must be signed in to change notification settings - Fork 0
/
board_test.go
133 lines (128 loc) · 3.06 KB
/
board_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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package main
import (
"testing"
)
func TestBoardInfo_GridToXY(t *testing.T) {
type fields struct {
GridWidth int
GridHeight int
GridSize int
}
type args struct {
gridX int
gridY int
}
tests := []struct {
name string
fields fields
args args
want float32
want1 float32
}{
{"0,0", fields{20, 20, 40}, args{0, 0}, 0, 0},
{"19,19", fields{20, 20, 40}, args{19, 19}, 760, 760},
{"18,19", fields{20, 20, 40}, args{18, 19}, 720, 760},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
b := &BoardInfo{
GridWidth: tt.fields.GridWidth,
GridHeight: tt.fields.GridHeight,
GridSize: tt.fields.GridSize,
}
got, got1, err := b.GridToXY(tt.args.gridX, tt.args.gridY)
if err != nil {
t.Errorf("Error should be nil, got %v", err)
}
if got != tt.want {
t.Errorf("BoardInfo.GridToXY() got = %v, want %v", got, tt.want)
}
if got1 != tt.want1 {
t.Errorf("BoardInfo.GridToXY() got1 = %v, want %v", got1, tt.want1)
}
})
}
}
func TestBoardInfo_GridToXYError(t *testing.T) {
type fields struct {
GridWidth int
GridHeight int
GridSize int
}
type args struct {
gridX int
gridY int
}
tests := []struct {
name string
fields fields
args args
wantErr error
}{
{"21,20", fields{20, 20, 40}, args{21, 20}, &BoardError{}},
{"-1,-1", fields{20, 20, 40}, args{-1, -1}, &BoardError{}},
{"20,100", fields{20, 20, 40}, args{20, 100}, &BoardError{}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
b := &BoardInfo{
GridWidth: tt.fields.GridWidth,
GridHeight: tt.fields.GridHeight,
GridSize: tt.fields.GridSize,
}
got, got1, err := b.GridToXY(tt.args.gridX, tt.args.gridY)
if err == nil {
t.Errorf("Expected error but got nil %v, %v", got, got1)
} else {
switch err.(type) {
case *BoardError:
default:
t.Errorf("Expected BoardError but got %T", err)
}
}
if got != -1 {
t.Errorf("Expected -1 for x got %v", got)
}
if got1 != -1 {
t.Errorf("Expected -1 for y got %v", got1)
}
})
}
}
func TestBoardInfo_CanOccupySpace(t *testing.T) {
type args struct {
o Object
gx int
gy int
}
occupied := make([]Object, 16)
occupied[3] = &ObjectInfo{}
occupied[5] = &ObjectInfo{}
board := BoardInfo{
GridWidth: 4,
GridHeight: 4,
occupied: occupied,
}
tests := []struct {
name string
args args
want bool
}{
// TODO: Add test cases.
{"0,3", args{&ObjectInfo{0, 1, 1}, 0, 3}, false},
{"3,0", args{&ObjectInfo{0, 1, 1}, 3, 0}, true},
{"0,4", args{&ObjectInfo{0, 1, 1}, 0, 4}, false},
{"1,1", args{&ObjectInfo{0, 1, 1}, 1, 1}, false},
{"1,2", args{&ObjectInfo{0, 1, 1}, 1, 2}, true},
{"2,1", args{&ObjectInfo{0, 1, 1}, 2, 1}, true},
{"-1,-1", args{&ObjectInfo{0, 1, 1}, -1, -1}, false},
{"10,10", args{&ObjectInfo{0, 1, 1}, 10, 10}, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := board.CanOccupySpace(tt.args.o, tt.args.gx, tt.args.gy); got != tt.want {
t.Errorf("BoardInfo.CanOccupySpace() = %v, want %v", got, tt.want)
}
})
}
}