BasicJsonDocument.cpp 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. // ArduinoJson - arduinojson.org
  2. // Copyright Benoit Blanchon 2014-2020
  3. // MIT License
  4. #include <ArduinoJson.h>
  5. #include <stdlib.h> // malloc, free
  6. #include <catch.hpp>
  7. #include <sstream>
  8. using ARDUINOJSON_NAMESPACE::addPadding;
  9. class SpyingAllocator {
  10. public:
  11. SpyingAllocator(const SpyingAllocator& src) : _log(src._log) {}
  12. SpyingAllocator(std::ostream& log) : _log(log) {}
  13. void* allocate(size_t n) {
  14. _log << "A" << n;
  15. return malloc(n);
  16. }
  17. void deallocate(void* p) {
  18. _log << "F";
  19. free(p);
  20. }
  21. private:
  22. SpyingAllocator& operator=(const SpyingAllocator& src);
  23. std::ostream& _log;
  24. };
  25. typedef BasicJsonDocument<SpyingAllocator> MyJsonDocument;
  26. TEST_CASE("BasicJsonDocument") {
  27. std::stringstream log;
  28. SECTION("Construct/Destruct") {
  29. { MyJsonDocument doc(4096, log); }
  30. REQUIRE(log.str() == "A4096F");
  31. }
  32. SECTION("Copy construct") {
  33. {
  34. MyJsonDocument doc1(4096, log);
  35. doc1.set(std::string("The size of this string is 32!!"));
  36. MyJsonDocument doc2(doc1);
  37. }
  38. REQUIRE(log.str() == "A4096A32FF");
  39. }
  40. }