| 1 | // Copyright (C) 2015 Klaralvdalens Datakonsult AB (KDAB). |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
| 3 | |
| 4 | #include <QtTest/QTest> |
| 5 | #include <private/functionrangefinder_p.h> |
| 6 | |
| 7 | using namespace Qt3DAnimation::Animation; |
| 8 | |
| 9 | class tst_FunctionRangeFinder : public QObject |
| 10 | { |
| 11 | Q_OBJECT |
| 12 | |
| 13 | private Q_SLOTS: |
| 14 | void checkDefaultConstruction() |
| 15 | { |
| 16 | // GIVEN |
| 17 | QList<float> data; |
| 18 | |
| 19 | // WHEN |
| 20 | FunctionRangeFinder finder(&data); |
| 21 | |
| 22 | // THEN |
| 23 | QCOMPARE(finder.rangeSize(), 2); |
| 24 | QCOMPARE(finder.isAscending(), true); |
| 25 | QCOMPARE(finder.correlationThreshold(), 1); |
| 26 | } |
| 27 | |
| 28 | void checkConstructionWithData_data() |
| 29 | { |
| 30 | QTest::addColumn<QList<float>>("x"); |
| 31 | QTest::addColumn<int>("correlationThreshold"); |
| 32 | QTest::addColumn<bool>("ascending"); |
| 33 | |
| 34 | QVector<float> data = { 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f, 9.0f, 10.0f }; |
| 35 | int correlationThreshold = 1; |
| 36 | bool ascending = true; |
| 37 | QTest::newRow("10 entries, ascending") << data << correlationThreshold << ascending; |
| 38 | data.clear(); |
| 39 | |
| 40 | data.resize(10000); |
| 41 | for (int i = 0; i < 10000; ++i) |
| 42 | data[i] = float(i); |
| 43 | ascending = true; |
| 44 | correlationThreshold = std::pow(float(10000), 0.25f); |
| 45 | QTest::newRow("10k entries, ascending") << data << correlationThreshold << ascending; |
| 46 | data.clear(); |
| 47 | |
| 48 | data.resize(10000); |
| 49 | for (int i = 0; i < 10000; ++i) |
| 50 | data[10000 - i - 1] = float(i); |
| 51 | ascending = false; |
| 52 | correlationThreshold = std::pow(float(10000), 0.25f); |
| 53 | QTest::newRow("10k entries, descending") << data << correlationThreshold << ascending; |
| 54 | data.clear(); |
| 55 | } |
| 56 | |
| 57 | void checkConstructionWithData() |
| 58 | { |
| 59 | // GIVEN |
| 60 | QFETCH(QList<float>, x); |
| 61 | QFETCH(int, correlationThreshold); |
| 62 | QFETCH(bool, ascending); |
| 63 | |
| 64 | // WHEN |
| 65 | FunctionRangeFinder finder(&x); |
| 66 | |
| 67 | // THEN |
| 68 | QCOMPARE(finder.rangeSize(), 2); |
| 69 | QCOMPARE(finder.isAscending(), ascending); |
| 70 | QCOMPARE(finder.correlationThreshold(), correlationThreshold); |
| 71 | } |
| 72 | |
| 73 | void checkFindLowerBound_data() |
| 74 | { |
| 75 | QTest::addColumn<QList<float>>("x"); |
| 76 | QTest::addColumn<QList<float>>("needles"); |
| 77 | QTest::addColumn<QList<int>>("lowerBounds"); |
| 78 | |
| 79 | QVector<float> data = { 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f, 9.0f, 10.0f }; |
| 80 | QVector<float> needles = { 2.5f }; |
| 81 | QVector<int> lowerBounds = { 1 }; |
| 82 | QTest::newRow("10 entries, ascending") << data << needles << lowerBounds; |
| 83 | data.clear(); |
| 84 | needles.clear(); |
| 85 | lowerBounds.clear(); |
| 86 | |
| 87 | data.resize(10000); |
| 88 | for (int i = 0; i < 10000; ++i) |
| 89 | data[i] = float(i); |
| 90 | |
| 91 | needles.push_back(23.75f); |
| 92 | lowerBounds.push_back(23); |
| 93 | |
| 94 | needles.push_back(500.03f); |
| 95 | lowerBounds.push_back(500); |
| 96 | |
| 97 | needles.push_back(500.2f); |
| 98 | lowerBounds.push_back(500); |
| 99 | |
| 100 | needles.push_back(501.4f); // Will trigger hunt codepath as previous test is correlated |
| 101 | lowerBounds.push_back(501); |
| 102 | |
| 103 | needles.push_back(3405.123f); |
| 104 | lowerBounds.push_back(3405); |
| 105 | |
| 106 | QTest::newRow("10k entries, ascending") << data << needles << lowerBounds; |
| 107 | data.clear(); |
| 108 | needles.clear(); |
| 109 | lowerBounds.clear(); |
| 110 | |
| 111 | data.resize(10); |
| 112 | for (int i = 0; i < 10; ++i) |
| 113 | data[10 - i - 1] = float(i); |
| 114 | |
| 115 | needles.push_back(8.1f); |
| 116 | lowerBounds.push_back(0); |
| 117 | |
| 118 | needles.push_back(7.2f); |
| 119 | lowerBounds.push_back(1); |
| 120 | |
| 121 | needles.push_back(0.5f); |
| 122 | lowerBounds.push_back(8); |
| 123 | |
| 124 | QTest::newRow("10 entries, descending") << data << needles << lowerBounds; |
| 125 | data.clear(); |
| 126 | } |
| 127 | |
| 128 | void checkFindLowerBound() |
| 129 | { |
| 130 | // GIVEN |
| 131 | QFETCH(QList<float>, x); |
| 132 | QFETCH(QList<float>, needles); |
| 133 | QFETCH(QList<int>, lowerBounds); |
| 134 | FunctionRangeFinder finder(&x); |
| 135 | |
| 136 | for (int i = 0; i < needles.size(); ++i) { |
| 137 | // WHEN |
| 138 | int result = finder.findLowerBound(needles[i]); |
| 139 | |
| 140 | // THEN |
| 141 | QCOMPARE(result, lowerBounds[i]); |
| 142 | } |
| 143 | } |
| 144 | }; |
| 145 | |
| 146 | QTEST_APPLESS_MAIN(tst_FunctionRangeFinder) |
| 147 | |
| 148 | #include "tst_functionrangefinder.moc" |