dsdl_uavcan_compilability.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2014 Pavel Kirienko <pavel.kirienko@gmail.com>
3  */
4 
5 #include <gtest/gtest.h>
6 
9 
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>
22 
23 #include <root_ns_a/Deep.hpp>
24 #include <root_ns_a/UnionTest.hpp>
25 #include <root_ns_a/UnionTest4.hpp>
26 
27 template <typename T>
28 static bool validateYaml(const T& obj, const std::string& reference)
29 {
30  uavcan::OStream::instance() << "Validating YAML:\n" << obj << "\n" << uavcan::OStream::endl;
31 
32  std::ostringstream os;
33  os << obj;
34  if (os.str() == reference)
35  {
36  return true;
37  }
38  else
39  {
40  std::cout << "INVALID YAML:\n"
41  << "EXPECTED:\n"
42  << "===\n"
43  << reference
44  << "\n===\n"
45  << "ACTUAL:\n"
46  << "\n===\n"
47  << os.str()
48  << "\n===\n" << std::endl;
49  return false;
50  }
51 }
52 
53 TEST(Dsdl, Streaming)
54 {
55  EXPECT_TRUE(validateYaml(uavcan::protocol::GetNodeInfo::Response(),
56  "status: \n"
57  " uptime_sec: 0\n"
58  " health: 0\n"
59  " mode: 0\n"
60  " sub_mode: 0\n"
61  " vendor_specific_status_code: 0\n"
62  "software_version: \n"
63  " major: 0\n"
64  " minor: 0\n"
65  " optional_field_flags: 0\n"
66  " vcs_commit: 0\n"
67  " image_crc: 0\n"
68  "hardware_version: \n"
69  " major: 0\n"
70  " minor: 0\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"
73  "name: \"\""));
74 
75  root_ns_a::Deep ps;
76  ps.a.resize(1);
77  EXPECT_TRUE(validateYaml(ps,
78  "c: 0\n"
79  "str: \"\"\n"
80  "a: \n"
81  " - \n"
82  " scalar: 0\n"
83  " vector: \n"
84  " - \n"
85  " vector: [0, 0]\n"
86  " bools: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]\n"
87  " - \n"
88  " vector: [0, 0]\n"
89  " bools: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]\n"
90  "b: \n"
91  " - \n"
92  " vector: [0, 0]\n"
93  " bools: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]\n"
94  " - \n"
95  " vector: [0, 0]\n"
96  " bools: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]"));
97 }
98 
99 
100 template <typename T>
101 static bool encodeDecodeValidate(const T& obj, const std::string& reference_bit_string)
102 {
104 
105  {
106  uavcan::BitStream bits(buf);
107  uavcan::ScalarCodec codec(bits);
108  /*
109  * Coding
110  */
111  if (0 > T::encode(obj, codec))
112  {
113  std::cout << "Failed to encode" << std::endl;
114  return false;
115  }
116 
117  /*
118  * Validating the encoded bitstream
119  */
120  const std::string result = bits.toString();
121  if (result != reference_bit_string)
122  {
123  std::cout << "ENCODED VALUE DOESN'T MATCH THE REFERENCE:\nEXPECTED:\n"
124  << reference_bit_string << "\nACTUAL:\n"
125  << result << std::endl;
126  return false;
127  }
128  }
129 
130  /*
131  * Decoding back and comparing
132  */
133  uavcan::BitStream bits(buf);
134  uavcan::ScalarCodec codec(bits);
135 
136  T decoded;
137 
138  if (0 > T::decode(decoded, codec))
139  {
140  std::cout << "Failed to decode" << std::endl;
141  return false;
142  }
143 
144  if (!decoded.isClose(obj))
145  {
146  std::cout << "DECODED OBJECT DOESN'T MATCH THE REFERENCE:\nEXPECTED:\n"
147  << obj << "\nACTUAL:\n"
148  << decoded << std::endl;
149  return false;
150  }
151  return true;
152 }
153 
154 
155 TEST(Dsdl, Union)
156 {
157  using root_ns_a::UnionTest;
158  using root_ns_a::NestedInUnion;
159 
160  ASSERT_EQ(3, UnionTest::MinBitLen);
161  ASSERT_EQ(16, UnionTest::MaxBitLen);
162  ASSERT_EQ(13, NestedInUnion::MinBitLen);
163  ASSERT_EQ(13, NestedInUnion::MaxBitLen);
164 
165  UnionTest s;
166 
167  EXPECT_TRUE(validateYaml(s, "z: "));
168  encodeDecodeValidate(s, "00000000");
169 
170  s.to<UnionTest::Tag::a>() = 16;
171  EXPECT_TRUE(validateYaml(s, "a: 16"));
172  EXPECT_TRUE(encodeDecodeValidate(s, "00110000"));
173 
174  s.to<UnionTest::Tag::b>() = 31;
175  EXPECT_TRUE(validateYaml(s, "b: 31"));
176  EXPECT_TRUE(encodeDecodeValidate(s, "01011111"));
177 
178  s.to<UnionTest::Tag::c>() = 256;
179  EXPECT_TRUE(validateYaml(s, "c: 256"));
180  EXPECT_TRUE(encodeDecodeValidate(s, "01100000 00000001"));
181 
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]"));
193  EXPECT_TRUE(encodeDecodeValidate(s, "10010011 01100111"));
194 
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]"));
200  EXPECT_TRUE(encodeDecodeValidate(s, "10100000 11011000"));
201 }
202 
203 
204 TEST(Dsdl, UnionTagWidth)
205 {
206  using root_ns_a::UnionTest4;
207 
208  ASSERT_EQ(2, UnionTest4::MinBitLen);
209  ASSERT_EQ(8, UnionTest4::MaxBitLen);
210 
211  UnionTest4 s;
212 
213  {
215  uavcan::BitStream bs_wr(buf);
216  uavcan::ScalarCodec sc_wr(bs_wr);
217 
218  ASSERT_EQ(1, UnionTest4::encode(s, sc_wr));
219  ASSERT_EQ("00000000", bs_wr.toString());
220  }
221 
222  {
224  uavcan::BitStream bs_wr(buf);
225  uavcan::ScalarCodec sc_wr(bs_wr);
226 
227  s.to<UnionTest4::Tag::third>() = 1U << 5U; // 32, 0b100000
228 
229  ASSERT_EQ(1, UnionTest4::encode(s, sc_wr));
230  ASSERT_EQ("10100000", bs_wr.toString());
231  }
232 }
233 
234 
235 TEST(Dsdl, ParamGetSetRequestUnion)
236 {
237  uavcan::protocol::param::GetSet::Request req;
238 
239  req.index = 8191;
240  req.name = "123"; // 49, 50, 51 // 00110001, 00110010, 00110011
241  EXPECT_TRUE(encodeDecodeValidate(req, "11111111 11111000 00110001 00110010 00110011"));
242 
243  req.value.to<uavcan::protocol::param::Value::Tag::string_value>() = "abc"; // 01100001, 01100010, 01100011
244  EXPECT_TRUE(encodeDecodeValidate(req,
245  "11111111 11111100 " // Index, Union tag
246  "00000011 " // Array length
247  "01100001 01100010 01100011 " // Payload
248  "00110001 00110010 00110011")); // Name
249 
250  EXPECT_TRUE(validateYaml(req,
251  "index: 8191\n"
252  "value: \n"
253  " string_value: \"abc\"\n"
254  "name: \"123\""));
255 
256  req.value.to<uavcan::protocol::param::Value::Tag::integer_value>() = 1;
257  EXPECT_TRUE(encodeDecodeValidate(req,
258  "11111111 11111001 " // Index, Union tag
259  "00000001 00000000 00000000 00000000 00000000 00000000 00000000 00000000 " // Payload
260  "00110001 00110010 00110011")); // Name
261 }
262 
263 
264 TEST(Dsdl, ParamGetSetResponseUnion)
265 {
266  uavcan::protocol::param::GetSet::Response res;
267 
268  res.value.to<uavcan::protocol::param::Value::Tag::string_value>() = "abc";
269  res.default_value.to<uavcan::protocol::param::Value::Tag::string_value>(); // Empty
270  res.name = "123";
271  EXPECT_TRUE(encodeDecodeValidate(res,
272  "00000100 " // Value union tag
273  "00000011 " // Value array length
274  "01100001 01100010 01100011 " // Value array payload
275  "00000100 " // Default union tag
276  "00000000 " // Default array length
277  "00000000 " // Max value tag
278  "00000000 " // Min value tag
279  "00110001 00110010 00110011")); // Name
280 
281  res.value.to<uavcan::protocol::param::Value::Tag::boolean_value>() = true;
282  res.default_value.to<uavcan::protocol::param::Value::Tag::boolean_value>(); // False
283  res.name = "123";
284  EXPECT_TRUE(encodeDecodeValidate(res,
285  "00000011 " // Value union tag
286  "00000001 " // Value
287  "00000011 " // Default union tag
288  "00000000 " // Default value
289  "00000000 " // Max value tag
290  "00000000 " // Min value tag
291  "00110001 00110010 00110011")); // Name
292 }
uavcan::BitStream
Definition: bit_stream.hpp:31
TEST
TEST(Dsdl, Streaming)
Definition: dsdl_uavcan_compilability.cpp:53
uavcan::OStream::instance
static OStream & instance()
Definition: ostream.hpp:27
uavcan::OStream::endl
static OStream & endl(OStream &stream)
Definition: ostream.hpp:33
uavcan::StaticTransferBuffer
Definition: transfer_buffer.hpp:50
validateYaml
static bool validateYaml(const T &obj, const std::string &reference)
Definition: dsdl_uavcan_compilability.cpp:28
pyuavcan_v0.dsdl.signature.s
s
Definition: signature.py:73
transfer_buffer.hpp
ostream.hpp
uavcan::ScalarCodec
Definition: scalar_codec.hpp:20
encodeDecodeValidate
static bool encodeDecodeValidate(const T &obj, const std::string &reference_bit_string)
Definition: dsdl_uavcan_compilability.cpp:101


uavcan_communicator
Author(s):
autogenerated on Fri Dec 13 2024 03:10:02