shrinkToFit.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. // ArduinoJson - arduinojson.org
  2. // Copyright Benoit Blanchon 2014-2020
  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. doc.shrinkToFit();
  42. REQUIRE(doc.capacity() == expected_size);
  43. REQUIRE(doc.memoryUsage() == expected_size);
  44. std::string json;
  45. serializeJson(doc, json);
  46. REQUIRE(json == expected_json);
  47. }
  48. TEST_CASE("BasicJsonDocument::shrinkToFit()") {
  49. ShrinkToFitTestDocument doc(4096);
  50. SECTION("null") {
  51. testShrinkToFit(doc, "null", 0);
  52. }
  53. SECTION("empty object") {
  54. deserializeJson(doc, "{}");
  55. testShrinkToFit(doc, "{}", JSON_OBJECT_SIZE(0));
  56. }
  57. SECTION("empty array") {
  58. deserializeJson(doc, "[]");
  59. testShrinkToFit(doc, "[]", JSON_ARRAY_SIZE(0));
  60. }
  61. SECTION("linked string") {
  62. doc.set("hello");
  63. testShrinkToFit(doc, "\"hello\"", 0);
  64. }
  65. SECTION("owned string") {
  66. doc.set(std::string("abcdefg"));
  67. testShrinkToFit(doc, "\"abcdefg\"", 8);
  68. }
  69. SECTION("linked raw") {
  70. doc.set(serialized("[{},123]"));
  71. testShrinkToFit(doc, "[{},123]", 0);
  72. }
  73. SECTION("owned raw") {
  74. doc.set(serialized(std::string("[{},123]")));
  75. testShrinkToFit(doc, "[{},123]", 8);
  76. }
  77. SECTION("linked key") {
  78. doc["key"] = 42;
  79. testShrinkToFit(doc, "{\"key\":42}", JSON_OBJECT_SIZE(1));
  80. }
  81. SECTION("owned key") {
  82. doc[std::string("abcdefg")] = 42;
  83. testShrinkToFit(doc, "{\"abcdefg\":42}", JSON_OBJECT_SIZE(1) + 8);
  84. }
  85. SECTION("linked string in array") {
  86. doc.add("hello");
  87. testShrinkToFit(doc, "[\"hello\"]", JSON_ARRAY_SIZE(1));
  88. }
  89. SECTION("owned string in array") {
  90. doc.add(std::string("abcdefg"));
  91. testShrinkToFit(doc, "[\"abcdefg\"]", JSON_ARRAY_SIZE(1) + 8);
  92. }
  93. SECTION("linked string in object") {
  94. doc["key"] = "hello";
  95. testShrinkToFit(doc, "{\"key\":\"hello\"}", JSON_OBJECT_SIZE(1));
  96. }
  97. SECTION("owned string in object") {
  98. doc["key"] = std::string("abcdefg");
  99. testShrinkToFit(doc, "{\"key\":\"abcdefg\"}", JSON_ARRAY_SIZE(1) + 8);
  100. }
  101. SECTION("unaligned") {
  102. doc.add(std::string("?")); // two bytes in the string pool
  103. REQUIRE(doc.memoryUsage() == JSON_OBJECT_SIZE(1) + 2);
  104. doc.shrinkToFit();
  105. // the new capacity should be padded to align the pointers
  106. REQUIRE(doc.capacity() == JSON_OBJECT_SIZE(1) + sizeof(void*));
  107. REQUIRE(doc.memoryUsage() == JSON_OBJECT_SIZE(1) + 2);
  108. REQUIRE(doc[0] == "?");
  109. }
  110. }
  111. TEST_CASE("DynamicJsonDocument::shrinkToFit()") {
  112. DynamicJsonDocument doc(4096);
  113. deserializeJson(doc, "{\"hello\":[\"world\"]");
  114. doc.shrinkToFit();
  115. std::string json;
  116. serializeJson(doc, json);
  117. REQUIRE(json == "{\"hello\":[\"world\"]}");
  118. }