-
-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathbrush_custom_test.go
More file actions
379 lines (334 loc) · 8.74 KB
/
Copy pathbrush_custom_test.go
File metadata and controls
379 lines (334 loc) · 8.74 KB
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
package gg
import (
"math"
"testing"
)
// TestCustomBrushColorAt tests CustomBrush sampling.
func TestCustomBrushColorAt(t *testing.T) {
tests := []struct {
name string
brush CustomBrush
x, y float64
want RGBA
}{
{
"constant color",
NewCustomBrush(func(_, _ float64) RGBA { return Red }),
50, 50, Red,
},
{
"x-based",
NewCustomBrush(func(x, _ float64) RGBA {
if x > 50 {
return Blue
}
return Red
}),
100, 0, Blue,
},
{
"y-based",
NewCustomBrush(func(_, y float64) RGBA {
if y > 50 {
return Green
}
return Red
}),
0, 100, Green,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := tt.brush.ColorAt(tt.x, tt.y)
if got != tt.want {
t.Errorf("ColorAt(%v, %v) = %v, want %v", tt.x, tt.y, got, tt.want)
}
})
}
}
// TestCustomBrushNilFunc tests that nil function returns transparent.
func TestCustomBrushNilFunc(t *testing.T) {
brush := CustomBrush{Func: nil}
got := brush.ColorAt(50, 50)
if got != Transparent {
t.Errorf("nil Func ColorAt = %v, want Transparent", got)
}
}
// TestCustomBrushWithName tests the WithName method.
func TestCustomBrushWithName(t *testing.T) {
brush := NewCustomBrush(func(_, _ float64) RGBA { return Red })
named := brush.WithName("test_brush")
if named.Name != "test_brush" {
t.Errorf("Name = %q, want %q", named.Name, "test_brush")
}
// Verify function still works
if named.ColorAt(0, 0) != Red {
t.Error("WithName broke the color function")
}
}
// TestCustomBrushInterface verifies CustomBrush implements Brush.
func TestCustomBrushInterface(t *testing.T) {
var _ Brush = CustomBrush{}
var _ Brush = NewCustomBrush(func(_, _ float64) RGBA { return Red })
}
// TestHorizontalGradient tests horizontal gradient creation.
func TestHorizontalGradient(t *testing.T) {
gradient := HorizontalGradient(Red, Blue, 0, 100)
tests := []struct {
name string
x float64
wantR float64
wantB float64
}{
{"left edge", 0, 1, 0},
{"middle", 50, 0.5, 0.5},
{"right edge", 100, 0, 1},
{"before left (clamped)", -50, 1, 0},
{"after right (clamped)", 150, 0, 1},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := gradient.ColorAt(tt.x, 50)
if !near(c.R, tt.wantR) {
t.Errorf("x=%v: R = %v, want %v", tt.x, c.R, tt.wantR)
}
if !near(c.B, tt.wantB) {
t.Errorf("x=%v: B = %v, want %v", tt.x, c.B, tt.wantB)
}
})
}
}
// TestVerticalGradient tests vertical gradient creation.
func TestVerticalGradient(t *testing.T) {
gradient := VerticalGradient(White, Black, 0, 100)
tests := []struct {
name string
y float64
wantRGB float64
}{
{"top edge", 0, 1},
{"middle", 50, 0.5},
{"bottom edge", 100, 0},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := gradient.ColorAt(50, tt.y)
if !near(c.R, tt.wantRGB) {
t.Errorf("y=%v: R = %v, want %v", tt.y, c.R, tt.wantRGB)
}
})
}
}
// TestLinearGradient tests arbitrary linear gradient.
func TestLinearGradient(t *testing.T) {
// Diagonal gradient from (0,0) to (100,100)
gradient := LinearGradient(Red, Blue, 0, 0, 100, 100)
tests := []struct {
name string
x, y float64
wantR float64
wantB float64
}{
{"origin", 0, 0, 1, 0},
{"end", 100, 100, 0, 1},
{"middle", 50, 50, 0.5, 0.5},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := gradient.ColorAt(tt.x, tt.y)
if !near2(c.R, tt.wantR, 0.01) {
t.Errorf("(%v,%v): R = %v, want %v", tt.x, tt.y, c.R, tt.wantR)
}
if !near2(c.B, tt.wantB, 0.01) {
t.Errorf("(%v,%v): B = %v, want %v", tt.x, tt.y, c.B, tt.wantB)
}
})
}
}
// TestLinearGradientZeroLength tests gradient with same start/end point.
func TestLinearGradientZeroLength(t *testing.T) {
gradient := LinearGradient(Red, Blue, 50, 50, 50, 50)
c := gradient.ColorAt(50, 50)
// Should return start color
if c != Red {
t.Errorf("Zero length gradient = %v, want Red", c)
}
}
// TestRadialGradient tests radial gradient creation.
func TestRadialGradient(t *testing.T) {
gradient := RadialGradient(White, Black, 50, 50, 50)
tests := []struct {
name string
x, y float64
wantRGB float64
}{
{"center", 50, 50, 1},
{"edge right", 100, 50, 0},
{"edge top", 50, 0, 0},
{"halfway", 75, 50, 0.5},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := gradient.ColorAt(tt.x, tt.y)
if !near2(c.R, tt.wantRGB, 0.01) {
t.Errorf("(%v,%v): R = %v, want %v", tt.x, tt.y, c.R, tt.wantRGB)
}
})
}
}
// TestRadialGradientZeroRadius tests gradient with zero radius.
func TestRadialGradientZeroRadius(t *testing.T) {
gradient := RadialGradient(White, Black, 50, 50, 0)
c := gradient.ColorAt(50, 50)
// Should return center color
if c != White {
t.Errorf("Zero radius gradient = %v, want White", c)
}
}
// TestCheckerboard tests checkerboard pattern.
func TestCheckerboard(t *testing.T) {
checker := Checkerboard(Black, White, 10)
tests := []struct {
name string
x, y float64
want RGBA
}{
{"origin", 0, 0, Black},
{"first white", 10, 0, White},
{"next black", 20, 0, Black},
{"diag white", 10, 10, Black},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := checker.ColorAt(tt.x, tt.y)
if got != tt.want {
t.Errorf("(%v,%v) = %v, want %v", tt.x, tt.y, got, tt.want)
}
})
}
}
// TestCheckerboardZeroSize tests checkerboard with zero size.
func TestCheckerboardZeroSize(t *testing.T) {
checker := Checkerboard(Black, White, 0)
// Should default to size 1
c1 := checker.ColorAt(0, 0)
c2 := checker.ColorAt(1, 0)
if c1 == c2 {
t.Error("Zero size should default to 1, producing alternating colors")
}
}
// TestStripes tests stripe pattern.
func TestStripes(t *testing.T) {
stripes := Stripes(Red, Blue, 10, 0) // Vertical stripes
tests := []struct {
name string
x float64
want RGBA
}{
{"first stripe", 5, Red},
{"second stripe", 15, Blue},
{"third stripe", 25, Red},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := stripes.ColorAt(tt.x, 50)
if got != tt.want {
t.Errorf("x=%v: got %v, want %v", tt.x, got, tt.want)
}
})
}
}
// TestStripesRotated tests rotated stripe pattern.
func TestStripesRotated(t *testing.T) {
// 90 degree rotation = horizontal stripes
stripes := Stripes(Red, Blue, 10, math.Pi/2)
// At 90 degrees, the pattern is based on y, not x
c1 := stripes.ColorAt(0, 5)
c2 := stripes.ColorAt(0, 15)
if c1 == c2 {
t.Error("Rotated stripes should produce different colors at different y")
}
}
// TestStripesZeroWidth tests stripes with zero width.
func TestStripesZeroWidth(t *testing.T) {
stripes := Stripes(Red, Blue, 0, 0)
// Should default to width 1
c1 := stripes.ColorAt(0, 0)
c2 := stripes.ColorAt(1, 0)
if c1 == c2 {
t.Error("Zero width should default to 1, producing alternating colors")
}
}
// TestClampT tests the clampT helper function.
func TestClampT(t *testing.T) {
tests := []struct {
name string
val float64
want float64
}{
{"below zero", -0.5, 0},
{"zero", 0, 0},
{"middle", 0.5, 0.5},
{"one", 1, 1},
{"above one", 1.5, 1},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := clampT(tt.val)
if got != tt.want {
t.Errorf("clampT(%v) = %v, want %v", tt.val, got, tt.want)
}
})
}
}
// TestSolidBrushToCustomBrush tests conversion.
func TestSolidBrushToCustomBrush(t *testing.T) {
solid := Solid(Red)
custom := solid.toCustomBrush()
if custom.Name != "solid" {
t.Errorf("Name = %q, want %q", custom.Name, "solid")
}
// Should return same color everywhere
if custom.ColorAt(0, 0) != Red {
t.Error("Expected Red at origin")
}
if custom.ColorAt(1000, 1000) != Red {
t.Error("Expected Red at (1000, 1000)")
}
}
// BenchmarkCustomBrushColorAt benchmarks CustomBrush sampling.
func BenchmarkCustomBrushColorAt(b *testing.B) {
brush := NewCustomBrush(func(x, y float64) RGBA {
return RGBA{R: x / 100, G: y / 100, B: 0.5, A: 1}
})
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = brush.ColorAt(float64(i%100), float64(i%100))
}
}
// BenchmarkHorizontalGradient benchmarks gradient sampling.
func BenchmarkHorizontalGradient(b *testing.B) {
gradient := HorizontalGradient(Red, Blue, 0, 100)
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = gradient.ColorAt(float64(i%100), 50)
}
}
// BenchmarkRadialGradient benchmarks radial gradient sampling.
func BenchmarkRadialGradient(b *testing.B) {
gradient := RadialGradient(White, Black, 50, 50, 50)
b.ResetTimer()
for i := 0; i < b.N; i++ {
x := float64(i % 100)
y := float64((i / 100) % 100)
_ = gradient.ColorAt(x, y)
}
}
// BenchmarkCheckerboard benchmarks checkerboard sampling.
func BenchmarkCheckerboard(b *testing.B) {
checker := Checkerboard(Black, White, 10)
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = checker.ColorAt(float64(i%100), float64(i%100))
}
}