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