transfer.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*
  2. * Copyright (C) 2014 Pavel Kirienko <pavel.kirienko@gmail.com>
  3. */
  4. #include <string>
  5. #include <gtest/gtest.h>
  6. #include <uavcan/transport/transfer.hpp>
  7. TEST(Transfer, TransferID)
  8. {
  9. using uavcan::TransferID;
  10. // Tests below are based on this assumption
  11. ASSERT_EQ(32, 1 << TransferID::BitLen);
  12. /*
  13. * forwardDistance()
  14. */
  15. EXPECT_EQ(0, TransferID(0).computeForwardDistance(0));
  16. EXPECT_EQ(1, TransferID(0).computeForwardDistance(1));
  17. EXPECT_EQ(7, TransferID(0).computeForwardDistance(7));
  18. EXPECT_EQ(0, TransferID(7).computeForwardDistance(7));
  19. EXPECT_EQ(31,TransferID(31).computeForwardDistance(30));
  20. EXPECT_EQ(1, TransferID(31).computeForwardDistance(0));
  21. EXPECT_EQ(30,TransferID(7).computeForwardDistance(5));
  22. EXPECT_EQ(5, TransferID(0).computeForwardDistance(5));
  23. /*
  24. * Misc
  25. */
  26. EXPECT_TRUE(TransferID(2) == TransferID(2));
  27. EXPECT_FALSE(TransferID(2) != TransferID(2));
  28. EXPECT_FALSE(TransferID(2) == TransferID(0));
  29. EXPECT_TRUE(TransferID(2) != TransferID(0));
  30. TransferID tid;
  31. for (int i = 0; i < 999; i++)
  32. {
  33. ASSERT_EQ(i & ((1 << TransferID::BitLen) - 1), tid.get());
  34. const TransferID copy = tid;
  35. tid.increment();
  36. ASSERT_EQ(1, copy.computeForwardDistance(tid));
  37. ASSERT_EQ(31, tid.computeForwardDistance(copy));
  38. ASSERT_EQ(0, tid.computeForwardDistance(tid));
  39. }
  40. }
  41. TEST(Transfer, NodeID)
  42. {
  43. uavcan::NodeID nid1(1);
  44. uavcan::NodeID nid127(127);
  45. uavcan::NodeID nid0(0);
  46. uavcan::NodeID nidx;
  47. ASSERT_TRUE(nid1.isUnicast());
  48. ASSERT_FALSE(nid1.isBroadcast());
  49. ASSERT_TRUE(nid1.isValid());
  50. ASSERT_TRUE(nid127.isUnicast());
  51. ASSERT_FALSE(nid127.isBroadcast());
  52. ASSERT_TRUE(nid127.isValid());
  53. ASSERT_FALSE(nid0.isUnicast());
  54. ASSERT_TRUE(nid0.isBroadcast());
  55. ASSERT_TRUE(nid0.isValid());
  56. ASSERT_FALSE(nidx.isUnicast());
  57. ASSERT_FALSE(nidx.isBroadcast());
  58. ASSERT_FALSE(nidx.isValid());
  59. /*
  60. * Comparison operators
  61. */
  62. ASSERT_TRUE(nid1 < nid127);
  63. ASSERT_TRUE(nid1 <= nid127);
  64. ASSERT_TRUE(nid0 < nid1);
  65. ASSERT_TRUE(nid0 <= nid1);
  66. ASSERT_FALSE(nid1 > nid127);
  67. ASSERT_FALSE(nid1 >= nid127);
  68. ASSERT_FALSE(nid0 > nid1);
  69. ASSERT_FALSE(nid0 >= nid1);
  70. ASSERT_FALSE(nid1 > uavcan::NodeID(1));
  71. ASSERT_TRUE(nid1 >= uavcan::NodeID(1));
  72. ASSERT_FALSE(nid1 == nid127);
  73. ASSERT_TRUE(nid127 == uavcan::NodeID(127));
  74. }