Revert "Device/Driver/LX: Add small delay after baud rate change"
[xcsoar.git] / test / src / TestClimbAvCalc.cpp
blob157e98f6088eea9ccb1dcb5579bc51f96ebae020
1 /* Copyright_License {
3 XCSoar Glide Computer - http://www.xcsoar.org/
4 Copyright (C) 2000-2013 The XCSoar Project
5 A detailed list of copyright holders can be found in the file "AUTHORS".
7 This program is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public License
9 as published by the Free Software Foundation; either version 2
10 of the License, or (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 #include "Computer/ClimbAverageCalculator.hpp"
24 #include "TestUtil.hpp"
26 #include <cstdio>
28 static void
29 TestBasic()
31 ClimbAverageCalculator c;
32 c.Reset();
34 fixed av;
36 constexpr fixed AVERAGE_TIME = fixed(30);
38 // Test normal behavior
39 c.GetAverage(fixed(0), fixed(0), AVERAGE_TIME);
40 for (unsigned i = 1; i <= 15; i++)
41 av = c.GetAverage(fixed(i), fixed(i), AVERAGE_TIME);
43 ok1(equals(av, 1.0));
45 for (unsigned i = 1; i <= 15; i++)
46 av = c.GetAverage(fixed(15 + i), fixed(15 + i * 2), AVERAGE_TIME);
48 ok1(equals(av, 1.5));
50 for (unsigned i = 1; i <= 15; i++)
51 av = c.GetAverage(fixed(30 + i), fixed(45 + i * 2), AVERAGE_TIME);
53 ok1(equals(av, 2.0));
56 static void
57 TestDuplicateTimestamps()
59 ClimbAverageCalculator c;
60 fixed av;
62 constexpr fixed AVERAGE_TIME = fixed(30);
64 // Test time difference = zero behavior
65 c.Reset();
66 c.GetAverage(fixed(0), fixed(0), AVERAGE_TIME);
67 for (unsigned i = 1; i <= 15; i++)
68 c.GetAverage(fixed(i), fixed(i), AVERAGE_TIME);
70 for (unsigned i = 1; i <= 15; i++) {
71 c.GetAverage(fixed(15 + i), fixed(15 + i * 2), AVERAGE_TIME);
72 c.GetAverage(fixed(15 + i), fixed(15 + i * 2), AVERAGE_TIME);
73 av = c.GetAverage(fixed(15 + i), fixed(15 + i * 2), AVERAGE_TIME);
76 ok1(equals(av, 1.5));
79 static void
80 TestExpiration()
82 ClimbAverageCalculator c;
83 c.Reset();
85 constexpr fixed AVERAGE_TIME = fixed(30);
88 // Test expiration for empty data
89 ok1(c.Expired(fixed(0), fixed(60)));
90 ok1(c.Expired(fixed(15), fixed(60)));
92 // Add values and test non-expiration
93 bool expired = false;
94 for (unsigned i = 1; i <= 60; i++) {
95 c.GetAverage(fixed(i), fixed(i), AVERAGE_TIME);
96 expired = expired || c.Expired(fixed(i), fixed(60));
99 ok1(!expired);
101 // Test expiration with 30sec
102 ok1(!c.Expired(fixed(89), fixed(30)));
103 ok1(!c.Expired(fixed(90), fixed(30)));
104 ok1(c.Expired(fixed(91), fixed(30)));
106 // Test expiration with 60sec
107 ok1(!c.Expired(fixed(119), fixed(60)));
108 ok1(!c.Expired(fixed(120), fixed(60)));
109 ok1(c.Expired(fixed(121), fixed(60)));
111 // Time warp
112 ok1(c.Expired(fixed(59), fixed(60)));
113 ok1(!c.Expired(fixed(60), fixed(60)));
114 ok1(!c.Expired(fixed(61), fixed(60)));
117 int main(int argc, char **argv)
119 plan_tests(16);
121 TestBasic();
122 TestDuplicateTimestamps();
123 TestExpiration();
125 return exit_status();