char_array_formatter.cpp 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /*
  2. * Copyright (C) 2014 Pavel Kirienko <pavel.kirienko@gmail.com>
  3. */
  4. #include <gtest/gtest.h>
  5. #include <uavcan/marshal/char_array_formatter.hpp>
  6. using uavcan::Array;
  7. using uavcan::ArrayModeDynamic;
  8. using uavcan::ArrayModeStatic;
  9. using uavcan::IntegerSpec;
  10. using uavcan::SignednessSigned;
  11. using uavcan::SignednessUnsigned;
  12. using uavcan::CastModeSaturate;
  13. using uavcan::CastModeTruncate;
  14. using uavcan::CharArrayFormatter;
  15. TEST(CharArrayFormatter, Basic)
  16. {
  17. typedef Array<IntegerSpec<8, SignednessUnsigned, CastModeSaturate>, ArrayModeDynamic, 45> A8;
  18. A8 a;
  19. CharArrayFormatter<A8> f(a);
  20. ASSERT_STREQ("", f.getArray().c_str());
  21. f.write("Don't %s.", "Panic");
  22. ASSERT_STREQ("Don't Panic.", f.getArray().c_str());
  23. f.write(" abc%idef ", 123);
  24. ASSERT_STREQ("Don't Panic. abc123def ", f.getArray().c_str());
  25. f.write("%g", 0.0);
  26. ASSERT_STREQ("Don't Panic. abc123def 0", f.getArray().c_str());
  27. a.clear();
  28. ASSERT_STREQ("", f.getArray().c_str());
  29. f.write("123456789");
  30. ASSERT_STREQ("123456789", f.getArray().c_str());
  31. }
  32. #if !defined(UAVCAN_CPP_VERSION) || !defined(UAVCAN_CPP11)
  33. # error UAVCAN_CPP_VERSION
  34. #endif
  35. #if UAVCAN_CPP_VERSION >= UAVCAN_CPP11
  36. TEST(CharArrayFormatter, Hardcore)
  37. {
  38. typedef Array<IntegerSpec<8, SignednessUnsigned, CastModeSaturate>, ArrayModeDynamic, 150> A8;
  39. A8 a;
  40. CharArrayFormatter<A8> f(a);
  41. f.write(
  42. "%% char='%*' double='%*' long='%*' unsigned long='%*' int='%s' long double='%*' bool='%*' const char='%*' %%",
  43. '%', -12.3456, -123456789123456789L, 987654321, -123456789, 0.000000001L, true, "Don't Panic.");
  44. static const std::string Reference =
  45. "% char='%' double='-12.3456' long='-123456789123456789' unsigned long='987654321' int='-123456789' "
  46. "long double='1e-09' bool='1' const char='Don't Pani"; // few chars truncated!
  47. ASSERT_STREQ(Reference.c_str(), f.getArray().c_str());
  48. a.clear();
  49. f.write("");
  50. ASSERT_STREQ("", f.getArray().c_str());
  51. f.write("%%"); // Nothing to format --> "%%" is not expanded
  52. ASSERT_STREQ("%%", f.getArray().c_str());
  53. f.write("%*", "Test", 123, true); // Extra args ignored
  54. ASSERT_STREQ("%%Test", f.getArray().c_str());
  55. f.write("%% %* %* %% %*", true); // Insufficient args are OK; second "%%" is not expanded
  56. ASSERT_STREQ("%%Test% 1 %* %% %*", f.getArray().c_str());
  57. }
  58. #endif