StringBuilder.cpp 948 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. // ArduinoJson - arduinojson.org
  2. // Copyright Benoit Blanchon 2014-2019
  3. // MIT License
  4. #include <ArduinoJson/Memory/MemoryPool.hpp>
  5. #include <ArduinoJson/Memory/StringBuilder.hpp>
  6. #include <catch.hpp>
  7. using namespace ARDUINOJSON_NAMESPACE;
  8. static char buffer[4096];
  9. TEST_CASE("StringBuilder") {
  10. SECTION("Works when buffer is big enough") {
  11. MemoryPool pool(buffer, addPadding(JSON_STRING_SIZE(6)));
  12. StringBuilder str(&pool);
  13. str.append("hello");
  14. REQUIRE(str.complete() == std::string("hello"));
  15. }
  16. SECTION("Returns null when too small") {
  17. MemoryPool pool(buffer, sizeof(void*));
  18. StringBuilder str(&pool);
  19. str.append("hello world!");
  20. REQUIRE(str.complete() == 0);
  21. }
  22. SECTION("Increases size of memory pool") {
  23. MemoryPool pool(buffer, addPadding(JSON_STRING_SIZE(6)));
  24. StringBuilder str(&pool);
  25. str.append('h');
  26. str.complete();
  27. REQUIRE(JSON_STRING_SIZE(2) == pool.size());
  28. }
  29. }