integer_spec.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. * Copyright (C) 2014 Pavel Kirienko <pavel.kirienko@gmail.com>
  3. */
  4. #include <gtest/gtest.h>
  5. #include <uavcan/marshal/types.hpp>
  6. #include <uavcan/transport/transfer_buffer.hpp>
  7. TEST(IntegerSpec, Limits)
  8. {
  9. using uavcan::IntegerSpec;
  10. using uavcan::SignednessSigned;
  11. using uavcan::SignednessUnsigned;
  12. using uavcan::CastModeSaturate;
  13. using uavcan::CastModeTruncate;
  14. typedef IntegerSpec<8, SignednessUnsigned, CastModeSaturate> UInt8;
  15. typedef IntegerSpec<4, SignednessSigned, CastModeTruncate> SInt4;
  16. typedef IntegerSpec<32, SignednessUnsigned, CastModeTruncate> UInt32;
  17. typedef IntegerSpec<40, SignednessUnsigned, CastModeSaturate> UInt40;
  18. typedef IntegerSpec<64, SignednessUnsigned, CastModeTruncate> UInt64;
  19. typedef IntegerSpec<64, SignednessSigned, CastModeSaturate> SInt64;
  20. typedef IntegerSpec<63, SignednessUnsigned, CastModeSaturate> UInt63;
  21. ASSERT_EQ(255, UInt8::max());
  22. ASSERT_EQ(0, UInt8::min());
  23. ASSERT_EQ(7, SInt4::max());
  24. ASSERT_EQ(-8, SInt4::min());
  25. ASSERT_EQ(0xFFFFFFFF, UInt32::max());
  26. ASSERT_EQ(0, UInt32::min());
  27. ASSERT_EQ(0xFFFFFFFFFF, UInt40::max());
  28. ASSERT_EQ(0, UInt40::min());
  29. ASSERT_EQ(0xFFFFFFFFFFFFFFFF, UInt64::max());
  30. ASSERT_EQ(0, UInt64::min());
  31. ASSERT_EQ(0x7FFFFFFFFFFFFFFF, SInt64::max());
  32. ASSERT_EQ(-0x8000000000000000, SInt64::min());
  33. ASSERT_EQ(0x7FFFFFFFFFFFFFFF, UInt63::max());
  34. ASSERT_EQ(0, UInt63::min());
  35. ASSERT_EQ(SInt64::max(), UInt63::max());
  36. }
  37. TEST(IntegerSpec, Basic)
  38. {
  39. using uavcan::IntegerSpec;
  40. using uavcan::SignednessSigned;
  41. using uavcan::SignednessUnsigned;
  42. using uavcan::CastModeSaturate;
  43. using uavcan::CastModeTruncate;
  44. using uavcan::StorageType;
  45. uavcan::StaticTransferBuffer<100> buf;
  46. uavcan::BitStream bs_wr(buf);
  47. uavcan::ScalarCodec sc_wr(bs_wr);
  48. typedef IntegerSpec<8, SignednessUnsigned, CastModeSaturate> UInt8S;
  49. typedef IntegerSpec<4, SignednessSigned, CastModeTruncate> SInt4T;
  50. typedef IntegerSpec<32, SignednessUnsigned, CastModeTruncate> UInt32T;
  51. typedef IntegerSpec<40, SignednessUnsigned, CastModeSaturate> UInt40S;
  52. typedef IntegerSpec<64, SignednessUnsigned, CastModeTruncate> UInt64T;
  53. typedef IntegerSpec<58, SignednessSigned, CastModeSaturate> SInt58S;
  54. typedef IntegerSpec<63, SignednessUnsigned, CastModeSaturate> UInt63S;
  55. typedef IntegerSpec<10, SignednessSigned, CastModeSaturate> SInt10S;
  56. typedef IntegerSpec<1, SignednessUnsigned, CastModeSaturate> UInt1S;
  57. ASSERT_EQ(1, UInt8S::encode(UInt8S::StorageType(123), sc_wr, uavcan::TailArrayOptDisabled));
  58. ASSERT_EQ(1, SInt4T::encode(SInt4T::StorageType(-0x44), sc_wr, uavcan::TailArrayOptDisabled));
  59. ASSERT_EQ(1, UInt32T::encode(UInt32T::StorageType(0xFFFFFFFF), sc_wr, uavcan::TailArrayOptDisabled));
  60. ASSERT_EQ(1, UInt40S::encode(UInt40S::StorageType(0xFFFFFFFFFFFFFFFF), sc_wr, uavcan::TailArrayOptDisabled));
  61. ASSERT_EQ(1, UInt64T::encode(UInt64T::StorageType(0xFFFFFFFFFFFFFFFF), sc_wr, uavcan::TailArrayOptDisabled));
  62. ASSERT_EQ(1, SInt58S::encode(SInt58S::StorageType(0xFFFFFFFFFFFFFFF), sc_wr, uavcan::TailArrayOptDisabled));
  63. ASSERT_EQ(1, UInt63S::encode(UInt63S::StorageType(0xFFFFFFFFFFFFFFFF), sc_wr, uavcan::TailArrayOptDisabled));
  64. ASSERT_EQ(1, SInt10S::encode(SInt10S::StorageType(-30000), sc_wr, uavcan::TailArrayOptDisabled));
  65. ASSERT_EQ(1, UInt1S::encode(UInt1S::StorageType(42), sc_wr, uavcan::TailArrayOptDisabled));
  66. std::cout << bs_wr.toString() << std::endl;
  67. uavcan::BitStream bs_rd(buf);
  68. uavcan::ScalarCodec sc_rd(bs_rd);
  69. #define CHECK(IntType, expected_value) \
  70. do { \
  71. StorageType<IntType>::Type var = StorageType<IntType>::Type(); \
  72. ASSERT_EQ(1, IntType::decode(var, sc_rd, uavcan::TailArrayOptDisabled)); \
  73. ASSERT_EQ(expected_value, var); \
  74. } while (0)
  75. CHECK(UInt8S, 123);
  76. CHECK(SInt4T, -4);
  77. CHECK(UInt32T, 0xFFFFFFFF);
  78. CHECK(UInt40S, 0xFFFFFFFFFF);
  79. CHECK(UInt64T, 0xFFFFFFFFFFFFFFFF);
  80. CHECK(SInt58S, 0x1FFFFFFFFFFFFFF);
  81. CHECK(UInt63S, 0x7FFFFFFFFFFFFFFF);
  82. CHECK(SInt10S, -512);
  83. CHECK(UInt1S, 1);
  84. #undef CHECK
  85. StorageType<UInt1S>::Type var;
  86. ASSERT_EQ(0, UInt1S::decode(var, sc_rd, uavcan::TailArrayOptDisabled));
  87. }