BasicJsonDocument.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. // ArduinoJson - https://arduinojson.org
  2. // Copyright © 2014-2022, 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. using ARDUINOJSON_NAMESPACE::addPadding;
  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 << "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. REQUIRE(doc2.capacity() == 4096);
  55. }
  56. REQUIRE(log.str() == "A4096A4096FF");
  57. }
  58. SECTION("Move construct") {
  59. {
  60. BasicJsonDocument<SpyingAllocator> doc1(4096, log);
  61. doc1.set(std::string("The size of this string is 32!!"));
  62. BasicJsonDocument<SpyingAllocator> doc2(std::move(doc1));
  63. REQUIRE(doc2.as<std::string>() == "The size of this string is 32!!");
  64. REQUIRE(doc1.as<std::string>() == "null");
  65. REQUIRE(doc1.capacity() == 0);
  66. REQUIRE(doc2.capacity() == 4096);
  67. }
  68. REQUIRE(log.str() == "A4096F");
  69. }
  70. SECTION("Copy assign larger") {
  71. {
  72. BasicJsonDocument<SpyingAllocator> doc1(4096, log);
  73. doc1.set(std::string("The size of this string is 32!!"));
  74. BasicJsonDocument<SpyingAllocator> doc2(8, log);
  75. doc2 = doc1;
  76. REQUIRE(doc1.as<std::string>() == "The size of this string is 32!!");
  77. REQUIRE(doc2.as<std::string>() == "The size of this string is 32!!");
  78. REQUIRE(doc2.capacity() == 4096);
  79. }
  80. REQUIRE(log.str() == "A4096A8FA4096FF");
  81. }
  82. SECTION("Copy assign smaller") {
  83. {
  84. BasicJsonDocument<SpyingAllocator> doc1(1024, log);
  85. doc1.set(std::string("The size of this string is 32!!"));
  86. BasicJsonDocument<SpyingAllocator> doc2(4096, log);
  87. doc2 = doc1;
  88. REQUIRE(doc1.as<std::string>() == "The size of this string is 32!!");
  89. REQUIRE(doc2.as<std::string>() == "The size of this string is 32!!");
  90. REQUIRE(doc2.capacity() == 1024);
  91. }
  92. REQUIRE(log.str() == "A1024A4096FA1024FF");
  93. }
  94. SECTION("Copy assign same size") {
  95. {
  96. BasicJsonDocument<SpyingAllocator> doc1(1024, log);
  97. doc1.set(std::string("The size of this string is 32!!"));
  98. BasicJsonDocument<SpyingAllocator> doc2(1024, log);
  99. doc2 = doc1;
  100. REQUIRE(doc1.as<std::string>() == "The size of this string is 32!!");
  101. REQUIRE(doc2.as<std::string>() == "The size of this string is 32!!");
  102. REQUIRE(doc2.capacity() == 1024);
  103. }
  104. REQUIRE(log.str() == "A1024A1024FF");
  105. }
  106. SECTION("Move assign") {
  107. {
  108. BasicJsonDocument<SpyingAllocator> doc1(4096, log);
  109. doc1.set(std::string("The size of this string is 32!!"));
  110. BasicJsonDocument<SpyingAllocator> doc2(8, log);
  111. doc2 = std::move(doc1);
  112. REQUIRE(doc2.as<std::string>() == "The size of this string is 32!!");
  113. REQUIRE(doc1.as<std::string>() == "null");
  114. REQUIRE(doc1.capacity() == 0);
  115. REQUIRE(doc2.capacity() == 4096);
  116. }
  117. REQUIRE(log.str() == "A4096A8FF");
  118. }
  119. SECTION("garbageCollect()") {
  120. BasicJsonDocument<ControllableAllocator> doc(4096);
  121. SECTION("when allocation succeeds") {
  122. deserializeJson(doc, "{\"blanket\":1,\"dancing\":2}");
  123. REQUIRE(doc.capacity() == 4096);
  124. REQUIRE(doc.memoryUsage() == JSON_OBJECT_SIZE(2) + 16);
  125. doc.remove("blanket");
  126. bool result = doc.garbageCollect();
  127. REQUIRE(result == true);
  128. REQUIRE(doc.memoryUsage() == JSON_OBJECT_SIZE(1) + 8);
  129. REQUIRE(doc.capacity() == 4096);
  130. REQUIRE(doc.as<std::string>() == "{\"dancing\":2}");
  131. }
  132. SECTION("when allocation fails") {
  133. deserializeJson(doc, "{\"blanket\":1,\"dancing\":2}");
  134. REQUIRE(doc.capacity() == 4096);
  135. REQUIRE(doc.memoryUsage() == JSON_OBJECT_SIZE(2) + 16);
  136. doc.remove("blanket");
  137. doc.allocator().disable();
  138. bool result = doc.garbageCollect();
  139. REQUIRE(result == false);
  140. REQUIRE(doc.memoryUsage() == JSON_OBJECT_SIZE(2) + 16);
  141. REQUIRE(doc.capacity() == 4096);
  142. REQUIRE(doc.as<std::string>() == "{\"dancing\":2}");
  143. }
  144. }
  145. }