StringBuilder.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. // ArduinoJson - https://arduinojson.org
  2. // Copyright © 2014-2023, Benoit BLANCHON
  3. // MIT License
  4. #include <ArduinoJson/Memory/StringBuilder.hpp>
  5. #include <catch.hpp>
  6. #include "Allocators.hpp"
  7. using namespace ArduinoJson::detail;
  8. TEST_CASE("StringBuilder") {
  9. ControllableAllocator controllableAllocator;
  10. SpyingAllocator spyingAllocator(&controllableAllocator);
  11. MemoryPool pool(0, &spyingAllocator);
  12. SECTION("Empty string") {
  13. StringBuilder str(&pool);
  14. str.startString();
  15. str.save();
  16. REQUIRE(pool.size() == sizeofString(0));
  17. REQUIRE(pool.overflowed() == false);
  18. REQUIRE(spyingAllocator.log() ==
  19. AllocatorLog() << AllocatorLog::Allocate(sizeofString(31))
  20. << AllocatorLog::Reallocate(sizeofString(31),
  21. sizeofString(0)));
  22. }
  23. SECTION("Short string fits in first allocation") {
  24. StringBuilder str(&pool);
  25. str.startString();
  26. str.append("hello");
  27. REQUIRE(str.isValid() == true);
  28. REQUIRE(str.str() == "hello");
  29. REQUIRE(pool.overflowed() == false);
  30. REQUIRE(spyingAllocator.log() ==
  31. AllocatorLog() << AllocatorLog::Allocate(sizeofString(31)));
  32. }
  33. SECTION("Long string needs reallocation") {
  34. StringBuilder str(&pool);
  35. str.startString();
  36. str.append(
  37. "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do "
  38. "eiusmod tempor incididunt ut labore et dolore magna aliqua.");
  39. REQUIRE(str.isValid() == true);
  40. REQUIRE(str.str() ==
  41. "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do "
  42. "eiusmod tempor incididunt ut labore et dolore magna aliqua.");
  43. REQUIRE(pool.overflowed() == false);
  44. REQUIRE(spyingAllocator.log() ==
  45. AllocatorLog() << AllocatorLog::Allocate(sizeofString(31))
  46. << AllocatorLog::Reallocate(sizeofString(31),
  47. sizeofString(63))
  48. << AllocatorLog::Reallocate(sizeofString(63),
  49. sizeofString(127)));
  50. }
  51. SECTION("Realloc fails") {
  52. StringBuilder str(&pool);
  53. str.startString();
  54. controllableAllocator.disable();
  55. str.append(
  56. "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do "
  57. "eiusmod tempor incididunt ut labore et dolore magna aliqua.");
  58. REQUIRE(spyingAllocator.log() ==
  59. AllocatorLog() << AllocatorLog::Allocate(sizeofString(31))
  60. << AllocatorLog::ReallocateFail(sizeofString(31),
  61. sizeofString(63))
  62. << AllocatorLog::Deallocate(sizeofString(31)));
  63. REQUIRE(str.isValid() == false);
  64. REQUIRE(pool.overflowed() == true);
  65. }
  66. SECTION("Initial allocation fails") {
  67. StringBuilder str(&pool);
  68. controllableAllocator.disable();
  69. str.startString();
  70. REQUIRE(str.isValid() == false);
  71. REQUIRE(pool.overflowed() == true);
  72. REQUIRE(spyingAllocator.log() ==
  73. AllocatorLog() << AllocatorLog::AllocateFail(sizeofString(31)));
  74. }
  75. }
  76. static StringNode* addStringToPool(MemoryPool& pool, const char* s) {
  77. StringBuilder str(&pool);
  78. str.startString();
  79. str.append(s);
  80. return str.save();
  81. }
  82. TEST_CASE("StringBuilder::save() deduplicates strings") {
  83. MemoryPool pool(4096);
  84. SECTION("Basic") {
  85. auto s1 = addStringToPool(pool, "hello");
  86. auto s2 = addStringToPool(pool, "world");
  87. auto s3 = addStringToPool(pool, "hello");
  88. REQUIRE(s1 == s3);
  89. REQUIRE(s2 != s3);
  90. REQUIRE(s1->references == 2);
  91. REQUIRE(s2->references == 1);
  92. REQUIRE(s3->references == 2);
  93. REQUIRE(pool.size() == 2 * sizeofString(5));
  94. }
  95. SECTION("Requires terminator") {
  96. auto s1 = addStringToPool(pool, "hello world");
  97. auto s2 = addStringToPool(pool, "hello");
  98. REQUIRE(s2 != s1);
  99. REQUIRE(s1->references == 1);
  100. REQUIRE(s2->references == 1);
  101. REQUIRE(pool.size() == sizeofString(11) + sizeofString(5));
  102. }
  103. SECTION("Don't overrun") {
  104. auto s1 = addStringToPool(pool, "hello world");
  105. auto s2 = addStringToPool(pool, "wor");
  106. REQUIRE(s2 != s1);
  107. REQUIRE(s1->references == 1);
  108. REQUIRE(s2->references == 1);
  109. REQUIRE(pool.size() == sizeofString(11) + sizeofString(3));
  110. }
  111. }