printTo.cpp 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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("Double") {
  27. check(3.1415927, "3.1415927");
  28. }
  29. SECTION("Integer") {
  30. check(42, "42");
  31. }
  32. SECTION("NegativeLong") {
  33. check(-42, "-42");
  34. }
  35. SECTION("UnsignedLong") {
  36. check(4294967295UL, "4294967295");
  37. }
  38. SECTION("True") {
  39. check(true, "true");
  40. }
  41. SECTION("OneFalse") {
  42. check(false, "false");
  43. }
  44. #if ARDUINOJSON_USE_LONG_LONG || ARDUINOJSON_USE_INT64
  45. SECTION("NegativeInt64") {
  46. check(-9223372036854775807 - 1, "-9223372036854775808");
  47. }
  48. SECTION("PositiveInt64") {
  49. check(9223372036854775807, "9223372036854775807");
  50. }
  51. SECTION("UInt64") {
  52. check(18446744073709551615U, "18446744073709551615");
  53. }
  54. #endif
  55. }