| 1 | /**************************************************************************** | 
| 2 | ** | 
| 3 | ** Copyright (C) 2015 Klaralvdalens Datakonsult AB (KDAB). | 
| 4 | ** Contact: https://www.qt.io/licensing/ | 
| 5 | ** | 
| 6 | ** This file is part of the Qt3D module of the Qt Toolkit. | 
| 7 | ** | 
| 8 | ** $QT_BEGIN_LICENSE:GPL-EXCEPT$ | 
| 9 | ** Commercial License Usage | 
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in | 
| 11 | ** accordance with the commercial license agreement provided with the | 
| 12 | ** Software or, alternatively, in accordance with the terms contained in | 
| 13 | ** a written agreement between you and The Qt Company. For licensing terms | 
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further | 
| 15 | ** information use the contact form at https://www.qt.io/contact-us. | 
| 16 | ** | 
| 17 | ** GNU General Public License Usage | 
| 18 | ** Alternatively, this file may be used under the terms of the GNU | 
| 19 | ** General Public License version 3 as published by the Free Software | 
| 20 | ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT | 
| 21 | ** included in the packaging of this file. Please review the following | 
| 22 | ** information to ensure the GNU General Public License requirements will | 
| 23 | ** be met: https://www.gnu.org/licenses/gpl-3.0.html. | 
| 24 | ** | 
| 25 | ** $QT_END_LICENSE$ | 
| 26 | ** | 
| 27 | ****************************************************************************/ | 
| 28 |  | 
| 29 | #include <QtTest/QTest> | 
| 30 | #include <private/functionrangefinder_p.h> | 
| 31 |  | 
| 32 | using namespace Qt3DAnimation::Animation; | 
| 33 |  | 
| 34 | class tst_FunctionRangeFinder : public QObject | 
| 35 | { | 
| 36 |     Q_OBJECT | 
| 37 |  | 
| 38 | private Q_SLOTS: | 
| 39 |     void checkDefaultConstruction() | 
| 40 |     { | 
| 41 |         // GIVEN | 
| 42 |         QVector<float> data; | 
| 43 |  | 
| 44 |         // WHEN | 
| 45 |         FunctionRangeFinder finder(data); | 
| 46 |  | 
| 47 |         // THEN | 
| 48 |         QCOMPARE(finder.rangeSize(), 2); | 
| 49 |         QCOMPARE(finder.isAscending(), true); | 
| 50 |         QCOMPARE(finder.correlationThreshold(), 1); | 
| 51 |     } | 
| 52 |  | 
| 53 |     void checkConstructionWithData_data() | 
| 54 |     { | 
| 55 |         QTest::addColumn<QVector<float>>(name: "x" ); | 
| 56 |         QTest::addColumn<int>(name: "correlationThreshold" ); | 
| 57 |         QTest::addColumn<bool>(name: "ascending" ); | 
| 58 |  | 
| 59 |         QVector<float> data = { 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f, 9.0f, 10.0f }; | 
| 60 |         int correlationThreshold = 1; | 
| 61 |         bool ascending = true; | 
| 62 |         QTest::newRow(dataTag: "10 entries, ascending" ) << data << correlationThreshold << ascending; | 
| 63 |         data.clear(); | 
| 64 |  | 
| 65 |         data.resize(asize: 10000); | 
| 66 |         for (int i = 0; i < 10000; ++i) | 
| 67 |             data[i] = float(i); | 
| 68 |         ascending = true; | 
| 69 |         correlationThreshold = std::pow(x: float(10000), y: 0.25f); | 
| 70 |         QTest::newRow(dataTag: "10k entries, ascending" ) << data << correlationThreshold << ascending; | 
| 71 |         data.clear(); | 
| 72 |  | 
| 73 |         data.resize(asize: 10000); | 
| 74 |         for (int i = 0; i < 10000; ++i) | 
| 75 |             data[10000 - i - 1] = float(i); | 
| 76 |         ascending = false; | 
| 77 |         correlationThreshold = std::pow(x: float(10000), y: 0.25f); | 
| 78 |         QTest::newRow(dataTag: "10k entries, descending" ) << data << correlationThreshold << ascending; | 
| 79 |         data.clear(); | 
| 80 |     } | 
| 81 |  | 
| 82 |     void checkConstructionWithData() | 
| 83 |     { | 
| 84 |         // GIVEN | 
| 85 |         QFETCH(QVector<float>, x); | 
| 86 |         QFETCH(int, correlationThreshold); | 
| 87 |         QFETCH(bool, ascending); | 
| 88 |  | 
| 89 |         // WHEN | 
| 90 |         FunctionRangeFinder finder(x); | 
| 91 |  | 
| 92 |         // THEN | 
| 93 |         QCOMPARE(finder.rangeSize(), 2); | 
| 94 |         QCOMPARE(finder.isAscending(), ascending); | 
| 95 |         QCOMPARE(finder.correlationThreshold(), correlationThreshold); | 
| 96 |     } | 
| 97 |  | 
| 98 |     void checkFindLowerBound_data() | 
| 99 |     { | 
| 100 |         QTest::addColumn<QVector<float>>(name: "x" ); | 
| 101 |         QTest::addColumn<QVector<float>>(name: "needles" ); | 
| 102 |         QTest::addColumn<QVector<int>>(name: "lowerBounds" ); | 
| 103 |  | 
| 104 |         QVector<float> data = { 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f, 9.0f, 10.0f }; | 
| 105 |         QVector<float> needles = { 2.5f }; | 
| 106 |         QVector<int> lowerBounds = { 1 }; | 
| 107 |         QTest::newRow(dataTag: "10 entries, ascending" ) << data << needles << lowerBounds; | 
| 108 |         data.clear(); | 
| 109 |         needles.clear(); | 
| 110 |         lowerBounds.clear(); | 
| 111 |  | 
| 112 |         data.resize(asize: 10000); | 
| 113 |         for (int i = 0; i < 10000; ++i) | 
| 114 |             data[i] = float(i); | 
| 115 |  | 
| 116 |         needles.push_back(t: 23.75f); | 
| 117 |         lowerBounds.push_back(t: 23); | 
| 118 |  | 
| 119 |         needles.push_back(t: 500.03f); | 
| 120 |         lowerBounds.push_back(t: 500); | 
| 121 |  | 
| 122 |         needles.push_back(t: 500.2f); | 
| 123 |         lowerBounds.push_back(t: 500); | 
| 124 |  | 
| 125 |         needles.push_back(t: 501.4f); // Will trigger hunt codepath as previous test is correlated | 
| 126 |         lowerBounds.push_back(t: 501); | 
| 127 |  | 
| 128 |         needles.push_back(t: 3405.123f); | 
| 129 |         lowerBounds.push_back(t: 3405); | 
| 130 |  | 
| 131 |         QTest::newRow(dataTag: "10k entries, ascending" ) << data << needles << lowerBounds; | 
| 132 |         data.clear(); | 
| 133 |         needles.clear(); | 
| 134 |         lowerBounds.clear(); | 
| 135 |  | 
| 136 |         data.resize(asize: 10); | 
| 137 |         for (int i = 0; i < 10; ++i) | 
| 138 |             data[10 - i - 1] = float(i); | 
| 139 |  | 
| 140 |         needles.push_back(t: 8.1f); | 
| 141 |         lowerBounds.push_back(t: 0); | 
| 142 |  | 
| 143 |         needles.push_back(t: 7.2f); | 
| 144 |         lowerBounds.push_back(t: 1); | 
| 145 |  | 
| 146 |         needles.push_back(t: 0.5f); | 
| 147 |         lowerBounds.push_back(t: 8); | 
| 148 |  | 
| 149 |         QTest::newRow(dataTag: "10 entries, descending" ) << data << needles << lowerBounds; | 
| 150 |         data.clear(); | 
| 151 |     } | 
| 152 |  | 
| 153 |     void checkFindLowerBound() | 
| 154 |     { | 
| 155 |         // GIVEN | 
| 156 |         QFETCH(QVector<float>, x); | 
| 157 |         QFETCH(QVector<float>, needles); | 
| 158 |         QFETCH(QVector<int>, lowerBounds); | 
| 159 |         FunctionRangeFinder finder(x); | 
| 160 |  | 
| 161 |         for (int i = 0; i < needles.size(); ++i) { | 
| 162 |             // WHEN | 
| 163 |             int result = finder.findLowerBound(x: needles[i]); | 
| 164 |  | 
| 165 |             // THEN | 
| 166 |             QCOMPARE(result, lowerBounds[i]); | 
| 167 |         } | 
| 168 |     } | 
| 169 | }; | 
| 170 |  | 
| 171 | QTEST_APPLESS_MAIN(tst_FunctionRangeFinder) | 
| 172 |  | 
| 173 | #include "tst_functionrangefinder.moc" | 
| 174 |  |