annotate base/test/TestScaleTickIntervals.h @ 1412:b7a9edee85e0 scale-ticks

Change loop to something that feels more correct, though it makes no difference to the tests here. More tests, one failing.
author Chris Cannam
date Thu, 04 May 2017 08:32:41 +0100
parents 1f0d071e7ce6
children c6fa111b4553
rev   line source
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@1412 293 void linear_sqrt2_pi_7()
Chris@1412 294 {
Chris@1412 295 auto ticks = ScaleTickIntervals::linear({ sqrt(2.0), M_PI, 7 });
Chris@1412 296 // This would be better in steps of 0.25, but we only round to
Chris@1412 297 // integral powers of ten
Chris@1412 298 vector<ScaleTickIntervals::Tick> expected {
Chris@1412 299 { 1.5, "1.5" },
Chris@1412 300 { 1.7, "1.7" },
Chris@1412 301 { 1.9, "1.9" },
Chris@1412 302 { 2.1, "2.1" },
Chris@1412 303 { 2.3, "2.3" },
Chris@1412 304 { 2.5, "2.5" },
Chris@1412 305 { 2.7, "2.7" },
Chris@1412 306 { 2.9, "2.9" },
Chris@1412 307 { 3.1, "3.1" },
Chris@1412 308 };
Chris@1412 309 compareTicks(ticks.ticks, expected);
Chris@1412 310 }
Chris@1412 311
Chris@1412 312 void linear_pi_avogadro_7()
Chris@1412 313 {
Chris@1412 314 auto ticks = ScaleTickIntervals::linear({ M_PI, 6.022140857e23, 7 });
Chris@1412 315 vector<ScaleTickIntervals::Tick> expected {
Chris@1412 316 { 1e+21, "1.0e+21" },
Chris@1412 317 { 8.7e+22, "8.7e+22" },
Chris@1412 318 { 1.73e+23, "1.73e+23" },
Chris@1412 319 { 2.59e+23, "2.59e+23" },
Chris@1412 320 { 3.45e+23, "3.45e+23" },
Chris@1412 321 { 4.31e+23, "4.31e+23" },
Chris@1412 322 { 5.17e+23, "5.17e+23" },
Chris@1412 323 };
Chris@1412 324 compareTicks(ticks.ticks, expected);
Chris@1412 325 }
Chris@1412 326
Chris@1412 327 void linear_2_3_1()
Chris@1412 328 {
Chris@1412 329 auto ticks = ScaleTickIntervals::linear({ 2, 3, 1 });
Chris@1412 330 vector<ScaleTickIntervals::Tick> expected {
Chris@1412 331 { 2.0, "2.0" },
Chris@1412 332 { 3.0, "3.0" }
Chris@1412 333 };
Chris@1412 334 compareTicks(ticks.ticks, expected);
Chris@1412 335 }
Chris@1412 336
Chris@1412 337 void linear_2_3_2()
Chris@1412 338 {
Chris@1412 339 auto ticks = ScaleTickIntervals::linear({ 2, 3, 2 });
Chris@1412 340 vector<ScaleTickIntervals::Tick> expected {
Chris@1412 341 { 2.0, "2.0" },
Chris@1412 342 { 2.5, "2.5" },
Chris@1412 343 { 3.0, "3.0" }
Chris@1412 344 };
Chris@1412 345 compareTicks(ticks.ticks, expected);
Chris@1412 346 }
Chris@1412 347
Chris@1412 348 void linear_2_3_3()
Chris@1412 349 {
Chris@1412 350 auto ticks = ScaleTickIntervals::linear({ 2, 3, 3 });
Chris@1412 351 vector<ScaleTickIntervals::Tick> expected {
Chris@1412 352 { 2.0, "2.0" },
Chris@1412 353 { 2.3, "2.3" },
Chris@1412 354 { 2.6, "2.6" },
Chris@1412 355 { 2.9, "2.9" }
Chris@1412 356 };
Chris@1412 357 compareTicks(ticks.ticks, expected);
Chris@1412 358 }
Chris@1412 359
Chris@1412 360 void linear_2_3_4()
Chris@1412 361 {
Chris@1412 362 auto ticks = ScaleTickIntervals::linear({ 2, 3, 4 });
Chris@1412 363 // This would be better in steps of 0.25, but we only round to
Chris@1412 364 // integral powers of ten
Chris@1412 365 vector<ScaleTickIntervals::Tick> expected {
Chris@1412 366 { 2.0, "2.0" },
Chris@1412 367 { 2.3, "2.3" },
Chris@1412 368 { 2.6, "2.6" },
Chris@1412 369 { 2.9, "2.9" }
Chris@1412 370 };
Chris@1412 371 compareTicks(ticks.ticks, expected);
Chris@1412 372 }
Chris@1412 373
Chris@1412 374 void linear_2_3_5()
Chris@1412 375 {
Chris@1412 376 auto ticks = ScaleTickIntervals::linear({ 2, 3, 5 });
Chris@1412 377 vector<ScaleTickIntervals::Tick> expected {
Chris@1412 378 { 2.0, "2.0" },
Chris@1412 379 { 2.2, "2.2" },
Chris@1412 380 { 2.4, "2.4" },
Chris@1412 381 { 2.6, "2.6" },
Chris@1412 382 { 2.8, "2.8" },
Chris@1412 383 { 3.0, "3.0" }
Chris@1412 384 };
Chris@1412 385 compareTicks(ticks.ticks, expected);
Chris@1412 386 }
Chris@1412 387
Chris@1412 388 void linear_2_3_6()
Chris@1412 389 {
Chris@1412 390 auto ticks = ScaleTickIntervals::linear({ 2, 3, 6 });
Chris@1412 391 vector<ScaleTickIntervals::Tick> expected {
Chris@1412 392 { 2.0, "2.0" },
Chris@1412 393 { 2.2, "2.2" },
Chris@1412 394 { 2.4, "2.4" },
Chris@1412 395 { 2.6, "2.6" },
Chris@1412 396 { 2.8, "2.8" },
Chris@1412 397 { 3.0, "3.0" }
Chris@1412 398 };
Chris@1412 399 compareTicks(ticks.ticks, expected);
Chris@1412 400 }
Chris@1412 401
Chris@1411 402 void linear_1_1_10()
Chris@1411 403 {
Chris@1411 404 auto ticks = ScaleTickIntervals::linear({ 1, 1, 10 });
Chris@1411 405 vector<ScaleTickIntervals::Tick> expected {
Chris@1411 406 { 1.0, "1.0" }
Chris@1411 407 };
Chris@1411 408 compareTicks(ticks.ticks, expected);
Chris@1411 409 }
Chris@1411 410
Chris@1411 411 void linear_0_0_10()
Chris@1411 412 {
Chris@1411 413 auto ticks = ScaleTickIntervals::linear({ 0, 0, 10 });
Chris@1411 414 vector<ScaleTickIntervals::Tick> expected {
Chris@1411 415 { 0.0, "0.0" }
Chris@1411 416 };
Chris@1411 417 compareTicks(ticks.ticks, expected);
Chris@1411 418 }
Chris@1411 419
Chris@1411 420 void linear_0_1_1()
Chris@1411 421 {
Chris@1411 422 auto ticks = ScaleTickIntervals::linear({ 0, 1, 1 });
Chris@1411 423 vector<ScaleTickIntervals::Tick> expected {
Chris@1411 424 { 0.0, "0.0" },
Chris@1411 425 { 1.0, "1.0" }
Chris@1411 426 };
Chris@1411 427 compareTicks(ticks.ticks, expected);
Chris@1411 428 }
Chris@1411 429
Chris@1411 430 void linear_0_1_0()
Chris@1411 431 {
Chris@1411 432 auto ticks = ScaleTickIntervals::linear({ 0, 1, 0 });
Chris@1411 433 vector<ScaleTickIntervals::Tick> expected {
Chris@1411 434 };
Chris@1411 435 compareTicks(ticks.ticks, expected);
Chris@1411 436 }
Chris@1411 437
Chris@1411 438 void linear_0_1_m1()
Chris@1411 439 {
Chris@1411 440 auto ticks = ScaleTickIntervals::linear({ 0, 1, -1 });
Chris@1411 441 vector<ScaleTickIntervals::Tick> expected {
Chris@1411 442 };
Chris@1411 443 compareTicks(ticks.ticks, expected);
Chris@1411 444 }
Chris@1407 445 };
Chris@1407 446
Chris@1407 447 #endif
Chris@1407 448
Chris@1407 449