allocVariant.cpp 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. // ArduinoJson - https://arduinojson.org
  2. // Copyright © 2014-2022, Benoit BLANCHON
  3. // MIT License
  4. #include <ArduinoJson/Memory/MemoryPool.hpp>
  5. #include <catch.hpp>
  6. using namespace ARDUINOJSON_NAMESPACE;
  7. TEST_CASE("MemoryPool::allocVariant()") {
  8. char buffer[4096];
  9. SECTION("Returns different pointer") {
  10. MemoryPool pool(buffer, sizeof(buffer));
  11. VariantSlot* s1 = pool.allocVariant();
  12. REQUIRE(s1 != 0);
  13. VariantSlot* s2 = pool.allocVariant();
  14. REQUIRE(s2 != 0);
  15. REQUIRE(s1 != s2);
  16. }
  17. SECTION("Returns aligned pointers") {
  18. MemoryPool pool(buffer, sizeof(buffer));
  19. REQUIRE(isAligned(pool.allocVariant()));
  20. REQUIRE(isAligned(pool.allocVariant()));
  21. }
  22. SECTION("Returns zero if capacity is 0") {
  23. MemoryPool pool(buffer, 0);
  24. REQUIRE(pool.allocVariant() == 0);
  25. }
  26. SECTION("Returns zero if buffer is null") {
  27. MemoryPool pool(0, sizeof(buffer));
  28. REQUIRE(pool.allocVariant() == 0);
  29. }
  30. SECTION("Returns zero if capacity is insufficient") {
  31. MemoryPool pool(buffer, sizeof(VariantSlot));
  32. pool.allocVariant();
  33. REQUIRE(pool.allocVariant() == 0);
  34. }
  35. }