StringBuilder.cpp 5.6 KB

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