clear.cpp 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. // ArduinoJson - arduinojson.org
  2. // Copyright Benoit Blanchon 2014-2018
  3. // MIT License
  4. #include <ArduinoJson/Memory/StaticMemoryPool.hpp>
  5. #include <catch.hpp>
  6. using namespace ARDUINOJSON_NAMESPACE;
  7. static const size_t poolCapacity = 512;
  8. TEST_CASE("StaticMemoryPool::clear()") {
  9. StaticMemoryPool<poolCapacity> memoryPool;
  10. SECTION("Discards allocated variants") {
  11. memoryPool.allocVariant();
  12. memoryPool.clear();
  13. REQUIRE(memoryPool.size() == 0);
  14. }
  15. SECTION("Discards allocated strings") {
  16. memoryPool.allocFrozenString(10);
  17. memoryPool.clear();
  18. REQUIRE(memoryPool.size() == 0);
  19. }
  20. SECTION("Purges variant cache") {
  21. VariantSlot* a = memoryPool.allocVariant();
  22. REQUIRE(a != 0);
  23. VariantSlot* b = memoryPool.allocVariant();
  24. REQUIRE(b != 0);
  25. // place slot a in the pool of free slots
  26. memoryPool.freeVariant(a);
  27. memoryPool.clear();
  28. REQUIRE(memoryPool.size() == 0);
  29. }
  30. SECTION("Purges string cache") {
  31. StringSlot* a = memoryPool.allocFrozenString(10);
  32. REQUIRE(a != 0);
  33. StringSlot* b = memoryPool.allocFrozenString(10);
  34. REQUIRE(b != 0);
  35. // place slot a in the pool of free slots
  36. memoryPool.freeString(a);
  37. memoryPool.clear();
  38. REQUIRE(memoryPool.size() == 0);
  39. }
  40. SECTION("Purges list of string") {
  41. StringSlot* a = memoryPool.allocFrozenString(6);
  42. REQUIRE(a != 0);
  43. strcpy(a->value, "hello");
  44. StringSlot* b = memoryPool.allocFrozenString(6);
  45. REQUIRE(b != 0);
  46. strcpy(b->value, "world");
  47. memoryPool.clear(); // ACT!
  48. StringSlot* c = memoryPool.allocFrozenString(2);
  49. REQUIRE(c != 0);
  50. strcpy(c->value, "H");
  51. StringSlot* d = memoryPool.allocFrozenString(2);
  52. REQUIRE(d != 0);
  53. strcpy(d->value, "W");
  54. // if the memory pool keeps pointer to the old strings
  55. // it will try to compact the strings
  56. memoryPool.freeString(c);
  57. REQUIRE(d->value == std::string("W"));
  58. }
  59. }