5 #include <gtest/gtest.h> 10 #include <uavcan/Timestamp.hpp> 11 #include <uavcan/protocol/param/GetSet.hpp> 12 #include <uavcan/protocol/GetTransportStats.hpp> 13 #include <uavcan/protocol/Panic.hpp> 14 #include <uavcan/protocol/RestartNode.hpp> 15 #include <uavcan/protocol/GlobalTimeSync.hpp> 16 #include <uavcan/protocol/DataTypeKind.hpp> 17 #include <uavcan/protocol/GetDataTypeInfo.hpp> 18 #include <uavcan/protocol/NodeStatus.hpp> 19 #include <uavcan/protocol/GetNodeInfo.hpp> 20 #include <uavcan/protocol/debug/LogMessage.hpp> 21 #include <uavcan/protocol/debug/KeyValue.hpp> 23 #include <root_ns_a/Deep.hpp> 24 #include <root_ns_a/UnionTest.hpp> 25 #include <root_ns_a/UnionTest4.hpp> 28 static bool validateYaml(
const T& obj,
const std::string& reference)
32 std::ostringstream os;
34 if (os.str() == reference)
40 std::cout <<
"INVALID YAML:\n" 48 <<
"\n===\n" << std::endl;
55 EXPECT_TRUE(
validateYaml(uavcan::protocol::GetNodeInfo::Response(),
61 " vendor_specific_status_code: 0\n" 62 "software_version: \n" 65 " optional_field_flags: 0\n" 68 "hardware_version: \n" 71 " unique_id: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]\n" 72 " certificate_of_authenticity: \"\"\n" 86 " bools: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]\n" 89 " bools: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]\n" 93 " bools: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]\n" 96 " bools: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]"));
100 template <
typename T>
111 if (0 > T::encode(obj, codec))
113 std::cout <<
"Failed to encode" << std::endl;
120 const std::string result = bits.toString();
121 if (result != reference_bit_string)
123 std::cout <<
"ENCODED VALUE DOESN'T MATCH THE REFERENCE:\nEXPECTED:\n" 124 << reference_bit_string <<
"\nACTUAL:\n" 125 << result << std::endl;
138 if (0 > T::decode(decoded, codec))
140 std::cout <<
"Failed to decode" << std::endl;
144 if (!decoded.isClose(obj))
146 std::cout <<
"DECODED OBJECT DOESN'T MATCH THE REFERENCE:\nEXPECTED:\n" 147 << obj <<
"\nACTUAL:\n" 148 << decoded << std::endl;
157 using root_ns_a::UnionTest;
158 using root_ns_a::NestedInUnion;
160 ASSERT_EQ(3, UnionTest::MinBitLen);
161 ASSERT_EQ(16, UnionTest::MaxBitLen);
162 ASSERT_EQ(13, NestedInUnion::MinBitLen);
163 ASSERT_EQ(13, NestedInUnion::MaxBitLen);
170 s.to<UnionTest::Tag::a>() = 16;
174 s.to<UnionTest::Tag::b>() = 31;
178 s.to<UnionTest::Tag::c>() = 256;
182 s.to<UnionTest::Tag::d>().push_back(
true);
183 s.to<UnionTest::Tag::d>().push_back(
false);
184 s.to<UnionTest::Tag::d>().push_back(
true);
185 s.to<UnionTest::Tag::d>().push_back(
true);
186 s.to<UnionTest::Tag::d>().push_back(
false);
187 s.to<UnionTest::Tag::d>().push_back(
false);
188 s.to<UnionTest::Tag::d>().push_back(
true);
189 s.to<UnionTest::Tag::d>().push_back(
true);
190 s.to<UnionTest::Tag::d>().push_back(
true);
191 ASSERT_EQ(9, s.to<UnionTest::Tag::d>().size());
192 EXPECT_TRUE(
validateYaml(s,
"d: [1, 0, 1, 1, 0, 0, 1, 1, 1]"));
195 s.to<UnionTest::Tag::e>().array[0] = 0;
196 s.to<UnionTest::Tag::e>().array[1] = 1;
197 s.to<UnionTest::Tag::e>().array[2] = 2;
198 s.to<UnionTest::Tag::e>().array[3] = 3;
199 EXPECT_TRUE(
validateYaml(s,
"e: \n array: [0, 1, 2, 3]"));
206 using root_ns_a::UnionTest4;
208 ASSERT_EQ(2, UnionTest4::MinBitLen);
209 ASSERT_EQ(8, UnionTest4::MaxBitLen);
218 ASSERT_EQ(1, UnionTest4::encode(s, sc_wr));
219 ASSERT_EQ(
"00000000", bs_wr.toString());
227 s.to<UnionTest4::Tag::third>() = 1U << 5U;
229 ASSERT_EQ(1, UnionTest4::encode(s, sc_wr));
230 ASSERT_EQ(
"10100000", bs_wr.toString());
235 TEST(Dsdl, ParamGetSetRequestUnion)
237 uavcan::protocol::param::GetSet::Request req;
243 req.value.to<uavcan::protocol::param::Value::Tag::string_value>() =
"abc";
247 "01100001 01100010 01100011 " 248 "00110001 00110010 00110011"));
253 " string_value: \"abc\"\n" 256 req.value.to<uavcan::protocol::param::Value::Tag::integer_value>() = 1;
259 "00000001 00000000 00000000 00000000 00000000 00000000 00000000 00000000 " 260 "00110001 00110010 00110011"));
264 TEST(Dsdl, ParamGetSetResponseUnion)
266 uavcan::protocol::param::GetSet::Response res;
268 res.value.to<uavcan::protocol::param::Value::Tag::string_value>() =
"abc";
269 res.default_value.to<uavcan::protocol::param::Value::Tag::string_value>();
274 "01100001 01100010 01100011 " 279 "00110001 00110010 00110011"));
281 res.value.to<uavcan::protocol::param::Value::Tag::boolean_value>() =
true;
282 res.default_value.to<uavcan::protocol::param::Value::Tag::boolean_value>();
291 "00110001 00110010 00110011"));
static bool validateYaml(const T &obj, const std::string &reference)
static OStream & instance()
static bool encodeDecodeValidate(const T &obj, const std::string &reference_bit_string)
static OStream & endl(OStream &stream)