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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
|
package tui
import (
"testing"
)
func TestRect_Inset(t *testing.T) {
type tc struct {
rect Rect
edges Edges
expectedX, expectedY int
expectedWidth, expectedHeight int
}
tests := map[string]tc{
"uniform positive inset": {
rect: NewRect(10, 10, 100, 100),
edges: EdgeAll(5),
expectedX: 15,
expectedY: 15,
expectedWidth: 90,
expectedHeight: 90,
},
"different insets": {
rect: NewRect(0, 0, 100, 100),
edges: EdgeTRBL(10, 20, 30, 40),
expectedX: 40,
expectedY: 10,
expectedWidth: 40,
expectedHeight: 60,
},
"negative insets (expand)": {
rect: NewRect(10, 10, 50, 50),
edges: EdgeAll(-5),
expectedX: 5,
expectedY: 5,
expectedWidth: 60,
expectedHeight: 60,
},
"inset to zero": {
rect: NewRect(0, 0, 10, 10),
edges: EdgeAll(5),
expectedX: 5,
expectedY: 5,
expectedWidth: 0,
expectedHeight: 0,
},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
got := tt.rect.Inset(tt.edges)
if got.X != tt.expectedX || got.Y != tt.expectedY ||
got.Width != tt.expectedWidth || got.Height != tt.expectedHeight {
t.Errorf("Inset() = {%d, %d, %d, %d}, want {%d, %d, %d, %d}",
got.X, got.Y, got.Width, got.Height,
tt.expectedX, tt.expectedY, tt.expectedWidth, tt.expectedHeight)
}
})
}
}
func TestInsetUniform(t *testing.T) {
r := NewRect(10, 10, 100, 100)
got := InsetUniform(r, 10)
if got.X != 20 || got.Y != 20 || got.Width != 80 || got.Height != 80 {
t.Errorf("InsetUniform(10) = {%d, %d, %d, %d}, want {20, 20, 80, 80}",
got.X, got.Y, got.Width, got.Height)
}
}
func TestRect_Intersect(t *testing.T) {
type tc struct {
a, b Rect
expected Rect
}
tests := map[string]tc{
"overlapping rects": {
a: NewRect(0, 0, 20, 20),
b: NewRect(10, 10, 20, 20),
expected: NewRect(10, 10, 10, 10),
},
"same rect": {
a: NewRect(10, 10, 20, 20),
b: NewRect(10, 10, 20, 20),
expected: NewRect(10, 10, 20, 20),
},
"one inside other": {
a: NewRect(0, 0, 100, 100),
b: NewRect(20, 20, 30, 30),
expected: NewRect(20, 20, 30, 30),
},
"adjacent horizontal (no overlap)": {
a: NewRect(0, 0, 10, 10),
b: NewRect(10, 0, 10, 10),
expected: Rect{},
},
"adjacent vertical (no overlap)": {
a: NewRect(0, 0, 10, 10),
b: NewRect(0, 10, 10, 10),
expected: Rect{},
},
"disjoint": {
a: NewRect(0, 0, 10, 10),
b: NewRect(50, 50, 10, 10),
expected: Rect{},
},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
got := tt.a.Intersect(tt.b)
if got != tt.expected {
t.Errorf("Intersect() = %+v, want %+v", got, tt.expected)
}
got2 := tt.b.Intersect(tt.a)
if got2 != tt.expected {
t.Errorf("Intersect() (reversed) = %+v, want %+v", got2, tt.expected)
}
})
}
}
func TestRect_Union(t *testing.T) {
type tc struct {
a, b Rect
expected Rect
}
tests := map[string]tc{
"overlapping rects": {
a: NewRect(0, 0, 20, 20),
b: NewRect(10, 10, 20, 20),
expected: NewRect(0, 0, 30, 30),
},
"disjoint rects": {
a: NewRect(0, 0, 10, 10),
b: NewRect(20, 20, 10, 10),
expected: NewRect(0, 0, 30, 30),
},
"one inside other": {
a: NewRect(0, 0, 100, 100),
b: NewRect(20, 20, 30, 30),
expected: NewRect(0, 0, 100, 100),
},
"same rect": {
a: NewRect(10, 10, 20, 20),
b: NewRect(10, 10, 20, 20),
expected: NewRect(10, 10, 20, 20),
},
"one empty": {
a: NewRect(10, 10, 20, 20),
b: Rect{},
expected: NewRect(10, 10, 20, 20),
},
"both empty": {
a: Rect{},
b: Rect{},
expected: Rect{},
},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
got := tt.a.Union(tt.b)
if got != tt.expected {
t.Errorf("Union() = %+v, want %+v", got, tt.expected)
}
got2 := tt.b.Union(tt.a)
if got2 != tt.expected {
t.Errorf("Union() (reversed) = %+v, want %+v", got2, tt.expected)
}
})
}
}
func TestRect_Translate(t *testing.T) {
type tc struct {
rect Rect
dx, dy int
expected Rect
}
tests := map[string]tc{
"positive translation": {
rect: NewRect(10, 20, 30, 40),
dx: 5,
dy: 15,
expected: NewRect(15, 35, 30, 40),
},
"negative translation": {
rect: NewRect(10, 20, 30, 40),
dx: -5,
dy: -10,
expected: NewRect(5, 10, 30, 40),
},
"no translation": {
rect: NewRect(10, 20, 30, 40),
dx: 0,
dy: 0,
expected: NewRect(10, 20, 30, 40),
},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
got := tt.rect.Translate(tt.dx, tt.dy)
if got != tt.expected {
t.Errorf("Translate(%d, %d) = %+v, want %+v", tt.dx, tt.dy, got, tt.expected)
}
})
}
}
func TestRect_Clamp(t *testing.T) {
type tc struct {
rect Rect
x, y int
expectedX int
expectedY int
}
r := NewRect(10, 20, 30, 40)
tests := map[string]tc{
"point inside": {
rect: r,
x: 20,
y: 30,
expectedX: 20,
expectedY: 30,
},
"point left of rect": {
rect: r,
x: 5,
y: 30,
expectedX: 10,
expectedY: 30,
},
"point above rect": {
rect: r,
x: 20,
y: 10,
expectedX: 20,
expectedY: 20,
},
"point right of rect": {
rect: r,
x: 50,
y: 30,
expectedX: 39,
expectedY: 30,
},
"point below rect": {
rect: r,
x: 20,
y: 70,
expectedX: 20,
expectedY: 59,
},
"point outside all corners": {
rect: r,
x: 100,
y: 100,
expectedX: 39,
expectedY: 59,
},
"point at exact right edge": {
rect: r,
x: 40,
y: 30,
expectedX: 39,
expectedY: 30,
},
"point at exact bottom edge": {
rect: r,
x: 20,
y: 60,
expectedX: 20,
expectedY: 59,
},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
gotX, gotY := tt.rect.Clamp(tt.x, tt.y)
if gotX != tt.expectedX || gotY != tt.expectedY {
t.Errorf("Clamp(%d, %d) = (%d, %d), want (%d, %d)",
tt.x, tt.y, gotX, gotY, tt.expectedX, tt.expectedY)
}
})
}
}
func TestRect_Clamp_EmptyRect(t *testing.T) {
empty := Rect{}
x, y := empty.Clamp(10, 20)
if x != 0 || y != 0 {
t.Errorf("Clamp on empty rect = (%d, %d), want (0, 0)", x, y)
}
}
func TestRect_Immutability(t *testing.T) {
original := NewRect(10, 10, 20, 20)
_ = original.Inset(EdgeAll(5))
_ = InsetUniform(original, 5)
_ = original.Intersect(NewRect(0, 0, 100, 100))
_ = original.Union(NewRect(50, 50, 20, 20))
_ = original.Translate(10, 10)
if original.X != 10 || original.Y != 10 || original.Width != 20 || original.Height != 20 {
t.Error("original rect was modified by method calls")
}
}
|