saveString.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. // ArduinoJson - https://arduinojson.org
  2. // Copyright © 2014-2022, Benoit BLANCHON
  3. // MIT License
  4. #include <ArduinoJson/Memory/MemoryPool.hpp>
  5. #include <ArduinoJson/Strings/StringAdapters.hpp>
  6. #include <catch.hpp>
  7. using namespace ARDUINOJSON_NAMESPACE;
  8. static const char* saveString(MemoryPool& pool, const char* s) {
  9. return pool.saveString(adaptString(const_cast<char*>(s)));
  10. }
  11. static const char* saveString(MemoryPool& pool, const char* s, size_t n) {
  12. return pool.saveString(adaptString(s, n));
  13. }
  14. TEST_CASE("MemoryPool::saveString()") {
  15. char buffer[32];
  16. MemoryPool pool(buffer, 32);
  17. SECTION("Duplicates different strings") {
  18. const char* a = saveString(pool, "hello");
  19. const char* b = saveString(pool, "world");
  20. REQUIRE(a != b);
  21. REQUIRE(pool.size() == 6 + 6);
  22. }
  23. SECTION("Deduplicates identical strings") {
  24. const char* a = saveString(pool, "hello");
  25. const char* b = saveString(pool, "hello");
  26. REQUIRE(a == b);
  27. REQUIRE(pool.size() == 6);
  28. }
  29. SECTION("Deduplicates identical strings that contain NUL") {
  30. const char* a = saveString(pool, "hello\0world", 11);
  31. const char* b = saveString(pool, "hello\0world", 11);
  32. REQUIRE(a == b);
  33. REQUIRE(pool.size() == 12);
  34. }
  35. SECTION("Reuse part of a string if it ends with NUL") {
  36. const char* a = saveString(pool, "hello\0world", 11);
  37. const char* b = saveString(pool, "hello");
  38. REQUIRE(a == b);
  39. REQUIRE(pool.size() == 12);
  40. }
  41. SECTION("Don't stop on first NUL") {
  42. const char* a = saveString(pool, "hello");
  43. const char* b = saveString(pool, "hello\0world", 11);
  44. REQUIRE(a != b);
  45. REQUIRE(pool.size() == 18);
  46. }
  47. SECTION("Returns NULL when full") {
  48. REQUIRE(pool.capacity() == 32);
  49. const void* p1 = saveString(pool, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
  50. REQUIRE(p1 != 0);
  51. REQUIRE(pool.size() == 32);
  52. const void* p2 = saveString(pool, "b");
  53. REQUIRE(p2 == 0);
  54. }
  55. SECTION("Returns NULL when pool is too small") {
  56. const void* p = saveString(pool, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
  57. REQUIRE(0 == p);
  58. }
  59. SECTION("Returns NULL when buffer is NULL") {
  60. MemoryPool pool2(0, 32);
  61. REQUIRE(0 == saveString(pool2, "a"));
  62. }
  63. SECTION("Returns NULL when capacity is 0") {
  64. MemoryPool pool2(buffer, 0);
  65. REQUIRE(0 == saveString(pool2, "a"));
  66. }
  67. SECTION("Returns same address after clear()") {
  68. const void* a = saveString(pool, "hello");
  69. pool.clear();
  70. const void* b = saveString(pool, "world");
  71. REQUIRE(a == b);
  72. }
  73. SECTION("Can use full capacity when fresh") {
  74. const void* a = saveString(pool, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
  75. REQUIRE(a != 0);
  76. }
  77. SECTION("Can use full capacity after clear") {
  78. saveString(pool, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
  79. pool.clear();
  80. const void* a = saveString(pool, "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb");
  81. REQUIRE(a != 0);
  82. }
  83. }