comparison base/test/TestLogRange.h @ 1392:667e369cfeab

LogRange tests
author Chris Cannam
date Tue, 28 Feb 2017 11:21:49 +0000
parents
children
comparison
equal deleted inserted replaced
1391:2c0e04062a99 1392:667e369cfeab
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_LOG_RANGE_H
16 #define TEST_LOG_RANGE_H
17
18 #include "../LogRange.h"
19
20 #include <QObject>
21 #include <QtTest>
22
23 #include <iostream>
24 #include <cmath>
25
26 using namespace std;
27
28 class TestLogRange : public QObject
29 {
30 Q_OBJECT
31
32 private slots:
33
34 void mapPositiveAboveDefaultThreshold()
35 {
36 QCOMPARE(LogRange::map(10.0), 1.0);
37 QCOMPARE(LogRange::map(100.0), 2.0);
38 QCOMPARE(LogRange::map(0.1), -1.0);
39 QCOMPARE(LogRange::map(1.0), 0.0);
40 QCOMPARE(LogRange::map(0.0000001), -7.0);
41 QCOMPARE(LogRange::map(20.0), log10(20.0));
42 }
43
44 void mapPositiveAboveSetThreshold()
45 {
46 QCOMPARE(LogRange::map(10.0, -10.0), 1.0);
47 QCOMPARE(LogRange::map(100.0, 1.0), 2.0);
48 QCOMPARE(LogRange::map(0.1, -5.0), -1.0);
49 QCOMPARE(LogRange::map(1.0, -0.01), 0.0);
50 QCOMPARE(LogRange::map(0.0000001, -20.0), -7.0);
51 QCOMPARE(LogRange::map(20.0, 0.0), log10(20.0));
52 }
53
54 void mapZeroDefaultThreshold()
55 {
56 QCOMPARE(LogRange::map(0.0), -10.0);
57 }
58
59 void mapZeroSetThreshold()
60 {
61 QCOMPARE(LogRange::map(0.0, 12.0), 12.0);
62 QCOMPARE(LogRange::map(0.0, -12.0), -12.0);
63 QCOMPARE(LogRange::map(0.0, 0.0), 0.0);
64 }
65
66 void mapPositiveBelowDefaultThreshold()
67 {
68 // The threshold is used only for zero values, not for very
69 // small ones -- it's arguably a stand-in or replacement value
70 // rather than a threshold. So this should behave the same as
71 // for values above the threshold.
72 QCOMPARE(LogRange::map(1e-10), -10.0);
73 QCOMPARE(LogRange::map(1e-20), -20.0);
74 QCOMPARE(LogRange::map(1e-100), -100.0);
75 }
76
77 void mapPositiveBelowSetThreshold()
78 {
79 // As above
80 QCOMPARE(LogRange::map(10.0, 4.0), 1.0);
81 QCOMPARE(LogRange::map(1e-10, 4.0), -10.0);
82 QCOMPARE(LogRange::map(1e-20, -15.0), -20.0);
83 QCOMPARE(LogRange::map(1e-100, -100.0), -100.0);
84 }
85
86 void mapNegative()
87 {
88 // Should always return map of absolute value. These are
89 // picked from vaarious of the above tests.
90
91 QCOMPARE(LogRange::map(-10.0), 1.0);
92 QCOMPARE(LogRange::map(-100.0), 2.0);
93 QCOMPARE(LogRange::map(-0.1), -1.0);
94 QCOMPARE(LogRange::map(-1.0), 0.0);
95 QCOMPARE(LogRange::map(-0.0000001), -7.0);
96 QCOMPARE(LogRange::map(-20.0), log10(20.0));
97 QCOMPARE(LogRange::map(-10.0, 4.0), 1.0);
98 QCOMPARE(LogRange::map(-1e-10, 4.0), -10.0);
99 QCOMPARE(LogRange::map(-1e-20, -15.0), -20.0);
100 QCOMPARE(LogRange::map(-1e-100, -100.0), -100.0);
101 QCOMPARE(LogRange::map(-0.0, 12.0), 12.0);
102 QCOMPARE(LogRange::map(-0.0, -12.0), -12.0);
103 QCOMPARE(LogRange::map(-0.0, 0.0), 0.0);
104 }
105
106 void unmap()
107 {
108 // Simply pow(10, x)
109
110 QCOMPARE(LogRange::unmap(0.0), 1.0);
111 QCOMPARE(LogRange::unmap(1.0), 10.0);
112 QCOMPARE(LogRange::unmap(-1.0), 0.1);
113 QCOMPARE(LogRange::unmap(100.0), 1e+100);
114 QCOMPARE(LogRange::unmap(-100.0), 1e-100);
115 }
116
117 void mapRangeAllPositiveDefaultThreshold()
118 {
119 double min, max;
120
121 min = 1.0; max = 10.0;
122 LogRange::mapRange(min, max);
123 QCOMPARE(min, 0.0); QCOMPARE(max, 1.0);
124
125 min = 10.0; max = 1.0;
126 LogRange::mapRange(min, max);
127 QCOMPARE(min, 0.0); QCOMPARE(max, 1.0);
128
129 // if equal, the function uses an arbitrary 1.0 range before mapping
130 min = 10.0; max = 10.0;
131 LogRange::mapRange(min, max);
132 QCOMPARE(min, 1.0); QCOMPARE(max, log10(11.0));
133 }
134
135 void mapRangeAllPositiveSetThreshold()
136 {
137 double min, max;
138
139 min = 1.0; max = 10.0;
140 LogRange::mapRange(min, max, -4.0);
141 QCOMPARE(min, 0.0); QCOMPARE(max, 1.0);
142
143 min = 10.0; max = 1.0;
144 LogRange::mapRange(min, max, -4.0);
145 QCOMPARE(min, 0.0); QCOMPARE(max, 1.0);
146
147 // if equal, the function uses an arbitrary 1.0 range before mapping
148 min = 10.0; max = 10.0;
149 LogRange::mapRange(min, max, -4.0);
150 QCOMPARE(min, 1.0); QCOMPARE(max, log10(11.0));
151 }
152
153 void mapRangeAllNegativeDefaultThreshold()
154 {
155 double min, max;
156
157 min = -1.0; max = -10.0;
158 LogRange::mapRange(min, max);
159 QCOMPARE(min, 0.0); QCOMPARE(max, 1.0);
160
161 min = -10.0; max = -1.0;
162 LogRange::mapRange(min, max);
163 QCOMPARE(min, 0.0); QCOMPARE(max, 1.0);
164
165 // if equal, the function uses an arbitrary 1.0 range before mapping
166 min = -10.0; max = -10.0;
167 LogRange::mapRange(min, max);
168 QCOMPARE(min, log10(9.0)); QCOMPARE(max, 1.0);
169 }
170
171 void mapRangeAllNegativeSetThreshold()
172 {
173 double min, max;
174
175 min = -1.0; max = -10.0;
176 LogRange::mapRange(min, max, -4.0);
177 QCOMPARE(min, 0.0); QCOMPARE(max, 1.0);
178
179 min = -10.0; max = -1.0;
180 LogRange::mapRange(min, max, -4.0);
181 QCOMPARE(min, 0.0); QCOMPARE(max, 1.0);
182
183 // if equal, the function uses an arbitrary 1.0 range before mapping
184 min = -10.0; max = -10.0;
185 LogRange::mapRange(min, max, -4.0);
186 QCOMPARE(min, log10(9.0)); QCOMPARE(max, 1.0);
187 }
188
189 void mapRangeAllNonNegativeDefaultThreshold()
190 {
191 double min, max;
192
193 min = 0.0; max = 10.0;
194 LogRange::mapRange(min, max);
195 QCOMPARE(min, -10.0); QCOMPARE(max, 1.0);
196
197 min = 10.0; max = 0.0;
198 LogRange::mapRange(min, max);
199 QCOMPARE(min, -10.0); QCOMPARE(max, 1.0);
200
201 // if equal, the function uses an arbitrary 1.0 range before mapping
202 min = 0.0; max = 0.0;
203 LogRange::mapRange(min, max);
204 QCOMPARE(min, -10.0); QCOMPARE(max, 0.0);
205 }
206
207 void mapRangeAllNonNegativeSetThreshold()
208 {
209 double min, max;
210
211 min = 0.0; max = 10.0;
212 LogRange::mapRange(min, max, -4.0);
213 QCOMPARE(min, -4.0); QCOMPARE(max, 1.0);
214
215 min = 10.0; max = 0.0;
216 LogRange::mapRange(min, max, -4.0);
217 QCOMPARE(min, -4.0); QCOMPARE(max, 1.0);
218
219 // if equal, the function uses an arbitrary 1.0 range before mapping
220 min = 0.0; max = 0.0;
221 LogRange::mapRange(min, max, -4.0);
222 QCOMPARE(min, -4.0); QCOMPARE(max, 0.0);
223 }
224
225 void mapRangeAllNonPositiveDefaultThreshold()
226 {
227 double min, max;
228
229 min = 0.0; max = -10.0;
230 LogRange::mapRange(min, max);
231 QCOMPARE(min, -10.0); QCOMPARE(max, 1.0);
232
233 min = -10.0; max = 0.0;
234 LogRange::mapRange(min, max);
235 QCOMPARE(min, -10.0); QCOMPARE(max, 1.0);
236 }
237
238 void mapRangeAllNonPositiveSetThreshold()
239 {
240 double min, max;
241
242 min = 0.0; max = -10.0;
243 LogRange::mapRange(min, max, -4.0);
244 QCOMPARE(min, -4.0); QCOMPARE(max, 1.0);
245
246 min = -10.0; max = 0.0;
247 LogRange::mapRange(min, max, -4.0);
248 QCOMPARE(min, -4.0); QCOMPARE(max, 1.0);
249 }
250
251 void mapRangeSpanningZeroDefaultThreshold()
252 {
253 double min, max;
254
255 min = -1.0; max = 10.0;
256 LogRange::mapRange(min, max);
257 QCOMPARE(min, -10.0); QCOMPARE(max, 1.0);
258
259 min = -100.0; max = 1.0;
260 LogRange::mapRange(min, max);
261 QCOMPARE(min, -10.0); QCOMPARE(max, 2.0);
262
263 min = -10.0; max = 1e-200;
264 LogRange::mapRange(min, max);
265 QCOMPARE(min, -10.0); QCOMPARE(max, 1.0);
266
267 min = 1e-200; max = -10.0;
268 LogRange::mapRange(min, max);
269 QCOMPARE(min, -10.0); QCOMPARE(max, 1.0);
270
271 min = -1e-200; max = 100.0;
272 LogRange::mapRange(min, max);
273 QCOMPARE(min, -10.0); QCOMPARE(max, 2.0);
274
275 min = 10.0; max = -1e-200;
276 LogRange::mapRange(min, max);
277 QCOMPARE(min, -10.0); QCOMPARE(max, 1.0);
278
279 // if none of the input range is above the threshold in
280 // magnitude, but it still spans zero, we use the input max as
281 // threshold and then add 1 for range
282 min = -1e-200; max = 1e-300;
283 LogRange::mapRange(min, max);
284 QCOMPARE(min, -201.0); QCOMPARE(max, -200.0);
285
286 min = 1e-200; max = -1e-300;
287 LogRange::mapRange(min, max);
288 QCOMPARE(min, -201.0); QCOMPARE(max, -200.0);
289 }
290
291 void mapRangeSpanningZeroSetThreshold()
292 {
293 double min, max;
294
295 min = -1.0; max = 10.0;
296 LogRange::mapRange(min, max, -4.0);
297 QCOMPARE(min, -4.0); QCOMPARE(max, 1.0);
298
299 min = -100.0; max = 1.0;
300 LogRange::mapRange(min, max, -4.0);
301 QCOMPARE(min, -4.0); QCOMPARE(max, 2.0);
302
303 min = -10.0; max = 1e-200;
304 LogRange::mapRange(min, max, -4.0);
305 QCOMPARE(min, -4.0); QCOMPARE(max, 1.0);
306
307 min = 1e-200; max = -10.0;
308 LogRange::mapRange(min, max, -4.0);
309 QCOMPARE(min, -4.0); QCOMPARE(max, 1.0);
310
311 min = -1e-200; max = 100.0;
312 LogRange::mapRange(min, max, -4.0);
313 QCOMPARE(min, -4.0); QCOMPARE(max, 2.0);
314
315 min = 10.0; max = -1e-200;
316 LogRange::mapRange(min, max, -4.0);
317 QCOMPARE(min, -4.0); QCOMPARE(max, 1.0);
318
319 // if none of the input range is above the threshold in
320 // magnitude, but it still spans zero, we use the input max as
321 // threshold and then add 1 for range
322 min = -1e-200; max = 1e-300;
323 LogRange::mapRange(min, max, -4.0);
324 QCOMPARE(min, -201.0); QCOMPARE(max, -200.0);
325
326 min = 1e-200; max = -1e-300;
327 LogRange::mapRange(min, max, -4.0);
328 QCOMPARE(min, -201.0); QCOMPARE(max, -200.0);
329 }
330
331 };
332
333 #endif