annotate base/test/TestRangeMapper.h @ 1073:d4212687520e

minor debug thing
author Chris Cannam
date Thu, 21 May 2015 16:38:07 +0100
parents 12a6140b3ae0
children ccc8658914ef
rev   line source
Chris@879 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@879 2
Chris@879 3 /*
Chris@879 4 Sonic Visualiser
Chris@879 5 An audio file viewer and annotation editor.
Chris@879 6 Centre for Digital Music, Queen Mary, University of London.
Chris@879 7
Chris@879 8 This program is free software; you can redistribute it and/or
Chris@879 9 modify it under the terms of the GNU General Public License as
Chris@879 10 published by the Free Software Foundation; either version 2 of the
Chris@879 11 License, or (at your option) any later version. See the file
Chris@879 12 COPYING included with this distribution for more information.
Chris@879 13 */
Chris@879 14
Chris@879 15 #ifndef TEST_RANGE_MAPPER_H
Chris@879 16 #define TEST_RANGE_MAPPER_H
Chris@879 17
Chris@879 18 #include "../RangeMapper.h"
Chris@879 19
Chris@879 20 #include <QObject>
Chris@879 21 #include <QtTest>
Chris@879 22 #include <QDir>
Chris@879 23
Chris@879 24 #include <iostream>
Chris@879 25
Chris@879 26 using namespace std;
Chris@879 27
Chris@884 28 class TestRangeMapper : public QObject
Chris@879 29 {
Chris@879 30 Q_OBJECT
Chris@879 31
Chris@879 32 private slots:
Chris@879 33 void linearUpForward()
Chris@879 34 {
Chris@879 35 LinearRangeMapper rm(1, 8, 0.5, 4.0, "x", false);
Chris@879 36 QCOMPARE(rm.getUnit(), QString("x"));
Chris@879 37 QCOMPARE(rm.getPositionForValue(0.5), 1);
Chris@879 38 QCOMPARE(rm.getPositionForValue(4.0), 8);
Chris@879 39 QCOMPARE(rm.getPositionForValue(3.0), 6);
Chris@879 40 QCOMPARE(rm.getPositionForValue(3.1), 6);
Chris@879 41 QCOMPARE(rm.getPositionForValue(3.4), 7);
Chris@879 42 QCOMPARE(rm.getPositionForValue(0.2), 1);
Chris@879 43 QCOMPARE(rm.getPositionForValue(-12), 1);
Chris@879 44 QCOMPARE(rm.getPositionForValue(6.1), 8);
Chris@885 45 QCOMPARE(rm.getPositionForValueUnclamped(3.0), 6);
Chris@885 46 QCOMPARE(rm.getPositionForValueUnclamped(0.2), 0);
Chris@885 47 QCOMPARE(rm.getPositionForValueUnclamped(-12), -24);
Chris@885 48 QCOMPARE(rm.getPositionForValueUnclamped(6.1), 12);
Chris@879 49 }
Chris@879 50
Chris@879 51 void linearDownForward()
Chris@879 52 {
Chris@879 53 LinearRangeMapper rm(1, 8, 0.5, 4.0, "x", true);
Chris@879 54 QCOMPARE(rm.getUnit(), QString("x"));
Chris@879 55 QCOMPARE(rm.getPositionForValue(0.5), 8);
Chris@879 56 QCOMPARE(rm.getPositionForValue(4.0), 1);
Chris@879 57 QCOMPARE(rm.getPositionForValue(3.0), 3);
Chris@879 58 QCOMPARE(rm.getPositionForValue(3.1), 3);
Chris@879 59 QCOMPARE(rm.getPositionForValue(3.4), 2);
Chris@879 60 QCOMPARE(rm.getPositionForValue(0.2), 8);
Chris@879 61 QCOMPARE(rm.getPositionForValue(-12), 8);
Chris@879 62 QCOMPARE(rm.getPositionForValue(6.1), 1);
Chris@885 63 QCOMPARE(rm.getPositionForValueUnclamped(3.0), 3);
Chris@885 64 QCOMPARE(rm.getPositionForValueUnclamped(0.2), 9);
Chris@885 65 QCOMPARE(rm.getPositionForValueUnclamped(-12), 33);
Chris@885 66 QCOMPARE(rm.getPositionForValueUnclamped(6.1), -3);
Chris@879 67 }
Chris@879 68
Chris@879 69 void linearUpBackward()
Chris@879 70 {
Chris@879 71 LinearRangeMapper rm(1, 8, 0.5, 4.0, "x", false);
Chris@879 72 QCOMPARE(rm.getUnit(), QString("x"));
Chris@879 73 QCOMPARE(rm.getValueForPosition(1), 0.5);
Chris@879 74 QCOMPARE(rm.getValueForPosition(8), 4.0);
Chris@879 75 QCOMPARE(rm.getValueForPosition(6), 3.0);
Chris@879 76 QCOMPARE(rm.getValueForPosition(7), 3.5);
Chris@879 77 QCOMPARE(rm.getValueForPosition(0), rm.getValueForPosition(1));
Chris@879 78 QCOMPARE(rm.getValueForPosition(9), rm.getValueForPosition(8));
Chris@885 79 QCOMPARE(rm.getValueForPositionUnclamped(6), 3.0);
Chris@885 80 QCOMPARE(rm.getValueForPositionUnclamped(0), 0.0);
Chris@885 81 QCOMPARE(rm.getValueForPositionUnclamped(-24), -12.0);
Chris@885 82 QCOMPARE(rm.getValueForPositionUnclamped(12), 6.0);
Chris@879 83 }
Chris@879 84
Chris@879 85 void linearDownBackward()
Chris@879 86 {
Chris@879 87 LinearRangeMapper rm(1, 8, 0.5, 4.0, "x", true);
Chris@879 88 QCOMPARE(rm.getUnit(), QString("x"));
Chris@879 89 QCOMPARE(rm.getValueForPosition(8), 0.5);
Chris@879 90 QCOMPARE(rm.getValueForPosition(1), 4.0);
Chris@879 91 QCOMPARE(rm.getValueForPosition(3), 3.0);
Chris@879 92 QCOMPARE(rm.getValueForPosition(2), 3.5);
Chris@879 93 QCOMPARE(rm.getValueForPosition(0), rm.getValueForPosition(1));
Chris@879 94 QCOMPARE(rm.getValueForPosition(9), rm.getValueForPosition(8));
Chris@885 95 QCOMPARE(rm.getValueForPositionUnclamped(3), 3.0);
Chris@885 96 QCOMPARE(rm.getValueForPositionUnclamped(9), 0.0);
Chris@885 97 QCOMPARE(rm.getValueForPositionUnclamped(33), -12.0);
Chris@885 98 QCOMPARE(rm.getValueForPositionUnclamped(-3), 6.0);
Chris@879 99 }
Chris@879 100
Chris@879 101 void logUpForward()
Chris@879 102 {
Chris@879 103 LogRangeMapper rm(3, 7, 10, 100000, "x", false);
Chris@879 104 QCOMPARE(rm.getUnit(), QString("x"));
Chris@879 105 QCOMPARE(rm.getPositionForValue(10.0), 3);
Chris@879 106 QCOMPARE(rm.getPositionForValue(100000.0), 7);
Chris@879 107 QCOMPARE(rm.getPositionForValue(1.0), 3);
Chris@879 108 QCOMPARE(rm.getPositionForValue(1000000.0), 7);
Chris@879 109 QCOMPARE(rm.getPositionForValue(1000.0), 5);
Chris@879 110 QCOMPARE(rm.getPositionForValue(900.0), 5);
Chris@879 111 QCOMPARE(rm.getPositionForValue(20000), 6);
Chris@885 112 QCOMPARE(rm.getPositionForValueUnclamped(1.0), 2);
Chris@885 113 QCOMPARE(rm.getPositionForValueUnclamped(1000000.0), 8);
Chris@885 114 QCOMPARE(rm.getPositionForValueUnclamped(1000.0), 5);
Chris@879 115 }
Chris@879 116
Chris@879 117 void logDownForward()
Chris@879 118 {
Chris@879 119 LogRangeMapper rm(3, 7, 10, 100000, "x", true);
Chris@879 120 QCOMPARE(rm.getUnit(), QString("x"));
Chris@879 121 QCOMPARE(rm.getPositionForValue(10.0), 7);
Chris@879 122 QCOMPARE(rm.getPositionForValue(100000.0), 3);
Chris@879 123 QCOMPARE(rm.getPositionForValue(1.0), 7);
Chris@879 124 QCOMPARE(rm.getPositionForValue(1000000.0), 3);
Chris@879 125 QCOMPARE(rm.getPositionForValue(1000.0), 5);
Chris@879 126 QCOMPARE(rm.getPositionForValue(900.0), 5);
Chris@879 127 QCOMPARE(rm.getPositionForValue(20000), 4);
Chris@885 128 QCOMPARE(rm.getPositionForValueUnclamped(1.0), 8);
Chris@885 129 QCOMPARE(rm.getPositionForValueUnclamped(1000000.0), 2);
Chris@885 130 QCOMPARE(rm.getPositionForValueUnclamped(1000.0), 5);
Chris@879 131 }
Chris@879 132
Chris@879 133 void logUpBackward()
Chris@879 134 {
Chris@879 135 LogRangeMapper rm(3, 7, 10, 100000, "x", false);
Chris@879 136 QCOMPARE(rm.getUnit(), QString("x"));
Chris@879 137 QCOMPARE(rm.getValueForPosition(3), 10.0);
Chris@879 138 QCOMPARE(rm.getValueForPosition(7), 100000.0);
Chris@879 139 QCOMPARE(rm.getValueForPosition(5), 1000.0);
Chris@879 140 QCOMPARE(rm.getValueForPosition(6), 10000.0);
Chris@879 141 QCOMPARE(rm.getValueForPosition(0), rm.getValueForPosition(3));
Chris@879 142 QCOMPARE(rm.getValueForPosition(9), rm.getValueForPosition(7));
Chris@885 143 QCOMPARE(rm.getValueForPositionUnclamped(2), 1.0);
Chris@885 144 QCOMPARE(rm.getValueForPositionUnclamped(8), 1000000.0);
Chris@885 145 QCOMPARE(rm.getValueForPositionUnclamped(5), 1000.0);
Chris@879 146 }
Chris@879 147
Chris@879 148 void logDownBackward()
Chris@879 149 {
Chris@879 150 LogRangeMapper rm(3, 7, 10, 100000, "x", true);
Chris@879 151 QCOMPARE(rm.getUnit(), QString("x"));
Chris@879 152 QCOMPARE(rm.getValueForPosition(7), 10.0);
Chris@879 153 QCOMPARE(rm.getValueForPosition(3), 100000.0);
Chris@879 154 QCOMPARE(rm.getValueForPosition(5), 1000.0);
Chris@879 155 QCOMPARE(rm.getValueForPosition(4), 10000.0);
Chris@879 156 QCOMPARE(rm.getValueForPosition(0), rm.getValueForPosition(3));
Chris@879 157 QCOMPARE(rm.getValueForPosition(9), rm.getValueForPosition(7));
Chris@885 158 QCOMPARE(rm.getValueForPositionUnclamped(8), 1.0);
Chris@885 159 QCOMPARE(rm.getValueForPositionUnclamped(2), 1000000.0);
Chris@885 160 QCOMPARE(rm.getValueForPositionUnclamped(5), 1000.0);
Chris@879 161 }
Chris@880 162
Chris@880 163 void interpolatingForward()
Chris@880 164 {
Chris@880 165 InterpolatingRangeMapper::CoordMap mappings;
Chris@880 166 mappings[1] = 10;
Chris@880 167 mappings[3] = 30;
Chris@880 168 mappings[5] = 70;
Chris@880 169 InterpolatingRangeMapper rm(mappings, "x");
Chris@880 170 QCOMPARE(rm.getUnit(), QString("x"));
Chris@880 171 QCOMPARE(rm.getPositionForValue(1.0), 10);
Chris@880 172 QCOMPARE(rm.getPositionForValue(0.0), 10);
Chris@880 173 QCOMPARE(rm.getPositionForValue(5.0), 70);
Chris@880 174 QCOMPARE(rm.getPositionForValue(6.0), 70);
Chris@880 175 QCOMPARE(rm.getPositionForValue(3.0), 30);
Chris@880 176 QCOMPARE(rm.getPositionForValue(2.5), 25);
Chris@880 177 QCOMPARE(rm.getPositionForValue(4.5), 60);
Chris@885 178 QCOMPARE(rm.getPositionForValueUnclamped(0.0), 0);
Chris@885 179 QCOMPARE(rm.getPositionForValueUnclamped(2.5), 25);
Chris@885 180 QCOMPARE(rm.getPositionForValueUnclamped(6.0), 90);
Chris@880 181 }
Chris@880 182
Chris@880 183 void interpolatingBackward()
Chris@880 184 {
Chris@880 185 InterpolatingRangeMapper::CoordMap mappings;
Chris@880 186 mappings[1] = 10;
Chris@880 187 mappings[3] = 30;
Chris@880 188 mappings[5] = 70;
Chris@880 189 InterpolatingRangeMapper rm(mappings, "x");
Chris@880 190 QCOMPARE(rm.getUnit(), QString("x"));
Chris@880 191 QCOMPARE(rm.getValueForPosition(10), 1.0);
Chris@880 192 QCOMPARE(rm.getValueForPosition(9), 1.0);
Chris@880 193 QCOMPARE(rm.getValueForPosition(70), 5.0);
Chris@880 194 QCOMPARE(rm.getValueForPosition(80), 5.0);
Chris@880 195 QCOMPARE(rm.getValueForPosition(30), 3.0);
Chris@880 196 QCOMPARE(rm.getValueForPosition(25), 2.5);
Chris@880 197 QCOMPARE(rm.getValueForPosition(60), 4.5);
Chris@880 198 }
Chris@880 199
Chris@880 200 void autoLinearForward()
Chris@880 201 {
Chris@880 202 AutoRangeMapper::CoordMap mappings;
Chris@880 203 mappings[0.5] = 1;
Chris@880 204 mappings[4.0] = 8;
Chris@880 205 AutoRangeMapper rm1(mappings, "x");
Chris@880 206 QCOMPARE(rm1.getUnit(), QString("x"));
Chris@880 207 QCOMPARE(rm1.getType(), AutoRangeMapper::StraightLine);
Chris@885 208 QCOMPARE(rm1.getPositionForValue(0.1), 1);
Chris@880 209 QCOMPARE(rm1.getPositionForValue(0.5), 1);
Chris@880 210 QCOMPARE(rm1.getPositionForValue(4.0), 8);
Chris@885 211 QCOMPARE(rm1.getPositionForValue(4.5), 8);
Chris@880 212 QCOMPARE(rm1.getPositionForValue(3.0), 6);
Chris@880 213 QCOMPARE(rm1.getPositionForValue(3.1), 6);
Chris@885 214 QCOMPARE(rm1.getPositionForValueUnclamped(0.1), 0);
Chris@885 215 QCOMPARE(rm1.getPositionForValueUnclamped(3.1), 6);
Chris@885 216 QCOMPARE(rm1.getPositionForValueUnclamped(4.5), 9);
Chris@880 217 mappings[3.0] = 6;
Chris@880 218 mappings[3.5] = 7;
Chris@880 219 AutoRangeMapper rm2(mappings, "x");
Chris@880 220 QCOMPARE(rm2.getUnit(), QString("x"));
Chris@880 221 QCOMPARE(rm2.getType(), AutoRangeMapper::StraightLine);
Chris@880 222 QCOMPARE(rm2.getPositionForValue(0.5), 1);
Chris@880 223 QCOMPARE(rm2.getPositionForValue(4.0), 8);
Chris@880 224 QCOMPARE(rm2.getPositionForValue(3.0), 6);
Chris@880 225 QCOMPARE(rm2.getPositionForValue(3.1), 6);
Chris@880 226 }
Chris@880 227
Chris@880 228 void autoLogForward()
Chris@880 229 {
Chris@880 230 AutoRangeMapper::CoordMap mappings;
Chris@880 231 mappings[10] = 3;
Chris@880 232 mappings[1000] = 5;
Chris@880 233 mappings[100000] = 7;
Chris@880 234 AutoRangeMapper rm1(mappings, "x");
Chris@880 235 QCOMPARE(rm1.getUnit(), QString("x"));
Chris@880 236 QCOMPARE(rm1.getType(), AutoRangeMapper::Logarithmic);
Chris@880 237 QCOMPARE(rm1.getPositionForValue(10.0), 3);
Chris@880 238 QCOMPARE(rm1.getPositionForValue(100000.0), 7);
Chris@880 239 QCOMPARE(rm1.getPositionForValue(1.0), 3);
Chris@880 240 QCOMPARE(rm1.getPositionForValue(1000000.0), 7);
Chris@880 241 QCOMPARE(rm1.getPositionForValue(1000.0), 5);
Chris@880 242 QCOMPARE(rm1.getPositionForValue(900.0), 5);
Chris@880 243 QCOMPARE(rm1.getPositionForValue(20000), 6);
Chris@885 244 QCOMPARE(rm1.getPositionForValueUnclamped(1.0), 2);
Chris@885 245 QCOMPARE(rm1.getPositionForValueUnclamped(900.0), 5);
Chris@885 246 QCOMPARE(rm1.getPositionForValueUnclamped(1000000.0), 8);
Chris@880 247 mappings[100] = 4;
Chris@880 248 AutoRangeMapper rm2(mappings, "x");
Chris@880 249 QCOMPARE(rm2.getUnit(), QString("x"));
Chris@880 250 QCOMPARE(rm2.getType(), AutoRangeMapper::Logarithmic);
Chris@880 251 QCOMPARE(rm2.getPositionForValue(10.0), 3);
Chris@880 252 QCOMPARE(rm2.getPositionForValue(100000.0), 7);
Chris@880 253 QCOMPARE(rm2.getPositionForValue(1.0), 3);
Chris@880 254 QCOMPARE(rm2.getPositionForValue(1000000.0), 7);
Chris@880 255 QCOMPARE(rm2.getPositionForValue(1000.0), 5);
Chris@880 256 QCOMPARE(rm2.getPositionForValue(900.0), 5);
Chris@880 257 QCOMPARE(rm2.getPositionForValue(20000), 6);
Chris@880 258 }
Chris@880 259
Chris@880 260 void autoInterpolatingForward()
Chris@880 261 {
Chris@880 262 AutoRangeMapper::CoordMap mappings;
Chris@880 263 mappings[1] = 10;
Chris@880 264 mappings[3] = 30;
Chris@880 265 mappings[5] = 70;
Chris@880 266 AutoRangeMapper rm(mappings, "x");
Chris@880 267 QCOMPARE(rm.getUnit(), QString("x"));
Chris@880 268 QCOMPARE(rm.getType(), AutoRangeMapper::Interpolating);
Chris@880 269 QCOMPARE(rm.getPositionForValue(1.0), 10);
Chris@880 270 QCOMPARE(rm.getPositionForValue(0.0), 10);
Chris@880 271 QCOMPARE(rm.getPositionForValue(5.0), 70);
Chris@880 272 QCOMPARE(rm.getPositionForValue(6.0), 70);
Chris@880 273 QCOMPARE(rm.getPositionForValue(3.0), 30);
Chris@880 274 QCOMPARE(rm.getPositionForValue(2.5), 25);
Chris@880 275 QCOMPARE(rm.getPositionForValue(4.5), 60);
Chris@885 276 QCOMPARE(rm.getPositionForValueUnclamped(0.0), 0);
Chris@885 277 QCOMPARE(rm.getPositionForValueUnclamped(5.0), 70);
Chris@885 278 QCOMPARE(rm.getPositionForValueUnclamped(6.0), 90);
Chris@880 279 }
Chris@879 280 };
Chris@879 281
Chris@879 282 #endif
Chris@879 283
Chris@879 284