Chris@1407: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ Chris@1407: Chris@1407: /* Chris@1407: Sonic Visualiser Chris@1407: An audio file viewer and annotation editor. Chris@1407: Centre for Digital Music, Queen Mary, University of London. Chris@1407: Chris@1407: This program is free software; you can redistribute it and/or Chris@1407: modify it under the terms of the GNU General Public License as Chris@1407: published by the Free Software Foundation; either version 2 of the Chris@1407: License, or (at your option) any later version. See the file Chris@1407: COPYING included with this distribution for more information. Chris@1407: */ Chris@1407: Chris@1407: #ifndef TEST_SCALE_TICK_INTERVALS_H Chris@1407: #define TEST_SCALE_TICK_INTERVALS_H Chris@1407: Chris@1407: #include "../ScaleTickIntervals.h" Chris@1407: Chris@1407: #include Chris@1407: #include Chris@1407: #include Chris@1407: Chris@1407: #include Chris@1407: Chris@1407: using namespace std; Chris@1407: Chris@1407: class TestScaleTickIntervals : public QObject Chris@1407: { Chris@1407: Q_OBJECT Chris@1407: Chris@1407: void printDiff(vector ticks, Chris@1407: vector expected) { Chris@1407: Chris@1407: cerr << "Have " << ticks.size() << " ticks, expected " Chris@1407: << expected.size() << endl; Chris@1407: for (int i = 0; i < int(expected.size()); ++i) { Chris@1407: if (i < int(ticks.size())) { Chris@1407: cerr << i << ": have " << ticks[i].value << " \"" Chris@1407: << ticks[i].label << "\", expected " Chris@1407: << expected[i].value << " \"" << expected[i].label Chris@1407: << "\"" << endl; Chris@1407: } Chris@1407: } Chris@1407: } Chris@1407: Chris@1407: void compareTicks(vector ticks, Chris@1407: vector expected) Chris@1407: { Chris@1408: double eps = 1e-7; Chris@1407: for (int i = 0; i < int(expected.size()); ++i) { Chris@1407: if (i < int(ticks.size())) { Chris@1407: if (ticks[i].label != expected[i].label || Chris@1408: fabs(ticks[i].value - expected[i].value) > eps) { Chris@1407: printDiff(ticks, expected); Chris@1407: } Chris@1407: QCOMPARE(ticks[i].label, expected[i].label); Chris@1407: QCOMPARE(ticks[i].value, expected[i].value); Chris@1407: } Chris@1407: } Chris@1408: if (ticks.size() != expected.size()) { Chris@1408: printDiff(ticks, expected); Chris@1408: } Chris@1407: QCOMPARE(ticks.size(), expected.size()); Chris@1407: } Chris@1407: Chris@1407: private slots: Chris@1407: void linear_0_1_10() Chris@1407: { Chris@1407: auto ticks = ScaleTickIntervals::linear({ 0, 1, 10 }); Chris@1407: vector expected { Chris@1407: { 0.0, "0.00" }, Chris@1407: { 0.1, "0.10" }, Chris@1407: { 0.2, "0.20" }, Chris@1407: { 0.3, "0.30" }, Chris@1407: { 0.4, "0.40" }, Chris@1407: { 0.5, "0.50" }, Chris@1407: { 0.6, "0.60" }, Chris@1407: { 0.7, "0.70" }, Chris@1407: { 0.8, "0.80" }, Chris@1407: { 0.9, "0.90" }, Chris@1407: { 1.0, "1.00" } Chris@1407: }; Chris@1407: compareTicks(ticks.ticks, expected); Chris@1407: } Chris@1407: Chris@1407: void linear_0_0p1_5() Chris@1407: { Chris@1407: auto ticks = ScaleTickIntervals::linear({ 0, 0.1, 5 }); Chris@1407: vector expected { Chris@1407: { 0.0, "0.00" }, Chris@1407: { 0.02, "0.02" }, Chris@1407: { 0.04, "0.04" }, Chris@1407: { 0.06, "0.06" }, Chris@1407: { 0.08, "0.08" }, Chris@1407: { 0.1, "0.10" } Chris@1407: }; Chris@1407: compareTicks(ticks.ticks, expected); Chris@1407: } Chris@1407: Chris@1407: void linear_0_0p01_5() Chris@1407: { Chris@1407: auto ticks = ScaleTickIntervals::linear({ 0, 0.01, 5 }); Chris@1407: vector expected { Chris@1407: { 0.00, "0.000" }, Chris@1407: { 0.002, "0.002" }, Chris@1407: { 0.004, "0.004" }, Chris@1407: { 0.006, "0.006" }, Chris@1407: { 0.008, "0.008" }, Chris@1407: { 0.01, "0.010" } Chris@1407: }; Chris@1407: compareTicks(ticks.ticks, expected); Chris@1407: } Chris@1407: Chris@1407: void linear_0_0p001_5() Chris@1407: { Chris@1407: auto ticks = ScaleTickIntervals::linear({ 0, 0.001, 5 }); Chris@1407: vector expected { Chris@1407: { 0.000, "0.0e+00" }, Chris@1407: { 0.0002, "2.0e-04" }, Chris@1407: { 0.0004, "4.0e-04" }, Chris@1407: { 0.0006, "6.0e-04" }, Chris@1407: { 0.0008, "8.0e-04" }, Chris@1407: { 0.001, "1.0e-03" } Chris@1407: }; Chris@1407: compareTicks(ticks.ticks, expected); Chris@1407: } Chris@1407: Chris@1407: void linear_1_1p001_5() Chris@1407: { Chris@1407: auto ticks = ScaleTickIntervals::linear({ 1, 1.001, 5 }); Chris@1407: vector expected { Chris@1407: { 1.000, "1.0000" }, Chris@1407: { 1.0002, "1.0002" }, Chris@1407: { 1.0004, "1.0004" }, Chris@1407: { 1.0006, "1.0006" }, Chris@1407: { 1.0008, "1.0008" }, Chris@1407: { 1.001, "1.0010" } Chris@1407: }; Chris@1407: compareTicks(ticks.ticks, expected); Chris@1407: } Chris@1407: }; Chris@1407: Chris@1407: #endif Chris@1407: Chris@1407: