ScalarCodec - added representation correctness test

This commit is contained in:
Pavel Kirienko 2014-02-21 00:42:11 +04:00
parent dc3111c77d
commit df04599ac0
2 changed files with 21 additions and 0 deletions

View File

@ -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);
}

View File

@ -74,8 +74,23 @@ TEST(ScalarCodec, Basic)
CHECK(64, u64, std::numeric_limits<uint64_t>::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());
}