Mercurial > hg > svcore
comparison base/test/TestRangeMapper.h @ 891:8962f80f5d8e tony_integration
Merge from default branch
author | Chris Cannam |
---|---|
date | Tue, 11 Mar 2014 17:32:31 +0000 |
parents | 12a6140b3ae0 |
children | ccc8658914ef |
comparison
equal
deleted
inserted
replaced
890:4cbf8c6a462d | 891:8962f80f5d8e |
---|---|
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 TestRangeMapper : 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 QCOMPARE(rm.getPositionForValueUnclamped(3.0), 6); | |
46 QCOMPARE(rm.getPositionForValueUnclamped(0.2), 0); | |
47 QCOMPARE(rm.getPositionForValueUnclamped(-12), -24); | |
48 QCOMPARE(rm.getPositionForValueUnclamped(6.1), 12); | |
49 } | |
50 | |
51 void linearDownForward() | |
52 { | |
53 LinearRangeMapper rm(1, 8, 0.5, 4.0, "x", true); | |
54 QCOMPARE(rm.getUnit(), QString("x")); | |
55 QCOMPARE(rm.getPositionForValue(0.5), 8); | |
56 QCOMPARE(rm.getPositionForValue(4.0), 1); | |
57 QCOMPARE(rm.getPositionForValue(3.0), 3); | |
58 QCOMPARE(rm.getPositionForValue(3.1), 3); | |
59 QCOMPARE(rm.getPositionForValue(3.4), 2); | |
60 QCOMPARE(rm.getPositionForValue(0.2), 8); | |
61 QCOMPARE(rm.getPositionForValue(-12), 8); | |
62 QCOMPARE(rm.getPositionForValue(6.1), 1); | |
63 QCOMPARE(rm.getPositionForValueUnclamped(3.0), 3); | |
64 QCOMPARE(rm.getPositionForValueUnclamped(0.2), 9); | |
65 QCOMPARE(rm.getPositionForValueUnclamped(-12), 33); | |
66 QCOMPARE(rm.getPositionForValueUnclamped(6.1), -3); | |
67 } | |
68 | |
69 void linearUpBackward() | |
70 { | |
71 LinearRangeMapper rm(1, 8, 0.5, 4.0, "x", false); | |
72 QCOMPARE(rm.getUnit(), QString("x")); | |
73 QCOMPARE(rm.getValueForPosition(1), 0.5); | |
74 QCOMPARE(rm.getValueForPosition(8), 4.0); | |
75 QCOMPARE(rm.getValueForPosition(6), 3.0); | |
76 QCOMPARE(rm.getValueForPosition(7), 3.5); | |
77 QCOMPARE(rm.getValueForPosition(0), rm.getValueForPosition(1)); | |
78 QCOMPARE(rm.getValueForPosition(9), rm.getValueForPosition(8)); | |
79 QCOMPARE(rm.getValueForPositionUnclamped(6), 3.0); | |
80 QCOMPARE(rm.getValueForPositionUnclamped(0), 0.0); | |
81 QCOMPARE(rm.getValueForPositionUnclamped(-24), -12.0); | |
82 QCOMPARE(rm.getValueForPositionUnclamped(12), 6.0); | |
83 } | |
84 | |
85 void linearDownBackward() | |
86 { | |
87 LinearRangeMapper rm(1, 8, 0.5, 4.0, "x", true); | |
88 QCOMPARE(rm.getUnit(), QString("x")); | |
89 QCOMPARE(rm.getValueForPosition(8), 0.5); | |
90 QCOMPARE(rm.getValueForPosition(1), 4.0); | |
91 QCOMPARE(rm.getValueForPosition(3), 3.0); | |
92 QCOMPARE(rm.getValueForPosition(2), 3.5); | |
93 QCOMPARE(rm.getValueForPosition(0), rm.getValueForPosition(1)); | |
94 QCOMPARE(rm.getValueForPosition(9), rm.getValueForPosition(8)); | |
95 QCOMPARE(rm.getValueForPositionUnclamped(3), 3.0); | |
96 QCOMPARE(rm.getValueForPositionUnclamped(9), 0.0); | |
97 QCOMPARE(rm.getValueForPositionUnclamped(33), -12.0); | |
98 QCOMPARE(rm.getValueForPositionUnclamped(-3), 6.0); | |
99 } | |
100 | |
101 void logUpForward() | |
102 { | |
103 LogRangeMapper rm(3, 7, 10, 100000, "x", false); | |
104 QCOMPARE(rm.getUnit(), QString("x")); | |
105 QCOMPARE(rm.getPositionForValue(10.0), 3); | |
106 QCOMPARE(rm.getPositionForValue(100000.0), 7); | |
107 QCOMPARE(rm.getPositionForValue(1.0), 3); | |
108 QCOMPARE(rm.getPositionForValue(1000000.0), 7); | |
109 QCOMPARE(rm.getPositionForValue(1000.0), 5); | |
110 QCOMPARE(rm.getPositionForValue(900.0), 5); | |
111 QCOMPARE(rm.getPositionForValue(20000), 6); | |
112 QCOMPARE(rm.getPositionForValueUnclamped(1.0), 2); | |
113 QCOMPARE(rm.getPositionForValueUnclamped(1000000.0), 8); | |
114 QCOMPARE(rm.getPositionForValueUnclamped(1000.0), 5); | |
115 } | |
116 | |
117 void logDownForward() | |
118 { | |
119 LogRangeMapper rm(3, 7, 10, 100000, "x", true); | |
120 QCOMPARE(rm.getUnit(), QString("x")); | |
121 QCOMPARE(rm.getPositionForValue(10.0), 7); | |
122 QCOMPARE(rm.getPositionForValue(100000.0), 3); | |
123 QCOMPARE(rm.getPositionForValue(1.0), 7); | |
124 QCOMPARE(rm.getPositionForValue(1000000.0), 3); | |
125 QCOMPARE(rm.getPositionForValue(1000.0), 5); | |
126 QCOMPARE(rm.getPositionForValue(900.0), 5); | |
127 QCOMPARE(rm.getPositionForValue(20000), 4); | |
128 QCOMPARE(rm.getPositionForValueUnclamped(1.0), 8); | |
129 QCOMPARE(rm.getPositionForValueUnclamped(1000000.0), 2); | |
130 QCOMPARE(rm.getPositionForValueUnclamped(1000.0), 5); | |
131 } | |
132 | |
133 void logUpBackward() | |
134 { | |
135 LogRangeMapper rm(3, 7, 10, 100000, "x", false); | |
136 QCOMPARE(rm.getUnit(), QString("x")); | |
137 QCOMPARE(rm.getValueForPosition(3), 10.0); | |
138 QCOMPARE(rm.getValueForPosition(7), 100000.0); | |
139 QCOMPARE(rm.getValueForPosition(5), 1000.0); | |
140 QCOMPARE(rm.getValueForPosition(6), 10000.0); | |
141 QCOMPARE(rm.getValueForPosition(0), rm.getValueForPosition(3)); | |
142 QCOMPARE(rm.getValueForPosition(9), rm.getValueForPosition(7)); | |
143 QCOMPARE(rm.getValueForPositionUnclamped(2), 1.0); | |
144 QCOMPARE(rm.getValueForPositionUnclamped(8), 1000000.0); | |
145 QCOMPARE(rm.getValueForPositionUnclamped(5), 1000.0); | |
146 } | |
147 | |
148 void logDownBackward() | |
149 { | |
150 LogRangeMapper rm(3, 7, 10, 100000, "x", true); | |
151 QCOMPARE(rm.getUnit(), QString("x")); | |
152 QCOMPARE(rm.getValueForPosition(7), 10.0); | |
153 QCOMPARE(rm.getValueForPosition(3), 100000.0); | |
154 QCOMPARE(rm.getValueForPosition(5), 1000.0); | |
155 QCOMPARE(rm.getValueForPosition(4), 10000.0); | |
156 QCOMPARE(rm.getValueForPosition(0), rm.getValueForPosition(3)); | |
157 QCOMPARE(rm.getValueForPosition(9), rm.getValueForPosition(7)); | |
158 QCOMPARE(rm.getValueForPositionUnclamped(8), 1.0); | |
159 QCOMPARE(rm.getValueForPositionUnclamped(2), 1000000.0); | |
160 QCOMPARE(rm.getValueForPositionUnclamped(5), 1000.0); | |
161 } | |
162 | |
163 void interpolatingForward() | |
164 { | |
165 InterpolatingRangeMapper::CoordMap mappings; | |
166 mappings[1] = 10; | |
167 mappings[3] = 30; | |
168 mappings[5] = 70; | |
169 InterpolatingRangeMapper rm(mappings, "x"); | |
170 QCOMPARE(rm.getUnit(), QString("x")); | |
171 QCOMPARE(rm.getPositionForValue(1.0), 10); | |
172 QCOMPARE(rm.getPositionForValue(0.0), 10); | |
173 QCOMPARE(rm.getPositionForValue(5.0), 70); | |
174 QCOMPARE(rm.getPositionForValue(6.0), 70); | |
175 QCOMPARE(rm.getPositionForValue(3.0), 30); | |
176 QCOMPARE(rm.getPositionForValue(2.5), 25); | |
177 QCOMPARE(rm.getPositionForValue(4.5), 60); | |
178 QCOMPARE(rm.getPositionForValueUnclamped(0.0), 0); | |
179 QCOMPARE(rm.getPositionForValueUnclamped(2.5), 25); | |
180 QCOMPARE(rm.getPositionForValueUnclamped(6.0), 90); | |
181 } | |
182 | |
183 void interpolatingBackward() | |
184 { | |
185 InterpolatingRangeMapper::CoordMap mappings; | |
186 mappings[1] = 10; | |
187 mappings[3] = 30; | |
188 mappings[5] = 70; | |
189 InterpolatingRangeMapper rm(mappings, "x"); | |
190 QCOMPARE(rm.getUnit(), QString("x")); | |
191 QCOMPARE(rm.getValueForPosition(10), 1.0); | |
192 QCOMPARE(rm.getValueForPosition(9), 1.0); | |
193 QCOMPARE(rm.getValueForPosition(70), 5.0); | |
194 QCOMPARE(rm.getValueForPosition(80), 5.0); | |
195 QCOMPARE(rm.getValueForPosition(30), 3.0); | |
196 QCOMPARE(rm.getValueForPosition(25), 2.5); | |
197 QCOMPARE(rm.getValueForPosition(60), 4.5); | |
198 } | |
199 | |
200 void autoLinearForward() | |
201 { | |
202 AutoRangeMapper::CoordMap mappings; | |
203 mappings[0.5] = 1; | |
204 mappings[4.0] = 8; | |
205 AutoRangeMapper rm1(mappings, "x"); | |
206 QCOMPARE(rm1.getUnit(), QString("x")); | |
207 QCOMPARE(rm1.getType(), AutoRangeMapper::StraightLine); | |
208 QCOMPARE(rm1.getPositionForValue(0.1), 1); | |
209 QCOMPARE(rm1.getPositionForValue(0.5), 1); | |
210 QCOMPARE(rm1.getPositionForValue(4.0), 8); | |
211 QCOMPARE(rm1.getPositionForValue(4.5), 8); | |
212 QCOMPARE(rm1.getPositionForValue(3.0), 6); | |
213 QCOMPARE(rm1.getPositionForValue(3.1), 6); | |
214 QCOMPARE(rm1.getPositionForValueUnclamped(0.1), 0); | |
215 QCOMPARE(rm1.getPositionForValueUnclamped(3.1), 6); | |
216 QCOMPARE(rm1.getPositionForValueUnclamped(4.5), 9); | |
217 mappings[3.0] = 6; | |
218 mappings[3.5] = 7; | |
219 AutoRangeMapper rm2(mappings, "x"); | |
220 QCOMPARE(rm2.getUnit(), QString("x")); | |
221 QCOMPARE(rm2.getType(), AutoRangeMapper::StraightLine); | |
222 QCOMPARE(rm2.getPositionForValue(0.5), 1); | |
223 QCOMPARE(rm2.getPositionForValue(4.0), 8); | |
224 QCOMPARE(rm2.getPositionForValue(3.0), 6); | |
225 QCOMPARE(rm2.getPositionForValue(3.1), 6); | |
226 } | |
227 | |
228 void autoLogForward() | |
229 { | |
230 AutoRangeMapper::CoordMap mappings; | |
231 mappings[10] = 3; | |
232 mappings[1000] = 5; | |
233 mappings[100000] = 7; | |
234 AutoRangeMapper rm1(mappings, "x"); | |
235 QCOMPARE(rm1.getUnit(), QString("x")); | |
236 QCOMPARE(rm1.getType(), AutoRangeMapper::Logarithmic); | |
237 QCOMPARE(rm1.getPositionForValue(10.0), 3); | |
238 QCOMPARE(rm1.getPositionForValue(100000.0), 7); | |
239 QCOMPARE(rm1.getPositionForValue(1.0), 3); | |
240 QCOMPARE(rm1.getPositionForValue(1000000.0), 7); | |
241 QCOMPARE(rm1.getPositionForValue(1000.0), 5); | |
242 QCOMPARE(rm1.getPositionForValue(900.0), 5); | |
243 QCOMPARE(rm1.getPositionForValue(20000), 6); | |
244 QCOMPARE(rm1.getPositionForValueUnclamped(1.0), 2); | |
245 QCOMPARE(rm1.getPositionForValueUnclamped(900.0), 5); | |
246 QCOMPARE(rm1.getPositionForValueUnclamped(1000000.0), 8); | |
247 mappings[100] = 4; | |
248 AutoRangeMapper rm2(mappings, "x"); | |
249 QCOMPARE(rm2.getUnit(), QString("x")); | |
250 QCOMPARE(rm2.getType(), AutoRangeMapper::Logarithmic); | |
251 QCOMPARE(rm2.getPositionForValue(10.0), 3); | |
252 QCOMPARE(rm2.getPositionForValue(100000.0), 7); | |
253 QCOMPARE(rm2.getPositionForValue(1.0), 3); | |
254 QCOMPARE(rm2.getPositionForValue(1000000.0), 7); | |
255 QCOMPARE(rm2.getPositionForValue(1000.0), 5); | |
256 QCOMPARE(rm2.getPositionForValue(900.0), 5); | |
257 QCOMPARE(rm2.getPositionForValue(20000), 6); | |
258 } | |
259 | |
260 void autoInterpolatingForward() | |
261 { | |
262 AutoRangeMapper::CoordMap mappings; | |
263 mappings[1] = 10; | |
264 mappings[3] = 30; | |
265 mappings[5] = 70; | |
266 AutoRangeMapper rm(mappings, "x"); | |
267 QCOMPARE(rm.getUnit(), QString("x")); | |
268 QCOMPARE(rm.getType(), AutoRangeMapper::Interpolating); | |
269 QCOMPARE(rm.getPositionForValue(1.0), 10); | |
270 QCOMPARE(rm.getPositionForValue(0.0), 10); | |
271 QCOMPARE(rm.getPositionForValue(5.0), 70); | |
272 QCOMPARE(rm.getPositionForValue(6.0), 70); | |
273 QCOMPARE(rm.getPositionForValue(3.0), 30); | |
274 QCOMPARE(rm.getPositionForValue(2.5), 25); | |
275 QCOMPARE(rm.getPositionForValue(4.5), 60); | |
276 QCOMPARE(rm.getPositionForValueUnclamped(0.0), 0); | |
277 QCOMPARE(rm.getPositionForValueUnclamped(5.0), 70); | |
278 QCOMPARE(rm.getPositionForValueUnclamped(6.0), 90); | |
279 } | |
280 }; | |
281 | |
282 #endif | |
283 | |
284 |