-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutil_test.go
189 lines (150 loc) · 4.55 KB
/
util_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
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
package gopie
import (
"math"
"testing"
)
func TestPointsToPixels96DPI(t *testing.T) {
dpi := float64(96)
points := float64(3)
pixels := pointsToPixels(dpi, points)
expectedPixels := float64(4)
if pixels != expectedPixels {
t.Fatalf("Expected %vpt to be %vpx with %v DPI but found %vpx", points, expectedPixels, dpi, pixels)
}
}
func TestPointsToPixels72DPI(t *testing.T) {
dpi := float64(72)
points := float64(3)
pixels := pointsToPixels(dpi, points)
expectedPixels := float64(3)
if pixels != expectedPixels {
t.Fatalf("Expected %vpt to be %vpx with %v DPI but found %vpx", points, expectedPixels, dpi, pixels)
}
}
var eps = float64(0.00000001)
func floatEquals(a, b float64) bool {
if math.Abs(a-b) < eps {
return true
}
return false
}
func TestToDecartAngle0(t *testing.T) {
angle := float64(0)
radius := float64(10)
expectedX := float64(0)
expectedY := float64(-10)
x, y := toDecart(angle, radius)
if !(floatEquals(x, expectedX) && floatEquals(y, expectedY)) {
t.Fatalf("Expected point to be on (%v, %v) but found (%v, %v)", expectedX, expectedY, x, y)
}
}
func TestToDecartAngle90(t *testing.T) {
angle := math.Pi / 2
radius := float64(10)
expectedX := float64(10)
expectedY := float64(0)
x, y := toDecart(angle, radius)
if !(floatEquals(x, expectedX) && floatEquals(y, expectedY)) {
t.Fatalf("Expected point to be on (%v, %v) but found (%v, %v)", expectedX, expectedY, x, y)
}
}
func TestToDecartAngle45(t *testing.T) {
angle := math.Pi / 4
radius := float64(10)
expectedX := 10 / math.Sqrt(2)
expectedY := -(10 / math.Sqrt(2))
x, y := toDecart(angle, radius)
if !(floatEquals(x, expectedX) && floatEquals(y, expectedY)) {
t.Fatalf("Expected point to be on (%v, %v) but found (%v, %v)", expectedX, expectedY, x, y)
}
}
func TestToDecartTranslateAngle0(t *testing.T) {
dx := float64(13)
dy := float64(11)
angle := float64(0)
radius := float64(10)
expectedX := float64(0) + dx
expectedY := float64(-10) + dy
x, y := toDecartTranslate(angle, radius, dx, dy)
if !(floatEquals(x, expectedX) && floatEquals(y, expectedY)) {
t.Fatalf("Expected point to be on (%v, %v) but found (%v, %v)", expectedX, expectedY, x, y)
}
}
func TestToDecartTranslateTranslatesCoordinates(t *testing.T) {
dx := float64(-5)
dy := float64(-4)
angle := float64(0)
radius := float64(10)
expectedX := float64(0) + dx
expectedY := float64(-10) + dy
x, y := toDecartTranslate(angle, radius, dx, dy)
if !(floatEquals(x, expectedX) && floatEquals(y, expectedY)) {
t.Fatalf("Expected point to be on (%v, %v) but found (%v, %v)", expectedX, expectedY, x, y)
}
}
func TestToDecartTranslateAngle90(t *testing.T) {
dx := float64(13)
dy := float64(11)
angle := math.Pi / 2
radius := float64(10)
expectedX := float64(10) + dx
expectedY := float64(0) + dy
x, y := toDecartTranslate(angle, radius, dx, dy)
if !(floatEquals(x, expectedX) && floatEquals(y, expectedY)) {
t.Fatalf("Expected point to be on (%v, %v) but found (%v, %v)", expectedX, expectedY, x, y)
}
}
func TestToDecartTranslateAngle45(t *testing.T) {
dx := float64(13)
dy := float64(11)
angle := math.Pi / 4
radius := float64(10)
expectedX := (10 / math.Sqrt(2)) + dx
expectedY := (-(10 / math.Sqrt(2))) + dy
x, y := toDecartTranslate(angle, radius, dx, dy)
if !(floatEquals(x, expectedX) && floatEquals(y, expectedY)) {
t.Fatalf("Expected point to be on (%v, %v) but found (%v, %v)", expectedX, expectedY, x, y)
}
}
func TestFindLongestLabelFindsLabelOnFirstPlace(t *testing.T) {
longLabel := "theLongetLabel"
chart := PieChart{
Values: []Value{
Value{Value: 1, Label: longLabel},
Value{Value: 1, Label: "label1"},
Value{Value: 1, Label: "label2"},
},
}
label := findLongestLabel(chart)
if label != longLabel {
t.Fatalf("Expected the longetLabel to be %v but found %v", longLabel, label)
}
}
func TestFindLongestLabelFindsLabelInMiddle(t *testing.T) {
longLabel := "theLongetLabel"
chart := PieChart{
Values: []Value{
Value{Value: 1, Label: "label1"},
Value{Value: 1, Label: longLabel},
Value{Value: 1, Label: "label2"},
},
}
label := findLongestLabel(chart)
if label != longLabel {
t.Fatalf("Expected the longetLabel to be %v but found %v", longLabel, label)
}
}
func TestFindLongestLabelFindsLabelOnLastPlace(t *testing.T) {
longLabel := "theLongetLabel"
chart := PieChart{
Values: []Value{
Value{Value: 1, Label: "label1"},
Value{Value: 1, Label: longLabel},
Value{Value: 1, Label: "label2"},
},
}
label := findLongestLabel(chart)
if label != longLabel {
t.Fatalf("Expected the longetLabel to be %v but found %v", longLabel, label)
}
}