From fc3f0808a6dc9cd0f4bae2c4a61f817a37467c22 Mon Sep 17 00:00:00 2001 From: Pavel Kirienko Date: Sat, 11 Jul 2015 13:38:23 +0300 Subject: [PATCH] Union test --- .../dsdl_test/dsdl_uavcan_compilability.cpp | 109 +++++++++++++++++- .../test/dsdl_test/root_ns_a/Empty.uavcan | 0 .../dsdl_test/root_ns_a/NestedInUnion.uavcan | 4 + .../test/dsdl_test/root_ns_a/UnionTest.uavcan | 8 ++ 4 files changed, 120 insertions(+), 1 deletion(-) create mode 100644 libuavcan/test/dsdl_test/root_ns_a/Empty.uavcan create mode 100644 libuavcan/test/dsdl_test/root_ns_a/NestedInUnion.uavcan create mode 100644 libuavcan/test/dsdl_test/root_ns_a/UnionTest.uavcan diff --git a/libuavcan/test/dsdl_test/dsdl_uavcan_compilability.cpp b/libuavcan/test/dsdl_test/dsdl_uavcan_compilability.cpp index eb4227c73e..153521a00c 100644 --- a/libuavcan/test/dsdl_test/dsdl_uavcan_compilability.cpp +++ b/libuavcan/test/dsdl_test/dsdl_uavcan_compilability.cpp @@ -5,6 +5,7 @@ #include #include +#include #include #include @@ -24,6 +25,8 @@ template static bool validateYaml(const T& obj, const std::string& reference) { + uavcan::OStream::instance() << "Validating YAML:\n" << obj << "\n" << uavcan::OStream::endl; + std::ostringstream os; os << obj; if (os.str() == reference) @@ -40,7 +43,7 @@ static bool validateYaml(const T& obj, const std::string& reference) << "ACTUAL:\n" << "\n===\n" << os.str() - << "===" << std::endl; + << "\n===\n" << std::endl; return false; } } @@ -90,3 +93,107 @@ TEST(Dsdl, Streaming) " vector: [0, 0]\n" " bools: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]")); } + + +template +static bool encodeDecodeValidate(const T& obj, const std::string& reference_bit_string) +{ + uavcan::StaticTransferBuffer<256> buf; + + { + uavcan::BitStream bits(buf); + uavcan::ScalarCodec codec(bits); + /* + * Coding + */ + if (0 > T::encode(obj, codec)) + { + std::cout << "Failed to encode" << std::endl; + return false; + } + + /* + * Validating the encoded bitstream + */ + const std::string result = bits.toString(); + if (result != reference_bit_string) + { + std::cout << "ENCODED VALUE DOESN'T MATCH THE REFERENCE:\nEXPECTED:\n" + << reference_bit_string << "\nACTUAL:\n" + << result << std::endl; + return false; + } + } + + /* + * Decoding back and comparing + */ + uavcan::BitStream bits(buf); + uavcan::ScalarCodec codec(bits); + + T decoded; + + if (0 > T::decode(decoded, codec)) + { + std::cout << "Failed to decode" << std::endl; + return false; + } + + if (decoded != obj) + { + std::cout << "DECODED OBJECT DOESN'T MATCH THE REFERENCE:\nEXPECTED:\n" + << obj << "\nACTUAL:\n" + << decoded << std::endl; + return false; + } + return true; +} + + +TEST(Dsdl, Union) +{ + using root_ns_a::UnionTest; + using root_ns_a::NestedInUnion; + + ASSERT_EQ(3, UnionTest::MinBitLen); + ASSERT_EQ(16, UnionTest::MaxBitLen); + ASSERT_EQ(13, NestedInUnion::MinBitLen); + ASSERT_EQ(13, NestedInUnion::MaxBitLen); + + UnionTest s; + + EXPECT_TRUE(validateYaml(s, "z: ")); + encodeDecodeValidate(s, "00000000"); + + s.to() = 16; + EXPECT_TRUE(validateYaml(s, "a: 16")); + EXPECT_TRUE(encodeDecodeValidate(s, "00110000")); + + s.to() = 31; + EXPECT_TRUE(validateYaml(s, "b: 31")); + EXPECT_TRUE(encodeDecodeValidate(s, "01011111")); + + s.to() = 256; + EXPECT_TRUE(validateYaml(s, "c: 256")); + EXPECT_TRUE(encodeDecodeValidate(s, "01100000 00000001")); + + s.to().push_back(true); + s.to().push_back(false); + s.to().push_back(true); + s.to().push_back(true); + s.to().push_back(false); + s.to().push_back(false); + s.to().push_back(true); + s.to().push_back(true); + s.to().push_back(true); + ASSERT_EQ(9, s.to().size()); + EXPECT_TRUE(validateYaml(s, "d: [1, 0, 1, 1, 0, 0, 1, 1, 1]")); + EXPECT_TRUE(encodeDecodeValidate(s, "10010011 01100111")); + + s.to().array[0] = 0; + s.to().array[1] = 1; + s.to().array[2] = 2; + s.to().array[3] = 3; + EXPECT_TRUE(validateYaml(s, "e: \n array: [0, 1, 2, 3]")); + EXPECT_TRUE(encodeDecodeValidate(s, "10100000 11011000")); +} diff --git a/libuavcan/test/dsdl_test/root_ns_a/Empty.uavcan b/libuavcan/test/dsdl_test/root_ns_a/Empty.uavcan new file mode 100644 index 0000000000..e69de29bb2 diff --git a/libuavcan/test/dsdl_test/root_ns_a/NestedInUnion.uavcan b/libuavcan/test/dsdl_test/root_ns_a/NestedInUnion.uavcan new file mode 100644 index 0000000000..ca6785ed66 --- /dev/null +++ b/libuavcan/test/dsdl_test/root_ns_a/NestedInUnion.uavcan @@ -0,0 +1,4 @@ +# This should be 13 bits long +void2 # 2 bits +uint2[4] array # 8 bits +void3 # 3 bits diff --git a/libuavcan/test/dsdl_test/root_ns_a/UnionTest.uavcan b/libuavcan/test/dsdl_test/root_ns_a/UnionTest.uavcan new file mode 100644 index 0000000000..b143a2735f --- /dev/null +++ b/libuavcan/test/dsdl_test/root_ns_a/UnionTest.uavcan @@ -0,0 +1,8 @@ +# Max bit len 16 bits +@union # 3 bits +Empty z +uint5 a +uint5 b # Conflicting with a +uint13 c +bool[<=9] d # 4 bit length field + 9 bit payload +NestedInUnion e