Mercurial > hg > svcore
comparison base/test/TestScaleTickIntervals.h @ 1415:12316a9bcc8f scale-ticks
After seeing them in action, I changed my mind about the wisdom of these trailing .0s
author | Chris Cannam |
---|---|
date | Thu, 04 May 2017 10:46:38 +0100 |
parents | c57994e1edd7 |
children | 9a8995785827 |
comparison
equal
deleted
inserted
replaced
1414:c57994e1edd7 | 1415:12316a9bcc8f |
---|---|
45 } | 45 } |
46 } | 46 } |
47 } | 47 } |
48 | 48 |
49 void compareTicks(vector<ScaleTickIntervals::Tick> ticks, | 49 void compareTicks(vector<ScaleTickIntervals::Tick> ticks, |
50 vector<ScaleTickIntervals::Tick> expected) | 50 vector<ScaleTickIntervals::Tick> expected, |
51 bool fuzzier = false) | |
51 { | 52 { |
52 double eps = 1e-7; | 53 double eps = 1e-7; |
53 for (int i = 0; i < int(expected.size()); ++i) { | 54 for (int i = 0; i < int(expected.size()); ++i) { |
54 if (i < int(ticks.size())) { | 55 if (i < int(ticks.size())) { |
55 if (ticks[i].label != expected[i].label || | 56 bool pass = true; |
56 fabs(ticks[i].value - expected[i].value) > eps) { | 57 if (ticks[i].label != expected[i].label) { |
58 pass = false; | |
59 } else if (!fuzzier) { | |
60 if (fabs(ticks[i].value - expected[i].value) > eps) { | |
61 pass = false; | |
62 } | |
63 } else { | |
64 if (fabs(ticks[i].value - expected[i].value) > | |
65 fabs(ticks[i].value) * 1e-5) { | |
66 pass = false; | |
67 } | |
68 } | |
69 if (!pass) { | |
57 printDiff(ticks, expected); | 70 printDiff(ticks, expected); |
58 } | 71 QCOMPARE(ticks[i].label, expected[i].label); |
59 QCOMPARE(ticks[i].label, expected[i].label); | 72 QCOMPARE(ticks[i].value, expected[i].value); |
60 QCOMPARE(ticks[i].value, expected[i].value); | 73 } |
61 } | 74 } |
62 } | 75 } |
63 if (ticks.size() != expected.size()) { | 76 if (ticks.size() != expected.size()) { |
64 printDiff(ticks, expected); | 77 printDiff(ticks, expected); |
65 } | 78 } |
87 } | 100 } |
88 | 101 |
89 void linear_0_5_5() | 102 void linear_0_5_5() |
90 { | 103 { |
91 auto ticks = ScaleTickIntervals::linear({ 0, 5, 5 }); | 104 auto ticks = ScaleTickIntervals::linear({ 0, 5, 5 }); |
92 // generally if we have some activity in the units column, we | 105 vector<ScaleTickIntervals::Tick> expected { |
93 // should add .0 to satisfy the human worry that we aren't | 106 { 0, "0" }, |
94 // being told the whole story... | 107 { 1, "1" }, |
95 vector<ScaleTickIntervals::Tick> expected { | 108 { 2, "2" }, |
96 { 0, "0.0" }, | 109 { 3, "3" }, |
97 { 1, "1.0" }, | 110 { 4, "4" }, |
98 { 2, "2.0" }, | 111 { 5, "5" }, |
99 { 3, "3.0" }, | |
100 { 4, "4.0" }, | |
101 { 5, "5.0" }, | |
102 }; | 112 }; |
103 compareTicks(ticks.ticks, expected); | 113 compareTicks(ticks.ticks, expected); |
104 } | 114 } |
105 | 115 |
106 void linear_0_10_5() | 116 void linear_0_10_5() |
107 { | 117 { |
108 auto ticks = ScaleTickIntervals::linear({ 0, 10, 5 }); | 118 auto ticks = ScaleTickIntervals::linear({ 0, 10, 5 }); |
109 vector<ScaleTickIntervals::Tick> expected { | 119 vector<ScaleTickIntervals::Tick> expected { |
110 { 0, "0.0" }, | 120 { 0, "0" }, |
111 { 2, "2.0" }, | 121 { 2, "2" }, |
112 { 4, "4.0" }, | 122 { 4, "4" }, |
113 { 6, "6.0" }, | 123 { 6, "6" }, |
114 { 8, "8.0" }, | 124 { 8, "8" }, |
115 { 10, "10.0" } | 125 { 10, "10" } |
116 }; | 126 }; |
117 compareTicks(ticks.ticks, expected); | 127 compareTicks(ticks.ticks, expected); |
118 } | 128 } |
119 | 129 |
120 void linear_10_0_5() | 130 void linear_10_0_5() |
121 { | 131 { |
122 auto ticks = ScaleTickIntervals::linear({ 10, 0, 5 }); | 132 auto ticks = ScaleTickIntervals::linear({ 10, 0, 5 }); |
123 vector<ScaleTickIntervals::Tick> expected { | 133 vector<ScaleTickIntervals::Tick> expected { |
124 { 0, "0.0" }, | 134 { 0, "0" }, |
125 { 2, "2.0" }, | 135 { 2, "2" }, |
126 { 4, "4.0" }, | 136 { 4, "4" }, |
127 { 6, "6.0" }, | 137 { 6, "6" }, |
128 { 8, "8.0" }, | 138 { 8, "8" }, |
129 { 10, "10.0" } | 139 { 10, "10" } |
130 }; | 140 }; |
131 compareTicks(ticks.ticks, expected); | 141 compareTicks(ticks.ticks, expected); |
132 } | 142 } |
133 | 143 |
134 void linear_m10_0_5() | 144 void linear_m10_0_5() |
135 { | 145 { |
136 auto ticks = ScaleTickIntervals::linear({ -10, 0, 5 }); | 146 auto ticks = ScaleTickIntervals::linear({ -10, 0, 5 }); |
137 vector<ScaleTickIntervals::Tick> expected { | 147 vector<ScaleTickIntervals::Tick> expected { |
138 { -10, "-10.0" }, | 148 { -10, "-10" }, |
139 { -8, "-8.0" }, | 149 { -8, "-8" }, |
140 { -6, "-6.0" }, | 150 { -6, "-6" }, |
141 { -4, "-4.0" }, | 151 { -4, "-4" }, |
142 { -2, "-2.0" }, | 152 { -2, "-2" }, |
143 { 0, "0.0" } | 153 { 0, "0" } |
144 }; | 154 }; |
145 compareTicks(ticks.ticks, expected); | 155 compareTicks(ticks.ticks, expected); |
146 } | 156 } |
147 | 157 |
148 void linear_0_m10_5() | 158 void linear_0_m10_5() |
149 { | 159 { |
150 auto ticks = ScaleTickIntervals::linear({ 0, -10, 5 }); | 160 auto ticks = ScaleTickIntervals::linear({ 0, -10, 5 }); |
151 vector<ScaleTickIntervals::Tick> expected { | 161 vector<ScaleTickIntervals::Tick> expected { |
152 { -10, "-10.0" }, | 162 { -10, "-10" }, |
153 { -8, "-8.0" }, | 163 { -8, "-8" }, |
154 { -6, "-6.0" }, | 164 { -6, "-6" }, |
155 { -4, "-4.0" }, | 165 { -4, "-4" }, |
156 { -2, "-2.0" }, | 166 { -2, "-2" }, |
157 { 0, "0.0" } | 167 { 0, "0" } |
158 }; | 168 }; |
159 compareTicks(ticks.ticks, expected); | 169 compareTicks(ticks.ticks, expected); |
160 } | 170 } |
161 | 171 |
162 void linear_0_0p1_5() | 172 void linear_0_0p1_5() |
244 | 254 |
245 void linear_10000_10010_5() | 255 void linear_10000_10010_5() |
246 { | 256 { |
247 auto ticks = ScaleTickIntervals::linear({ 10000, 10010, 5 }); | 257 auto ticks = ScaleTickIntervals::linear({ 10000, 10010, 5 }); |
248 vector<ScaleTickIntervals::Tick> expected { | 258 vector<ScaleTickIntervals::Tick> expected { |
249 { 10000, "10000.0" }, | 259 { 10000, "10000" }, |
250 { 10002, "10002.0" }, | 260 { 10002, "10002" }, |
251 { 10004, "10004.0" }, | 261 { 10004, "10004" }, |
252 { 10006, "10006.0" }, | 262 { 10006, "10006" }, |
253 { 10008, "10008.0" }, | 263 { 10008, "10008" }, |
254 { 10010, "10010.0" }, | 264 { 10010, "10010" }, |
255 }; | 265 }; |
256 compareTicks(ticks.ticks, expected); | 266 compareTicks(ticks.ticks, expected); |
257 } | 267 } |
258 | 268 |
259 void linear_10000_20000_5() | 269 void linear_10000_20000_5() |
339 | 349 |
340 void linear_2_3_1() | 350 void linear_2_3_1() |
341 { | 351 { |
342 auto ticks = ScaleTickIntervals::linear({ 2, 3, 1 }); | 352 auto ticks = ScaleTickIntervals::linear({ 2, 3, 1 }); |
343 vector<ScaleTickIntervals::Tick> expected { | 353 vector<ScaleTickIntervals::Tick> expected { |
344 { 2.0, "2.0" }, | 354 { 2.0, "2" }, |
345 { 3.0, "3.0" } | 355 { 3.0, "3" } |
346 }; | 356 }; |
347 compareTicks(ticks.ticks, expected); | 357 compareTicks(ticks.ticks, expected); |
348 } | 358 } |
349 | 359 |
350 void linear_2_3_2() | 360 void linear_2_3_2() |
412 compareTicks(ticks.ticks, expected); | 422 compareTicks(ticks.ticks, expected); |
413 } | 423 } |
414 | 424 |
415 void linear_1_1_10() | 425 void linear_1_1_10() |
416 { | 426 { |
427 // pathological range | |
417 auto ticks = ScaleTickIntervals::linear({ 1, 1, 10 }); | 428 auto ticks = ScaleTickIntervals::linear({ 1, 1, 10 }); |
418 vector<ScaleTickIntervals::Tick> expected { | 429 vector<ScaleTickIntervals::Tick> expected { |
419 { 1.0, "1.0" } | 430 { 1.0, "1.0" } |
420 }; | 431 }; |
421 compareTicks(ticks.ticks, expected); | 432 compareTicks(ticks.ticks, expected); |
422 } | 433 } |
423 | 434 |
424 void linear_0_0_10() | 435 void linear_0_0_10() |
425 { | 436 { |
437 // pathological range | |
426 auto ticks = ScaleTickIntervals::linear({ 0, 0, 10 }); | 438 auto ticks = ScaleTickIntervals::linear({ 0, 0, 10 }); |
427 vector<ScaleTickIntervals::Tick> expected { | 439 vector<ScaleTickIntervals::Tick> expected { |
428 { 0.0, "0.0" } | 440 { 0.0, "0.0" } |
429 }; | 441 }; |
430 compareTicks(ticks.ticks, expected); | 442 compareTicks(ticks.ticks, expected); |
432 | 444 |
433 void linear_0_1_1() | 445 void linear_0_1_1() |
434 { | 446 { |
435 auto ticks = ScaleTickIntervals::linear({ 0, 1, 1 }); | 447 auto ticks = ScaleTickIntervals::linear({ 0, 1, 1 }); |
436 vector<ScaleTickIntervals::Tick> expected { | 448 vector<ScaleTickIntervals::Tick> expected { |
437 { 0.0, "0.0" }, | 449 { 0.0, "0" }, |
438 { 1.0, "1.0" } | 450 { 1.0, "1" } |
439 }; | 451 }; |
440 compareTicks(ticks.ticks, expected); | 452 compareTicks(ticks.ticks, expected); |
441 } | 453 } |
442 | 454 |
443 void linear_0_1_0() | 455 void linear_0_1_0() |
469 | 481 |
470 void log_0_10_2() | 482 void log_0_10_2() |
471 { | 483 { |
472 auto ticks = ScaleTickIntervals::logarithmic({ 0, 10, 2 }); | 484 auto ticks = ScaleTickIntervals::logarithmic({ 0, 10, 2 }); |
473 vector<ScaleTickIntervals::Tick> expected { | 485 vector<ScaleTickIntervals::Tick> expected { |
474 { 1e-10, "1.0e-10" }, | 486 { 1e-10, "1e-10" }, |
475 { pow(10.0, -4.5), "3.2e-05" }, | 487 { pow(10.0, -4.5), "3e-05" }, |
476 { 10.0, "1.0e+01" }, | 488 { 10.0, "1e+01" }, |
477 }; | 489 }; |
478 compareTicks(ticks.ticks, expected); | 490 compareTicks(ticks.ticks, expected); |
491 } | |
492 | |
493 void log_pi_avogadro_7() | |
494 { | |
495 auto ticks = ScaleTickIntervals::logarithmic({ M_PI, 6.022140857e23, 7 }); | |
496 vector<ScaleTickIntervals::Tick> expected { | |
497 { 3.16228, "3e+00" }, | |
498 { 6309.57, "6e+03" }, | |
499 { 1.25893e+07, "1e+07" }, | |
500 { 2.51189e+10, "3e+10" }, | |
501 { 5.01187e+13, "5e+13" }, | |
502 { 1e+17, "1e+17" }, | |
503 { 1.99526e+20, "2e+20" }, | |
504 { 3.98107e+23, "4e+23" }, | |
505 }; | |
506 compareTicks(ticks.ticks, expected, true); | |
479 } | 507 } |
480 }; | 508 }; |
481 | 509 |
482 #endif | 510 #endif |
483 | 511 |