size.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. char buffer[4096];
  8. TEST_CASE("MemoryPool::capacity()") {
  9. const size_t capacity = 64;
  10. MemoryPool memoryPool(buffer, capacity);
  11. REQUIRE(capacity == memoryPool.capacity());
  12. }
  13. TEST_CASE("MemoryPool::size()") {
  14. MemoryPool memoryPool(buffer, sizeof(buffer));
  15. SECTION("Initial size is 0") {
  16. REQUIRE(0 == memoryPool.size());
  17. }
  18. SECTION("size() == capacity() after allocExpandableString()") {
  19. memoryPool.allocExpandableString();
  20. REQUIRE(memoryPool.size() == memoryPool.capacity());
  21. }
  22. SECTION("Decreases after freezeString()") {
  23. StringSlot* a = memoryPool.allocExpandableString();
  24. memoryPool.freezeString(a, 1);
  25. REQUIRE(memoryPool.size() == JSON_STRING_SIZE(1));
  26. StringSlot* b = memoryPool.allocExpandableString();
  27. memoryPool.freezeString(b, 1);
  28. REQUIRE(memoryPool.size() == 2 * JSON_STRING_SIZE(1));
  29. }
  30. SECTION("Increases after allocFrozenString()") {
  31. memoryPool.allocFrozenString(0);
  32. REQUIRE(memoryPool.size() == JSON_STRING_SIZE(0));
  33. memoryPool.allocFrozenString(0);
  34. REQUIRE(memoryPool.size() == 2 * JSON_STRING_SIZE(0));
  35. }
  36. SECTION("Decreases after freeVariant()") {
  37. VariantSlot* a = memoryPool.allocVariant();
  38. VariantSlot* b = memoryPool.allocVariant();
  39. memoryPool.freeVariant(b);
  40. REQUIRE(memoryPool.size() == sizeof(VariantSlot));
  41. memoryPool.freeVariant(a);
  42. REQUIRE(memoryPool.size() == 0);
  43. }
  44. SECTION("Decreases after calling freeString() in order") {
  45. StringSlot* a = memoryPool.allocFrozenString(5);
  46. REQUIRE(a != 0);
  47. StringSlot* b = memoryPool.allocFrozenString(6);
  48. REQUIRE(b != 0);
  49. memoryPool.freeString(b);
  50. REQUIRE(memoryPool.size() == JSON_STRING_SIZE(5));
  51. memoryPool.freeString(a);
  52. REQUIRE(memoryPool.size() == 0);
  53. }
  54. SECTION("Decreases after calling freeString() in reverse order") {
  55. StringSlot* a = memoryPool.allocFrozenString(5);
  56. REQUIRE(a != 0);
  57. StringSlot* b = memoryPool.allocFrozenString(6);
  58. REQUIRE(b != 0);
  59. memoryPool.freeString(a);
  60. REQUIRE(memoryPool.size() == JSON_STRING_SIZE(6));
  61. memoryPool.freeString(b);
  62. REQUIRE(memoryPool.size() == 0);
  63. }
  64. SECTION("Doesn't grow when memory pool is full") {
  65. const size_t variantCount = sizeof(buffer) / sizeof(VariantSlot);
  66. for (size_t i = 0; i < variantCount; i++) memoryPool.allocVariant();
  67. size_t size = memoryPool.size();
  68. memoryPool.allocVariant();
  69. REQUIRE(size == memoryPool.size());
  70. }
  71. }