copy.cpp 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. // ArduinoJson - arduinojson.org
  2. // Copyright Benoit Blanchon 2014-2019
  3. // MIT License
  4. #include <ArduinoJson.h>
  5. #include <catch.hpp>
  6. TEST_CASE("JsonVariant::set(JsonVariant)") {
  7. DynamicJsonDocument doc1(4096);
  8. DynamicJsonDocument doc2(4096);
  9. JsonVariant var1 = doc1.to<JsonVariant>();
  10. JsonVariant var2 = doc2.to<JsonVariant>();
  11. SECTION("stores JsonArray by copy") {
  12. JsonArray arr = doc2.to<JsonArray>();
  13. JsonObject obj = arr.createNestedObject();
  14. obj["hello"] = "world";
  15. var1.set(arr);
  16. arr[0] = 666;
  17. REQUIRE(var1.as<std::string>() == "[{\"hello\":\"world\"}]");
  18. }
  19. SECTION("stores JsonObject by copy") {
  20. JsonObject obj = doc2.to<JsonObject>();
  21. JsonArray arr = obj.createNestedArray("value");
  22. arr.add(42);
  23. var1.set(obj);
  24. obj["value"] = 666;
  25. REQUIRE(var1.as<std::string>() == "{\"value\":[42]}");
  26. }
  27. SECTION("stores const char* by reference") {
  28. var1.set("hello!!");
  29. var2.set(var1);
  30. REQUIRE(doc1.memoryUsage() == 0);
  31. REQUIRE(doc2.memoryUsage() == 0);
  32. }
  33. SECTION("stores char* by copy") {
  34. char str[] = "hello!!";
  35. var1.set(str);
  36. var2.set(var1);
  37. REQUIRE(doc1.memoryUsage() == JSON_STRING_SIZE(8));
  38. REQUIRE(doc2.memoryUsage() == JSON_STRING_SIZE(8));
  39. }
  40. SECTION("stores std::string by copy") {
  41. var1.set(std::string("hello!!"));
  42. var2.set(var1);
  43. REQUIRE(doc1.memoryUsage() == JSON_STRING_SIZE(8));
  44. REQUIRE(doc2.memoryUsage() == JSON_STRING_SIZE(8));
  45. }
  46. SECTION("stores Serialized<const char*> by reference") {
  47. var1.set(serialized("hello!!", JSON_STRING_SIZE(8)));
  48. var2.set(var1);
  49. REQUIRE(doc1.memoryUsage() == 0);
  50. REQUIRE(doc2.memoryUsage() == 0);
  51. }
  52. SECTION("stores Serialized<char*> by copy") {
  53. char str[] = "hello!!";
  54. var1.set(serialized(str, 8));
  55. var2.set(var1);
  56. REQUIRE(doc1.memoryUsage() == JSON_STRING_SIZE(8));
  57. REQUIRE(doc2.memoryUsage() == JSON_STRING_SIZE(8));
  58. }
  59. SECTION("stores Serialized<std::string> by copy") {
  60. var1.set(serialized(std::string("hello!!!")));
  61. var2.set(var1);
  62. REQUIRE(doc1.memoryUsage() == JSON_STRING_SIZE(8));
  63. REQUIRE(doc2.memoryUsage() == JSON_STRING_SIZE(8));
  64. }
  65. }