allocVariant.cpp 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  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::allocVariant()") {
  8. DynamicMemoryPool memoryPool;
  9. SECTION("Returns different pointer") {
  10. VariantSlot* s1 = memoryPool.allocVariant();
  11. VariantSlot* s2 = memoryPool.allocVariant();
  12. REQUIRE(s1 != s2);
  13. }
  14. SECTION("Returns same pointer after freeSlot()") {
  15. VariantSlot* s1 = memoryPool.allocVariant();
  16. memoryPool.freeVariant(s1);
  17. VariantSlot* s2 = memoryPool.allocVariant();
  18. REQUIRE(s1 == s2);
  19. }
  20. SECTION("Returns aligned pointers") {
  21. // make room for two but not three
  22. // pass an uneven capacity
  23. DynamicMemoryPool pool(2 * sizeof(VariantSlot) + 1);
  24. REQUIRE(isAligned(pool.allocVariant()));
  25. REQUIRE(isAligned(pool.allocVariant()));
  26. REQUIRE(pool.blockCount() == 1);
  27. REQUIRE(isAligned(pool.allocVariant()));
  28. REQUIRE(isAligned(pool.allocVariant()));
  29. REQUIRE(pool.blockCount() == 2);
  30. }
  31. }