123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102 |
- #include <cstdio>
- #include "gtest/gtest.h"
- #include "car/CommonAverager.h"
- namespace cc
- {
- using CAR = cc::CommonAverager<int16_t>;
- using UCAR = cc::CommonAverager<uint16_t>;
- using DCAR = cc::CommonAverager<double>;
- TEST(CommonAverager, Constructor1)
- {
- ChannelNumList_t chmap{0, 1, 3};
-
- CAR car(chmap);
-
- ASSERT_EQ(car.m_car_channel_map.size(), 3);
- ASSERT_EQ(car.m_car_channel_map[0], 0);
- ASSERT_EQ(car.m_car_channel_map[1], 1);
- ASSERT_EQ(car.m_car_channel_map[2], 3);
- }
- TEST(CommonAverager, Constructor2)
- {
- ChannelNumList_t chmap{0, 1, 3};
-
- CAR car(std::move(chmap));
-
- ASSERT_EQ(car.m_car_channel_map.size(), 3);
- ASSERT_EQ(car.m_car_channel_map[0], 0);
- ASSERT_EQ(car.m_car_channel_map[1], 1);
- ASSERT_EQ(car.m_car_channel_map[2], 3);
- }
- TEST(CommonAverager, Operator)
- {
- ChannelNumList_t chmap{0, 1, 3};
- CAR::ContinuousDataSampleVT sample{1, -2, 4, -8, 16};
- const CAR::ContinuousDataSampleVT sample_expect{4, 1, 7, -5, 19};
-
- CAR car(std::move(chmap));
-
- car(sample);
- // average should be (1 - 2 - 8) / 3 = -9 / 3 = -3
- EXPECT_EQ(sample, sample_expect);
- }
- TEST(CommonAverager, Overflow1)
- {
- ChannelNumList_t chmap{0, 1};
- CAR::ContinuousDataSampleVT sample{32767, 32767, 0, 0, 0};
- const CAR::ContinuousDataSampleVT sample_expect{0, 0, -32767, -32767, -32767};
-
- CAR car(std::move(chmap));
-
- car(sample);
- EXPECT_EQ(sample, sample_expect);
- }
- TEST(CommonAverager, UOperator)
- {
- ChannelNumList_t chmap{0, 1, 3};
- UCAR::ContinuousDataSampleVT sample{10, 20, 40, 80, 160};
- const UCAR::ContinuousDataSampleVT sample_expect{0, 0, 4, 44, 124};
-
- UCAR car(std::move(chmap));
-
- car(sample);
- EXPECT_EQ(sample, sample_expect);
- }
- TEST(CommonAverager, DOperator)
- {
- ChannelNumList_t chmap{0, 1, 3};
- DCAR::ContinuousDataSampleVT sample{1.2, -.83, 1e-4, 6.2e-3, 3.1223e5};
- double avg = (1.2 - 0.83 + 6.2e-3) / 3.0;
- DCAR::ContinuousDataSampleVT sample_expect(sample);
- for (auto &x:sample_expect)
- {
- x -= avg;
- }
- // {1.074566667, -.83 - 0.125433333, 1e-4 - 0.125433333, 6.2e-3 - 0.125433333, 3.1223e5 - 0.125433333};
-
- DCAR car(std::move(chmap));
-
- car(sample);
- ASSERT_EQ(sample.size(), sample_expect.size()) << "Vectors sample and sample_expect are of unequal length";
- for (int i = 0; i < sample_expect.size(); ++i) {
- EXPECT_DOUBLE_EQ(sample[i], sample_expect[i]) << "Vectors sample and sample_expect differ at index " << i;
- }
-
- }
- } /* cc */
|