size.cpp 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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. char buffer[4096];
  8. TEST_CASE("MemoryPool::capacity()") {
  9. const size_t capacity = 64;
  10. MemoryPool pool(buffer, capacity);
  11. REQUIRE(capacity == pool.capacity());
  12. }
  13. TEST_CASE("MemoryPool::size()") {
  14. MemoryPool pool(buffer, sizeof(buffer));
  15. SECTION("Initial size is 0") {
  16. REQUIRE(0 == pool.size());
  17. }
  18. SECTION("size() == capacity() after allocExpandableString()") {
  19. pool.allocExpandableString();
  20. REQUIRE(pool.size() == pool.capacity());
  21. }
  22. SECTION("Decreases after freezeString()") {
  23. StringSlot a = pool.allocExpandableString();
  24. pool.freezeString(a, 1);
  25. REQUIRE(pool.size() == JSON_STRING_SIZE(1));
  26. StringSlot b = pool.allocExpandableString();
  27. pool.freezeString(b, 1);
  28. REQUIRE(pool.size() == 2 * JSON_STRING_SIZE(1));
  29. }
  30. SECTION("Increases after allocFrozenString()") {
  31. pool.allocFrozenString(0);
  32. REQUIRE(pool.size() == JSON_STRING_SIZE(0));
  33. pool.allocFrozenString(0);
  34. REQUIRE(pool.size() == 2 * JSON_STRING_SIZE(0));
  35. }
  36. SECTION("Doesn't grow when memory pool is full") {
  37. const size_t variantCount = sizeof(buffer) / sizeof(VariantSlot);
  38. for (size_t i = 0; i < variantCount; i++) pool.allocVariant();
  39. size_t size = pool.size();
  40. pool.allocVariant();
  41. REQUIRE(size == pool.size());
  42. }
  43. }