printTo.cpp 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. // ArduinoJson - arduinojson.org
  2. // Copyright Benoit Blanchon 2014-2023
  3. // MIT License
  4. #include <ArduinoJson.h>
  5. #include <catch.hpp>
  6. #include <limits>
  7. void check(JsonVariant variant, const std::string &expected) {
  8. char buffer[256] = "";
  9. size_t returnValue = variant.printTo(buffer, sizeof(buffer));
  10. REQUIRE(expected == buffer);
  11. REQUIRE(expected.size() == returnValue);
  12. }
  13. TEST_CASE("JsonVariant::printTo()") {
  14. SECTION("Empty") {
  15. check(JsonVariant(), "");
  16. }
  17. SECTION("Null") {
  18. check(static_cast<char *>(0), "null");
  19. }
  20. SECTION("String") {
  21. check("hello", "\"hello\"");
  22. }
  23. SECTION("Double") {
  24. check(3.1415927, "3.1415927");
  25. }
  26. SECTION("Integer") {
  27. check(42, "42");
  28. }
  29. SECTION("NegativeLong") {
  30. check(-42, "-42");
  31. }
  32. SECTION("UnsignedLong") {
  33. check(4294967295UL, "4294967295");
  34. }
  35. SECTION("True") {
  36. check(true, "true");
  37. }
  38. SECTION("OneFalse") {
  39. check(false, "false");
  40. }
  41. #if ARDUINOJSON_USE_LONG_LONG || ARDUINOJSON_USE_INT64
  42. SECTION("NegativeInt64") {
  43. check(-9223372036854775807 - 1, "-9223372036854775808");
  44. }
  45. SECTION("PositiveInt64") {
  46. check(9223372036854775807, "9223372036854775807");
  47. }
  48. SECTION("UInt64") {
  49. check(18446744073709551615U, "18446744073709551615");
  50. }
  51. #endif
  52. }