testCommonAverager.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #include <cstdio>
  2. #include "gtest/gtest.h"
  3. #include "car/CommonAverager.h"
  4. namespace cc
  5. {
  6. using CAR = cc::CommonAverager<int16_t>;
  7. using UCAR = cc::CommonAverager<uint16_t>;
  8. using DCAR = cc::CommonAverager<double>;
  9. TEST(CommonAverager, Constructor1)
  10. {
  11. ChannelNumList_t chmap{0, 1, 3};
  12. CAR car(chmap);
  13. ASSERT_EQ(car.m_car_channel_map.size(), 3);
  14. ASSERT_EQ(car.m_car_channel_map[0], 0);
  15. ASSERT_EQ(car.m_car_channel_map[1], 1);
  16. ASSERT_EQ(car.m_car_channel_map[2], 3);
  17. }
  18. TEST(CommonAverager, Constructor2)
  19. {
  20. ChannelNumList_t chmap{0, 1, 3};
  21. CAR car(std::move(chmap));
  22. ASSERT_EQ(car.m_car_channel_map.size(), 3);
  23. ASSERT_EQ(car.m_car_channel_map[0], 0);
  24. ASSERT_EQ(car.m_car_channel_map[1], 1);
  25. ASSERT_EQ(car.m_car_channel_map[2], 3);
  26. }
  27. TEST(CommonAverager, Operator)
  28. {
  29. ChannelNumList_t chmap{0, 1, 3};
  30. CAR::ContinuousDataSampleVT sample{1, -2, 4, -8, 16};
  31. const CAR::ContinuousDataSampleVT sample_expect{4, 1, 7, -5, 19};
  32. CAR car(std::move(chmap));
  33. car(sample);
  34. // average should be (1 - 2 - 8) / 3 = -9 / 3 = -3
  35. EXPECT_EQ(sample, sample_expect);
  36. }
  37. TEST(CommonAverager, Overflow1)
  38. {
  39. ChannelNumList_t chmap{0, 1};
  40. CAR::ContinuousDataSampleVT sample{32767, 32767, 0, 0, 0};
  41. const CAR::ContinuousDataSampleVT sample_expect{0, 0, -32767, -32767, -32767};
  42. CAR car(std::move(chmap));
  43. car(sample);
  44. EXPECT_EQ(sample, sample_expect);
  45. }
  46. TEST(CommonAverager, UOperator)
  47. {
  48. ChannelNumList_t chmap{0, 1, 3};
  49. UCAR::ContinuousDataSampleVT sample{10, 20, 40, 80, 160};
  50. const UCAR::ContinuousDataSampleVT sample_expect{0, 0, 4, 44, 124};
  51. UCAR car(std::move(chmap));
  52. car(sample);
  53. EXPECT_EQ(sample, sample_expect);
  54. }
  55. TEST(CommonAverager, DOperator)
  56. {
  57. ChannelNumList_t chmap{0, 1, 3};
  58. DCAR::ContinuousDataSampleVT sample{1.2, -.83, 1e-4, 6.2e-3, 3.1223e5};
  59. double avg = (1.2 - 0.83 + 6.2e-3) / 3.0;
  60. DCAR::ContinuousDataSampleVT sample_expect(sample);
  61. for (auto &x:sample_expect)
  62. {
  63. x -= avg;
  64. }
  65. // {1.074566667, -.83 - 0.125433333, 1e-4 - 0.125433333, 6.2e-3 - 0.125433333, 3.1223e5 - 0.125433333};
  66. DCAR car(std::move(chmap));
  67. car(sample);
  68. ASSERT_EQ(sample.size(), sample_expect.size()) << "Vectors sample and sample_expect are of unequal length";
  69. for (int i = 0; i < sample_expect.size(); ++i) {
  70. EXPECT_DOUBLE_EQ(sample[i], sample_expect[i]) << "Vectors sample and sample_expect differ at index " << i;
  71. }
  72. }
  73. } /* cc */