allocVariant.cpp 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. // ArduinoJson - arduinojson.org
  2. // Copyright Benoit Blanchon 2014-2018
  3. // MIT License
  4. #include <ArduinoJson/Memory/MemoryPool.hpp>
  5. #include <catch.hpp>
  6. using namespace ARDUINOJSON_NAMESPACE;
  7. static char buffer[4096];
  8. TEST_CASE("MemoryPool::allocVariant()") {
  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 same pointer after freeSlot()") {
  18. MemoryPool pool(buffer, sizeof(buffer));
  19. VariantSlot* s1 = pool.allocVariant();
  20. pool.freeVariant(s1);
  21. VariantSlot* s2 = pool.allocVariant();
  22. REQUIRE(s1 == s2);
  23. }
  24. SECTION("Returns aligned pointers") {
  25. MemoryPool pool(buffer, sizeof(buffer));
  26. REQUIRE(isAligned(pool.allocVariant()));
  27. REQUIRE(isAligned(pool.allocVariant()));
  28. }
  29. SECTION("Returns zero if capacity is 0") {
  30. MemoryPool pool(buffer, 0);
  31. REQUIRE(pool.allocVariant() == 0);
  32. }
  33. SECTION("Returns zero if buffer is null") {
  34. MemoryPool pool(0, sizeof(buffer));
  35. REQUIRE(pool.allocVariant() == 0);
  36. }
  37. SECTION("Returns zero if capacity is insufficient") {
  38. MemoryPool pool(buffer, sizeof(VariantSlot));
  39. pool.allocVariant();
  40. REQUIRE(pool.allocVariant() == 0);
  41. }
  42. }