comparison base/test/TestRangeMapper.h @ 883:4cc8265c4611

RangeMapperTest -> TestRangeMapper
author Chris Cannam
date Thu, 06 Feb 2014 10:14:19 +0000
parents base/test/RangeMapperTest.h@b4787b595db3
children 633a8fa622c9
comparison
equal deleted inserted replaced
881:816c751a7979 883:4cc8265c4611
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_RANGE_MAPPER_H
16 #define TEST_RANGE_MAPPER_H
17
18 #include "../RangeMapper.h"
19
20 #include <QObject>
21 #include <QtTest>
22 #include <QDir>
23
24 #include <iostream>
25
26 using namespace std;
27
28 class RangeMapperTest : public QObject
29 {
30 Q_OBJECT
31
32 private slots:
33 void linearUpForward()
34 {
35 LinearRangeMapper rm(1, 8, 0.5, 4.0, "x", false);
36 QCOMPARE(rm.getUnit(), QString("x"));
37 QCOMPARE(rm.getPositionForValue(0.5), 1);
38 QCOMPARE(rm.getPositionForValue(4.0), 8);
39 QCOMPARE(rm.getPositionForValue(3.0), 6);
40 QCOMPARE(rm.getPositionForValue(3.1), 6);
41 QCOMPARE(rm.getPositionForValue(3.4), 7);
42 QCOMPARE(rm.getPositionForValue(0.2), 1);
43 QCOMPARE(rm.getPositionForValue(-12), 1);
44 QCOMPARE(rm.getPositionForValue(6.1), 8);
45 }
46
47 void linearDownForward()
48 {
49 LinearRangeMapper rm(1, 8, 0.5, 4.0, "x", true);
50 QCOMPARE(rm.getUnit(), QString("x"));
51 QCOMPARE(rm.getPositionForValue(0.5), 8);
52 QCOMPARE(rm.getPositionForValue(4.0), 1);
53 QCOMPARE(rm.getPositionForValue(3.0), 3);
54 QCOMPARE(rm.getPositionForValue(3.1), 3);
55 QCOMPARE(rm.getPositionForValue(3.4), 2);
56 QCOMPARE(rm.getPositionForValue(0.2), 8);
57 QCOMPARE(rm.getPositionForValue(-12), 8);
58 QCOMPARE(rm.getPositionForValue(6.1), 1);
59 }
60
61 void linearUpBackward()
62 {
63 LinearRangeMapper rm(1, 8, 0.5, 4.0, "x", false);
64 QCOMPARE(rm.getUnit(), QString("x"));
65 QCOMPARE(rm.getValueForPosition(1), 0.5);
66 QCOMPARE(rm.getValueForPosition(8), 4.0);
67 QCOMPARE(rm.getValueForPosition(6), 3.0);
68 QCOMPARE(rm.getValueForPosition(7), 3.5);
69 QCOMPARE(rm.getValueForPosition(0), rm.getValueForPosition(1));
70 QCOMPARE(rm.getValueForPosition(9), rm.getValueForPosition(8));
71 }
72
73 void linearDownBackward()
74 {
75 LinearRangeMapper rm(1, 8, 0.5, 4.0, "x", true);
76 QCOMPARE(rm.getUnit(), QString("x"));
77 QCOMPARE(rm.getValueForPosition(8), 0.5);
78 QCOMPARE(rm.getValueForPosition(1), 4.0);
79 QCOMPARE(rm.getValueForPosition(3), 3.0);
80 QCOMPARE(rm.getValueForPosition(2), 3.5);
81 QCOMPARE(rm.getValueForPosition(0), rm.getValueForPosition(1));
82 QCOMPARE(rm.getValueForPosition(9), rm.getValueForPosition(8));
83 }
84
85 void logUpForward()
86 {
87 LogRangeMapper rm(3, 7, 10, 100000, "x", false);
88 QCOMPARE(rm.getUnit(), QString("x"));
89 QCOMPARE(rm.getPositionForValue(10.0), 3);
90 QCOMPARE(rm.getPositionForValue(100000.0), 7);
91 QCOMPARE(rm.getPositionForValue(1.0), 3);
92 QCOMPARE(rm.getPositionForValue(1000000.0), 7);
93 QCOMPARE(rm.getPositionForValue(1000.0), 5);
94 QCOMPARE(rm.getPositionForValue(900.0), 5);
95 QCOMPARE(rm.getPositionForValue(20000), 6);
96 }
97
98 void logDownForward()
99 {
100 LogRangeMapper rm(3, 7, 10, 100000, "x", true);
101 QCOMPARE(rm.getUnit(), QString("x"));
102 QCOMPARE(rm.getPositionForValue(10.0), 7);
103 QCOMPARE(rm.getPositionForValue(100000.0), 3);
104 QCOMPARE(rm.getPositionForValue(1.0), 7);
105 QCOMPARE(rm.getPositionForValue(1000000.0), 3);
106 QCOMPARE(rm.getPositionForValue(1000.0), 5);
107 QCOMPARE(rm.getPositionForValue(900.0), 5);
108 QCOMPARE(rm.getPositionForValue(20000), 4);
109 }
110
111 void logUpBackward()
112 {
113 LogRangeMapper rm(3, 7, 10, 100000, "x", false);
114 QCOMPARE(rm.getUnit(), QString("x"));
115 QCOMPARE(rm.getValueForPosition(3), 10.0);
116 QCOMPARE(rm.getValueForPosition(7), 100000.0);
117 QCOMPARE(rm.getValueForPosition(5), 1000.0);
118 QCOMPARE(rm.getValueForPosition(6), 10000.0);
119 QCOMPARE(rm.getValueForPosition(0), rm.getValueForPosition(3));
120 QCOMPARE(rm.getValueForPosition(9), rm.getValueForPosition(7));
121 }
122
123 void logDownBackward()
124 {
125 LogRangeMapper rm(3, 7, 10, 100000, "x", true);
126 QCOMPARE(rm.getUnit(), QString("x"));
127 QCOMPARE(rm.getValueForPosition(7), 10.0);
128 QCOMPARE(rm.getValueForPosition(3), 100000.0);
129 QCOMPARE(rm.getValueForPosition(5), 1000.0);
130 QCOMPARE(rm.getValueForPosition(4), 10000.0);
131 QCOMPARE(rm.getValueForPosition(0), rm.getValueForPosition(3));
132 QCOMPARE(rm.getValueForPosition(9), rm.getValueForPosition(7));
133 }
134
135 void interpolatingForward()
136 {
137 InterpolatingRangeMapper::CoordMap mappings;
138 mappings[1] = 10;
139 mappings[3] = 30;
140 mappings[5] = 70;
141 InterpolatingRangeMapper rm(mappings, "x");
142 QCOMPARE(rm.getUnit(), QString("x"));
143 QCOMPARE(rm.getPositionForValue(1.0), 10);
144 QCOMPARE(rm.getPositionForValue(0.0), 10);
145 QCOMPARE(rm.getPositionForValue(5.0), 70);
146 QCOMPARE(rm.getPositionForValue(6.0), 70);
147 QCOMPARE(rm.getPositionForValue(3.0), 30);
148 QCOMPARE(rm.getPositionForValue(2.5), 25);
149 QCOMPARE(rm.getPositionForValue(4.5), 60);
150 }
151
152 void interpolatingBackward()
153 {
154 InterpolatingRangeMapper::CoordMap mappings;
155 mappings[1] = 10;
156 mappings[3] = 30;
157 mappings[5] = 70;
158 InterpolatingRangeMapper rm(mappings, "x");
159 QCOMPARE(rm.getUnit(), QString("x"));
160 QCOMPARE(rm.getValueForPosition(10), 1.0);
161 QCOMPARE(rm.getValueForPosition(9), 1.0);
162 QCOMPARE(rm.getValueForPosition(70), 5.0);
163 QCOMPARE(rm.getValueForPosition(80), 5.0);
164 QCOMPARE(rm.getValueForPosition(30), 3.0);
165 QCOMPARE(rm.getValueForPosition(25), 2.5);
166 QCOMPARE(rm.getValueForPosition(60), 4.5);
167 }
168
169 void autoLinearForward()
170 {
171 AutoRangeMapper::CoordMap mappings;
172 mappings[0.5] = 1;
173 mappings[4.0] = 8;
174 AutoRangeMapper rm1(mappings, "x");
175 QCOMPARE(rm1.getUnit(), QString("x"));
176 QCOMPARE(rm1.getType(), AutoRangeMapper::StraightLine);
177 QCOMPARE(rm1.getPositionForValue(0.5), 1);
178 QCOMPARE(rm1.getPositionForValue(4.0), 8);
179 QCOMPARE(rm1.getPositionForValue(3.0), 6);
180 QCOMPARE(rm1.getPositionForValue(3.1), 6);
181 mappings[3.0] = 6;
182 mappings[3.5] = 7;
183 AutoRangeMapper rm2(mappings, "x");
184 QCOMPARE(rm2.getUnit(), QString("x"));
185 QCOMPARE(rm2.getType(), AutoRangeMapper::StraightLine);
186 QCOMPARE(rm2.getPositionForValue(0.5), 1);
187 QCOMPARE(rm2.getPositionForValue(4.0), 8);
188 QCOMPARE(rm2.getPositionForValue(3.0), 6);
189 QCOMPARE(rm2.getPositionForValue(3.1), 6);
190 }
191
192 void autoLogForward()
193 {
194 AutoRangeMapper::CoordMap mappings;
195 mappings[10] = 3;
196 mappings[1000] = 5;
197 mappings[100000] = 7;
198 AutoRangeMapper rm1(mappings, "x");
199 QCOMPARE(rm1.getUnit(), QString("x"));
200 QCOMPARE(rm1.getType(), AutoRangeMapper::Logarithmic);
201 QCOMPARE(rm1.getPositionForValue(10.0), 3);
202 QCOMPARE(rm1.getPositionForValue(100000.0), 7);
203 QCOMPARE(rm1.getPositionForValue(1.0), 3);
204 QCOMPARE(rm1.getPositionForValue(1000000.0), 7);
205 QCOMPARE(rm1.getPositionForValue(1000.0), 5);
206 QCOMPARE(rm1.getPositionForValue(900.0), 5);
207 QCOMPARE(rm1.getPositionForValue(20000), 6);
208 mappings[100] = 4;
209 AutoRangeMapper rm2(mappings, "x");
210 QCOMPARE(rm2.getUnit(), QString("x"));
211 QCOMPARE(rm2.getType(), AutoRangeMapper::Logarithmic);
212 QCOMPARE(rm2.getPositionForValue(10.0), 3);
213 QCOMPARE(rm2.getPositionForValue(100000.0), 7);
214 QCOMPARE(rm2.getPositionForValue(1.0), 3);
215 QCOMPARE(rm2.getPositionForValue(1000000.0), 7);
216 QCOMPARE(rm2.getPositionForValue(1000.0), 5);
217 QCOMPARE(rm2.getPositionForValue(900.0), 5);
218 QCOMPARE(rm2.getPositionForValue(20000), 6);
219 }
220
221 void autoInterpolatingForward()
222 {
223 AutoRangeMapper::CoordMap mappings;
224 mappings[1] = 10;
225 mappings[3] = 30;
226 mappings[5] = 70;
227 AutoRangeMapper rm(mappings, "x");
228 QCOMPARE(rm.getUnit(), QString("x"));
229 QCOMPARE(rm.getType(), AutoRangeMapper::Interpolating);
230 QCOMPARE(rm.getPositionForValue(1.0), 10);
231 QCOMPARE(rm.getPositionForValue(0.0), 10);
232 QCOMPARE(rm.getPositionForValue(5.0), 70);
233 QCOMPARE(rm.getPositionForValue(6.0), 70);
234 QCOMPARE(rm.getPositionForValue(3.0), 30);
235 QCOMPARE(rm.getPositionForValue(2.5), 25);
236 QCOMPARE(rm.getPositionForValue(4.5), 60);
237 }
238 };
239
240 #endif
241
242