transfer_test_helpers.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*
  2. * Copyright (C) 2014 Pavel Kirienko <pavel.kirienko@gmail.com>
  3. */
  4. #include <gtest/gtest.h>
  5. #include "transfer_test_helpers.hpp"
  6. #include "../clock.hpp"
  7. TEST(TransferTestHelpers, Transfer)
  8. {
  9. uavcan::PoolAllocator<uavcan::MemPoolBlockSize * 8, uavcan::MemPoolBlockSize> pool;
  10. uavcan::TransferBufferManager mgr(128, pool);
  11. uavcan::TransferBufferAccessor tba(mgr, uavcan::TransferBufferManagerKey(0, uavcan::TransferTypeMessageBroadcast));
  12. uavcan::RxFrame frame(uavcan::Frame(123, uavcan::TransferTypeMessageBroadcast, 1, 0, 0),
  13. uavcan::MonotonicTime(), uavcan::UtcTime(), 0);
  14. frame.setEndOfTransfer(true);
  15. uavcan::MultiFrameIncomingTransfer mfit(tsMono(10), tsUtc(1000), frame, tba);
  16. // Filling the buffer with data
  17. static const std::string TEST_DATA = "Kaneda! What do you see? Kaneda! What do you see? Kaneda! Kaneda!!!";
  18. ASSERT_TRUE(tba.create());
  19. ASSERT_EQ(TEST_DATA.length(), tba.access()->write(0, reinterpret_cast<const uint8_t*>(TEST_DATA.c_str()),
  20. unsigned(TEST_DATA.length())));
  21. // Reading back
  22. const Transfer transfer(mfit, uavcan::DataTypeDescriptor());
  23. ASSERT_EQ(TEST_DATA, transfer.payload);
  24. }
  25. TEST(TransferTestHelpers, MFTSerialization)
  26. {
  27. uavcan::DataTypeDescriptor type(uavcan::DataTypeKindMessage, 123, uavcan::DataTypeSignature(123456789), "Foo");
  28. static const std::string DATA = "To go wrong in one's own way is better than to go right in someone else's.";
  29. const Transfer transfer(1, 100000, 10,
  30. uavcan::TransferTypeServiceRequest, 2, 42, 127, DATA, type);
  31. const std::vector<uavcan::RxFrame> ser = serializeTransfer(transfer);
  32. std::cout << "Serialized transfer:\n";
  33. for (std::vector<uavcan::RxFrame>::const_iterator it = ser.begin(); it != ser.end(); ++it)
  34. {
  35. std::cout << "\t" << it->toString() << "\n";
  36. }
  37. for (std::vector<uavcan::RxFrame>::const_iterator it = ser.begin(); it != ser.end(); ++it)
  38. {
  39. std::cout << "\t'";
  40. for (unsigned i = 0; i < it->getPayloadLen(); i++)
  41. {
  42. uint8_t ch = it->getPayloadPtr()[i];
  43. if (ch < 0x20 || ch > 0x7E)
  44. {
  45. ch = '.';
  46. }
  47. std::cout << static_cast<char>(ch);
  48. }
  49. std::cout << "'\n";
  50. }
  51. std::cout << std::flush;
  52. }
  53. TEST(TransferTestHelpers, SFTSerialization)
  54. {
  55. uavcan::DataTypeDescriptor type(uavcan::DataTypeKindMessage, 123, uavcan::DataTypeSignature(123456789), "Foo");
  56. {
  57. const Transfer transfer(1, 100000, 10,
  58. uavcan::TransferTypeMessageBroadcast, 7, 42, 0, "Nvrfrgt", type);
  59. const std::vector<uavcan::RxFrame> ser = serializeTransfer(transfer);
  60. ASSERT_EQ(1, ser.size());
  61. std::cout << "Serialized transfer:\n\t" << ser[0].toString() << "\n";
  62. }
  63. {
  64. const Transfer transfer(1, 100000, 11,
  65. uavcan::TransferTypeServiceRequest, 7, 42, 127, "7-chars", type);
  66. const std::vector<uavcan::RxFrame> ser = serializeTransfer(transfer);
  67. ASSERT_EQ(1, ser.size());
  68. std::cout << "Serialized transfer:\n\t" << ser[0].toString() << "\n";
  69. }
  70. {
  71. const Transfer transfer(1, 100000, 12,
  72. uavcan::TransferTypeMessageBroadcast, 7, 42, 0, "", type);
  73. const std::vector<uavcan::RxFrame> ser = serializeTransfer(transfer);
  74. ASSERT_EQ(1, ser.size());
  75. std::cout << "Serialized transfer:\n\t" << ser[0].toString() << "\n";
  76. }
  77. {
  78. const Transfer transfer(1, 100000, 13,
  79. uavcan::TransferTypeServiceResponse, 7, 42, 127, "", type);
  80. const std::vector<uavcan::RxFrame> ser = serializeTransfer(transfer);
  81. ASSERT_EQ(1, ser.size());
  82. std::cout << "Serialized transfer:\n\t" << ser[0].toString() << "\n";
  83. }
  84. }