shrinkToFit.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. // ArduinoJson - https://arduinojson.org
  2. // Copyright © 2014-2022, Benoit BLANCHON
  3. // MIT License
  4. #include <ArduinoJson.h>
  5. #include <catch.hpp>
  6. #include <stdlib.h> // malloc, free
  7. #include <string>
  8. using ARDUINOJSON_NAMESPACE::addPadding;
  9. class ArmoredAllocator {
  10. public:
  11. ArmoredAllocator() : _ptr(0), _size(0) {}
  12. void* allocate(size_t size) {
  13. _ptr = malloc(size);
  14. _size = size;
  15. return _ptr;
  16. }
  17. void deallocate(void* ptr) {
  18. REQUIRE(ptr == _ptr);
  19. free(ptr);
  20. _ptr = 0;
  21. _size = 0;
  22. }
  23. void* reallocate(void* ptr, size_t new_size) {
  24. REQUIRE(ptr == _ptr);
  25. // don't call realloc, instead alloc a new buffer and erase the old one
  26. // this way we make sure we support relocation
  27. void* new_ptr = malloc(new_size);
  28. memcpy(new_ptr, _ptr, std::min(new_size, _size));
  29. memset(_ptr, '#', _size); // erase
  30. free(_ptr);
  31. _ptr = new_ptr;
  32. return new_ptr;
  33. }
  34. private:
  35. void* _ptr;
  36. size_t _size;
  37. };
  38. typedef BasicJsonDocument<ArmoredAllocator> ShrinkToFitTestDocument;
  39. void testShrinkToFit(ShrinkToFitTestDocument& doc, std::string expected_json,
  40. size_t expected_size) {
  41. // test twice: shrinkToFit() should be idempotent
  42. for (int i = 0; i < 2; i++) {
  43. doc.shrinkToFit();
  44. REQUIRE(doc.capacity() == expected_size);
  45. REQUIRE(doc.memoryUsage() == expected_size);
  46. std::string json;
  47. serializeJson(doc, json);
  48. REQUIRE(json == expected_json);
  49. }
  50. }
  51. TEST_CASE("BasicJsonDocument::shrinkToFit()") {
  52. ShrinkToFitTestDocument doc(4096);
  53. SECTION("null") {
  54. testShrinkToFit(doc, "null", 0);
  55. }
  56. SECTION("empty object") {
  57. deserializeJson(doc, "{}");
  58. testShrinkToFit(doc, "{}", JSON_OBJECT_SIZE(0));
  59. }
  60. SECTION("empty array") {
  61. deserializeJson(doc, "[]");
  62. testShrinkToFit(doc, "[]", JSON_ARRAY_SIZE(0));
  63. }
  64. SECTION("linked string") {
  65. doc.set("hello");
  66. testShrinkToFit(doc, "\"hello\"", 0);
  67. }
  68. SECTION("owned string") {
  69. doc.set(std::string("abcdefg"));
  70. testShrinkToFit(doc, "\"abcdefg\"", 8);
  71. }
  72. SECTION("linked raw") {
  73. doc.set(serialized("[{},123]"));
  74. testShrinkToFit(doc, "[{},123]", 0);
  75. }
  76. SECTION("owned raw") {
  77. doc.set(serialized(std::string("[{},12]")));
  78. testShrinkToFit(doc, "[{},12]", 8);
  79. }
  80. SECTION("linked key") {
  81. doc["key"] = 42;
  82. testShrinkToFit(doc, "{\"key\":42}", JSON_OBJECT_SIZE(1));
  83. }
  84. SECTION("owned key") {
  85. doc[std::string("abcdefg")] = 42;
  86. testShrinkToFit(doc, "{\"abcdefg\":42}", JSON_OBJECT_SIZE(1) + 8);
  87. }
  88. SECTION("linked string in array") {
  89. doc.add("hello");
  90. testShrinkToFit(doc, "[\"hello\"]", JSON_ARRAY_SIZE(1));
  91. }
  92. SECTION("owned string in array") {
  93. doc.add(std::string("abcdefg"));
  94. testShrinkToFit(doc, "[\"abcdefg\"]", JSON_ARRAY_SIZE(1) + 8);
  95. }
  96. SECTION("linked string in object") {
  97. doc["key"] = "hello";
  98. testShrinkToFit(doc, "{\"key\":\"hello\"}", JSON_OBJECT_SIZE(1));
  99. }
  100. SECTION("owned string in object") {
  101. doc["key"] = std::string("abcdefg");
  102. testShrinkToFit(doc, "{\"key\":\"abcdefg\"}", JSON_ARRAY_SIZE(1) + 8);
  103. }
  104. SECTION("unaligned") {
  105. doc.add(std::string("?")); // two bytes in the string pool
  106. REQUIRE(doc.memoryUsage() == JSON_OBJECT_SIZE(1) + 2);
  107. doc.shrinkToFit();
  108. // the new capacity should be padded to align the pointers
  109. REQUIRE(doc.capacity() == JSON_OBJECT_SIZE(1) + sizeof(void*));
  110. REQUIRE(doc.memoryUsage() == JSON_OBJECT_SIZE(1) + 2);
  111. REQUIRE(doc[0] == "?");
  112. }
  113. }
  114. TEST_CASE("DynamicJsonDocument::shrinkToFit()") {
  115. DynamicJsonDocument doc(4096);
  116. deserializeJson(doc, "{\"hello\":[\"world\"]");
  117. doc.shrinkToFit();
  118. std::string json;
  119. serializeJson(doc, json);
  120. REQUIRE(json == "{\"hello\":[\"world\"]}");
  121. }