BasicJsonDocument.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. class ControllableAllocator {
  27. public:
  28. ControllableAllocator() : _enabled(true) {}
  29. void* allocate(size_t n) {
  30. return _enabled ? malloc(n) : 0;
  31. }
  32. void deallocate(void* p) {
  33. free(p);
  34. }
  35. void disable() {
  36. _enabled = false;
  37. }
  38. private:
  39. bool _enabled;
  40. };
  41. TEST_CASE("BasicJsonDocument") {
  42. std::stringstream log;
  43. SECTION("Construct/Destruct") {
  44. { BasicJsonDocument<SpyingAllocator> doc(4096, log); }
  45. REQUIRE(log.str() == "A4096F");
  46. }
  47. SECTION("Copy construct") {
  48. {
  49. BasicJsonDocument<SpyingAllocator> doc1(4096, log);
  50. doc1.set(std::string("The size of this string is 32!!"));
  51. BasicJsonDocument<SpyingAllocator> doc2(doc1);
  52. REQUIRE(doc1.as<std::string>() == "The size of this string is 32!!");
  53. REQUIRE(doc2.as<std::string>() == "The size of this string is 32!!");
  54. }
  55. REQUIRE(log.str() == "A4096A32FF");
  56. }
  57. SECTION("Move construct") {
  58. {
  59. BasicJsonDocument<SpyingAllocator> doc1(4096, log);
  60. doc1.set(std::string("The size of this string is 32!!"));
  61. BasicJsonDocument<SpyingAllocator> doc2(move(doc1));
  62. REQUIRE(doc2.as<std::string>() == "The size of this string is 32!!");
  63. #if ARDUINOJSON_HAS_RVALUE_REFERENCES
  64. REQUIRE(doc1.as<std::string>() == "null");
  65. REQUIRE(doc1.capacity() == 0);
  66. REQUIRE(doc2.capacity() == 4096);
  67. #endif
  68. }
  69. #if ARDUINOJSON_HAS_RVALUE_REFERENCES
  70. REQUIRE(log.str() == "A4096Ff");
  71. #else
  72. REQUIRE(log.str() == "A4096A32FF");
  73. #endif
  74. }
  75. SECTION("Copy assign") {
  76. {
  77. BasicJsonDocument<SpyingAllocator> doc1(4096, log);
  78. doc1.set(std::string("The size of this string is 32!!"));
  79. BasicJsonDocument<SpyingAllocator> doc2(8, log);
  80. doc2 = doc1;
  81. REQUIRE(doc1.as<std::string>() == "The size of this string is 32!!");
  82. REQUIRE(doc2.as<std::string>() == "The size of this string is 32!!");
  83. }
  84. REQUIRE(log.str() == "A4096A8FA32FF");
  85. }
  86. SECTION("Move assign") {
  87. {
  88. BasicJsonDocument<SpyingAllocator> doc1(4096, log);
  89. doc1.set(std::string("The size of this string is 32!!"));
  90. BasicJsonDocument<SpyingAllocator> doc2(8, log);
  91. doc2 = move(doc1);
  92. REQUIRE(doc2.as<std::string>() == "The size of this string is 32!!");
  93. #if ARDUINOJSON_HAS_RVALUE_REFERENCES
  94. REQUIRE(doc1.as<std::string>() == "null");
  95. REQUIRE(doc1.capacity() == 0);
  96. REQUIRE(doc2.capacity() == 4096);
  97. #endif
  98. }
  99. #if ARDUINOJSON_HAS_RVALUE_REFERENCES
  100. REQUIRE(log.str() == "A4096A8FFf");
  101. #else
  102. REQUIRE(log.str() == "A4096A8FA32FF");
  103. #endif
  104. }
  105. SECTION("garbageCollect()") {
  106. BasicJsonDocument<ControllableAllocator> doc(4096);
  107. SECTION("when allocation succeeds") {
  108. deserializeJson(doc, "{\"blanket\":1,\"dancing\":2}");
  109. REQUIRE(doc.capacity() == 4096);
  110. REQUIRE(doc.memoryUsage() == JSON_OBJECT_SIZE(2) + 16);
  111. doc.remove("blanket");
  112. bool result = doc.garbageCollect();
  113. REQUIRE(result == true);
  114. REQUIRE(doc.memoryUsage() == JSON_OBJECT_SIZE(1) + 8);
  115. REQUIRE(doc.capacity() == 4096);
  116. REQUIRE(doc.as<std::string>() == "{\"dancing\":2}");
  117. }
  118. SECTION("when allocation fails") {
  119. deserializeJson(doc, "{\"blanket\":1,\"dancing\":2}");
  120. REQUIRE(doc.capacity() == 4096);
  121. REQUIRE(doc.memoryUsage() == JSON_OBJECT_SIZE(2) + 16);
  122. doc.remove("blanket");
  123. doc.allocator().disable();
  124. bool result = doc.garbageCollect();
  125. REQUIRE(result == false);
  126. REQUIRE(doc.memoryUsage() == JSON_OBJECT_SIZE(2) + 16);
  127. REQUIRE(doc.capacity() == 4096);
  128. REQUIRE(doc.as<std::string>() == "{\"dancing\":2}");
  129. }
  130. }
  131. }