annotate base/test/TestRangeMapper.h @ 1480:5ac102155409 plugin-path-config

Environment variable lookup fixes, & use this implementation throughout
author Chris Cannam
date Fri, 08 Jun 2018 11:27:40 +0100
parents 48e9f538e6e9
children
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@1429 35 LinearRangeMapper rm(1, 8, 0.5, 4.0, "x", false);
Chris@1429 36 QCOMPARE(rm.getUnit(), QString("x"));
Chris@1429 37 QCOMPARE(rm.getPositionForValue(0.5), 1);
Chris@1429 38 QCOMPARE(rm.getPositionForValue(4.0), 8);
Chris@1429 39 QCOMPARE(rm.getPositionForValue(3.0), 6);
Chris@1429 40 QCOMPARE(rm.getPositionForValue(3.1), 6);
Chris@1429 41 QCOMPARE(rm.getPositionForValue(3.4), 7);
Chris@1429 42 QCOMPARE(rm.getPositionForValue(0.2), 1);
Chris@1429 43 QCOMPARE(rm.getPositionForValue(-12), 1);
Chris@1429 44 QCOMPARE(rm.getPositionForValue(6.1), 8);
Chris@1429 45 QCOMPARE(rm.getPositionForValueUnclamped(3.0), 6);
Chris@1429 46 QCOMPARE(rm.getPositionForValueUnclamped(0.2), 0);
Chris@1429 47 QCOMPARE(rm.getPositionForValueUnclamped(-12), -24);
Chris@1429 48 QCOMPARE(rm.getPositionForValueUnclamped(6.1), 12);
Chris@879 49 }
Chris@879 50
Chris@879 51 void linearDownForward()
Chris@879 52 {
Chris@1429 53 LinearRangeMapper rm(1, 8, 0.5, 4.0, "x", true);
Chris@1429 54 QCOMPARE(rm.getUnit(), QString("x"));
Chris@1429 55 QCOMPARE(rm.getPositionForValue(0.5), 8);
Chris@1429 56 QCOMPARE(rm.getPositionForValue(4.0), 1);
Chris@1429 57 QCOMPARE(rm.getPositionForValue(3.0), 3);
Chris@1429 58 QCOMPARE(rm.getPositionForValue(3.1), 3);
Chris@1429 59 QCOMPARE(rm.getPositionForValue(3.4), 2);
Chris@1429 60 QCOMPARE(rm.getPositionForValue(0.2), 8);
Chris@1429 61 QCOMPARE(rm.getPositionForValue(-12), 8);
Chris@1429 62 QCOMPARE(rm.getPositionForValue(6.1), 1);
Chris@1429 63 QCOMPARE(rm.getPositionForValueUnclamped(3.0), 3);
Chris@1429 64 QCOMPARE(rm.getPositionForValueUnclamped(0.2), 9);
Chris@1429 65 QCOMPARE(rm.getPositionForValueUnclamped(-12), 33);
Chris@1429 66 QCOMPARE(rm.getPositionForValueUnclamped(6.1), -3);
Chris@879 67 }
Chris@879 68
Chris@879 69 void linearUpBackward()
Chris@879 70 {
Chris@1429 71 LinearRangeMapper rm(1, 8, 0.5, 4.0, "x", false);
Chris@1429 72 QCOMPARE(rm.getUnit(), QString("x"));
Chris@1429 73 QCOMPARE(rm.getValueForPosition(1), 0.5);
Chris@1429 74 QCOMPARE(rm.getValueForPosition(8), 4.0);
Chris@1429 75 QCOMPARE(rm.getValueForPosition(6), 3.0);
Chris@1429 76 QCOMPARE(rm.getValueForPosition(7), 3.5);
Chris@1429 77 QCOMPARE(rm.getValueForPosition(0), rm.getValueForPosition(1));
Chris@1429 78 QCOMPARE(rm.getValueForPosition(9), rm.getValueForPosition(8));
Chris@1429 79 QCOMPARE(rm.getValueForPositionUnclamped(6), 3.0);
Chris@1429 80 QCOMPARE(rm.getValueForPositionUnclamped(0) + 1.0, 0.0 + 1.0);
Chris@1429 81 QCOMPARE(rm.getValueForPositionUnclamped(-24), -12.0);
Chris@1429 82 QCOMPARE(rm.getValueForPositionUnclamped(12), 6.0);
Chris@879 83 }
Chris@879 84
Chris@879 85 void linearDownBackward()
Chris@879 86 {
Chris@1429 87 LinearRangeMapper rm(1, 8, 0.5, 4.0, "x", true);
Chris@1429 88 QCOMPARE(rm.getUnit(), QString("x"));
Chris@1429 89 QCOMPARE(rm.getValueForPosition(8), 0.5);
Chris@1429 90 QCOMPARE(rm.getValueForPosition(1), 4.0);
Chris@1429 91 QCOMPARE(rm.getValueForPosition(3), 3.0);
Chris@1429 92 QCOMPARE(rm.getValueForPosition(2), 3.5);
Chris@1429 93 QCOMPARE(rm.getValueForPosition(0), rm.getValueForPosition(1));
Chris@1429 94 QCOMPARE(rm.getValueForPosition(9), rm.getValueForPosition(8));
Chris@1429 95 QCOMPARE(rm.getValueForPositionUnclamped(3), 3.0);
Chris@1429 96 QCOMPARE(rm.getValueForPositionUnclamped(9) + 1.0, 0.0 + 1.0);
Chris@1429 97 QCOMPARE(rm.getValueForPositionUnclamped(33), -12.0);
Chris@1429 98 QCOMPARE(rm.getValueForPositionUnclamped(-3), 6.0);
Chris@879 99 }
Chris@879 100
Chris@879 101 void logUpForward()
Chris@879 102 {
Chris@1429 103 LogRangeMapper rm(3, 7, 10, 100000, "x", false);
Chris@1429 104 QCOMPARE(rm.getUnit(), QString("x"));
Chris@1429 105 QCOMPARE(rm.getPositionForValue(10.0), 3);
Chris@1429 106 QCOMPARE(rm.getPositionForValue(100000.0), 7);
Chris@1429 107 QCOMPARE(rm.getPositionForValue(1.0), 3);
Chris@1429 108 QCOMPARE(rm.getPositionForValue(1000000.0), 7);
Chris@1429 109 QCOMPARE(rm.getPositionForValue(1000.0), 5);
Chris@1429 110 QCOMPARE(rm.getPositionForValue(900.0), 5);
Chris@1429 111 QCOMPARE(rm.getPositionForValue(20000), 6);
Chris@1429 112 QCOMPARE(rm.getPositionForValueUnclamped(1.0), 2);
Chris@1429 113 QCOMPARE(rm.getPositionForValueUnclamped(1000000.0), 8);
Chris@1429 114 QCOMPARE(rm.getPositionForValueUnclamped(1000.0), 5);
Chris@879 115 }
Chris@879 116
Chris@879 117 void logDownForward()
Chris@879 118 {
Chris@1429 119 LogRangeMapper rm(3, 7, 10, 100000, "x", true);
Chris@1429 120 QCOMPARE(rm.getUnit(), QString("x"));
Chris@1429 121 QCOMPARE(rm.getPositionForValue(10.0), 7);
Chris@1429 122 QCOMPARE(rm.getPositionForValue(100000.0), 3);
Chris@1429 123 QCOMPARE(rm.getPositionForValue(1.0), 7);
Chris@1429 124 QCOMPARE(rm.getPositionForValue(1000000.0), 3);
Chris@1429 125 QCOMPARE(rm.getPositionForValue(1000.0), 5);
Chris@1429 126 QCOMPARE(rm.getPositionForValue(900.0), 5);
Chris@1429 127 QCOMPARE(rm.getPositionForValue(20000), 4);
Chris@1429 128 QCOMPARE(rm.getPositionForValueUnclamped(1.0), 8);
Chris@1429 129 QCOMPARE(rm.getPositionForValueUnclamped(1000000.0), 2);
Chris@1429 130 QCOMPARE(rm.getPositionForValueUnclamped(1000.0), 5);
Chris@879 131 }
Chris@879 132
Chris@879 133 void logUpBackward()
Chris@879 134 {
Chris@1429 135 LogRangeMapper rm(3, 7, 10, 100000, "x", false);
Chris@1429 136 QCOMPARE(rm.getUnit(), QString("x"));
Chris@1429 137 QCOMPARE(rm.getValueForPosition(3), 10.0);
Chris@1429 138 QCOMPARE(rm.getValueForPosition(7), 100000.0);
Chris@1429 139 QCOMPARE(rm.getValueForPosition(5), 1000.0);
Chris@1429 140 QCOMPARE(rm.getValueForPosition(6), 10000.0);
Chris@1429 141 QCOMPARE(rm.getValueForPosition(0), rm.getValueForPosition(3));
Chris@1429 142 QCOMPARE(rm.getValueForPosition(9), rm.getValueForPosition(7));
Chris@1429 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@1429 150 LogRangeMapper rm(3, 7, 10, 100000, "x", true);
Chris@1429 151 QCOMPARE(rm.getUnit(), QString("x"));
Chris@1429 152 QCOMPARE(rm.getValueForPosition(7), 10.0);
Chris@1429 153 QCOMPARE(rm.getValueForPosition(3), 100000.0);
Chris@1429 154 QCOMPARE(rm.getValueForPosition(5), 1000.0);
Chris@1429 155 QCOMPARE(rm.getValueForPosition(4), 10000.0);
Chris@1429 156 QCOMPARE(rm.getValueForPosition(0), rm.getValueForPosition(3));
Chris@1429 157 QCOMPARE(rm.getValueForPosition(9), rm.getValueForPosition(7));
Chris@1429 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@1429 165 InterpolatingRangeMapper::CoordMap mappings;
Chris@1429 166 mappings[1] = 10;
Chris@1429 167 mappings[3] = 30;
Chris@1429 168 mappings[5] = 70;
Chris@1429 169 InterpolatingRangeMapper rm(mappings, "x");
Chris@1429 170 QCOMPARE(rm.getUnit(), QString("x"));
Chris@1429 171 QCOMPARE(rm.getPositionForValue(1.0), 10);
Chris@1429 172 QCOMPARE(rm.getPositionForValue(0.0), 10);
Chris@1429 173 QCOMPARE(rm.getPositionForValue(5.0), 70);
Chris@1429 174 QCOMPARE(rm.getPositionForValue(6.0), 70);
Chris@1429 175 QCOMPARE(rm.getPositionForValue(3.0), 30);
Chris@1429 176 QCOMPARE(rm.getPositionForValue(2.5), 25);
Chris@1429 177 QCOMPARE(rm.getPositionForValue(4.5), 60);
Chris@1429 178 QCOMPARE(rm.getPositionForValueUnclamped(0.0), 0);
Chris@1429 179 QCOMPARE(rm.getPositionForValueUnclamped(2.5), 25);
Chris@1429 180 QCOMPARE(rm.getPositionForValueUnclamped(6.0), 90);
Chris@880 181 }
Chris@880 182
Chris@880 183 void interpolatingBackward()
Chris@880 184 {
Chris@1429 185 InterpolatingRangeMapper::CoordMap mappings;
Chris@1429 186 mappings[1] = 10;
Chris@1429 187 mappings[3] = 30;
Chris@1429 188 mappings[5] = 70;
Chris@1429 189 InterpolatingRangeMapper rm(mappings, "x");
Chris@1429 190 QCOMPARE(rm.getUnit(), QString("x"));
Chris@1429 191 QCOMPARE(rm.getValueForPosition(10), 1.0);
Chris@1429 192 QCOMPARE(rm.getValueForPosition(9), 1.0);
Chris@1429 193 QCOMPARE(rm.getValueForPosition(70), 5.0);
Chris@1429 194 QCOMPARE(rm.getValueForPosition(80), 5.0);
Chris@1429 195 QCOMPARE(rm.getValueForPosition(30), 3.0);
Chris@1429 196 QCOMPARE(rm.getValueForPosition(25), 2.5);
Chris@1429 197 QCOMPARE(rm.getValueForPosition(60), 4.5);
Chris@880 198 }
Chris@880 199
Chris@880 200 void autoLinearForward()
Chris@880 201 {
Chris@1429 202 AutoRangeMapper::CoordMap mappings;
Chris@1429 203 mappings[0.5] = 1;
Chris@1429 204 mappings[4.0] = 8;
Chris@1429 205 AutoRangeMapper rm1(mappings, "x");
Chris@1429 206 QCOMPARE(rm1.getUnit(), QString("x"));
Chris@1429 207 QCOMPARE(rm1.getType(), AutoRangeMapper::StraightLine);
Chris@1429 208 QCOMPARE(rm1.getPositionForValue(0.1), 1);
Chris@1429 209 QCOMPARE(rm1.getPositionForValue(0.5), 1);
Chris@1429 210 QCOMPARE(rm1.getPositionForValue(4.0), 8);
Chris@1429 211 QCOMPARE(rm1.getPositionForValue(4.5), 8);
Chris@1429 212 QCOMPARE(rm1.getPositionForValue(3.0), 6);
Chris@1429 213 QCOMPARE(rm1.getPositionForValue(3.1), 6);
Chris@1429 214 QCOMPARE(rm1.getPositionForValueUnclamped(0.1), 0);
Chris@1429 215 QCOMPARE(rm1.getPositionForValueUnclamped(3.1), 6);
Chris@1429 216 QCOMPARE(rm1.getPositionForValueUnclamped(4.5), 9);
Chris@1429 217 mappings[3.0] = 6;
Chris@1429 218 mappings[3.5] = 7;
Chris@1429 219 AutoRangeMapper rm2(mappings, "x");
Chris@1429 220 QCOMPARE(rm2.getUnit(), QString("x"));
Chris@1429 221 QCOMPARE(rm2.getType(), AutoRangeMapper::StraightLine);
Chris@1429 222 QCOMPARE(rm2.getPositionForValue(0.5), 1);
Chris@1429 223 QCOMPARE(rm2.getPositionForValue(4.0), 8);
Chris@1429 224 QCOMPARE(rm2.getPositionForValue(3.0), 6);
Chris@1429 225 QCOMPARE(rm2.getPositionForValue(3.1), 6);
Chris@880 226 }
Chris@880 227
Chris@880 228 void autoLogForward()
Chris@880 229 {
Chris@1429 230 AutoRangeMapper::CoordMap mappings;
Chris@1429 231 mappings[10] = 3;
Chris@1429 232 mappings[1000] = 5;
Chris@1429 233 mappings[100000] = 7;
Chris@1429 234 AutoRangeMapper rm1(mappings, "x");
Chris@1429 235 QCOMPARE(rm1.getUnit(), QString("x"));
Chris@1429 236 QCOMPARE(rm1.getType(), AutoRangeMapper::Logarithmic);
Chris@1429 237 QCOMPARE(rm1.getPositionForValue(10.0), 3);
Chris@1429 238 QCOMPARE(rm1.getPositionForValue(100000.0), 7);
Chris@1429 239 QCOMPARE(rm1.getPositionForValue(1.0), 3);
Chris@1429 240 QCOMPARE(rm1.getPositionForValue(1000000.0), 7);
Chris@1429 241 QCOMPARE(rm1.getPositionForValue(1000.0), 5);
Chris@1429 242 QCOMPARE(rm1.getPositionForValue(900.0), 5);
Chris@1429 243 QCOMPARE(rm1.getPositionForValue(20000), 6);
Chris@1429 244 QCOMPARE(rm1.getPositionForValueUnclamped(1.0), 2);
Chris@1429 245 QCOMPARE(rm1.getPositionForValueUnclamped(900.0), 5);
Chris@1429 246 QCOMPARE(rm1.getPositionForValueUnclamped(1000000.0), 8);
Chris@1429 247 mappings[100] = 4;
Chris@1429 248 AutoRangeMapper rm2(mappings, "x");
Chris@1429 249 QCOMPARE(rm2.getUnit(), QString("x"));
Chris@1429 250 QCOMPARE(rm2.getType(), AutoRangeMapper::Logarithmic);
Chris@1429 251 QCOMPARE(rm2.getPositionForValue(10.0), 3);
Chris@1429 252 QCOMPARE(rm2.getPositionForValue(100000.0), 7);
Chris@1429 253 QCOMPARE(rm2.getPositionForValue(1.0), 3);
Chris@1429 254 QCOMPARE(rm2.getPositionForValue(1000000.0), 7);
Chris@1429 255 QCOMPARE(rm2.getPositionForValue(1000.0), 5);
Chris@1429 256 QCOMPARE(rm2.getPositionForValue(900.0), 5);
Chris@1429 257 QCOMPARE(rm2.getPositionForValue(20000), 6);
Chris@880 258 }
Chris@880 259
Chris@880 260 void autoInterpolatingForward()
Chris@880 261 {
Chris@1429 262 AutoRangeMapper::CoordMap mappings;
Chris@1429 263 mappings[1] = 10;
Chris@1429 264 mappings[3] = 30;
Chris@1429 265 mappings[5] = 70;
Chris@1429 266 AutoRangeMapper rm(mappings, "x");
Chris@1429 267 QCOMPARE(rm.getUnit(), QString("x"));
Chris@1429 268 QCOMPARE(rm.getType(), AutoRangeMapper::Interpolating);
Chris@1429 269 QCOMPARE(rm.getPositionForValue(1.0), 10);
Chris@1429 270 QCOMPARE(rm.getPositionForValue(0.0), 10);
Chris@1429 271 QCOMPARE(rm.getPositionForValue(5.0), 70);
Chris@1429 272 QCOMPARE(rm.getPositionForValue(6.0), 70);
Chris@1429 273 QCOMPARE(rm.getPositionForValue(3.0), 30);
Chris@1429 274 QCOMPARE(rm.getPositionForValue(2.5), 25);
Chris@1429 275 QCOMPARE(rm.getPositionForValue(4.5), 60);
Chris@1429 276 QCOMPARE(rm.getPositionForValueUnclamped(0.0), 0);
Chris@1429 277 QCOMPARE(rm.getPositionForValueUnclamped(5.0), 70);
Chris@1429 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