comparison 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
comparison
equal deleted inserted replaced
1406:09751743647e 1407:25ed6dde2ce0
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
2
3 /*
4 Sonic Visualiser
5 An audio file viewer and annotation editor.
6 Centre for Digital Music, Queen Mary, University of London.
7
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License as
10 published by the Free Software Foundation; either version 2 of the
11 License, or (at your option) any later version. See the file
12 COPYING included with this distribution for more information.
13 */
14
15 #ifndef TEST_SCALE_TICK_INTERVALS_H
16 #define TEST_SCALE_TICK_INTERVALS_H
17
18 #include "../ScaleTickIntervals.h"
19
20 #include <QObject>
21 #include <QtTest>
22 #include <QDir>
23
24 #include <iostream>
25
26 using namespace std;
27
28 class TestScaleTickIntervals : public QObject
29 {
30 Q_OBJECT
31
32 void printDiff(vector<ScaleTickIntervals::Tick> ticks,
33 vector<ScaleTickIntervals::Tick> expected) {
34
35 cerr << "Have " << ticks.size() << " ticks, expected "
36 << expected.size() << endl;
37 for (int i = 0; i < int(expected.size()); ++i) {
38 if (i < int(ticks.size())) {
39 cerr << i << ": have " << ticks[i].value << " \""
40 << ticks[i].label << "\", expected "
41 << expected[i].value << " \"" << expected[i].label
42 << "\"" << endl;
43 }
44 }
45 }
46
47 void compareTicks(vector<ScaleTickIntervals::Tick> ticks,
48 vector<ScaleTickIntervals::Tick> expected)
49 {
50 for (int i = 0; i < int(expected.size()); ++i) {
51 if (i < int(ticks.size())) {
52 if (ticks[i].label != expected[i].label ||
53 ticks[i].value != expected[i].value) {
54 printDiff(ticks, expected);
55 }
56 QCOMPARE(ticks[i].label, expected[i].label);
57 QCOMPARE(ticks[i].value, expected[i].value);
58 }
59 }
60 QCOMPARE(ticks.size(), expected.size());
61 }
62
63 private slots:
64 void linear_0_1_10()
65 {
66 auto ticks = ScaleTickIntervals::linear({ 0, 1, 10 });
67 vector<ScaleTickIntervals::Tick> expected {
68 { 0.0, "0.00" },
69 { 0.1, "0.10" },
70 { 0.2, "0.20" },
71 { 0.3, "0.30" },
72 { 0.4, "0.40" },
73 { 0.5, "0.50" },
74 { 0.6, "0.60" },
75 { 0.7, "0.70" },
76 { 0.8, "0.80" },
77 { 0.9, "0.90" },
78 { 1.0, "1.00" }
79 };
80 compareTicks(ticks.ticks, expected);
81 }
82
83 void linear_0_0p1_5()
84 {
85 auto ticks = ScaleTickIntervals::linear({ 0, 0.1, 5 });
86 vector<ScaleTickIntervals::Tick> expected {
87 { 0.0, "0.00" },
88 { 0.02, "0.02" },
89 { 0.04, "0.04" },
90 { 0.06, "0.06" },
91 { 0.08, "0.08" },
92 { 0.1, "0.10" }
93 };
94 compareTicks(ticks.ticks, expected);
95 }
96
97 void linear_0_0p01_5()
98 {
99 auto ticks = ScaleTickIntervals::linear({ 0, 0.01, 5 });
100 vector<ScaleTickIntervals::Tick> expected {
101 { 0.00, "0.000" },
102 { 0.002, "0.002" },
103 { 0.004, "0.004" },
104 { 0.006, "0.006" },
105 { 0.008, "0.008" },
106 { 0.01, "0.010" }
107 };
108 compareTicks(ticks.ticks, expected);
109 }
110
111 void linear_0_0p001_5()
112 {
113 auto ticks = ScaleTickIntervals::linear({ 0, 0.001, 5 });
114 vector<ScaleTickIntervals::Tick> expected {
115 { 0.000, "0.0e+00" },
116 { 0.0002, "2.0e-04" },
117 { 0.0004, "4.0e-04" },
118 { 0.0006, "6.0e-04" },
119 { 0.0008, "8.0e-04" },
120 { 0.001, "1.0e-03" }
121 };
122 compareTicks(ticks.ticks, expected);
123 }
124
125 void linear_1_1p001_5()
126 {
127 auto ticks = ScaleTickIntervals::linear({ 1, 1.001, 5 });
128 vector<ScaleTickIntervals::Tick> expected {
129 { 1.000, "1.0000" },
130 { 1.0002, "1.0002" },
131 { 1.0004, "1.0004" },
132 { 1.0006, "1.0006" },
133 { 1.0008, "1.0008" },
134 { 1.001, "1.0010" }
135 };
136 compareTicks(ticks.ticks, expected);
137 }
138 };
139
140 #endif
141
142