blob: 9a7fefc482c4c46b03b8a845104a4e06fa728d4c [file] [log] [blame]
Austin Schuhe4106142019-12-01 18:19:53 -08001#include "aos/events/timing_statistics.h"
2
Austin Schuhe4106142019-12-01 18:19:53 -08003#include "gtest/gtest.h"
4
Philipp Schrader790cb542023-07-05 21:06:52 -07005#include "aos/flatbuffers.h"
6
Austin Schuhe4106142019-12-01 18:19:53 -08007namespace aos {
8namespace internal {
9namespace testing {
10
11TEST(TimingStatistic, StatisticsTest) {
12 flatbuffers::FlatBufferBuilder fbb;
13 fbb.Finish(timing::CreateStatistic(fbb));
14
15 FlatbufferDetachedBuffer<timing::Statistic> statistic(fbb.Release());
16
17 TimingStatistic ts;
18 ts.set_statistic(statistic.mutable_message());
19
20 // Make sure we can add 2 numbers and get the expected result.
21 ts.Add(5.0);
22
23 EXPECT_EQ(statistic.message().average(), 5.0);
24 EXPECT_EQ(statistic.message().min(), 5.0);
25 EXPECT_EQ(statistic.message().max(), 5.0);
26 EXPECT_EQ(statistic.message().standard_deviation(), 0.0);
27
28 ts.Add(5.0);
29
30 EXPECT_EQ(statistic.message().average(), 5.0);
31 EXPECT_EQ(statistic.message().min(), 5.0);
32 EXPECT_EQ(statistic.message().max(), 5.0);
33 EXPECT_EQ(statistic.message().standard_deviation(), 0.0);
34
35 // Make sure reset works.
36 ts.Reset();
37
38 // And all the results are nan.
39 EXPECT_TRUE(std::isnan(statistic.message().average()));
40 EXPECT_TRUE(std::isnan(statistic.message().min()));
41 EXPECT_TRUE(std::isnan(statistic.message().max()));
42 EXPECT_TRUE(std::isnan(statistic.message().standard_deviation()));
43
44 ts.Add(7.0);
45
46 EXPECT_EQ(statistic.message().average(), 7.0);
47 EXPECT_EQ(statistic.message().min(), 7.0);
48 EXPECT_EQ(statistic.message().max(), 7.0);
49 EXPECT_EQ(statistic.message().standard_deviation(), 0.0);
50
51 ts.Reset();
52
53 // Now add a predetermined set of data and make sure we get a result which
54 // agrees with online calculators.
55 ts.Add(10);
56 ts.Add(12);
57 ts.Add(23);
58 ts.Add(23);
59 ts.Add(16);
60 ts.Add(23);
61 ts.Add(21);
62 ts.Add(16);
63
64 EXPECT_EQ(statistic.message().average(), 18.0);
65 EXPECT_EQ(statistic.message().min(), 10.0);
66 EXPECT_EQ(statistic.message().max(), 23.0);
67 EXPECT_NEAR(statistic.message().standard_deviation(), 5.2372293656638, 1e-6);
68}
69
70} // namespace testing
71} // namespace internal
72} // namespace aos