allocString.cpp 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. // ArduinoJson - arduinojson.org
  2. // Copyright Benoit Blanchon 2014-2019
  3. // MIT License
  4. #include <ArduinoJson/Memory/MemoryPool.hpp>
  5. #include <catch.hpp>
  6. using namespace ARDUINOJSON_NAMESPACE;
  7. TEST_CASE("MemoryPool::allocFrozenString()") {
  8. const size_t poolCapacity = 64;
  9. const size_t longestString = poolCapacity;
  10. char buffer[poolCapacity];
  11. MemoryPool pool(buffer, poolCapacity);
  12. SECTION("Returns different addresses") {
  13. char *a = pool.allocFrozenString(1);
  14. char *b = pool.allocFrozenString(1);
  15. REQUIRE(a != b);
  16. }
  17. SECTION("Returns NULL when full") {
  18. void *p1 = pool.allocFrozenString(longestString);
  19. REQUIRE(p1 != 0);
  20. void *p2 = pool.allocFrozenString(1);
  21. REQUIRE(p2 == 0);
  22. }
  23. SECTION("Returns NULL when pool is too small") {
  24. void *p = pool.allocFrozenString(longestString + 1);
  25. REQUIRE(0 == p);
  26. }
  27. SECTION("Returns NULL when buffer is NULL") {
  28. MemoryPool pool2(0, poolCapacity);
  29. REQUIRE(0 == pool2.allocFrozenString(2));
  30. }
  31. SECTION("Returns NULL when capacity is 0") {
  32. MemoryPool pool2(buffer, 0);
  33. REQUIRE(0 == pool2.allocFrozenString(2));
  34. }
  35. SECTION("Returns same address after clear()") {
  36. void *a = pool.allocFrozenString(1);
  37. pool.clear();
  38. void *b = pool.allocFrozenString(1);
  39. REQUIRE(a == b);
  40. }
  41. SECTION("Can use full capacity when fresh") {
  42. void *a = pool.allocFrozenString(longestString);
  43. REQUIRE(a != 0);
  44. }
  45. SECTION("Can use full capacity after clear") {
  46. pool.allocFrozenString(longestString);
  47. pool.clear();
  48. void *a = pool.allocFrozenString(longestString);
  49. REQUIRE(a != 0);
  50. }
  51. }