dsdl_uavcan_compilability.cpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. /*
  2. * Copyright (C) 2014 Pavel Kirienko <pavel.kirienko@gmail.com>
  3. */
  4. #include <gtest/gtest.h>
  5. #include <uavcan/helpers/ostream.hpp>
  6. #include <uavcan/transport/transfer_buffer.hpp>
  7. #include <uavcan/Timestamp.hpp>
  8. #include <uavcan/protocol/param/GetSet.hpp>
  9. #include <uavcan/protocol/GetTransportStats.hpp>
  10. #include <uavcan/protocol/Panic.hpp>
  11. #include <uavcan/protocol/RestartNode.hpp>
  12. #include <uavcan/protocol/GlobalTimeSync.hpp>
  13. #include <uavcan/protocol/DataTypeKind.hpp>
  14. #include <uavcan/protocol/GetDataTypeInfo.hpp>
  15. #include <uavcan/protocol/NodeStatus.hpp>
  16. #include <uavcan/protocol/GetNodeInfo.hpp>
  17. #include <uavcan/protocol/debug/LogMessage.hpp>
  18. #include <uavcan/protocol/debug/KeyValue.hpp>
  19. #include <root_ns_a/Deep.hpp>
  20. #include <root_ns_a/UnionTest.hpp>
  21. template <typename T>
  22. static bool validateYaml(const T& obj, const std::string& reference)
  23. {
  24. uavcan::OStream::instance() << "Validating YAML:\n" << obj << "\n" << uavcan::OStream::endl;
  25. std::ostringstream os;
  26. os << obj;
  27. if (os.str() == reference)
  28. {
  29. return true;
  30. }
  31. else
  32. {
  33. std::cout << "INVALID YAML:\n"
  34. << "EXPECTED:\n"
  35. << "===\n"
  36. << reference
  37. << "\n===\n"
  38. << "ACTUAL:\n"
  39. << "\n===\n"
  40. << os.str()
  41. << "\n===\n" << std::endl;
  42. return false;
  43. }
  44. }
  45. TEST(Dsdl, Streaming)
  46. {
  47. EXPECT_TRUE(validateYaml(uavcan::protocol::GetNodeInfo::Response(),
  48. "status: \n"
  49. " uptime_sec: 0\n"
  50. " health: 0\n"
  51. " mode: 0\n"
  52. " sub_mode: 0\n"
  53. " vendor_specific_status_code: 0\n"
  54. "software_version: \n"
  55. " major: 0\n"
  56. " minor: 0\n"
  57. " optional_field_flags: 0\n"
  58. " vcs_commit: 0\n"
  59. " image_crc: 0\n"
  60. "hardware_version: \n"
  61. " major: 0\n"
  62. " minor: 0\n"
  63. " unique_id: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]\n"
  64. " certificate_of_authenticity: \"\"\n"
  65. "name: \"\""));
  66. root_ns_a::Deep ps;
  67. ps.a.resize(1);
  68. EXPECT_TRUE(validateYaml(ps,
  69. "c: 0\n"
  70. "str: \"\"\n"
  71. "a: \n"
  72. " - \n"
  73. " scalar: 0\n"
  74. " vector: \n"
  75. " - \n"
  76. " vector: [0, 0]\n"
  77. " bools: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]\n"
  78. " - \n"
  79. " vector: [0, 0]\n"
  80. " bools: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]\n"
  81. "b: \n"
  82. " - \n"
  83. " vector: [0, 0]\n"
  84. " bools: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]\n"
  85. " - \n"
  86. " vector: [0, 0]\n"
  87. " bools: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]"));
  88. }
  89. template <typename T>
  90. static bool encodeDecodeValidate(const T& obj, const std::string& reference_bit_string)
  91. {
  92. uavcan::StaticTransferBuffer<256> buf;
  93. {
  94. uavcan::BitStream bits(buf);
  95. uavcan::ScalarCodec codec(bits);
  96. /*
  97. * Coding
  98. */
  99. if (0 > T::encode(obj, codec))
  100. {
  101. std::cout << "Failed to encode" << std::endl;
  102. return false;
  103. }
  104. /*
  105. * Validating the encoded bitstream
  106. */
  107. const std::string result = bits.toString();
  108. if (result != reference_bit_string)
  109. {
  110. std::cout << "ENCODED VALUE DOESN'T MATCH THE REFERENCE:\nEXPECTED:\n"
  111. << reference_bit_string << "\nACTUAL:\n"
  112. << result << std::endl;
  113. return false;
  114. }
  115. }
  116. /*
  117. * Decoding back and comparing
  118. */
  119. uavcan::BitStream bits(buf);
  120. uavcan::ScalarCodec codec(bits);
  121. T decoded;
  122. if (0 > T::decode(decoded, codec))
  123. {
  124. std::cout << "Failed to decode" << std::endl;
  125. return false;
  126. }
  127. if (!decoded.isClose(obj))
  128. {
  129. std::cout << "DECODED OBJECT DOESN'T MATCH THE REFERENCE:\nEXPECTED:\n"
  130. << obj << "\nACTUAL:\n"
  131. << decoded << std::endl;
  132. return false;
  133. }
  134. return true;
  135. }
  136. TEST(Dsdl, Union)
  137. {
  138. using root_ns_a::UnionTest;
  139. using root_ns_a::NestedInUnion;
  140. ASSERT_EQ(3, UnionTest::MinBitLen);
  141. ASSERT_EQ(16, UnionTest::MaxBitLen);
  142. ASSERT_EQ(13, NestedInUnion::MinBitLen);
  143. ASSERT_EQ(13, NestedInUnion::MaxBitLen);
  144. UnionTest s;
  145. EXPECT_TRUE(validateYaml(s, "z: "));
  146. encodeDecodeValidate(s, "00000000");
  147. s.to<UnionTest::Tag::a>() = 16;
  148. EXPECT_TRUE(validateYaml(s, "a: 16"));
  149. EXPECT_TRUE(encodeDecodeValidate(s, "00110000"));
  150. s.to<UnionTest::Tag::b>() = 31;
  151. EXPECT_TRUE(validateYaml(s, "b: 31"));
  152. EXPECT_TRUE(encodeDecodeValidate(s, "01011111"));
  153. s.to<UnionTest::Tag::c>() = 256;
  154. EXPECT_TRUE(validateYaml(s, "c: 256"));
  155. EXPECT_TRUE(encodeDecodeValidate(s, "01100000 00000001"));
  156. s.to<UnionTest::Tag::d>().push_back(true);
  157. s.to<UnionTest::Tag::d>().push_back(false);
  158. s.to<UnionTest::Tag::d>().push_back(true);
  159. s.to<UnionTest::Tag::d>().push_back(true);
  160. s.to<UnionTest::Tag::d>().push_back(false);
  161. s.to<UnionTest::Tag::d>().push_back(false);
  162. s.to<UnionTest::Tag::d>().push_back(true);
  163. s.to<UnionTest::Tag::d>().push_back(true);
  164. s.to<UnionTest::Tag::d>().push_back(true);
  165. ASSERT_EQ(9, s.to<UnionTest::Tag::d>().size());
  166. EXPECT_TRUE(validateYaml(s, "d: [1, 0, 1, 1, 0, 0, 1, 1, 1]"));
  167. EXPECT_TRUE(encodeDecodeValidate(s, "10010011 01100111"));
  168. s.to<UnionTest::Tag::e>().array[0] = 0;
  169. s.to<UnionTest::Tag::e>().array[1] = 1;
  170. s.to<UnionTest::Tag::e>().array[2] = 2;
  171. s.to<UnionTest::Tag::e>().array[3] = 3;
  172. EXPECT_TRUE(validateYaml(s, "e: \n array: [0, 1, 2, 3]"));
  173. EXPECT_TRUE(encodeDecodeValidate(s, "10100000 11011000"));
  174. }
  175. TEST(Dsdl, ParamGetSetRequestUnion)
  176. {
  177. uavcan::protocol::param::GetSet::Request req;
  178. req.index = 8191;
  179. req.name = "123"; // 49, 50, 51 // 00110001, 00110010, 00110011
  180. EXPECT_TRUE(encodeDecodeValidate(req, "11111111 11111000 00110001 00110010 00110011"));
  181. req.value.to<uavcan::protocol::param::Value::Tag::string_value>() = "abc"; // 01100001, 01100010, 01100011
  182. EXPECT_TRUE(encodeDecodeValidate(req,
  183. "11111111 11111100 " // Index, Union tag
  184. "00000011 " // Array length
  185. "01100001 01100010 01100011 " // Payload
  186. "00110001 00110010 00110011")); // Name
  187. EXPECT_TRUE(validateYaml(req,
  188. "index: 8191\n"
  189. "value: \n"
  190. " string_value: \"abc\"\n"
  191. "name: \"123\""));
  192. req.value.to<uavcan::protocol::param::Value::Tag::integer_value>() = 1;
  193. EXPECT_TRUE(encodeDecodeValidate(req,
  194. "11111111 11111001 " // Index, Union tag
  195. "00000001 00000000 00000000 00000000 00000000 00000000 00000000 00000000 " // Payload
  196. "00110001 00110010 00110011")); // Name
  197. }
  198. TEST(Dsdl, ParamGetSetResponseUnion)
  199. {
  200. uavcan::protocol::param::GetSet::Response res;
  201. res.value.to<uavcan::protocol::param::Value::Tag::string_value>() = "abc";
  202. res.default_value.to<uavcan::protocol::param::Value::Tag::string_value>(); // Empty
  203. res.name = "123";
  204. EXPECT_TRUE(encodeDecodeValidate(res,
  205. "00000100 " // Value union tag
  206. "00000011 " // Value array length
  207. "01100001 01100010 01100011 " // Value array payload
  208. "00000100 " // Default union tag
  209. "00000000 " // Default array length
  210. "00000000 " // Max value tag
  211. "00000000 " // Min value tag
  212. "00110001 00110010 00110011")); // Name
  213. res.value.to<uavcan::protocol::param::Value::Tag::boolean_value>() = true;
  214. res.default_value.to<uavcan::protocol::param::Value::Tag::boolean_value>(); // False
  215. res.name = "123";
  216. EXPECT_TRUE(encodeDecodeValidate(res,
  217. "00000011 " // Value union tag
  218. "00000001 " // Value
  219. "00000011 " // Default union tag
  220. "00000000 " // Default value
  221. "00000000 " // Max value tag
  222. "00000000 " // Min value tag
  223. "00110001 00110010 00110011")); // Name
  224. }