BasicJsonDocument.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // ArduinoJson - arduinojson.org
  2. // Copyright Benoit Blanchon 2014-2020
  3. // MIT License
  4. #include <ArduinoJson.h>
  5. #include <stdlib.h> // malloc, free
  6. #include <catch.hpp>
  7. #include <sstream>
  8. using ARDUINOJSON_NAMESPACE::addPadding;
  9. using ARDUINOJSON_NAMESPACE::move;
  10. class SpyingAllocator {
  11. public:
  12. SpyingAllocator(const SpyingAllocator& src) : _log(src._log) {}
  13. SpyingAllocator(std::ostream& log) : _log(log) {}
  14. void* allocate(size_t n) {
  15. _log << "A" << n;
  16. return malloc(n);
  17. }
  18. void deallocate(void* p) {
  19. _log << (p ? "F" : "f");
  20. free(p);
  21. }
  22. private:
  23. SpyingAllocator& operator=(const SpyingAllocator& src);
  24. std::ostream& _log;
  25. };
  26. typedef BasicJsonDocument<SpyingAllocator> MyJsonDocument;
  27. TEST_CASE("BasicJsonDocument") {
  28. std::stringstream log;
  29. SECTION("Construct/Destruct") {
  30. { MyJsonDocument doc(4096, log); }
  31. REQUIRE(log.str() == "A4096F");
  32. }
  33. SECTION("Copy construct") {
  34. {
  35. MyJsonDocument doc1(4096, log);
  36. doc1.set(std::string("The size of this string is 32!!"));
  37. MyJsonDocument doc2(doc1);
  38. REQUIRE(doc1.as<std::string>() == "The size of this string is 32!!");
  39. REQUIRE(doc2.as<std::string>() == "The size of this string is 32!!");
  40. }
  41. REQUIRE(log.str() == "A4096A32FF");
  42. }
  43. SECTION("Move construct") {
  44. {
  45. MyJsonDocument doc1(4096, log);
  46. doc1.set(std::string("The size of this string is 32!!"));
  47. MyJsonDocument doc2(move(doc1));
  48. REQUIRE(doc2.as<std::string>() == "The size of this string is 32!!");
  49. #if ARDUINOJSON_HAS_RVALUE_REFERENCES
  50. REQUIRE(doc1.as<std::string>() == "null");
  51. REQUIRE(doc1.capacity() == 0);
  52. REQUIRE(doc2.capacity() == 4096);
  53. #endif
  54. }
  55. #if ARDUINOJSON_HAS_RVALUE_REFERENCES
  56. REQUIRE(log.str() == "A4096Ff");
  57. #else
  58. REQUIRE(log.str() == "A4096A32FF");
  59. #endif
  60. }
  61. SECTION("Copy assign") {
  62. {
  63. MyJsonDocument doc1(4096, log);
  64. doc1.set(std::string("The size of this string is 32!!"));
  65. MyJsonDocument doc2(8, log);
  66. doc2 = doc1;
  67. REQUIRE(doc1.as<std::string>() == "The size of this string is 32!!");
  68. REQUIRE(doc2.as<std::string>() == "The size of this string is 32!!");
  69. }
  70. REQUIRE(log.str() == "A4096A8FA32FF");
  71. }
  72. SECTION("Move assign") {
  73. {
  74. MyJsonDocument doc1(4096, log);
  75. doc1.set(std::string("The size of this string is 32!!"));
  76. MyJsonDocument doc2(8, log);
  77. doc2 = move(doc1);
  78. REQUIRE(doc2.as<std::string>() == "The size of this string is 32!!");
  79. #if ARDUINOJSON_HAS_RVALUE_REFERENCES
  80. REQUIRE(doc1.as<std::string>() == "null");
  81. REQUIRE(doc1.capacity() == 0);
  82. REQUIRE(doc2.capacity() == 4096);
  83. #endif
  84. }
  85. #if ARDUINOJSON_HAS_RVALUE_REFERENCES
  86. REQUIRE(log.str() == "A4096A8FFf");
  87. #else
  88. REQUIRE(log.str() == "A4096A8FA32FF");
  89. #endif
  90. }
  91. }