StringBuilder.cpp 4.1 KB

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