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>
23
class FakeTimeSource : public AxisVelocityCalculator::TimeSource {
25
FakeTimeSource() : m_value(0) {}
27
virtual qint64 msecsSinceReference() {
31
void setMsecsSinceReference(qint64 time) {
35
void increaseMsecsSinceReference(qint64 time) {
43
class tst_AxisVelocityCalculator : public QObject
49
void overflowSamples();
53
void tst_AxisVelocityCalculator::simpleSamples()
55
FakeTimeSource *fakeTimeSource = new FakeTimeSource;
56
AxisVelocityCalculator velCalc(fakeTimeSource);
59
velCalc.setTrackedPosition(pos);
62
fakeTimeSource->setMsecsSinceReference(10);
64
velCalc.setTrackedPosition(pos);
66
fakeTimeSource->setMsecsSinceReference(20);
68
velCalc.setTrackedPosition(pos);
70
fakeTimeSource->setMsecsSinceReference(30);
72
velCalc.setTrackedPosition(pos);
74
qreal velocity = velCalc.calculate();
76
QCOMPARE(velocity, 2.0f);
79
void tst_AxisVelocityCalculator::noSamples()
81
FakeTimeSource *fakeTimeSource = new FakeTimeSource;
82
AxisVelocityCalculator velCalc(fakeTimeSource);
84
float velocity = velCalc.calculate();
86
QCOMPARE(velocity, 0.0f);
89
void tst_AxisVelocityCalculator::overflowSamples()
91
FakeTimeSource *fakeTimeSource = new FakeTimeSource;
92
AxisVelocityCalculator velCalc(fakeTimeSource);
95
velCalc.setTrackedPosition(pos);
98
for (int i = 0; i < 1000; ++i) {
99
fakeTimeSource->increaseMsecsSinceReference(10);
101
velCalc.setTrackedPosition(pos);
104
/* overwrite all existing samples with faster ones */
105
for (int i = 0; i < AxisVelocityCalculator::MAX_SAMPLES; ++i) {
106
fakeTimeSource->increaseMsecsSinceReference(10);
108
velCalc.setTrackedPosition(pos);
111
float velocity = velCalc.calculate();
113
/* check that the calculated velocity correspond to the latest, faster, samples */
114
QCOMPARE(velocity, 4.0f);
117
void tst_AxisVelocityCalculator::average()
119
FakeTimeSource *fakeTimeSource = new FakeTimeSource;
120
AxisVelocityCalculator velCalc(fakeTimeSource);
123
velCalc.setTrackedPosition(pos);
126
fakeTimeSource->increaseMsecsSinceReference(10);
128
velCalc.setTrackedPosition(pos);
130
fakeTimeSource->increaseMsecsSinceReference(10);
132
velCalc.setTrackedPosition(pos);
134
/* the last sample is an erratic one and would yield a big velocity if
135
considered isolatedly */
136
fakeTimeSource->increaseMsecsSinceReference(10);
138
velCalc.setTrackedPosition(pos);
140
float velocity = velCalc.calculate();
142
/* calculated velocity is lower than the one from the last sample */
143
QVERIFY(velocity < 9.0f);
145
/* but it's higher the the slow samples */
146
QVERIFY(velocity > 2.5f);
149
QTEST_MAIN(tst_AxisVelocityCalculator)
151
#include "tst_AxisVelocityCalculator.moc"