From df04599ac0b21c8eb76d140bf7aa31986200d01c Mon Sep 17 00:00:00 2001 From: Pavel Kirienko Date: Fri, 21 Feb 2014 00:42:11 +0400 Subject: [PATCH] ScalarCodec - added representation correctness test --- .../uavcan/internal/marshalling/scalar_codec.hpp | 6 ++++++ libuavcan/test/marshalling/scalar_codec.cpp | 15 +++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/libuavcan/include/uavcan/internal/marshalling/scalar_codec.hpp b/libuavcan/include/uavcan/internal/marshalling/scalar_codec.hpp index 3049614562..f62336676d 100644 --- a/libuavcan/include/uavcan/internal/marshalling/scalar_codec.hpp +++ b/libuavcan/include/uavcan/internal/marshalling/scalar_codec.hpp @@ -40,6 +40,12 @@ class ScalarCodec u.l = 1; const bool big_endian = u.c[sizeof(long int) - 1] == 1; #endif + /* + * I didn't have any big endian machine nearby, so big endian support wasn't tested yet. + * It is likely to be OK anyway, so feel free to remove this assert() as needed. + */ + assert(big_endian == false); + if (big_endian) swapByteOrder(bytes); } diff --git a/libuavcan/test/marshalling/scalar_codec.cpp b/libuavcan/test/marshalling/scalar_codec.cpp index 40da7ac4d0..2dd2e32d05 100644 --- a/libuavcan/test/marshalling/scalar_codec.cpp +++ b/libuavcan/test/marshalling/scalar_codec.cpp @@ -74,8 +74,23 @@ TEST(ScalarCodec, Basic) CHECK(64, u64, std::numeric_limits::max()); #undef CHECK + + ASSERT_EQ(0, sc_rd.decode<64>(u64)); // Out of buffer space } TEST(ScalarCodec, RepresentationCorrectness) { + uavcan::StaticTransferBuffer<4> buf; + uavcan::BitStream bs_wr(buf); + uavcan::ScalarCodec sc_wr(bs_wr); + + ASSERT_EQ(1, sc_wr.encode<12>((uint16_t)0xbeda)); // --> 0xeda + ASSERT_EQ(1, sc_wr.encode<3>((int8_t)-1)); + ASSERT_EQ(1, sc_wr.encode<4>((int8_t)-5)); + ASSERT_EQ(1, sc_wr.encode<2>((int16_t)-1)); + ASSERT_EQ(1, sc_wr.encode<4>((uint8_t)0x88)); // --> 8 + + // This representation was carefully crafted and triple checked: + static const std::string REFERENCE = "11011010 11101111 01111100 00000000"; + ASSERT_EQ(REFERENCE, bs_wr.toString()); }