| 
									
										
										
										
											2021-03-29 17:14:01 +02:00
										 |  |  | // ArduinoJson - https://arduinojson.org
 | 
					
						
							| 
									
										
										
										
											2025-02-24 15:18:26 +01:00
										 |  |  | // Copyright © 2014-2025, Benoit BLANCHON
 | 
					
						
							| 
									
										
										
										
											2018-11-12 18:28:34 +01:00
										 |  |  | // MIT License
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <ArduinoJson.h>
 | 
					
						
							|  |  |  | #include <catch.hpp>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-11 10:03:47 +02:00
										 |  |  | #include "Allocators.hpp"
 | 
					
						
							| 
									
										
										
										
											2024-06-07 09:35:45 +02:00
										 |  |  | #include "Literals.hpp"
 | 
					
						
							| 
									
										
										
										
											2023-04-11 10:03:47 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-17 09:46:41 +02:00
										 |  |  | using ArduinoJson::detail::sizeofObject; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-26 16:16:20 +01:00
										 |  |  | enum ErrorCode { ERROR_01 = 1, ERROR_10 = 10 }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-05 17:33:47 +02:00
										 |  |  | TEST_CASE("JsonVariant::set() when there is enough memory") { | 
					
						
							| 
									
										
										
										
											2024-08-27 14:40:24 +02:00
										 |  |  |   SpyingAllocator spy; | 
					
						
							|  |  |  |   JsonDocument doc(&spy); | 
					
						
							| 
									
										
										
										
											2018-11-12 18:28:34 +01:00
										 |  |  |   JsonVariant variant = doc.to<JsonVariant>(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-25 11:32:03 +01:00
										 |  |  |   SECTION("string literal") { | 
					
						
							|  |  |  |     bool result = variant.set("hello\0world"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     REQUIRE(result == true); | 
					
						
							|  |  |  |     CHECK(variant == | 
					
						
							|  |  |  |           "hello"_s);  // linked string cannot contain '\0' at the moment
 | 
					
						
							|  |  |  |     CHECK(spy.log() == AllocatorLog{}); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-05 17:33:47 +02:00
										 |  |  |   SECTION("const char*") { | 
					
						
							| 
									
										
										
										
											2018-11-12 18:28:34 +01:00
										 |  |  |     char str[16]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     strcpy(str, "hello"); | 
					
						
							| 
									
										
										
										
											2022-08-09 16:52:17 +02:00
										 |  |  |     bool result = variant.set(static_cast<const char*>(str)); | 
					
						
							| 
									
										
										
										
											2018-11-12 18:28:34 +01:00
										 |  |  |     strcpy(str, "world"); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-05 17:33:47 +02:00
										 |  |  |     REQUIRE(result == true); | 
					
						
							| 
									
										
										
										
											2024-11-25 11:32:03 +01:00
										 |  |  |     REQUIRE(variant == "hello");  // stores by copy
 | 
					
						
							|  |  |  |     REQUIRE(spy.log() == AllocatorLog{ | 
					
						
							|  |  |  |                              Allocate(sizeofString("hello")), | 
					
						
							|  |  |  |                          }); | 
					
						
							| 
									
										
										
										
											2018-11-12 18:28:34 +01:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-05 17:33:47 +02:00
										 |  |  |   SECTION("(const char*)0") { | 
					
						
							| 
									
										
										
										
											2022-08-09 16:52:17 +02:00
										 |  |  |     bool result = variant.set(static_cast<const char*>(0)); | 
					
						
							| 
									
										
										
										
											2020-09-05 17:33:47 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     REQUIRE(result == true); | 
					
						
							|  |  |  |     REQUIRE(variant.isNull()); | 
					
						
							| 
									
										
										
										
											2024-11-25 11:32:03 +01:00
										 |  |  |     REQUIRE(variant.as<const char*>() == nullptr); | 
					
						
							| 
									
										
										
										
											2024-08-27 14:40:24 +02:00
										 |  |  |     REQUIRE(spy.log() == AllocatorLog{}); | 
					
						
							| 
									
										
										
										
											2020-09-05 17:33:47 +02:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   SECTION("char*") { | 
					
						
							| 
									
										
										
										
											2018-11-12 18:28:34 +01:00
										 |  |  |     char str[16]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     strcpy(str, "hello"); | 
					
						
							| 
									
										
										
										
											2020-09-05 17:33:47 +02:00
										 |  |  |     bool result = variant.set(str); | 
					
						
							| 
									
										
										
										
											2018-11-12 18:28:34 +01:00
										 |  |  |     strcpy(str, "world"); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-05 17:33:47 +02:00
										 |  |  |     REQUIRE(result == true); | 
					
						
							|  |  |  |     REQUIRE(variant == "hello");  // stores by copy
 | 
					
						
							| 
									
										
										
										
											2024-08-27 14:40:24 +02:00
										 |  |  |     REQUIRE(spy.log() == AllocatorLog{ | 
					
						
							|  |  |  |                              Allocate(sizeofString("hello")), | 
					
						
							|  |  |  |                          }); | 
					
						
							| 
									
										
										
										
											2018-11-12 18:28:34 +01:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-05 17:33:47 +02:00
										 |  |  |   SECTION("(char*)0") { | 
					
						
							| 
									
										
										
										
											2022-08-09 16:52:17 +02:00
										 |  |  |     bool result = variant.set(static_cast<char*>(0)); | 
					
						
							| 
									
										
										
										
											2020-09-05 17:33:47 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     REQUIRE(result == true); | 
					
						
							|  |  |  |     REQUIRE(variant.isNull()); | 
					
						
							| 
									
										
										
										
											2024-08-27 14:40:24 +02:00
										 |  |  |     REQUIRE(spy.log() == AllocatorLog{}); | 
					
						
							| 
									
										
										
										
											2020-09-05 17:33:47 +02:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   SECTION("unsigned char*") { | 
					
						
							| 
									
										
										
										
											2018-11-12 18:28:34 +01:00
										 |  |  |     char str[16]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     strcpy(str, "hello"); | 
					
						
							| 
									
										
										
										
											2022-08-09 16:52:17 +02:00
										 |  |  |     bool result = variant.set(reinterpret_cast<unsigned char*>(str)); | 
					
						
							| 
									
										
										
										
											2018-11-12 18:28:34 +01:00
										 |  |  |     strcpy(str, "world"); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-05 17:33:47 +02:00
										 |  |  |     REQUIRE(result == true); | 
					
						
							|  |  |  |     REQUIRE(variant == "hello");  // stores by copy
 | 
					
						
							| 
									
										
										
										
											2024-08-27 14:40:24 +02:00
										 |  |  |     REQUIRE(spy.log() == AllocatorLog{ | 
					
						
							|  |  |  |                              Allocate(sizeofString("hello")), | 
					
						
							|  |  |  |                          }); | 
					
						
							| 
									
										
										
										
											2018-11-12 18:28:34 +01:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-05 17:33:47 +02:00
										 |  |  |   SECTION("signed char*") { | 
					
						
							| 
									
										
										
										
											2018-11-12 18:28:34 +01:00
										 |  |  |     char str[16]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     strcpy(str, "hello"); | 
					
						
							| 
									
										
										
										
											2022-08-09 16:52:17 +02:00
										 |  |  |     bool result = variant.set(reinterpret_cast<signed char*>(str)); | 
					
						
							| 
									
										
										
										
											2018-11-12 18:28:34 +01:00
										 |  |  |     strcpy(str, "world"); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-05 17:33:47 +02:00
										 |  |  |     REQUIRE(result == true); | 
					
						
							|  |  |  |     REQUIRE(variant == "hello");  // stores by copy
 | 
					
						
							| 
									
										
										
										
											2024-08-27 14:40:24 +02:00
										 |  |  |     REQUIRE(spy.log() == AllocatorLog{ | 
					
						
							|  |  |  |                              Allocate(sizeofString("hello")), | 
					
						
							|  |  |  |                          }); | 
					
						
							| 
									
										
										
										
											2018-11-12 18:28:34 +01:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #ifdef HAS_VARIABLE_LENGTH_ARRAY
 | 
					
						
							| 
									
										
										
										
											2020-09-05 17:33:47 +02:00
										 |  |  |   SECTION("VLA") { | 
					
						
							| 
									
										
										
										
											2022-02-25 09:23:51 +01:00
										 |  |  |     size_t n = 16; | 
					
						
							| 
									
										
										
										
											2018-11-12 18:28:34 +01:00
										 |  |  |     char str[n]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     strcpy(str, "hello"); | 
					
						
							| 
									
										
										
										
											2020-09-05 17:33:47 +02:00
										 |  |  |     bool result = variant.set(str); | 
					
						
							| 
									
										
										
										
											2018-11-12 18:28:34 +01:00
										 |  |  |     strcpy(str, "world"); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-05 17:33:47 +02:00
										 |  |  |     REQUIRE(result == true); | 
					
						
							|  |  |  |     REQUIRE(variant == "hello");  // stores by copy
 | 
					
						
							| 
									
										
										
										
											2024-08-27 14:40:24 +02:00
										 |  |  |     REQUIRE(spy.log() == AllocatorLog{ | 
					
						
							|  |  |  |                              Allocate(sizeofString("hello")), | 
					
						
							|  |  |  |                          }); | 
					
						
							| 
									
										
										
										
											2018-11-12 18:28:34 +01:00
										 |  |  |   } | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-05 17:33:47 +02:00
										 |  |  |   SECTION("std::string") { | 
					
						
							| 
									
										
										
										
											2024-11-25 11:32:03 +01:00
										 |  |  |     std::string str = "hello\0world"_s; | 
					
						
							| 
									
										
										
										
											2020-09-05 17:33:47 +02:00
										 |  |  |     bool result = variant.set(str); | 
					
						
							| 
									
										
										
										
											2018-11-12 18:28:34 +01:00
										 |  |  |     str.replace(0, 5, "world"); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-05 17:33:47 +02:00
										 |  |  |     REQUIRE(result == true); | 
					
						
							| 
									
										
										
										
											2024-11-25 11:32:03 +01:00
										 |  |  |     REQUIRE(variant == "hello\0world"_s);  // stores by copy
 | 
					
						
							| 
									
										
										
										
											2024-08-27 14:40:24 +02:00
										 |  |  |     REQUIRE(spy.log() == AllocatorLog{ | 
					
						
							| 
									
										
										
										
											2024-11-25 11:32:03 +01:00
										 |  |  |                              Allocate(sizeofString("hello?world")), | 
					
						
							| 
									
										
										
										
											2024-08-27 14:40:24 +02:00
										 |  |  |                          }); | 
					
						
							| 
									
										
										
										
											2018-11-12 18:28:34 +01:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2019-01-29 17:00:11 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-05 17:33:47 +02:00
										 |  |  |   SECTION("static JsonString") { | 
					
						
							| 
									
										
										
										
											2019-01-29 17:00:11 +01:00
										 |  |  |     char str[16]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     strcpy(str, "hello"); | 
					
						
							| 
									
										
										
										
											2024-11-26 14:32:29 +01:00
										 |  |  |     bool result = variant.set(JsonString(str, true)); | 
					
						
							| 
									
										
										
										
											2019-01-29 17:00:11 +01:00
										 |  |  |     strcpy(str, "world"); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-05 17:33:47 +02:00
										 |  |  |     REQUIRE(result == true); | 
					
						
							|  |  |  |     REQUIRE(variant == "world");  // stores by pointer
 | 
					
						
							| 
									
										
										
										
											2024-08-27 14:40:24 +02:00
										 |  |  |     REQUIRE(spy.log() == AllocatorLog{}); | 
					
						
							| 
									
										
										
										
											2019-01-29 17:00:11 +01:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-05 17:33:47 +02:00
										 |  |  |   SECTION("non-static JsonString") { | 
					
						
							| 
									
										
										
										
											2019-01-29 17:00:11 +01:00
										 |  |  |     char str[16]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     strcpy(str, "hello"); | 
					
						
							| 
									
										
										
										
											2024-11-26 14:32:29 +01:00
										 |  |  |     bool result = variant.set(JsonString(str)); | 
					
						
							| 
									
										
										
										
											2019-01-29 17:00:11 +01:00
										 |  |  |     strcpy(str, "world"); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-05 17:33:47 +02:00
										 |  |  |     REQUIRE(result == true); | 
					
						
							|  |  |  |     REQUIRE(variant == "hello");  // stores by copy
 | 
					
						
							| 
									
										
										
										
											2024-08-27 14:40:24 +02:00
										 |  |  |     REQUIRE(spy.log() == AllocatorLog{ | 
					
						
							|  |  |  |                              Allocate(sizeofString("hello")), | 
					
						
							|  |  |  |                          }); | 
					
						
							| 
									
										
										
										
											2019-01-29 17:00:11 +01:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2020-02-26 16:16:20 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-05 17:33:47 +02:00
										 |  |  |   SECTION("enum") { | 
					
						
							| 
									
										
										
										
											2020-02-26 16:16:20 +01:00
										 |  |  |     ErrorCode code = ERROR_10; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-05 17:33:47 +02:00
										 |  |  |     bool result = variant.set(code); | 
					
						
							| 
									
										
										
										
											2020-02-26 16:16:20 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-05 17:33:47 +02:00
										 |  |  |     REQUIRE(result == true); | 
					
						
							| 
									
										
										
										
											2020-02-26 16:16:20 +01:00
										 |  |  |     REQUIRE(variant.is<int>() == true); | 
					
						
							|  |  |  |     REQUIRE(variant.as<int>() == 10); | 
					
						
							| 
									
										
										
										
											2024-08-27 14:40:24 +02:00
										 |  |  |     REQUIRE(spy.log() == AllocatorLog{}); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   SECTION("float") { | 
					
						
							|  |  |  |     bool result = variant.set(1.2f); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     REQUIRE(result == true); | 
					
						
							|  |  |  |     REQUIRE(variant.is<float>() == true); | 
					
						
							|  |  |  |     REQUIRE(variant.as<float>() == 1.2f); | 
					
						
							|  |  |  |     REQUIRE(spy.log() == AllocatorLog{}); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   SECTION("double") { | 
					
						
							|  |  |  |     bool result = variant.set(1.2); | 
					
						
							|  |  |  |     doc.shrinkToFit(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     REQUIRE(result == true); | 
					
						
							|  |  |  |     REQUIRE(variant.is<double>() == true); | 
					
						
							|  |  |  |     REQUIRE(variant.as<double>() == 1.2); | 
					
						
							|  |  |  |     REQUIRE(spy.log() == | 
					
						
							|  |  |  |             AllocatorLog{ | 
					
						
							|  |  |  |                 Allocate(sizeofPool()), | 
					
						
							|  |  |  |                 Reallocate(sizeofPool(), sizeofPool(1)),  // one extension slot
 | 
					
						
							|  |  |  |             }); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   SECTION("int32_t") { | 
					
						
							|  |  |  |     bool result = variant.set(int32_t(42)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     REQUIRE(result == true); | 
					
						
							|  |  |  |     REQUIRE(variant.is<int32_t>() == true); | 
					
						
							|  |  |  |     REQUIRE(variant.as<int32_t>() == 42); | 
					
						
							|  |  |  |     REQUIRE(spy.log() == AllocatorLog{}); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   SECTION("int64_t") { | 
					
						
							| 
									
										
										
										
											2024-08-27 14:51:22 +02:00
										 |  |  |     bool result = variant.set(int64_t(-2147483649LL)); | 
					
						
							| 
									
										
										
										
											2024-08-27 14:40:24 +02:00
										 |  |  |     doc.shrinkToFit(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     REQUIRE(result == true); | 
					
						
							|  |  |  |     REQUIRE(variant.is<int64_t>() == true); | 
					
						
							| 
									
										
										
										
											2024-08-27 14:51:22 +02:00
										 |  |  |     REQUIRE(variant.as<int64_t>() == -2147483649LL); | 
					
						
							| 
									
										
										
										
											2024-08-27 14:40:24 +02:00
										 |  |  |     REQUIRE(spy.log() == | 
					
						
							|  |  |  |             AllocatorLog{ | 
					
						
							|  |  |  |                 Allocate(sizeofPool()), | 
					
						
							|  |  |  |                 Reallocate(sizeofPool(), sizeofPool(1)),  // one extension slot
 | 
					
						
							|  |  |  |             }); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   SECTION("uint32_t") { | 
					
						
							|  |  |  |     bool result = variant.set(uint32_t(42)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     REQUIRE(result == true); | 
					
						
							|  |  |  |     REQUIRE(variant.is<uint32_t>() == true); | 
					
						
							|  |  |  |     REQUIRE(variant.as<uint32_t>() == 42); | 
					
						
							|  |  |  |     REQUIRE(spy.log() == AllocatorLog{}); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   SECTION("uint64_t") { | 
					
						
							|  |  |  |     bool result = variant.set(uint64_t(4294967296)); | 
					
						
							|  |  |  |     doc.shrinkToFit(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     REQUIRE(result == true); | 
					
						
							|  |  |  |     REQUIRE(variant.is<uint64_t>() == true); | 
					
						
							|  |  |  |     REQUIRE(variant.as<uint64_t>() == 4294967296); | 
					
						
							|  |  |  |     REQUIRE(spy.log() == | 
					
						
							|  |  |  |             AllocatorLog{ | 
					
						
							|  |  |  |                 Allocate(sizeofPool()), | 
					
						
							|  |  |  |                 Reallocate(sizeofPool(), sizeofPool(1)),  // one extension slot
 | 
					
						
							|  |  |  |             }); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   SECTION("JsonDocument") { | 
					
						
							|  |  |  |     JsonDocument doc1; | 
					
						
							|  |  |  |     doc1["hello"] = "world"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // Should copy the doc
 | 
					
						
							|  |  |  |     variant.set(doc1); | 
					
						
							|  |  |  |     doc1.clear(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     std::string json; | 
					
						
							|  |  |  |     serializeJson(doc, json); | 
					
						
							|  |  |  |     REQUIRE(json == "{\"hello\":\"world\"}"); | 
					
						
							| 
									
										
										
										
											2020-02-26 16:16:20 +01:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2018-11-12 18:28:34 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-05 17:33:47 +02:00
										 |  |  | TEST_CASE("JsonVariant::set() with not enough memory") { | 
					
						
							| 
									
										
										
										
											2023-07-17 18:15:13 +02:00
										 |  |  |   JsonDocument doc(FailingAllocator::instance()); | 
					
						
							| 
									
										
										
										
											2018-11-12 18:28:34 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |   JsonVariant v = doc.to<JsonVariant>(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   SECTION("std::string") { | 
					
						
							| 
									
										
										
										
											2024-06-07 09:35:45 +02:00
										 |  |  |     bool result = v.set("hello world!!"_s); | 
					
						
							| 
									
										
										
										
											2020-09-05 17:33:47 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     REQUIRE(result == false); | 
					
						
							| 
									
										
										
										
											2018-11-12 18:28:34 +01:00
										 |  |  |     REQUIRE(v.isNull()); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   SECTION("Serialized<std::string>") { | 
					
						
							| 
									
										
										
										
											2024-06-07 09:35:45 +02:00
										 |  |  |     bool result = v.set(serialized("hello world!!"_s)); | 
					
						
							| 
									
										
										
										
											2020-09-05 17:33:47 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     REQUIRE(result == false); | 
					
						
							|  |  |  |     REQUIRE(v.isNull()); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   SECTION("char*") { | 
					
						
							|  |  |  |     char s[] = "hello world!!"; | 
					
						
							|  |  |  |     bool result = v.set(s); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     REQUIRE(result == false); | 
					
						
							| 
									
										
										
										
											2018-11-12 18:28:34 +01:00
										 |  |  |     REQUIRE(v.isNull()); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2019-06-25 08:56:14 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-27 14:40:24 +02:00
										 |  |  |   SECTION("float") { | 
					
						
							|  |  |  |     bool result = v.set(1.2f); | 
					
						
							| 
									
										
										
										
											2019-06-25 08:56:14 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-27 14:40:24 +02:00
										 |  |  |     REQUIRE(result == true); | 
					
						
							|  |  |  |     REQUIRE(v.is<float>()); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2019-06-25 08:56:14 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-27 14:40:24 +02:00
										 |  |  |   SECTION("double") { | 
					
						
							|  |  |  |     bool result = v.set(1.2); | 
					
						
							| 
									
										
										
										
											2019-06-25 08:56:14 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-27 14:40:24 +02:00
										 |  |  |     REQUIRE(result == false); | 
					
						
							|  |  |  |     REQUIRE(v.isNull()); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2024-08-27 14:51:22 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |   SECTION("int32_t") { | 
					
						
							|  |  |  |     bool result = v.set(-42); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     REQUIRE(result == true); | 
					
						
							|  |  |  |     REQUIRE(v.is<int32_t>()); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   SECTION("int64_t") { | 
					
						
							|  |  |  |     bool result = v.set(-2147483649LL); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     REQUIRE(result == false); | 
					
						
							|  |  |  |     REQUIRE(v.isNull()); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   SECTION("uint32_t") { | 
					
						
							|  |  |  |     bool result = v.set(42); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     REQUIRE(result == true); | 
					
						
							|  |  |  |     REQUIRE(v.is<uint32_t>()); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   SECTION("uint64_t") { | 
					
						
							|  |  |  |     bool result = v.set(4294967296U); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     REQUIRE(result == false); | 
					
						
							|  |  |  |     REQUIRE(v.isNull()); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2019-06-25 08:56:14 +02:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2023-04-17 09:46:41 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | TEST_CASE("JsonVariant::set() releases the previous value") { | 
					
						
							| 
									
										
										
										
											2023-07-25 14:53:54 +02:00
										 |  |  |   SpyingAllocator spy; | 
					
						
							|  |  |  |   JsonDocument doc(&spy); | 
					
						
							| 
									
										
										
										
											2024-06-07 09:35:45 +02:00
										 |  |  |   doc["hello"] = "world"_s; | 
					
						
							| 
									
										
										
										
											2023-07-25 14:53:54 +02:00
										 |  |  |   spy.clearLog(); | 
					
						
							| 
									
										
										
										
											2023-04-17 09:46:41 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |   JsonVariant v = doc["hello"]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   SECTION("int") { | 
					
						
							|  |  |  |     v.set(42); | 
					
						
							| 
									
										
										
										
											2023-07-26 06:06:38 +02:00
										 |  |  |     REQUIRE(spy.log() == AllocatorLog{ | 
					
						
							|  |  |  |                              Deallocate(sizeofString("world")), | 
					
						
							|  |  |  |                          }); | 
					
						
							| 
									
										
										
										
											2023-04-17 09:46:41 +02:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   SECTION("bool") { | 
					
						
							|  |  |  |     v.set(false); | 
					
						
							| 
									
										
										
										
											2023-07-26 06:06:38 +02:00
										 |  |  |     REQUIRE(spy.log() == AllocatorLog{ | 
					
						
							|  |  |  |                              Deallocate(sizeofString("world")), | 
					
						
							|  |  |  |                          }); | 
					
						
							| 
									
										
										
										
											2023-04-17 09:46:41 +02:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   SECTION("const char*") { | 
					
						
							|  |  |  |     v.set("hello"); | 
					
						
							| 
									
										
										
										
											2023-07-26 06:06:38 +02:00
										 |  |  |     REQUIRE(spy.log() == AllocatorLog{ | 
					
						
							|  |  |  |                              Deallocate(sizeofString("world")), | 
					
						
							|  |  |  |                          }); | 
					
						
							| 
									
										
										
										
											2023-04-17 09:46:41 +02:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   SECTION("float") { | 
					
						
							|  |  |  |     v.set(1.2); | 
					
						
							| 
									
										
										
										
											2023-07-26 06:06:38 +02:00
										 |  |  |     REQUIRE(spy.log() == AllocatorLog{ | 
					
						
							|  |  |  |                              Deallocate(sizeofString("world")), | 
					
						
							|  |  |  |                          }); | 
					
						
							| 
									
										
										
										
											2023-04-17 09:46:41 +02:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   SECTION("Serialized<const char*>") { | 
					
						
							|  |  |  |     v.set(serialized("[]")); | 
					
						
							| 
									
										
										
										
											2023-07-26 06:06:38 +02:00
										 |  |  |     REQUIRE(spy.log() == AllocatorLog{ | 
					
						
							|  |  |  |                              Deallocate(sizeofString("world")), | 
					
						
							|  |  |  |                              Allocate(sizeofString("[]")), | 
					
						
							|  |  |  |                          }); | 
					
						
							| 
									
										
										
										
											2023-04-17 09:46:41 +02:00
										 |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2024-08-27 14:40:24 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-27 14:51:22 +02:00
										 |  |  | TEST_CASE("JsonVariant::set() reuses extension slot") { | 
					
						
							| 
									
										
										
										
											2024-08-27 14:40:24 +02:00
										 |  |  |   SpyingAllocator spy; | 
					
						
							|  |  |  |   JsonDocument doc(&spy); | 
					
						
							| 
									
										
										
										
											2024-08-27 14:51:22 +02:00
										 |  |  |   JsonVariant variant = doc.to<JsonVariant>(); | 
					
						
							| 
									
										
										
										
											2024-08-27 14:40:24 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-27 14:51:22 +02:00
										 |  |  |   variant.set(1.2); | 
					
						
							|  |  |  |   doc.shrinkToFit(); | 
					
						
							|  |  |  |   spy.clearLog(); | 
					
						
							| 
									
										
										
										
											2024-08-27 14:40:24 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-27 14:51:22 +02:00
										 |  |  |   SECTION("double") { | 
					
						
							|  |  |  |     bool result = variant.set(3.4); | 
					
						
							| 
									
										
										
										
											2024-08-27 14:40:24 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-27 14:51:22 +02:00
										 |  |  |     REQUIRE(result == true); | 
					
						
							| 
									
										
										
										
											2024-08-27 14:40:24 +02:00
										 |  |  |     REQUIRE(spy.log() == AllocatorLog{}); | 
					
						
							| 
									
										
										
										
											2024-08-27 14:51:22 +02:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2024-08-27 14:40:24 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-27 14:51:22 +02:00
										 |  |  |   SECTION("int64_t") { | 
					
						
							|  |  |  |     bool result = variant.set(-2147483649LL); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     REQUIRE(result == true); | 
					
						
							|  |  |  |     REQUIRE(spy.log() == AllocatorLog{}); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   SECTION("uint64_t") { | 
					
						
							|  |  |  |     bool result = variant.set(4294967296U); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     REQUIRE(result == true); | 
					
						
							|  |  |  |     REQUIRE(spy.log() == AllocatorLog{}); | 
					
						
							| 
									
										
										
										
											2024-08-27 14:40:24 +02:00
										 |  |  |   } | 
					
						
							|  |  |  | } |