2 * Copyright (C) 2013 Canonical, Ltd.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 3.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 * Authored by: Daniel d'Andrada <daniel.dandrada@canonical.com>
19#include <QtTest/QtTest>
21#include <AxisVelocityCalculator.h>
23class FakeTimeSource : public UbuntuGestures::TimeSource {
25 FakeTimeSource() : m_value(0) {}
27 qint64 msecsSinceReference() override {
31 void setMsecsSinceReference(qint64 time) {
35 void increaseMsecsSinceReference(qint64 time) {
43class tst_AxisVelocityCalculator : public QObject
47 void init(); // called right before each and every test function is executed
48 void cleanup(); // called right after each and every test function is executed
52 void overflowSamples();
56 AxisVelocityCalculator *velCalc;
57 QSharedPointer<FakeTimeSource> fakeTimeSource;
60void tst_AxisVelocityCalculator::init()
62 fakeTimeSource.reset(new FakeTimeSource);
64 velCalc = new AxisVelocityCalculator;
65 velCalc->setTimeSource(fakeTimeSource);
68void tst_AxisVelocityCalculator::cleanup()
71 fakeTimeSource.reset();
74void tst_AxisVelocityCalculator::simpleSamples()
78 velCalc->setTrackedPosition(pos);
81 fakeTimeSource->setMsecsSinceReference(10);
83 velCalc->setTrackedPosition(pos);
85 fakeTimeSource->setMsecsSinceReference(20);
87 velCalc->setTrackedPosition(pos);
89 fakeTimeSource->setMsecsSinceReference(30);
91 velCalc->setTrackedPosition(pos);
93 qreal velocity = velCalc->calculate();
95 QCOMPARE(velocity, 2.0f);
98void tst_AxisVelocityCalculator::noSamples()
100 float velocity = velCalc->calculate();
102 QCOMPARE(velocity, 0.0f);
105void tst_AxisVelocityCalculator::overflowSamples()
109 velCalc->setTrackedPosition(pos);
112 for (int i = 0; i < 1000; ++i) {
113 fakeTimeSource->increaseMsecsSinceReference(10);
115 velCalc->setTrackedPosition(pos);
118 /* overwrite all existing samples with faster ones */
119 for (int i = 0; i < AxisVelocityCalculator::MAX_SAMPLES; ++i) {
120 fakeTimeSource->increaseMsecsSinceReference(10);
122 velCalc->setTrackedPosition(pos);
125 float velocity = velCalc->calculate();
127 /* check that the calculated velocity correspond to the latest, faster, samples */
128 QCOMPARE(velocity, 4.0f);
131void tst_AxisVelocityCalculator::average()
135 velCalc->setTrackedPosition(pos);
138 fakeTimeSource->increaseMsecsSinceReference(10);
140 velCalc->setTrackedPosition(pos);
142 fakeTimeSource->increaseMsecsSinceReference(10);
144 velCalc->setTrackedPosition(pos);
146 /* the last sample is an erratic one and would yield a big velocity if
147 considered isolatedly */
148 fakeTimeSource->increaseMsecsSinceReference(10);
150 velCalc->setTrackedPosition(pos);
152 float velocity = velCalc->calculate();
154 /* calculated velocity is lower than the one from the last sample */
155 QVERIFY(velocity < 9.0f);
157 /* but it's higher the the slow samples */
158 QVERIFY(velocity > 2.5f);
161QTEST_MAIN(tst_AxisVelocityCalculator)
163#include "tst_AxisVelocityCalculator.moc"
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list