allocString.cpp 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. TEST_CASE("MemoryPool::allocFrozenString()") {
  8. const size_t poolCapacity = 64;
  9. const size_t longestString = poolCapacity - sizeof(StringSlot);
  10. char buffer[poolCapacity];
  11. MemoryPool pool(buffer, poolCapacity);
  12. SECTION("Returns different addresses") {
  13. StringSlot *a = pool.allocFrozenString(1);
  14. StringSlot *b = pool.allocFrozenString(1);
  15. REQUIRE(a != b);
  16. REQUIRE(a->value != b->value);
  17. }
  18. SECTION("Returns a StringSlot of the right size") {
  19. StringSlot *s = pool.allocFrozenString(12);
  20. REQUIRE(s->size == 12);
  21. }
  22. SECTION("Returns NULL when full") {
  23. pool.allocFrozenString(longestString);
  24. void *p = pool.allocFrozenString(1);
  25. REQUIRE(0 == p);
  26. }
  27. SECTION("Returns NULL when pool is too small") {
  28. void *p = pool.allocFrozenString(longestString + 1);
  29. REQUIRE(0 == p);
  30. }
  31. SECTION("Returns NULL when buffer is NULL") {
  32. MemoryPool pool2(0, poolCapacity);
  33. REQUIRE(0 == pool2.allocFrozenString(2));
  34. }
  35. SECTION("Returns NULL when capacity is 0") {
  36. MemoryPool pool2(buffer, 0);
  37. REQUIRE(0 == pool2.allocFrozenString(2));
  38. }
  39. SECTION("Returns aligned pointers") {
  40. REQUIRE(isAligned(pool.allocFrozenString(1)));
  41. REQUIRE(isAligned(pool.allocFrozenString(1)));
  42. }
  43. SECTION("Returns same address after clear()") {
  44. StringSlot *a = pool.allocFrozenString(1);
  45. pool.clear();
  46. StringSlot *b = pool.allocFrozenString(1);
  47. REQUIRE(a == b);
  48. REQUIRE(a->value == b->value);
  49. }
  50. SECTION("Can use full capacity when fresh") {
  51. StringSlot *a = pool.allocFrozenString(longestString);
  52. REQUIRE(a != 0);
  53. }
  54. SECTION("Can use full capacity after clear") {
  55. pool.allocFrozenString(longestString);
  56. pool.clear();
  57. StringSlot *a = pool.allocFrozenString(longestString);
  58. REQUIRE(a != 0);
  59. }
  60. }