BasicJsonDocument.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. // ArduinoJson - https://arduinojson.org
  2. // Copyright © 2014-2023, Benoit BLANCHON
  3. // MIT License
  4. #include <ArduinoJson.h>
  5. #include <stdlib.h> // malloc, free
  6. #include <catch.hpp>
  7. #include <sstream>
  8. #include <utility>
  9. class SpyingAllocator : public Allocator {
  10. public:
  11. virtual ~SpyingAllocator() {}
  12. void* allocate(size_t n) override {
  13. _log << "A" << n;
  14. return malloc(n);
  15. }
  16. void deallocate(void* p) override {
  17. _log << "F";
  18. free(p);
  19. }
  20. void* reallocate(void* ptr, size_t n) override {
  21. _log << "R" << n;
  22. return realloc(ptr, n);
  23. }
  24. std::string log() const {
  25. return _log.str();
  26. }
  27. private:
  28. std::ostringstream _log;
  29. };
  30. class ControllableAllocator : public Allocator {
  31. public:
  32. ControllableAllocator() : _enabled(true) {}
  33. virtual ~ControllableAllocator() {}
  34. void* allocate(size_t n) override {
  35. return _enabled ? malloc(n) : 0;
  36. }
  37. void deallocate(void* p) override {
  38. free(p);
  39. }
  40. void* reallocate(void* ptr, size_t n) override {
  41. return realloc(ptr, n);
  42. }
  43. void disable() {
  44. _enabled = false;
  45. }
  46. private:
  47. bool _enabled;
  48. };
  49. TEST_CASE("BasicJsonDocument") {
  50. SpyingAllocator spyingAllocator;
  51. ControllableAllocator controllableAllocator;
  52. SECTION("Construct/Destruct") {
  53. { BasicJsonDocument doc(4096, &spyingAllocator); }
  54. REQUIRE(spyingAllocator.log() == "A4096F");
  55. }
  56. SECTION("Copy construct") {
  57. {
  58. BasicJsonDocument doc1(4096, &spyingAllocator);
  59. doc1.set(std::string("The size of this string is 32!!"));
  60. BasicJsonDocument doc2(doc1);
  61. REQUIRE(doc1.as<std::string>() == "The size of this string is 32!!");
  62. REQUIRE(doc2.as<std::string>() == "The size of this string is 32!!");
  63. REQUIRE(doc2.capacity() == 4096);
  64. }
  65. REQUIRE(spyingAllocator.log() == "A4096A4096FF");
  66. }
  67. SECTION("Move construct") {
  68. {
  69. BasicJsonDocument doc1(4096, &spyingAllocator);
  70. doc1.set(std::string("The size of this string is 32!!"));
  71. BasicJsonDocument doc2(std::move(doc1));
  72. REQUIRE(doc2.as<std::string>() == "The size of this string is 32!!");
  73. REQUIRE(doc1.as<std::string>() == "null");
  74. REQUIRE(doc1.capacity() == 0);
  75. REQUIRE(doc2.capacity() == 4096);
  76. }
  77. REQUIRE(spyingAllocator.log() == "A4096F");
  78. }
  79. SECTION("Copy assign larger") {
  80. {
  81. BasicJsonDocument doc1(4096, &spyingAllocator);
  82. doc1.set(std::string("The size of this string is 32!!"));
  83. BasicJsonDocument doc2(8, &spyingAllocator);
  84. doc2 = doc1;
  85. REQUIRE(doc1.as<std::string>() == "The size of this string is 32!!");
  86. REQUIRE(doc2.as<std::string>() == "The size of this string is 32!!");
  87. REQUIRE(doc2.capacity() == 4096);
  88. }
  89. REQUIRE(spyingAllocator.log() == "A4096A8FA4096FF");
  90. }
  91. SECTION("Copy assign smaller") {
  92. {
  93. BasicJsonDocument doc1(1024, &spyingAllocator);
  94. doc1.set(std::string("The size of this string is 32!!"));
  95. BasicJsonDocument doc2(4096, &spyingAllocator);
  96. doc2 = doc1;
  97. REQUIRE(doc1.as<std::string>() == "The size of this string is 32!!");
  98. REQUIRE(doc2.as<std::string>() == "The size of this string is 32!!");
  99. REQUIRE(doc2.capacity() == 1024);
  100. }
  101. REQUIRE(spyingAllocator.log() == "A1024A4096FA1024FF");
  102. }
  103. SECTION("Copy assign same size") {
  104. {
  105. BasicJsonDocument doc1(1024, &spyingAllocator);
  106. doc1.set(std::string("The size of this string is 32!!"));
  107. BasicJsonDocument doc2(1024, &spyingAllocator);
  108. doc2 = doc1;
  109. REQUIRE(doc1.as<std::string>() == "The size of this string is 32!!");
  110. REQUIRE(doc2.as<std::string>() == "The size of this string is 32!!");
  111. REQUIRE(doc2.capacity() == 1024);
  112. }
  113. REQUIRE(spyingAllocator.log() == "A1024A1024FF");
  114. }
  115. SECTION("Move assign") {
  116. {
  117. BasicJsonDocument doc1(4096, &spyingAllocator);
  118. doc1.set(std::string("The size of this string is 32!!"));
  119. BasicJsonDocument doc2(8, &spyingAllocator);
  120. doc2 = std::move(doc1);
  121. REQUIRE(doc2.as<std::string>() == "The size of this string is 32!!");
  122. REQUIRE(doc1.as<std::string>() == "null");
  123. REQUIRE(doc1.capacity() == 0);
  124. REQUIRE(doc2.capacity() == 4096);
  125. }
  126. REQUIRE(spyingAllocator.log() == "A4096A8FF");
  127. }
  128. SECTION("garbageCollect()") {
  129. BasicJsonDocument doc(4096, &controllableAllocator);
  130. SECTION("when allocation succeeds") {
  131. deserializeJson(doc, "{\"blanket\":1,\"dancing\":2}");
  132. REQUIRE(doc.capacity() == 4096);
  133. REQUIRE(doc.memoryUsage() == JSON_OBJECT_SIZE(2) + 16);
  134. doc.remove("blanket");
  135. bool result = doc.garbageCollect();
  136. REQUIRE(result == true);
  137. REQUIRE(doc.memoryUsage() == JSON_OBJECT_SIZE(1) + 8);
  138. REQUIRE(doc.capacity() == 4096);
  139. REQUIRE(doc.as<std::string>() == "{\"dancing\":2}");
  140. }
  141. SECTION("when allocation fails") {
  142. deserializeJson(doc, "{\"blanket\":1,\"dancing\":2}");
  143. REQUIRE(doc.capacity() == 4096);
  144. REQUIRE(doc.memoryUsage() == JSON_OBJECT_SIZE(2) + 16);
  145. doc.remove("blanket");
  146. controllableAllocator.disable();
  147. bool result = doc.garbageCollect();
  148. REQUIRE(result == false);
  149. REQUIRE(doc.memoryUsage() == JSON_OBJECT_SIZE(2) + 16);
  150. REQUIRE(doc.capacity() == 4096);
  151. REQUIRE(doc.as<std::string>() == "{\"dancing\":2}");
  152. }
  153. }
  154. }