swap.cpp 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. // ArduinoJson - https://arduinojson.org
  2. // Copyright © 2014-2023, Benoit BLANCHON
  3. // MIT License
  4. #include <ArduinoJson/Memory/Alignment.hpp>
  5. #include <ArduinoJson/Memory/ResourceManager.hpp>
  6. #include <ArduinoJson/Memory/VariantPoolImpl.hpp>
  7. #include <catch.hpp>
  8. #include "Allocators.hpp"
  9. using namespace ArduinoJson::detail;
  10. static void fullPreallocatedPools(ResourceManager& resources) {
  11. for (int i = 0;
  12. i < ARDUINOJSON_INITIAL_POOL_COUNT * ARDUINOJSON_POOL_CAPACITY; i++)
  13. resources.allocSlot();
  14. }
  15. TEST_CASE("ResourceManager::swap()") {
  16. SECTION("Both using preallocated pool list") {
  17. SpyingAllocator allocator;
  18. ResourceManager a(&allocator);
  19. ResourceManager b(&allocator);
  20. auto a1 = a.allocSlot();
  21. auto b1 = b.allocSlot();
  22. swap(a, b);
  23. REQUIRE(a1->data() == b.getSlot(a1.id())->data());
  24. REQUIRE(b1->data() == a.getSlot(b1.id())->data());
  25. REQUIRE(allocator.log() == AllocatorLog()
  26. << AllocatorLog::Allocate(sizeofPool()) * 2);
  27. }
  28. SECTION("Only left using preallocated pool list") {
  29. SpyingAllocator allocator;
  30. ResourceManager a(&allocator);
  31. ResourceManager b(&allocator);
  32. fullPreallocatedPools(b);
  33. auto a1 = a.allocSlot();
  34. auto b1 = b.allocSlot();
  35. swap(a, b);
  36. REQUIRE(a1->data() == b.getSlot(a1.id())->data());
  37. REQUIRE(b1->data() == a.getSlot(b1.id())->data());
  38. REQUIRE(allocator.log() == AllocatorLog()
  39. << AllocatorLog::Allocate(sizeofPool()) *
  40. (ARDUINOJSON_INITIAL_POOL_COUNT + 1)
  41. << AllocatorLog::Allocate(sizeofPoolList(
  42. ARDUINOJSON_INITIAL_POOL_COUNT * 2))
  43. << AllocatorLog::Allocate(sizeofPool()));
  44. }
  45. SECTION("Only right using preallocated pool list") {
  46. SpyingAllocator allocator;
  47. ResourceManager a(&allocator);
  48. fullPreallocatedPools(a);
  49. ResourceManager b(&allocator);
  50. auto a1 = a.allocSlot();
  51. auto b1 = b.allocSlot();
  52. swap(a, b);
  53. REQUIRE(a1->data() == b.getSlot(a1.id())->data());
  54. REQUIRE(b1->data() == a.getSlot(b1.id())->data());
  55. REQUIRE(allocator.log() == AllocatorLog()
  56. << AllocatorLog::Allocate(sizeofPool()) *
  57. ARDUINOJSON_INITIAL_POOL_COUNT
  58. << AllocatorLog::Allocate(sizeofPoolList(
  59. ARDUINOJSON_INITIAL_POOL_COUNT * 2))
  60. << AllocatorLog::Allocate(sizeofPool()) * 2);
  61. }
  62. SECTION("None is using preallocated pool list") {
  63. SpyingAllocator allocator;
  64. ResourceManager a(&allocator);
  65. fullPreallocatedPools(a);
  66. ResourceManager b(&allocator);
  67. fullPreallocatedPools(b);
  68. auto a1 = a.allocSlot();
  69. auto b1 = b.allocSlot();
  70. swap(a, b);
  71. REQUIRE(a1->data() == b.getSlot(a1.id())->data());
  72. REQUIRE(b1->data() == a.getSlot(b1.id())->data());
  73. }
  74. }