BasicJsonDocument.cpp 1011 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. // ArduinoJson - arduinojson.org
  2. // Copyright Benoit Blanchon 2014-2019
  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(std::ostream& log) : _log(log) {}
  12. void* allocate(size_t n) {
  13. _log << "A" << n;
  14. return malloc(n);
  15. }
  16. void deallocate(void* p) {
  17. _log << "F";
  18. free(p);
  19. }
  20. private:
  21. SpyingAllocator& operator=(const SpyingAllocator& src);
  22. std::ostream& _log;
  23. };
  24. typedef BasicJsonDocument<SpyingAllocator> MyJsonDocument;
  25. TEST_CASE("BasicJsonDocument") {
  26. std::stringstream log;
  27. SECTION("Construct/Destruct") {
  28. { MyJsonDocument doc(4096, log); }
  29. REQUIRE(log.str() == "A4096F");
  30. }
  31. SECTION("Copy construct") {
  32. {
  33. MyJsonDocument doc1(4096, log);
  34. doc1.set(std::string("The size of this string is 32!!"));
  35. MyJsonDocument doc2(doc1);
  36. }
  37. REQUIRE(log.str() == "A4096A32FF");
  38. }
  39. }