StringBuilder.cpp 4.2 KB

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