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