shrinkToFit.cpp 1.6 KB

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