size.cpp 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. // ArduinoJson - arduinojson.org
  2. // Copyright Benoit Blanchon 2014-2018
  3. // MIT License
  4. #include <ArduinoJson/Memory/DynamicMemoryPool.hpp>
  5. #include <catch.hpp>
  6. using namespace ARDUINOJSON_NAMESPACE;
  7. TEST_CASE("DynamicMemoryPool::size()") {
  8. DynamicMemoryPool memoryPool;
  9. SECTION("Initial size is 0") {
  10. REQUIRE(0 == memoryPool.size());
  11. }
  12. SECTION("Increases after alloc()") {
  13. memoryPool.alloc(1);
  14. REQUIRE(1U <= memoryPool.size());
  15. memoryPool.alloc(1);
  16. REQUIRE(2U <= memoryPool.size());
  17. }
  18. SECTION("Goes back to 0 after clear()") {
  19. memoryPool.alloc(1);
  20. memoryPool.clear();
  21. REQUIRE(0 == memoryPool.size());
  22. }
  23. SECTION("Increases after allocSlot()") {
  24. memoryPool.allocSlot();
  25. REQUIRE(sizeof(Slot) == memoryPool.size());
  26. memoryPool.allocSlot();
  27. REQUIRE(2 * sizeof(Slot) == memoryPool.size());
  28. }
  29. SECTION("Decreases after freeSlot()") {
  30. Slot* s1 = memoryPool.allocSlot();
  31. Slot* s2 = memoryPool.allocSlot();
  32. memoryPool.freeSlot(s1);
  33. REQUIRE(sizeof(Slot) == memoryPool.size());
  34. memoryPool.freeSlot(s2);
  35. REQUIRE(0 == memoryPool.size());
  36. }
  37. }