StringCopier.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. // ArduinoJson - https://arduinojson.org
  2. // Copyright © 2014-2023, Benoit BLANCHON
  3. // MIT License
  4. #include <ArduinoJson/StringStorage/StringCopier.hpp>
  5. #include <catch.hpp>
  6. #include "Allocators.hpp"
  7. using namespace ArduinoJson::detail;
  8. TEST_CASE("StringCopier") {
  9. ControllableAllocator controllableAllocator;
  10. SpyingAllocator spyingAllocator(&controllableAllocator);
  11. MemoryPool pool(0, &spyingAllocator);
  12. SECTION("Empty string") {
  13. StringCopier str(&pool);
  14. str.startString();
  15. str.save();
  16. REQUIRE(pool.size() == sizeofString(0));
  17. REQUIRE(pool.overflowed() == false);
  18. REQUIRE(spyingAllocator.log() ==
  19. AllocatorLog() << AllocatorLog::Allocate(sizeofString(31))
  20. << AllocatorLog::Reallocate(sizeofString(31),
  21. sizeofString(0)));
  22. }
  23. SECTION("Short string fits in first allocation") {
  24. StringCopier str(&pool);
  25. str.startString();
  26. str.append("hello");
  27. REQUIRE(str.isValid() == true);
  28. REQUIRE(str.str() == "hello");
  29. REQUIRE(pool.overflowed() == false);
  30. REQUIRE(spyingAllocator.log() ==
  31. AllocatorLog() << AllocatorLog::Allocate(sizeofString(31)));
  32. }
  33. SECTION("Long string needs reallocation") {
  34. StringCopier str(&pool);
  35. str.startString();
  36. str.append(
  37. "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do "
  38. "eiusmod tempor incididunt ut labore et dolore magna aliqua.");
  39. REQUIRE(str.isValid() == true);
  40. REQUIRE(str.str() ==
  41. "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do "
  42. "eiusmod tempor incididunt ut labore et dolore magna aliqua.");
  43. REQUIRE(pool.overflowed() == false);
  44. REQUIRE(spyingAllocator.log() ==
  45. AllocatorLog() << AllocatorLog::Allocate(sizeofString(31))
  46. << AllocatorLog::Reallocate(sizeofString(31),
  47. sizeofString(63))
  48. << AllocatorLog::Reallocate(sizeofString(63),
  49. sizeofString(127)));
  50. }
  51. SECTION("Realloc fails") {
  52. StringCopier str(&pool);
  53. str.startString();
  54. controllableAllocator.disable();
  55. str.append(
  56. "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do "
  57. "eiusmod tempor incididunt ut labore et dolore magna aliqua.");
  58. REQUIRE(spyingAllocator.log() ==
  59. AllocatorLog() << AllocatorLog::Allocate(sizeofString(31))
  60. << AllocatorLog::ReallocateFail(sizeofString(31),
  61. sizeofString(63))
  62. << AllocatorLog::Deallocate(sizeofString(31)));
  63. REQUIRE(str.isValid() == false);
  64. REQUIRE(pool.overflowed() == true);
  65. }
  66. SECTION("Initial allocation fails") {
  67. StringCopier str(&pool);
  68. controllableAllocator.disable();
  69. str.startString();
  70. REQUIRE(str.isValid() == false);
  71. REQUIRE(pool.overflowed() == true);
  72. REQUIRE(spyingAllocator.log() ==
  73. AllocatorLog() << AllocatorLog::AllocateFail(sizeofString(31)));
  74. }
  75. }
  76. static const char* addStringToPool(MemoryPool& pool, const char* s) {
  77. StringCopier str(&pool);
  78. str.startString();
  79. str.append(s);
  80. return str.save().c_str();
  81. }
  82. TEST_CASE("StringCopier::save() deduplicates strings") {
  83. MemoryPool pool(4096);
  84. SECTION("Basic") {
  85. const char* s1 = addStringToPool(pool, "hello");
  86. const char* s2 = addStringToPool(pool, "world");
  87. const char* s3 = addStringToPool(pool, "hello");
  88. REQUIRE(s1 == s3);
  89. REQUIRE(s2 != s3);
  90. REQUIRE(pool.size() == 2 * sizeofString(5));
  91. }
  92. SECTION("Requires terminator") {
  93. const char* s1 = addStringToPool(pool, "hello world");
  94. const char* s2 = addStringToPool(pool, "hello");
  95. REQUIRE(s2 != s1);
  96. REQUIRE(pool.size() == sizeofString(11) + sizeofString(5));
  97. }
  98. SECTION("Don't overrun") {
  99. const char* s1 = addStringToPool(pool, "hello world");
  100. const char* s2 = addStringToPool(pool, "wor");
  101. REQUIRE(s2 != s1);
  102. REQUIRE(pool.size() == sizeofString(11) + sizeofString(3));
  103. }
  104. }