annotate base/test/TestScaleTickIntervals.h @ 1407:25ed6dde2ce0 scale-ticks

Scale tick labeller, and tests (some failing so far)
author Chris Cannam
date Wed, 03 May 2017 13:02:08 +0100
parents
children f89365917d02
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@1407 37 for (int i = 0; i < int(expected.size()); ++i) {
Chris@1407 38 if (i < int(ticks.size())) {
Chris@1407 39 cerr << i << ": have " << ticks[i].value << " \""
Chris@1407 40 << ticks[i].label << "\", expected "
Chris@1407 41 << expected[i].value << " \"" << expected[i].label
Chris@1407 42 << "\"" << endl;
Chris@1407 43 }
Chris@1407 44 }
Chris@1407 45 }
Chris@1407 46
Chris@1407 47 void compareTicks(vector<ScaleTickIntervals::Tick> ticks,
Chris@1407 48 vector<ScaleTickIntervals::Tick> expected)
Chris@1407 49 {
Chris@1407 50 for (int i = 0; i < int(expected.size()); ++i) {
Chris@1407 51 if (i < int(ticks.size())) {
Chris@1407 52 if (ticks[i].label != expected[i].label ||
Chris@1407 53 ticks[i].value != expected[i].value) {
Chris@1407 54 printDiff(ticks, expected);
Chris@1407 55 }
Chris@1407 56 QCOMPARE(ticks[i].label, expected[i].label);
Chris@1407 57 QCOMPARE(ticks[i].value, expected[i].value);
Chris@1407 58 }
Chris@1407 59 }
Chris@1407 60 QCOMPARE(ticks.size(), expected.size());
Chris@1407 61 }
Chris@1407 62
Chris@1407 63 private slots:
Chris@1407 64 void linear_0_1_10()
Chris@1407 65 {
Chris@1407 66 auto ticks = ScaleTickIntervals::linear({ 0, 1, 10 });
Chris@1407 67 vector<ScaleTickIntervals::Tick> expected {
Chris@1407 68 { 0.0, "0.00" },
Chris@1407 69 { 0.1, "0.10" },
Chris@1407 70 { 0.2, "0.20" },
Chris@1407 71 { 0.3, "0.30" },
Chris@1407 72 { 0.4, "0.40" },
Chris@1407 73 { 0.5, "0.50" },
Chris@1407 74 { 0.6, "0.60" },
Chris@1407 75 { 0.7, "0.70" },
Chris@1407 76 { 0.8, "0.80" },
Chris@1407 77 { 0.9, "0.90" },
Chris@1407 78 { 1.0, "1.00" }
Chris@1407 79 };
Chris@1407 80 compareTicks(ticks.ticks, expected);
Chris@1407 81 }
Chris@1407 82
Chris@1407 83 void linear_0_0p1_5()
Chris@1407 84 {
Chris@1407 85 auto ticks = ScaleTickIntervals::linear({ 0, 0.1, 5 });
Chris@1407 86 vector<ScaleTickIntervals::Tick> expected {
Chris@1407 87 { 0.0, "0.00" },
Chris@1407 88 { 0.02, "0.02" },
Chris@1407 89 { 0.04, "0.04" },
Chris@1407 90 { 0.06, "0.06" },
Chris@1407 91 { 0.08, "0.08" },
Chris@1407 92 { 0.1, "0.10" }
Chris@1407 93 };
Chris@1407 94 compareTicks(ticks.ticks, expected);
Chris@1407 95 }
Chris@1407 96
Chris@1407 97 void linear_0_0p01_5()
Chris@1407 98 {
Chris@1407 99 auto ticks = ScaleTickIntervals::linear({ 0, 0.01, 5 });
Chris@1407 100 vector<ScaleTickIntervals::Tick> expected {
Chris@1407 101 { 0.00, "0.000" },
Chris@1407 102 { 0.002, "0.002" },
Chris@1407 103 { 0.004, "0.004" },
Chris@1407 104 { 0.006, "0.006" },
Chris@1407 105 { 0.008, "0.008" },
Chris@1407 106 { 0.01, "0.010" }
Chris@1407 107 };
Chris@1407 108 compareTicks(ticks.ticks, expected);
Chris@1407 109 }
Chris@1407 110
Chris@1407 111 void linear_0_0p001_5()
Chris@1407 112 {
Chris@1407 113 auto ticks = ScaleTickIntervals::linear({ 0, 0.001, 5 });
Chris@1407 114 vector<ScaleTickIntervals::Tick> expected {
Chris@1407 115 { 0.000, "0.0e+00" },
Chris@1407 116 { 0.0002, "2.0e-04" },
Chris@1407 117 { 0.0004, "4.0e-04" },
Chris@1407 118 { 0.0006, "6.0e-04" },
Chris@1407 119 { 0.0008, "8.0e-04" },
Chris@1407 120 { 0.001, "1.0e-03" }
Chris@1407 121 };
Chris@1407 122 compareTicks(ticks.ticks, expected);
Chris@1407 123 }
Chris@1407 124
Chris@1407 125 void linear_1_1p001_5()
Chris@1407 126 {
Chris@1407 127 auto ticks = ScaleTickIntervals::linear({ 1, 1.001, 5 });
Chris@1407 128 vector<ScaleTickIntervals::Tick> expected {
Chris@1407 129 { 1.000, "1.0000" },
Chris@1407 130 { 1.0002, "1.0002" },
Chris@1407 131 { 1.0004, "1.0004" },
Chris@1407 132 { 1.0006, "1.0006" },
Chris@1407 133 { 1.0008, "1.0008" },
Chris@1407 134 { 1.001, "1.0010" }
Chris@1407 135 };
Chris@1407 136 compareTicks(ticks.ticks, expected);
Chris@1407 137 }
Chris@1407 138 };
Chris@1407 139
Chris@1407 140 #endif
Chris@1407 141
Chris@1407 142