]> gitweb.factorcode.org Git - factor.git/blob - extra/ui/gadgets/charts/lines/lines-tests.factor
e3c71a6ba5d2cd32451c73e5dfb3fb26f2fed954
[factor.git] / extra / ui / gadgets / charts / lines / lines-tests.factor
1 ! Copyright (C) 2017 Alexander Ilin.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: kernel math math.ratios sequences tools.test
4 ui.gadgets.charts.lines ui.gadgets.charts.lines.private
5 ui.gadgets.charts.utils ;
6 IN: ui.gadgets.charts.lines.tests
7
8 { -2/3 } [ { 1 3 } { -2 5 } calc-line-slope ] unit-test
9 [ { 5 0 } { 5 5 } calc-line-slope ] [ division-by-zero? ] must-fail-with
10 { 3 } [ -2/3 1 { 1 3 } calc-y ] unit-test
11 { 5 } [ -2/3 -2 { 1 3 } calc-y ] unit-test
12 { 3 } [ -2/3 1 { -2 5 } calc-y ] unit-test
13 { 5 } [ -2/3 -2 { -2 5 } calc-y ] unit-test
14 { 5 } [ -2 { 1 3 } { -2 5 } y-at ] unit-test
15 { 3 } [ 1 { 1 3 } { -2 5 } y-at ] unit-test
16 { 1 } [ 4 { -2 5 } { 1 3 } y-at ] unit-test
17 { 0.0 } [ 5.5 { -2 5 } { 1 3 } y-at ] unit-test
18 { 1 } [ -2/3 3 { 1 3 } calc-x ] unit-test
19 { -2 } [ -2/3 5 { 1 3 } calc-x ] unit-test
20 { 1 } [ -2/3 3 { -2 5 } calc-x ] unit-test
21 { -2 } [ -2/3 5 { -2 5 } calc-x ] unit-test
22
23 { 2 3 } [ { 1 2 3 } last2 ] unit-test
24 { 1 2 } [ { 1 2 } last2 ] unit-test
25 [ { 1 } last2 ] must-fail
26 [ { } last2 ] must-fail
27
28 ! 2-point-chunk upwards
29 {
30     { { -3 -3 } { 3 3 } }
31 } [
32     { { -6 0 } { -5 -5 } } { { 5 5 } { 6 0 } }
33     -3 3 2-point-chunk
34 ] unit-test
35
36 ! 2-point-chunk downwards
37 {
38     { { -3 3 } { 3 -3 } }
39 } [
40     { { -6 0 } { -5 5 } } { { 5 -5 } { 6 0 } }
41     -3 3 2-point-chunk
42 ] unit-test
43
44 ! 2-point-chunk: same x coord
45 {
46     { { -5 -3 } { -5 3 } }
47 } [
48     { { -6 0 } { -5 -5 } } { { -5 5 } { 6 0 } }
49     -3 3 2-point-chunk
50 ] unit-test
51
52 ! fix-left-chunk: y coord = top limit
53 {
54     { { -6 0 } { -3 3 } }
55 } [
56     { { -6 0 } { -3 3 } } { { 5 5 } { 6 6 } }
57     -3 3 fix-left-chunk
58 ] unit-test
59
60 ! fix-left-chunk: y coord = bottom limit
61 {
62     { { -6 0 } { -3 -3 } }
63 } [
64     { { -6 0 } { -3 -3 } } { { 5 -5 } { 6 -6 } }
65     -3 3 fix-left-chunk
66 ] unit-test
67
68 ! fix-left-chunk: going upwards
69 {
70     { { -6 0 } { 2 2 } { 3 3 } }
71 } [
72     { { -6 0 } { 2 2 } } { { 5 5 } { 6 6 } }
73     -3 3 fix-left-chunk
74 ] unit-test
75
76 ! fix-left-chunk: going downwards
77 {
78     { { -6 0 } { -2 -2 } { -1 -3 } }
79 } [
80     { { -6 0 } { -2 -2 } } { { 0 -4 } { 6 -6 } }
81     -3 3 fix-left-chunk
82 ] unit-test
83
84 ! TODO: add more tests for the recently discovered bugs in fix-left-chunk and fix-right-chunk
85
86 ! fix-right-chunk: y coord = top limit
87 {
88     { { 5 3 } { 6 0 } }
89 } [
90     { { -6 6 } { -3 4 } } { { 5 3 } { 6 0 } }
91     -3 3 fix-right-chunk
92 ] unit-test
93
94 ! fix-right-chunk: y coord = bottom limit
95 {
96     { { 5 -3 } { 6 0 } }
97 } [
98     { { -6 -6 } { -3 -4 } } { { 5 -3 } { 6 0 } }
99     -3 3 fix-right-chunk
100 ] unit-test
101
102 ! fix-right-chunk: going upwards
103 {
104     { { -3 -3 } { -2 -2 } { 6 0 } }
105 } [
106     { { -6 -6 } { -4 -4 } } { { -2 -2 } { 6 0 } }
107     -3 3 fix-right-chunk
108 ] unit-test
109
110 ! fix-right-chunk: going downwards
111 {
112     { { -3 3 } { -2 2 } { 6 0 } }
113 } [
114     { { -6 6 } { -4 4 } } { { -2 2 } { 6 0 } }
115     -3 3 fix-right-chunk
116 ] unit-test
117
118 ! tight bounds
119 {
120     { { { 0 0 } { 0 1 } { 1 2 } { 1 3 } { 2 5 } } }
121 } [
122     { { 0 0 } { 0 1 } { 1 2 } { 1 3 } { 2 5 } } { 0 5 }
123     drawable-chunks [ { } like ] map
124 ] unit-test
125
126 ! loose bounds
127 {
128     { { { 0 0 } { 0 1 } { 1 2 } { 1 3 } { 2 5 } } }
129 } [
130     { { 0 0 } { 0 1 } { 1 2 } { 1 3 } { 2 5 } } { -1 6 }
131     drawable-chunks [ { } like ] map
132 ] unit-test
133
134 ! only bottom element accepted
135 {
136     { { { 0 0 } } }
137 } [
138     { { 0 0 } { 0 1 } { 1 2 } { 1 3 } { 2 5 } } { -1 0 }
139     drawable-chunks [ { } like ] map
140 ] unit-test
141
142 ! only top element accepted
143 {
144     { { { 2 5 } } }
145 } [
146     { { 0 0 } { 0 1 } { 1 2 } { 1 3 } { 2 5 } } { 5 10 }
147     drawable-chunks [ { } like ] map
148 ] unit-test
149
150 ! top half of the elements accepted
151 {
152     { { { 1 2 } { 1 3 } { 2 5 } } }
153 } [
154     { { 0 0 } { 0 1 } { 1 2 } { 1 3 } { 2 5 } } { 2 10 }
155     drawable-chunks [ { } like ] map
156 ] unit-test
157
158 ! bottom half of the elements accepted
159 {
160     { { { 0 0 } { 0 1 } { 1 2 } } }
161 } [
162     { { 0 0 } { 0 1 } { 1 2 } { 1 3 } { 2 5 } } { -2 2 }
163     drawable-chunks [ { } like ] map
164 ] unit-test
165
166 ! middle section of the elements accepted
167 {
168     { { { 0 1 } { 1 2 } { 1 3 } } }
169 } [
170     { { 0 0 } { 0 1 } { 1 2 } { 1 3 } { 2 5 } } { 1 3 }
171     drawable-chunks [ { } like ] map
172 ] unit-test
173
174 ! two sections, including first but not last
175 {
176     {
177         { { 0 0 } { 1 2 } { 2 3 } }
178         { { 5 3 } { 6 2 } { 7 0 } }
179     }
180 } [
181     { { 0 0 } { 1 2 } { 2 3 } { 3 5 } { 4 5 } { 5 3 } { 6 2 } { 7 0 } { 8 -1 } { 9 -2 } } { 0 3 }
182     drawable-chunks [ { } like ] map
183 ] unit-test
184
185 ! two sections, including last but not first
186 {
187     {
188         { { 2 0 } { 3 3 } { 4 3 } }
189         { { 7 3 } { 8 2 } { 9 0 } }
190     }
191 } [
192     { { 0 -2 } { 1 -1 } { 2 0 } { 3 3 } { 4 3 } { 5 5 } { 6 9 } { 7 3 } { 8 2 } { 9 0 } } { 0 3 }
193     drawable-chunks [ { } like ] map
194 ] unit-test
195
196 ! single-element sequences, same x coord
197 {
198     {
199         { { 0 0 } { 0 3 } }
200         { { 0 3 } { 0 0 } }
201     }
202 } [
203     { { 0 -2 } { 0 0 } { 0 5 } { 0 3 } { 0 -1 } } { 0 3 }
204     drawable-chunks [ { } like ] map
205 ] unit-test
206
207 ! single point sticks out to within the limits from below
208 {
209     {
210         { { 1 1 } { 2 2 } { 3 1 } }
211     }
212 } [
213     { { 0 0 } { 2 2 } { 4 0 } } { 1 5 }
214     drawable-chunks [ { } like ] map
215 ] unit-test
216
217 ! single point sticks out to within the limits from above
218 {
219     {
220         { { 1 3 } { 2 2 } { 3 3 } }
221     }
222 } [
223     { { 0 4 } { 2 2 } { 4 4 } } { 1 3 }
224     drawable-chunks [ { } like ] map
225 ] unit-test
226
227 {
228     { { { 0 300 } { 1 200 } { 2 150 } { 3 100 } { 4 0 } } }
229 } [
230     { { { 0 0 } { 1 100 } { 2 150 } { 3 200 } { 4 300 } } }
231     { 0 300 } flip-y-axis
232 ] unit-test
233
234 {
235     { 0 30 60 90 120 150 180 210 240 270 300 }
236 } [
237     11 iota [ 10 + ] map [ 300 swap 20 10 scale ] map
238 ] unit-test
239
240 { { } }
241 [ { } { } clip-data ] unit-test
242
243 { { } }
244 [ { { 0 1 } { 0 5 } } { } clip-data ] unit-test
245
246 ! Adjustment after search is required in both directions.
247 {
248     {
249         { 1 3 } { 1 4 } { 1 5 }
250         { 2 6 } { 3 7 } { 4 8 }
251         { 5 9 } { 5 10 } { 5 11 } { 5 12 }
252     }
253 } [
254     { { 1 5 } { 0 14 } }
255     {
256         { 0 1 } { 0 2 }
257         { 1 3 } { 1 4 } { 1 5 }
258         { 2 6 } { 3 7 } { 4 8 }
259         { 5 9 } { 5 10 } { 5 11 } { 5 12 }
260         { 6 13 } { 7 14 }
261     } clip-data
262 ] unit-test
263
264 ! no points within the viewport, complete calculation
265 {
266     { { 1 1 } { 4 4 } }
267 } [
268     { { 1 4 } { 1 4 } }
269     { { 0 0 } { 5 5 } } clip-data
270 ] unit-test
271
272 ! no points within the viewport, complete calculation
273 {
274     { { 1 4 } { 4 1 } }
275 } [
276     { { 1 4 } { 1 4 } }
277     { { 0 5 } { 5 0 } } clip-data
278 ] unit-test
279
280 ! no points within the viewport, complete calculation
281 {
282     { { 1 3 } { 4 3 } }
283 } [
284     { { 1 4 } { 1 4 } }
285     { { 0 3 } { 5 3 } } clip-data
286 ] unit-test
287
288 ! all data are to the left of viewport
289 {
290     { }
291 } [
292     { { 1 4 } { 1 4 } }
293     { { -1 0 } { 0 1 } { 0.5 1 } } clip-data
294 ] unit-test
295
296 ! all data are to the right of viewport
297 {
298     { }
299 } [
300     { { 1 4 } { 1 4 } }
301     { { 4.5 0 } { 5 1 } { 6 1 } } clip-data
302 ] unit-test
303
304 ! just a little off the top
305 { t } [
306     { 0 99 }
307     { { 0 100 } { 100 0 } { 100 50 } { 150 50 } { 200 100 } }
308     y-in-bounds?
309 ] unit-test
310
311 ! TODO: add tests where after search there is no adjustment necessary, so that extra adjustment would take bad elements.