iface_mock.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. * Copyright (C) 2014 Pavel Kirienko <pavel.kirienko@gmail.com>
  3. */
  4. #include <gtest/gtest.h>
  5. #include "can.hpp"
  6. TEST(CanDriverMock, Basic)
  7. {
  8. using uavcan::CanFrame;
  9. using uavcan::CanSelectMasks;
  10. SystemClockMock clockmock;
  11. CanDriverMock driver(3, clockmock);
  12. const uavcan::CanFrame* pending_tx[uavcan::MaxCanIfaces] = { };
  13. ASSERT_EQ(3, driver.getNumIfaces());
  14. // All WR, no RD
  15. CanSelectMasks masks;
  16. masks.write = 7;
  17. masks.read = 7;
  18. EXPECT_LT(0, driver.select(masks, pending_tx, uavcan::MonotonicTime::fromUSec(100)));
  19. EXPECT_EQ(7, masks.write);
  20. EXPECT_EQ(0, masks.read);
  21. for (unsigned i = 0; i < 3; i++)
  22. {
  23. driver.ifaces.at(i).writeable = false;
  24. }
  25. // No WR, no RD
  26. masks.write = 7;
  27. masks.read = 7;
  28. EXPECT_EQ(0, driver.select(masks, pending_tx, uavcan::MonotonicTime::fromUSec(100)));
  29. EXPECT_EQ(0, masks.write);
  30. EXPECT_EQ(0, masks.read);
  31. EXPECT_EQ(100, clockmock.monotonic);
  32. EXPECT_EQ(100, clockmock.utc);
  33. // No WR, #1 RD
  34. const CanFrame fr1 = makeCanFrame(123, "foo", EXT);
  35. driver.ifaces.at(1).pushRx(fr1);
  36. masks.write = 7;
  37. masks.read = 6;
  38. EXPECT_LT(0, driver.select(masks, pending_tx, uavcan::MonotonicTime::fromUSec(100)));
  39. EXPECT_EQ(0, masks.write);
  40. EXPECT_EQ(2, masks.read);
  41. CanFrame fr2;
  42. uavcan::MonotonicTime ts_monotonic;
  43. uavcan::UtcTime ts_utc;
  44. uavcan::CanIOFlags flags = 0;
  45. EXPECT_EQ(1, driver.getIface(1)->receive(fr2, ts_monotonic, ts_utc, flags));
  46. EXPECT_EQ(0, flags);
  47. EXPECT_EQ(fr1, fr2);
  48. EXPECT_EQ(100, ts_monotonic.toUSec());
  49. EXPECT_EQ(0, ts_utc.toUSec());
  50. // #0 WR, #1 RD, Select failure
  51. driver.ifaces.at(0).writeable = true;
  52. driver.select_failure = true;
  53. masks.write = 1;
  54. masks.read = 7;
  55. EXPECT_EQ(-1, driver.select(masks, pending_tx, uavcan::MonotonicTime::fromUSec(100)));
  56. EXPECT_EQ(1, masks.write); // Leaving masks unchanged - the library must ignore them
  57. EXPECT_EQ(7, masks.read);
  58. }
  59. TEST(CanDriverMock, Loopback)
  60. {
  61. using uavcan::CanFrame;
  62. using uavcan::CanSelectMasks;
  63. SystemClockMock clockmock;
  64. CanDriverMock driver(1, clockmock);
  65. const uavcan::CanFrame* pending_tx[uavcan::MaxCanIfaces] = { };
  66. CanSelectMasks masks;
  67. masks.write = 1;
  68. masks.read = 1;
  69. EXPECT_LT(0, driver.select(masks, pending_tx, uavcan::MonotonicTime::fromUSec(100)));
  70. EXPECT_EQ(1, masks.write);
  71. EXPECT_EQ(0, masks.read);
  72. clockmock.advance(200);
  73. CanFrame fr1;
  74. fr1.id = 123 | CanFrame::FlagEFF;
  75. EXPECT_EQ(1, driver.getIface(0)->send(fr1, uavcan::MonotonicTime::fromUSec(10000), uavcan::CanIOFlagLoopback));
  76. masks.write = 0;
  77. masks.read = 1;
  78. EXPECT_LT(0, driver.select(masks, pending_tx, uavcan::MonotonicTime::fromUSec(100)));
  79. EXPECT_EQ(0, masks.write);
  80. EXPECT_EQ(1, masks.read);
  81. CanFrame fr2;
  82. uavcan::MonotonicTime ts_monotonic;
  83. uavcan::UtcTime ts_utc;
  84. uavcan::CanIOFlags flags = 0;
  85. EXPECT_EQ(1, driver.getIface(0)->receive(fr2, ts_monotonic, ts_utc, flags));
  86. EXPECT_EQ(uavcan::CanIOFlagLoopback, flags);
  87. EXPECT_EQ(fr1, fr2);
  88. EXPECT_EQ(200, ts_monotonic.toUSec());
  89. EXPECT_EQ(0, ts_utc.toUSec());
  90. }