shrinkToFit.cpp 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. // ArduinoJson - https://arduinojson.org
  2. // Copyright © 2014-2025, Benoit BLANCHON
  3. // MIT License
  4. #include <ArduinoJson/Memory/ResourceManager.hpp>
  5. #include <catch.hpp>
  6. #include "Allocators.hpp"
  7. using namespace ArduinoJson::detail;
  8. TEST_CASE("ResourceManager::shrinkToFit()") {
  9. SpyingAllocator spyingAllocator;
  10. ResourceManager resources(&spyingAllocator);
  11. SECTION("empty") {
  12. resources.shrinkToFit();
  13. REQUIRE(spyingAllocator.log() == AllocatorLog{});
  14. }
  15. SECTION("only one pool") {
  16. resources.allocVariant();
  17. resources.shrinkToFit();
  18. REQUIRE(spyingAllocator.log() ==
  19. AllocatorLog{
  20. Allocate(sizeofPool()),
  21. Reallocate(sizeofPool(), sizeofPool(1)),
  22. });
  23. }
  24. SECTION("more pools than initial count") {
  25. for (size_t i = 0;
  26. i < ARDUINOJSON_POOL_CAPACITY * ARDUINOJSON_INITIAL_POOL_COUNT + 1;
  27. i++)
  28. resources.allocVariant();
  29. REQUIRE(spyingAllocator.log() ==
  30. AllocatorLog{
  31. Allocate(sizeofPool()) * ARDUINOJSON_INITIAL_POOL_COUNT,
  32. Allocate(sizeofPoolList(ARDUINOJSON_INITIAL_POOL_COUNT * 2)),
  33. Allocate(sizeofPool()),
  34. });
  35. spyingAllocator.clearLog();
  36. resources.shrinkToFit();
  37. REQUIRE(spyingAllocator.log() ==
  38. AllocatorLog{
  39. Reallocate(sizeofPool(), sizeofPool(1)),
  40. Reallocate(sizeofPoolList(ARDUINOJSON_INITIAL_POOL_COUNT * 2),
  41. sizeofPoolList(ARDUINOJSON_INITIAL_POOL_COUNT + 1)),
  42. });
  43. }
  44. }