Chris@1407
|
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
|
Chris@1407
|
2
|
Chris@1407
|
3 /*
|
Chris@1407
|
4 Sonic Visualiser
|
Chris@1407
|
5 An audio file viewer and annotation editor.
|
Chris@1407
|
6 Centre for Digital Music, Queen Mary, University of London.
|
Chris@1407
|
7
|
Chris@1407
|
8 This program is free software; you can redistribute it and/or
|
Chris@1407
|
9 modify it under the terms of the GNU General Public License as
|
Chris@1407
|
10 published by the Free Software Foundation; either version 2 of the
|
Chris@1407
|
11 License, or (at your option) any later version. See the file
|
Chris@1407
|
12 COPYING included with this distribution for more information.
|
Chris@1407
|
13 */
|
Chris@1407
|
14
|
Chris@1407
|
15 #ifndef TEST_SCALE_TICK_INTERVALS_H
|
Chris@1407
|
16 #define TEST_SCALE_TICK_INTERVALS_H
|
Chris@1407
|
17
|
Chris@1407
|
18 #include "../ScaleTickIntervals.h"
|
Chris@1407
|
19
|
Chris@1407
|
20 #include <QObject>
|
Chris@1407
|
21 #include <QtTest>
|
Chris@1407
|
22 #include <QDir>
|
Chris@1407
|
23
|
Chris@1407
|
24 #include <iostream>
|
Chris@1407
|
25
|
Chris@1407
|
26 using namespace std;
|
Chris@1407
|
27
|
Chris@1407
|
28 class TestScaleTickIntervals : public QObject
|
Chris@1407
|
29 {
|
Chris@1407
|
30 Q_OBJECT
|
Chris@1407
|
31
|
Chris@1407
|
32 void printDiff(vector<ScaleTickIntervals::Tick> ticks,
|
Chris@1407
|
33 vector<ScaleTickIntervals::Tick> expected) {
|
Chris@1407
|
34
|
Chris@1407
|
35 cerr << "Have " << ticks.size() << " ticks, expected "
|
Chris@1407
|
36 << expected.size() << endl;
|
Chris@1411
|
37 for (int i = 0; i < int(ticks.size()); ++i) {
|
Chris@1411
|
38 cerr << i << ": have " << ticks[i].value << " \""
|
Chris@1411
|
39 << ticks[i].label << "\", expected ";
|
Chris@1411
|
40 if (i < int(expected.size())) {
|
Chris@1411
|
41 cerr << expected[i].value << " \"" << expected[i].label
|
Chris@1407
|
42 << "\"" << endl;
|
Chris@1411
|
43 } else {
|
Chris@1411
|
44 cerr << "(n/a)" << endl;
|
Chris@1407
|
45 }
|
Chris@1407
|
46 }
|
Chris@1407
|
47 }
|
Chris@1407
|
48
|
Chris@1407
|
49 void compareTicks(vector<ScaleTickIntervals::Tick> ticks,
|
Chris@1407
|
50 vector<ScaleTickIntervals::Tick> expected)
|
Chris@1407
|
51 {
|
Chris@1408
|
52 double eps = 1e-7;
|
Chris@1407
|
53 for (int i = 0; i < int(expected.size()); ++i) {
|
Chris@1407
|
54 if (i < int(ticks.size())) {
|
Chris@1407
|
55 if (ticks[i].label != expected[i].label ||
|
Chris@1408
|
56 fabs(ticks[i].value - expected[i].value) > eps) {
|
Chris@1407
|
57 printDiff(ticks, expected);
|
Chris@1407
|
58 }
|
Chris@1407
|
59 QCOMPARE(ticks[i].label, expected[i].label);
|
Chris@1407
|
60 QCOMPARE(ticks[i].value, expected[i].value);
|
Chris@1407
|
61 }
|
Chris@1407
|
62 }
|
Chris@1408
|
63 if (ticks.size() != expected.size()) {
|
Chris@1408
|
64 printDiff(ticks, expected);
|
Chris@1408
|
65 }
|
Chris@1407
|
66 QCOMPARE(ticks.size(), expected.size());
|
Chris@1407
|
67 }
|
Chris@1407
|
68
|
Chris@1407
|
69 private slots:
|
Chris@1407
|
70 void linear_0_1_10()
|
Chris@1407
|
71 {
|
Chris@1407
|
72 auto ticks = ScaleTickIntervals::linear({ 0, 1, 10 });
|
Chris@1407
|
73 vector<ScaleTickIntervals::Tick> expected {
|
Chris@1409
|
74 { 0.0, "0.0" },
|
Chris@1409
|
75 { 0.1, "0.1" },
|
Chris@1409
|
76 { 0.2, "0.2" },
|
Chris@1409
|
77 { 0.3, "0.3" },
|
Chris@1409
|
78 { 0.4, "0.4" },
|
Chris@1409
|
79 { 0.5, "0.5" },
|
Chris@1409
|
80 { 0.6, "0.6" },
|
Chris@1409
|
81 { 0.7, "0.7" },
|
Chris@1409
|
82 { 0.8, "0.8" },
|
Chris@1409
|
83 { 0.9, "0.9" },
|
Chris@1409
|
84 { 1.0, "1.0" }
|
Chris@1409
|
85 };
|
Chris@1409
|
86 compareTicks(ticks.ticks, expected);
|
Chris@1409
|
87 }
|
Chris@1409
|
88
|
Chris@1409
|
89 void linear_0_5_5()
|
Chris@1409
|
90 {
|
Chris@1409
|
91 auto ticks = ScaleTickIntervals::linear({ 0, 5, 5 });
|
Chris@1410
|
92 // generally if we have some activity in the units column, we
|
Chris@1410
|
93 // should add .0 to satisfy the human worry that we aren't
|
Chris@1410
|
94 // being told the whole story...
|
Chris@1409
|
95 vector<ScaleTickIntervals::Tick> expected {
|
Chris@1410
|
96 { 0, "0.0" },
|
Chris@1410
|
97 { 1, "1.0" },
|
Chris@1410
|
98 { 2, "2.0" },
|
Chris@1410
|
99 { 3, "3.0" },
|
Chris@1410
|
100 { 4, "4.0" },
|
Chris@1410
|
101 { 5, "5.0" },
|
Chris@1409
|
102 };
|
Chris@1409
|
103 compareTicks(ticks.ticks, expected);
|
Chris@1409
|
104 }
|
Chris@1409
|
105
|
Chris@1409
|
106 void linear_0_10_5()
|
Chris@1409
|
107 {
|
Chris@1409
|
108 auto ticks = ScaleTickIntervals::linear({ 0, 10, 5 });
|
Chris@1409
|
109 vector<ScaleTickIntervals::Tick> expected {
|
Chris@1410
|
110 { 0, "0.0" },
|
Chris@1410
|
111 { 2, "2.0" },
|
Chris@1410
|
112 { 4, "4.0" },
|
Chris@1410
|
113 { 6, "6.0" },
|
Chris@1410
|
114 { 8, "8.0" },
|
Chris@1410
|
115 { 10, "10.0" }
|
Chris@1407
|
116 };
|
Chris@1407
|
117 compareTicks(ticks.ticks, expected);
|
Chris@1407
|
118 }
|
Chris@1407
|
119
|
Chris@1411
|
120 void linear_10_0_5()
|
Chris@1411
|
121 {
|
Chris@1411
|
122 auto ticks = ScaleTickIntervals::linear({ 10, 0, 5 });
|
Chris@1411
|
123 vector<ScaleTickIntervals::Tick> expected {
|
Chris@1411
|
124 { 0, "0.0" },
|
Chris@1411
|
125 { 2, "2.0" },
|
Chris@1411
|
126 { 4, "4.0" },
|
Chris@1411
|
127 { 6, "6.0" },
|
Chris@1411
|
128 { 8, "8.0" },
|
Chris@1411
|
129 { 10, "10.0" }
|
Chris@1411
|
130 };
|
Chris@1411
|
131 compareTicks(ticks.ticks, expected);
|
Chris@1411
|
132 }
|
Chris@1411
|
133
|
Chris@1411
|
134 void linear_m10_0_5()
|
Chris@1411
|
135 {
|
Chris@1411
|
136 auto ticks = ScaleTickIntervals::linear({ -10, 0, 5 });
|
Chris@1411
|
137 vector<ScaleTickIntervals::Tick> expected {
|
Chris@1411
|
138 { -10, "-10.0" },
|
Chris@1411
|
139 { -8, "-8.0" },
|
Chris@1411
|
140 { -6, "-6.0" },
|
Chris@1411
|
141 { -4, "-4.0" },
|
Chris@1411
|
142 { -2, "-2.0" },
|
Chris@1411
|
143 { 0, "0.0" }
|
Chris@1411
|
144 };
|
Chris@1411
|
145 compareTicks(ticks.ticks, expected);
|
Chris@1411
|
146 }
|
Chris@1411
|
147
|
Chris@1411
|
148 void linear_0_m10_5()
|
Chris@1411
|
149 {
|
Chris@1411
|
150 auto ticks = ScaleTickIntervals::linear({ 0, -10, 5 });
|
Chris@1411
|
151 vector<ScaleTickIntervals::Tick> expected {
|
Chris@1411
|
152 { -10, "-10.0" },
|
Chris@1411
|
153 { -8, "-8.0" },
|
Chris@1411
|
154 { -6, "-6.0" },
|
Chris@1411
|
155 { -4, "-4.0" },
|
Chris@1411
|
156 { -2, "-2.0" },
|
Chris@1411
|
157 { 0, "0.0" }
|
Chris@1411
|
158 };
|
Chris@1411
|
159 compareTicks(ticks.ticks, expected);
|
Chris@1411
|
160 }
|
Chris@1411
|
161
|
Chris@1407
|
162 void linear_0_0p1_5()
|
Chris@1407
|
163 {
|
Chris@1407
|
164 auto ticks = ScaleTickIntervals::linear({ 0, 0.1, 5 });
|
Chris@1407
|
165 vector<ScaleTickIntervals::Tick> expected {
|
Chris@1409
|
166 { 0.00, "0.00" },
|
Chris@1407
|
167 { 0.02, "0.02" },
|
Chris@1407
|
168 { 0.04, "0.04" },
|
Chris@1407
|
169 { 0.06, "0.06" },
|
Chris@1407
|
170 { 0.08, "0.08" },
|
Chris@1409
|
171 { 0.10, "0.10" }
|
Chris@1407
|
172 };
|
Chris@1407
|
173 compareTicks(ticks.ticks, expected);
|
Chris@1407
|
174 }
|
Chris@1407
|
175
|
Chris@1407
|
176 void linear_0_0p01_5()
|
Chris@1407
|
177 {
|
Chris@1407
|
178 auto ticks = ScaleTickIntervals::linear({ 0, 0.01, 5 });
|
Chris@1407
|
179 vector<ScaleTickIntervals::Tick> expected {
|
Chris@1409
|
180 { 0.000, "0.000" },
|
Chris@1407
|
181 { 0.002, "0.002" },
|
Chris@1407
|
182 { 0.004, "0.004" },
|
Chris@1407
|
183 { 0.006, "0.006" },
|
Chris@1407
|
184 { 0.008, "0.008" },
|
Chris@1409
|
185 { 0.010, "0.010" }
|
Chris@1409
|
186 };
|
Chris@1409
|
187 compareTicks(ticks.ticks, expected);
|
Chris@1409
|
188 }
|
Chris@1409
|
189
|
Chris@1409
|
190 void linear_0_0p005_5()
|
Chris@1409
|
191 {
|
Chris@1409
|
192 auto ticks = ScaleTickIntervals::linear({ 0, 0.005, 5 });
|
Chris@1409
|
193 vector<ScaleTickIntervals::Tick> expected {
|
Chris@1409
|
194 { 0.000, "0.000" },
|
Chris@1409
|
195 { 0.001, "0.001" },
|
Chris@1409
|
196 { 0.002, "0.002" },
|
Chris@1409
|
197 { 0.003, "0.003" },
|
Chris@1409
|
198 { 0.004, "0.004" },
|
Chris@1409
|
199 { 0.005, "0.005" }
|
Chris@1407
|
200 };
|
Chris@1407
|
201 compareTicks(ticks.ticks, expected);
|
Chris@1407
|
202 }
|
Chris@1407
|
203
|
Chris@1407
|
204 void linear_0_0p001_5()
|
Chris@1407
|
205 {
|
Chris@1407
|
206 auto ticks = ScaleTickIntervals::linear({ 0, 0.001, 5 });
|
Chris@1407
|
207 vector<ScaleTickIntervals::Tick> expected {
|
Chris@1409
|
208 { 0.0000, "0.0e+00" },
|
Chris@1407
|
209 { 0.0002, "2.0e-04" },
|
Chris@1407
|
210 { 0.0004, "4.0e-04" },
|
Chris@1407
|
211 { 0.0006, "6.0e-04" },
|
Chris@1407
|
212 { 0.0008, "8.0e-04" },
|
Chris@1409
|
213 { 0.0010, "1.0e-03" }
|
Chris@1407
|
214 };
|
Chris@1407
|
215 compareTicks(ticks.ticks, expected);
|
Chris@1407
|
216 }
|
Chris@1407
|
217
|
Chris@1407
|
218 void linear_1_1p001_5()
|
Chris@1407
|
219 {
|
Chris@1407
|
220 auto ticks = ScaleTickIntervals::linear({ 1, 1.001, 5 });
|
Chris@1407
|
221 vector<ScaleTickIntervals::Tick> expected {
|
Chris@1409
|
222 { 1.0000, "1.0000" },
|
Chris@1407
|
223 { 1.0002, "1.0002" },
|
Chris@1407
|
224 { 1.0004, "1.0004" },
|
Chris@1407
|
225 { 1.0006, "1.0006" },
|
Chris@1407
|
226 { 1.0008, "1.0008" },
|
Chris@1409
|
227 { 1.0010, "1.0010" }
|
Chris@1409
|
228 };
|
Chris@1409
|
229 compareTicks(ticks.ticks, expected);
|
Chris@1409
|
230 }
|
Chris@1409
|
231
|
Chris@1409
|
232 void linear_10000_10010_5()
|
Chris@1409
|
233 {
|
Chris@1409
|
234 auto ticks = ScaleTickIntervals::linear({ 10000, 10010, 5 });
|
Chris@1409
|
235 vector<ScaleTickIntervals::Tick> expected {
|
Chris@1410
|
236 { 10000, "10000.0" },
|
Chris@1410
|
237 { 10002, "10002.0" },
|
Chris@1410
|
238 { 10004, "10004.0" },
|
Chris@1410
|
239 { 10006, "10006.0" },
|
Chris@1410
|
240 { 10008, "10008.0" },
|
Chris@1410
|
241 { 10010, "10010.0" },
|
Chris@1409
|
242 };
|
Chris@1409
|
243 compareTicks(ticks.ticks, expected);
|
Chris@1409
|
244 }
|
Chris@1409
|
245
|
Chris@1409
|
246 void linear_10000_20000_5()
|
Chris@1409
|
247 {
|
Chris@1409
|
248 auto ticks = ScaleTickIntervals::linear({ 10000, 20000, 5 });
|
Chris@1409
|
249 vector<ScaleTickIntervals::Tick> expected {
|
Chris@1409
|
250 { 10000, "10000" },
|
Chris@1409
|
251 { 12000, "12000" },
|
Chris@1409
|
252 { 14000, "14000" },
|
Chris@1409
|
253 { 16000, "16000" },
|
Chris@1409
|
254 { 18000, "18000" },
|
Chris@1409
|
255 { 20000, "20000" },
|
Chris@1409
|
256 };
|
Chris@1409
|
257 compareTicks(ticks.ticks, expected);
|
Chris@1409
|
258 }
|
Chris@1409
|
259
|
Chris@1409
|
260 void linear_m1_1_10()
|
Chris@1409
|
261 {
|
Chris@1409
|
262 auto ticks = ScaleTickIntervals::linear({ -1, 1, 10 });
|
Chris@1409
|
263 vector<ScaleTickIntervals::Tick> expected {
|
Chris@1409
|
264 { -1.0, "-1.0" },
|
Chris@1409
|
265 { -0.8, "-0.8" },
|
Chris@1409
|
266 { -0.6, "-0.6" },
|
Chris@1409
|
267 { -0.4, "-0.4" },
|
Chris@1409
|
268 { -0.2, "-0.2" },
|
Chris@1409
|
269 { 0.0, "0.0" },
|
Chris@1409
|
270 { 0.2, "0.2" },
|
Chris@1409
|
271 { 0.4, "0.4" },
|
Chris@1409
|
272 { 0.6, "0.6" },
|
Chris@1409
|
273 { 0.8, "0.8" },
|
Chris@1409
|
274 { 1.0, "1.0" }
|
Chris@1407
|
275 };
|
Chris@1407
|
276 compareTicks(ticks.ticks, expected);
|
Chris@1407
|
277 }
|
Chris@1411
|
278
|
Chris@1411
|
279 void linear_221p23_623p7_57p4()
|
Chris@1411
|
280 {
|
Chris@1411
|
281 auto ticks = ScaleTickIntervals::linear({ 221.23, 623.7, 4 });
|
Chris@1411
|
282 // only 4 ticks, not 5, because none of the rounded tick
|
Chris@1411
|
283 // values lies on an end value
|
Chris@1411
|
284 vector<ScaleTickIntervals::Tick> expected {
|
Chris@1411
|
285 { 230, "230" },
|
Chris@1411
|
286 { 330, "330" },
|
Chris@1411
|
287 { 430, "430" },
|
Chris@1411
|
288 { 530, "530" },
|
Chris@1411
|
289 };
|
Chris@1411
|
290 compareTicks(ticks.ticks, expected);
|
Chris@1411
|
291 }
|
Chris@1411
|
292
|
Chris@1411
|
293 void linear_1_1_10()
|
Chris@1411
|
294 {
|
Chris@1411
|
295 auto ticks = ScaleTickIntervals::linear({ 1, 1, 10 });
|
Chris@1411
|
296 vector<ScaleTickIntervals::Tick> expected {
|
Chris@1411
|
297 { 1.0, "1.0" }
|
Chris@1411
|
298 };
|
Chris@1411
|
299 compareTicks(ticks.ticks, expected);
|
Chris@1411
|
300 }
|
Chris@1411
|
301
|
Chris@1411
|
302 void linear_0_0_10()
|
Chris@1411
|
303 {
|
Chris@1411
|
304 auto ticks = ScaleTickIntervals::linear({ 0, 0, 10 });
|
Chris@1411
|
305 vector<ScaleTickIntervals::Tick> expected {
|
Chris@1411
|
306 { 0.0, "0.0" }
|
Chris@1411
|
307 };
|
Chris@1411
|
308 compareTicks(ticks.ticks, expected);
|
Chris@1411
|
309 }
|
Chris@1411
|
310
|
Chris@1411
|
311 void linear_0_1_1()
|
Chris@1411
|
312 {
|
Chris@1411
|
313 auto ticks = ScaleTickIntervals::linear({ 0, 1, 1 });
|
Chris@1411
|
314 vector<ScaleTickIntervals::Tick> expected {
|
Chris@1411
|
315 { 0.0, "0.0" },
|
Chris@1411
|
316 { 1.0, "1.0" }
|
Chris@1411
|
317 };
|
Chris@1411
|
318 compareTicks(ticks.ticks, expected);
|
Chris@1411
|
319 }
|
Chris@1411
|
320
|
Chris@1411
|
321 void linear_0_1_0()
|
Chris@1411
|
322 {
|
Chris@1411
|
323 auto ticks = ScaleTickIntervals::linear({ 0, 1, 0 });
|
Chris@1411
|
324 vector<ScaleTickIntervals::Tick> expected {
|
Chris@1411
|
325 };
|
Chris@1411
|
326 compareTicks(ticks.ticks, expected);
|
Chris@1411
|
327 }
|
Chris@1411
|
328
|
Chris@1411
|
329 void linear_0_1_m1()
|
Chris@1411
|
330 {
|
Chris@1411
|
331 auto ticks = ScaleTickIntervals::linear({ 0, 1, -1 });
|
Chris@1411
|
332 vector<ScaleTickIntervals::Tick> expected {
|
Chris@1411
|
333 };
|
Chris@1411
|
334 compareTicks(ticks.ticks, expected);
|
Chris@1411
|
335 }
|
Chris@1407
|
336 };
|
Chris@1407
|
337
|
Chris@1407
|
338 #endif
|
Chris@1407
|
339
|
Chris@1407
|
340
|