size.cpp 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. // ArduinoJson - arduinojson.org
  2. // Copyright Benoit Blanchon 2014-2020
  3. // MIT License
  4. #include <ArduinoJson/Memory/MemoryPool.hpp>
  5. #include <catch.hpp>
  6. using namespace ARDUINOJSON_NAMESPACE;
  7. TEST_CASE("MemoryPool::capacity()") {
  8. char buffer[4096];
  9. const size_t capacity = 64;
  10. MemoryPool pool(buffer, capacity);
  11. REQUIRE(capacity == pool.capacity());
  12. }
  13. TEST_CASE("MemoryPool::size()") {
  14. char buffer[4096];
  15. MemoryPool pool(buffer, sizeof(buffer));
  16. SECTION("Initial size is 0") {
  17. REQUIRE(0 == pool.size());
  18. }
  19. SECTION("size() == capacity() after allocExpandableString()") {
  20. pool.allocExpandableString();
  21. REQUIRE(pool.size() == pool.capacity());
  22. }
  23. SECTION("Decreases after freezeString()") {
  24. StringSlot a = pool.allocExpandableString();
  25. pool.freezeString(a, 1);
  26. REQUIRE(pool.size() == JSON_STRING_SIZE(1));
  27. StringSlot b = pool.allocExpandableString();
  28. pool.freezeString(b, 1);
  29. REQUIRE(pool.size() == 2 * JSON_STRING_SIZE(1));
  30. }
  31. SECTION("Increases after allocFrozenString()") {
  32. pool.allocFrozenString(0);
  33. REQUIRE(pool.size() == JSON_STRING_SIZE(0));
  34. pool.allocFrozenString(0);
  35. REQUIRE(pool.size() == 2 * JSON_STRING_SIZE(0));
  36. }
  37. SECTION("Doesn't grow when memory pool is full") {
  38. const size_t variantCount = sizeof(buffer) / sizeof(VariantSlot);
  39. for (size_t i = 0; i < variantCount; i++) pool.allocVariant();
  40. size_t size = pool.size();
  41. pool.allocVariant();
  42. REQUIRE(size == pool.size());
  43. }
  44. }