/* * Copyright (C) 2014 Pavel Kirienko */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include 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) { return true; } else { std::cout << "INVALID YAML:\n" << "EXPECTED:\n" << "===\n" << reference << "\n===\n" << "ACTUAL:\n" << "\n===\n" << os.str() << "\n===\n" << std::endl; return false; } } TEST(Dsdl, Streaming) { EXPECT_TRUE(validateYaml(uavcan::protocol::GetNodeInfo::Response(), "status: \n" " uptime_sec: 0\n" " health: 0\n" " mode: 0\n" " sub_mode: 0\n" " vendor_specific_status_code: 0\n" "software_version: \n" " major: 0\n" " minor: 0\n" " optional_field_flags: 0\n" " vcs_commit: 0\n" " image_crc: 0\n" "hardware_version: \n" " major: 0\n" " minor: 0\n" " unique_id: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]\n" " certificate_of_authenticity: \"\"\n" "name: \"\"")); root_ns_a::Deep ps; ps.a.resize(1); EXPECT_TRUE(validateYaml(ps, "c: 0\n" "str: \"\"\n" "a: \n" " - \n" " scalar: 0\n" " vector: \n" " - \n" " vector: [0, 0]\n" " bools: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]\n" " - \n" " vector: [0, 0]\n" " bools: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]\n" "b: \n" " - \n" " vector: [0, 0]\n" " bools: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]\n" " - \n" " 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")); }