StringBuilder.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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;
  8. using namespace ArduinoJson::detail;
  9. TEST_CASE("StringBuilder") {
  10. KillswitchAllocator killswitch;
  11. SpyingAllocator spyingAllocator(&killswitch);
  12. ResourceManager resources(&spyingAllocator);
  13. SECTION("Empty string") {
  14. StringBuilder str(&resources);
  15. VariantData data;
  16. str.startString();
  17. str.save(&data);
  18. REQUIRE(resources.overflowed() == false);
  19. REQUIRE(spyingAllocator.log() == AllocatorLog{
  20. Allocate(sizeofStringBuffer()),
  21. });
  22. REQUIRE(data.type() == VariantType::TinyString);
  23. }
  24. SECTION("Tiny string") {
  25. StringBuilder str(&resources);
  26. str.startString();
  27. str.append("url");
  28. REQUIRE(str.isValid() == true);
  29. REQUIRE(str.str() == "url");
  30. REQUIRE(spyingAllocator.log() == AllocatorLog{
  31. Allocate(sizeofStringBuffer()),
  32. });
  33. VariantData data;
  34. str.save(&data);
  35. REQUIRE(resources.overflowed() == false);
  36. REQUIRE(data.type() == VariantType::TinyString);
  37. REQUIRE(data.asString() == "url");
  38. }
  39. SECTION("Short string fits in first allocation") {
  40. StringBuilder str(&resources);
  41. str.startString();
  42. str.append("hello");
  43. REQUIRE(str.isValid() == true);
  44. REQUIRE(str.str() == "hello");
  45. REQUIRE(resources.overflowed() == false);
  46. REQUIRE(spyingAllocator.log() == AllocatorLog{
  47. Allocate(sizeofStringBuffer()),
  48. });
  49. }
  50. SECTION("Long string needs reallocation") {
  51. StringBuilder str(&resources);
  52. const char* lorem =
  53. "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do "
  54. "eiusmod tempor incididunt ut labore et dolore magna aliqua.";
  55. str.startString();
  56. str.append(lorem);
  57. REQUIRE(str.isValid() == true);
  58. REQUIRE(str.str() == lorem);
  59. REQUIRE(resources.overflowed() == false);
  60. REQUIRE(spyingAllocator.log() ==
  61. AllocatorLog{
  62. Allocate(sizeofStringBuffer(1)),
  63. Reallocate(sizeofStringBuffer(1), sizeofStringBuffer(2)),
  64. Reallocate(sizeofStringBuffer(2), sizeofStringBuffer(3)),
  65. });
  66. }
  67. SECTION("Realloc fails") {
  68. StringBuilder str(&resources);
  69. str.startString();
  70. killswitch.on();
  71. str.append(
  72. "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do "
  73. "eiusmod tempor incididunt ut labore et dolore magna aliqua.");
  74. REQUIRE(spyingAllocator.log() ==
  75. AllocatorLog{
  76. Allocate(sizeofStringBuffer()),
  77. ReallocateFail(sizeofStringBuffer(), sizeofStringBuffer(2)),
  78. Deallocate(sizeofStringBuffer()),
  79. });
  80. REQUIRE(str.isValid() == false);
  81. REQUIRE(resources.overflowed() == true);
  82. }
  83. SECTION("Initial allocation fails") {
  84. StringBuilder str(&resources);
  85. killswitch.on();
  86. str.startString();
  87. REQUIRE(str.isValid() == false);
  88. REQUIRE(resources.overflowed() == true);
  89. REQUIRE(spyingAllocator.log() == AllocatorLog{
  90. AllocateFail(sizeofStringBuffer()),
  91. });
  92. }
  93. }
  94. static VariantData saveString(StringBuilder& builder, const char* s) {
  95. VariantData data;
  96. builder.startString();
  97. builder.append(s);
  98. builder.save(&data);
  99. return data;
  100. }
  101. TEST_CASE("StringBuilder::save() deduplicates strings") {
  102. SpyingAllocator spy;
  103. ResourceManager resources(&spy);
  104. StringBuilder builder(&resources);
  105. SECTION("Basic") {
  106. auto s1 = saveString(builder, "hello");
  107. auto s2 = saveString(builder, "world");
  108. auto s3 = saveString(builder, "hello");
  109. REQUIRE(s1.asString() == "hello");
  110. REQUIRE(s2.asString() == "world");
  111. REQUIRE(+s1.asString().c_str() == +s3.asString().c_str()); // same address
  112. REQUIRE(spy.log() ==
  113. AllocatorLog{
  114. Allocate(sizeofStringBuffer()),
  115. Reallocate(sizeofStringBuffer(), sizeofString("hello")),
  116. Allocate(sizeofStringBuffer()),
  117. Reallocate(sizeofStringBuffer(), sizeofString("world")),
  118. Allocate(sizeofStringBuffer()),
  119. });
  120. }
  121. SECTION("Requires terminator") {
  122. auto s1 = saveString(builder, "hello world");
  123. auto s2 = saveString(builder, "hello");
  124. REQUIRE(s1.asString() == "hello world");
  125. REQUIRE(s2.asString() == "hello");
  126. REQUIRE(+s2.asString().c_str() !=
  127. +s1.asString().c_str()); // different address
  128. REQUIRE(spy.log() ==
  129. AllocatorLog{
  130. Allocate(sizeofStringBuffer()),
  131. Reallocate(sizeofStringBuffer(), sizeofString("hello world")),
  132. Allocate(sizeofStringBuffer()),
  133. Reallocate(sizeofStringBuffer(), sizeofString("hello")),
  134. });
  135. }
  136. SECTION("Don't overrun") {
  137. auto s1 = saveString(builder, "hello world");
  138. auto s2 = saveString(builder, "worl");
  139. REQUIRE(s1.asString() == "hello world");
  140. REQUIRE(s2.asString() == "worl");
  141. REQUIRE(s2.asString().c_str() !=
  142. s1.asString().c_str()); // different address
  143. REQUIRE(spy.log() ==
  144. AllocatorLog{
  145. Allocate(sizeofStringBuffer()),
  146. Reallocate(sizeofStringBuffer(), sizeofString("hello world")),
  147. Allocate(sizeofStringBuffer()),
  148. Reallocate(sizeofStringBuffer(), sizeofString("worl")),
  149. });
  150. }
  151. }