StringBuilder.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. // ArduinoJson - https://arduinojson.org
  2. // Copyright © 2014-2025, Benoit BLANCHON
  3. // MIT License
  4. #include <ArduinoJson.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(&resources) == "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(&resources) == "hello");
  110. REQUIRE(s2.asString(&resources) == "world");
  111. REQUIRE(+s1.asString(&resources).c_str() ==
  112. +s3.asString(&resources).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(&resources) == "hello world");
  126. REQUIRE(s2.asString(&resources) == "hello");
  127. REQUIRE(+s2.asString(&resources).c_str() !=
  128. +s1.asString(&resources).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(&resources) == "hello world");
  141. REQUIRE(s2.asString(&resources) == "worl");
  142. REQUIRE(s2.asString(&resources).c_str() !=
  143. s1.asString(&resources).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. }