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