shrinkToFit.cpp 1.6 KB

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