allocVariant.cpp 1.2 KB

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