printTo.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. // Copyright Benoit Blanchon 2014-2017
  2. // MIT License
  3. //
  4. // Arduino JSON library
  5. // https://bblanchon.github.io/ArduinoJson/
  6. // If you like this project, please add a star!
  7. #include <ArduinoJson.h>
  8. #include <catch.hpp>
  9. #include <limits>
  10. void check(JsonVariant variant, const std::string &expected) {
  11. char buffer[256] = "";
  12. size_t returnValue = variant.printTo(buffer, sizeof(buffer));
  13. REQUIRE(expected == buffer);
  14. REQUIRE(expected.size() == returnValue);
  15. }
  16. TEST_CASE("JsonVariant::printTo()") {
  17. SECTION("Empty") {
  18. check(JsonVariant(), "");
  19. }
  20. SECTION("Null") {
  21. check(static_cast<char *>(0), "null");
  22. }
  23. SECTION("String") {
  24. check("hello", "\"hello\"");
  25. }
  26. SECTION("DoubleZero") {
  27. check(0.0, "0.00");
  28. }
  29. SECTION("DoubleDefaultDigits") {
  30. check(3.14159265358979323846, "3.14");
  31. }
  32. SECTION("DoubleFourDigits") {
  33. check(JsonVariant(3.14159265358979323846, 4), "3.1416");
  34. }
  35. SECTION("Infinity") {
  36. check(std::numeric_limits<double>::infinity(), "Infinity");
  37. }
  38. SECTION("MinusInfinity") {
  39. check(-std::numeric_limits<double>::infinity(), "-Infinity");
  40. }
  41. SECTION("SignalingNaN") {
  42. check(std::numeric_limits<double>::signaling_NaN(), "NaN");
  43. }
  44. SECTION("QuietNaN") {
  45. check(std::numeric_limits<double>::quiet_NaN(), "NaN");
  46. }
  47. SECTION("VeryBigPositiveDouble") {
  48. check(JsonVariant(3.14159265358979323846e42, 4), "3.1416e42");
  49. }
  50. SECTION("VeryBigNegativeDouble") {
  51. check(JsonVariant(-3.14159265358979323846e42, 4), "-3.1416e42");
  52. }
  53. SECTION("VerySmallPositiveDouble") {
  54. check(JsonVariant(3.14159265358979323846e-42, 4), "3.1416e-42");
  55. }
  56. SECTION("VerySmallNegativeDouble") {
  57. check(JsonVariant(-3.14159265358979323846e-42, 4), "-3.1416e-42");
  58. }
  59. SECTION("Integer") {
  60. check(42, "42");
  61. }
  62. SECTION("NegativeLong") {
  63. check(-42, "-42");
  64. }
  65. SECTION("UnsignedLong") {
  66. check(4294967295UL, "4294967295");
  67. }
  68. SECTION("True") {
  69. check(true, "true");
  70. }
  71. SECTION("OneFalse") {
  72. check(false, "false");
  73. }
  74. #if ARDUINOJSON_USE_LONG_LONG || ARDUINOJSON_USE_INT64
  75. SECTION("NegativeInt64") {
  76. check(-9223372036854775807 - 1, "-9223372036854775808");
  77. }
  78. SECTION("PositiveInt64") {
  79. check(9223372036854775807, "9223372036854775807");
  80. }
  81. SECTION("UInt64") {
  82. check(18446744073709551615, "18446744073709551615");
  83. }
  84. #endif
  85. }