alloc.cpp 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. // Copyright Benoit Blanchon 2014-2017
  2. // MIT License
  3. //
  4. // Arduino JSON library
  5. // https://bblanchon.github.io/ArduinoJson/
  6. // If you like this project, please add a star!
  7. #include <ArduinoJson.h>
  8. #include <catch.hpp>
  9. static bool isAligned(void* ptr) {
  10. const size_t mask = sizeof(void*) - 1;
  11. size_t addr = reinterpret_cast<size_t>(ptr);
  12. return (addr & mask) == 0;
  13. }
  14. TEST_CASE("DynamicJsonBuffer::alloc()") {
  15. DynamicJsonBuffer buffer;
  16. SECTION("InitialSizeIsZero") {
  17. REQUIRE(0 == buffer.size());
  18. }
  19. SECTION("SizeIncreasesAfterAlloc") {
  20. buffer.alloc(1);
  21. REQUIRE(1U <= buffer.size());
  22. buffer.alloc(1);
  23. REQUIRE(2U <= buffer.size());
  24. }
  25. SECTION("ReturnDifferentPointer") {
  26. void* p1 = buffer.alloc(1);
  27. void* p2 = buffer.alloc(2);
  28. REQUIRE(p1 != p2);
  29. }
  30. SECTION("Alignment") {
  31. // make room for two but not three
  32. buffer = DynamicJsonBuffer(2 * sizeof(void*) + 1);
  33. REQUIRE(isAligned(buffer.alloc(1))); // this on is aligned by design
  34. REQUIRE(isAligned(buffer.alloc(1))); // this one fits in the first block
  35. REQUIRE(isAligned(buffer.alloc(1))); // this one requires a new block
  36. }
  37. }