dsdl_test.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*
  2. * Copyright (C) 2014 Pavel Kirienko <pavel.kirienko@gmail.com>
  3. */
  4. #if __GNUC__
  5. # pragma GCC diagnostic ignored "-Wfloat-equal"
  6. #endif
  7. #include <gtest/gtest.h>
  8. #include <uavcan/transport/transfer_buffer.hpp>
  9. #include <limits>
  10. #include <root_ns_a/EmptyService.hpp>
  11. #include <root_ns_a/EmptyMessage.hpp>
  12. #include <root_ns_a/NestedMessage.hpp>
  13. #include <root_ns_a/A.hpp>
  14. #include <root_ns_a/ReportBackSoldier.hpp>
  15. #include <root_ns_b/ServiceWithEmptyRequest.hpp>
  16. #include <root_ns_b/ServiceWithEmptyResponse.hpp>
  17. #include <root_ns_b/T.hpp>
  18. TEST(Dsdl, EmptyServices)
  19. {
  20. uavcan::StaticTransferBuffer<100> buf;
  21. uavcan::BitStream bs_wr(buf);
  22. uavcan::ScalarCodec sc_wr(bs_wr);
  23. root_ns_b::ServiceWithEmptyRequest::Request req;
  24. ASSERT_EQ(1, root_ns_b::ServiceWithEmptyRequest::Request::encode(req, sc_wr));
  25. ASSERT_EQ("", bs_wr.toString());
  26. root_ns_b::ServiceWithEmptyRequest::Response resp;
  27. ASSERT_EQ(1, root_ns_b::ServiceWithEmptyRequest::Response::encode(resp, sc_wr));
  28. ASSERT_EQ("", bs_wr.toString());
  29. resp.covariance.push_back(-2);
  30. resp.covariance.push_back(65504);
  31. root_ns_b::ServiceWithEmptyRequest::Response::encode(resp, sc_wr);
  32. ASSERT_EQ("00000000 11000000 11111111 01111011", bs_wr.toString());
  33. resp.covariance.push_back(42);
  34. resp.covariance[0] = 999;
  35. uavcan::BitStream bs_rd(buf);
  36. uavcan::ScalarCodec sc_rd(bs_rd);
  37. ASSERT_EQ(1, root_ns_b::ServiceWithEmptyRequest::Response::decode(resp, sc_rd));
  38. ASSERT_EQ(2, resp.covariance.size());
  39. ASSERT_EQ(-2, resp.covariance[0]);
  40. ASSERT_EQ(65504, resp.covariance[1]);
  41. }
  42. TEST(Dsdl, Signature)
  43. {
  44. ASSERT_EQ(0xe74617107a34aa9c, root_ns_a::EmptyService::getDataTypeSignature().get());
  45. ASSERT_STREQ("root_ns_a.EmptyService", root_ns_a::EmptyService::getDataTypeFullName());
  46. ASSERT_EQ(uavcan::DataTypeKindService, root_ns_a::EmptyService::DataTypeKind);
  47. ASSERT_EQ(0x99604d7066e0d713, root_ns_a::NestedMessage::getDataTypeSignature().get()); // Computed manually
  48. ASSERT_STREQ("root_ns_a.NestedMessage", root_ns_a::NestedMessage::getDataTypeFullName());
  49. ASSERT_EQ(uavcan::DataTypeKindMessage, root_ns_a::NestedMessage::DataTypeKind);
  50. }
  51. TEST(Dsdl, Operators)
  52. {
  53. {
  54. root_ns_a::EmptyService::Request a, b;
  55. ASSERT_TRUE(a == b);
  56. ASSERT_FALSE(a != b);
  57. }
  58. {
  59. root_ns_a::NestedMessage c, d;
  60. ASSERT_TRUE(c == d);
  61. ASSERT_FALSE(c != d);
  62. c.field = 1;
  63. ASSERT_FALSE(c == d);
  64. ASSERT_TRUE(c != d);
  65. }
  66. }
  67. TEST(Dsdl, CloseComparison)
  68. {
  69. root_ns_a::A first, second;
  70. ASSERT_TRUE(first == second);
  71. first.vector[1].vector[1] = std::numeric_limits<double>::epsilon();
  72. ASSERT_TRUE(first.isClose(second)); // Still close
  73. ASSERT_FALSE(first == second); // But not exactly
  74. first.vector[1].vector[1] = std::numeric_limits<float>::epsilon();
  75. ASSERT_FALSE(first.isClose(second)); // Nope
  76. ASSERT_FALSE(first == second); // Ditto
  77. }
  78. /*
  79. * This test assumes that it will be executed before other GDTR tests; otherwise it fails.
  80. * TODO: Probably it needs to be called directly from main()
  81. */
  82. //TEST(Dsdl, Registration)
  83. //{
  84. // using uavcan::GlobalDataTypeRegistry;
  85. // /*
  86. // * Descriptors
  87. // */
  88. // const uavcan::DataTypeDescriptor* desc = UAVCAN_NULLPTR;
  89. //
  90. // desc = GlobalDataTypeRegistry::instance().find(uavcan::DataTypeKindMessage, "root_ns_a.EmptyMessage");
  91. // ASSERT_TRUE(desc);
  92. // ASSERT_EQ(root_ns_a::EmptyMessage::DefaultDataTypeID, desc->getID());
  93. // ASSERT_EQ(root_ns_a::EmptyMessage::DataTypeKind, desc->getKind());
  94. // ASSERT_EQ(root_ns_a::EmptyMessage::getDataTypeSignature(), desc->getSignature());
  95. // ASSERT_STREQ(root_ns_a::EmptyMessage::getDataTypeFullName(), desc->getFullName());
  96. //
  97. // desc = GlobalDataTypeRegistry::instance().find(uavcan::DataTypeKindService, "root_ns_a.EmptyService");
  98. // ASSERT_TRUE(desc);
  99. // ASSERT_EQ(root_ns_a::EmptyService::DefaultDataTypeID, desc->getID());
  100. // ASSERT_EQ(root_ns_a::EmptyService::DataTypeKind, desc->getKind());
  101. // ASSERT_EQ(root_ns_a::EmptyService::getDataTypeSignature(), desc->getSignature());
  102. // ASSERT_STREQ(root_ns_a::EmptyService::getDataTypeFullName(), desc->getFullName());
  103. //
  104. // desc = GlobalDataTypeRegistry::instance().find(uavcan::DataTypeKindService, "root_ns_a.ReportBackSoldier");
  105. // ASSERT_TRUE(desc);
  106. // ASSERT_EQ(root_ns_a::ReportBackSoldier::DefaultDataTypeID, desc->getID());
  107. // ASSERT_EQ(root_ns_a::ReportBackSoldier::DataTypeKind, desc->getKind());
  108. // ASSERT_EQ(root_ns_a::ReportBackSoldier::getDataTypeSignature(), desc->getSignature());
  109. // ASSERT_STREQ(root_ns_a::ReportBackSoldier::getDataTypeFullName(), desc->getFullName());
  110. //
  111. // /*
  112. // * Mask
  113. // */
  114. // uavcan::DataTypeIDMask mask;
  115. //
  116. // GlobalDataTypeRegistry::instance().getDataTypeIDMask(uavcan::DataTypeKindMessage, mask);
  117. // ASSERT_TRUE(mask[8]);
  118. // mask[8] = false;
  119. //
  120. // GlobalDataTypeRegistry::instance().getDataTypeIDMask(uavcan::DataTypeKindService, mask);
  121. // ASSERT_TRUE(mask[1]);
  122. // ASSERT_TRUE(mask[3]);
  123. // mask[1] = mask[3] = false;
  124. //
  125. // /*
  126. // * Reset
  127. // */
  128. // GlobalDataTypeRegistry::instance().reset();
  129. // ASSERT_FALSE(GlobalDataTypeRegistry::instance().isFrozen());
  130. //}