forked from akavel/polyclip-go
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdraw_test.go
62 lines (54 loc) · 2.15 KB
/
draw_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
// Copyright (c) 2011 Mateusz Czapliński
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package polyutil
import (
. "testing"
)
func max(a, b int) int {
if a >= b {
return a
}
return b
}
func TestLine(t *T) {
cases := []struct{ x0, y0, x1, y1, sx, sy int }{
{0, 0, 0, 0, 0, 0},
{0, 0, 1, 0, 1, 0},
{0, 0, -1, 1, -1, 1},
{-2, -2, 2, 2, 1, 1},
{10, 2, 20, 4, 1, 1},
{2, 10, 10, 5, 1, -1},
{1000, -50, -100, -3, -1, 1},
{10, 20, 0, -30, -1, -1},
}
for i, c := range cases {
p := 0
lastx, lasty := c.x0, c.y0
drawline(c.x0, c.y0, c.x1, c.y1, func(x, y int) {
verify(t, x == lastx || x == (lastx+c.sx), "Line %d, point %d, got x=%d, expected %d or %d", i, p, x, lastx, lastx+c.sx)
verify(t, y == lasty || y == (lasty+c.sy), "Line %d, point %d, got y=%d, expected %d or %d", i, p, y, lasty, lasty+c.sy)
lastx, lasty = x, y
p++
})
verify(t, lastx == c.x1, "After line %d, got x==%d, expected %d", i, lastx, c.x1)
verify(t, lasty == c.y1, "After line %d, got y==%d, expected %d", i, lasty, c.y1)
pn := max(abs(c.x1-c.x0), abs(c.y1-c.y0)) + 1
verify(t, p == pn, "After line %d, got %d points, expected %d", i, p, pn)
}
}