alloc.cpp 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. // ArduinoJson - arduinojson.org
  2. // Copyright Benoit Blanchon 2014-2018
  3. // MIT License
  4. #include <ArduinoJson/Memory/StaticJsonBuffer.hpp>
  5. #include <catch.hpp>
  6. using namespace ArduinoJson::Internals;
  7. static bool isAligned(void *ptr) {
  8. const size_t mask = sizeof(void *) - 1;
  9. size_t addr = reinterpret_cast<size_t>(ptr);
  10. return (addr & mask) == 0;
  11. }
  12. TEST_CASE("StaticJsonBuffer::alloc()") {
  13. StaticJsonBuffer<64> buffer;
  14. SECTION("Returns different addresses") {
  15. void *p1 = buffer.alloc(1);
  16. void *p2 = buffer.alloc(1);
  17. REQUIRE(p1 != p2);
  18. }
  19. SECTION("Returns non-NULL when using full capacity") {
  20. void *p = buffer.alloc(64);
  21. REQUIRE(0 != p);
  22. }
  23. SECTION("Returns NULL when full") {
  24. buffer.alloc(64);
  25. void *p = buffer.alloc(1);
  26. REQUIRE(0 == p);
  27. }
  28. SECTION("Returns NULL when buffer is too small") {
  29. void *p = buffer.alloc(65);
  30. REQUIRE(0 == p);
  31. }
  32. SECTION("Returns aligned pointers") {
  33. for (size_t size = 1; size <= sizeof(void *); size++) {
  34. void *p = buffer.alloc(1);
  35. REQUIRE(isAligned(p));
  36. }
  37. }
  38. SECTION("Returns same address after clear()") {
  39. void *p1 = buffer.alloc(1);
  40. buffer.clear();
  41. void *p2 = buffer.alloc(1);
  42. REQUIRE(p1 == p2);
  43. }
  44. }