| 
									
										
										
										
											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
 | 
					
						
							| 
									
										
										
										
											2020-09-05 10:54:46 +02: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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-05 10:54:46 +02:00
										 |  |  | TEST_CASE("JsonDocument::overflowed()") { | 
					
						
							| 
									
										
										
										
											2023-07-25 14:53:54 +02:00
										 |  |  |   TimebombAllocator timebomb(10); | 
					
						
							|  |  |  |   JsonDocument doc(&timebomb); | 
					
						
							| 
									
										
										
										
											2023-07-17 14:39:57 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-05 10:54:46 +02:00
										 |  |  |   SECTION("returns false on a fresh object") { | 
					
						
							| 
									
										
										
										
											2023-07-25 14:53:54 +02:00
										 |  |  |     timebomb.setCountdown(0); | 
					
						
							| 
									
										
										
										
											2020-09-05 10:54:46 +02:00
										 |  |  |     CHECK(doc.overflowed() == false); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   SECTION("returns true after a failed insertion") { | 
					
						
							| 
									
										
										
										
											2023-07-25 14:53:54 +02:00
										 |  |  |     timebomb.setCountdown(0); | 
					
						
							| 
									
										
										
										
											2020-09-05 10:54:46 +02:00
										 |  |  |     doc.add(0); | 
					
						
							|  |  |  |     CHECK(doc.overflowed() == true); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   SECTION("returns false after successful insertion") { | 
					
						
							| 
									
										
										
										
											2023-07-25 14:53:54 +02:00
										 |  |  |     timebomb.setCountdown(2); | 
					
						
							| 
									
										
										
										
											2020-09-05 10:54:46 +02:00
										 |  |  |     doc.add(0); | 
					
						
							|  |  |  |     CHECK(doc.overflowed() == false); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   SECTION("returns true after a failed string copy") { | 
					
						
							| 
									
										
										
										
											2023-07-25 14:53:54 +02:00
										 |  |  |     timebomb.setCountdown(0); | 
					
						
							| 
									
										
										
										
											2024-06-07 09:35:45 +02:00
										 |  |  |     doc.add("example"_s); | 
					
						
							| 
									
										
										
										
											2020-09-05 10:54:46 +02:00
										 |  |  |     CHECK(doc.overflowed() == true); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   SECTION("returns false after a successful string copy") { | 
					
						
							| 
									
										
										
										
											2023-07-25 14:53:54 +02:00
										 |  |  |     timebomb.setCountdown(3); | 
					
						
							| 
									
										
										
										
											2024-06-07 09:35:45 +02:00
										 |  |  |     doc.add("example"_s); | 
					
						
							| 
									
										
										
										
											2020-09-05 10:54:46 +02:00
										 |  |  |     CHECK(doc.overflowed() == false); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-13 11:52:29 +01:00
										 |  |  |   SECTION("returns true after a failed member add") { | 
					
						
							| 
									
										
										
										
											2023-07-25 14:53:54 +02:00
										 |  |  |     timebomb.setCountdown(0); | 
					
						
							| 
									
										
										
										
											2022-01-13 11:52:29 +01:00
										 |  |  |     doc["example"] = true; | 
					
						
							|  |  |  |     CHECK(doc.overflowed() == true); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-05 10:54:46 +02:00
										 |  |  |   SECTION("returns true after a failed deserialization") { | 
					
						
							| 
									
										
										
										
											2023-07-25 14:53:54 +02:00
										 |  |  |     timebomb.setCountdown(0); | 
					
						
							| 
									
										
										
										
											2023-04-11 10:03:47 +02:00
										 |  |  |     deserializeJson(doc, "[1, 2]"); | 
					
						
							| 
									
										
										
										
											2020-09-05 10:54:46 +02:00
										 |  |  |     CHECK(doc.overflowed() == true); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   SECTION("returns false after a successful deserialization") { | 
					
						
							| 
									
										
										
										
											2023-07-25 14:53:54 +02:00
										 |  |  |     timebomb.setCountdown(3); | 
					
						
							| 
									
										
										
										
											2020-09-05 10:54:46 +02:00
										 |  |  |     deserializeJson(doc, "[\"example\"]"); | 
					
						
							|  |  |  |     CHECK(doc.overflowed() == false); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   SECTION("returns false after clear()") { | 
					
						
							| 
									
										
										
										
											2023-07-25 14:53:54 +02:00
										 |  |  |     timebomb.setCountdown(0); | 
					
						
							| 
									
										
										
										
											2020-09-05 10:54:46 +02:00
										 |  |  |     doc.add(0); | 
					
						
							|  |  |  |     doc.clear(); | 
					
						
							|  |  |  |     CHECK(doc.overflowed() == false); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   SECTION("remains false after shrinkToFit()") { | 
					
						
							| 
									
										
										
										
											2023-07-25 14:53:54 +02:00
										 |  |  |     timebomb.setCountdown(2); | 
					
						
							| 
									
										
										
										
											2020-09-05 10:54:46 +02:00
										 |  |  |     doc.add(0); | 
					
						
							| 
									
										
										
										
											2023-07-25 14:53:54 +02:00
										 |  |  |     timebomb.setCountdown(2); | 
					
						
							| 
									
										
										
										
											2020-09-05 10:54:46 +02:00
										 |  |  |     doc.shrinkToFit(); | 
					
						
							|  |  |  |     CHECK(doc.overflowed() == false); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   SECTION("remains true after shrinkToFit()") { | 
					
						
							| 
									
										
										
										
											2023-07-25 14:53:54 +02:00
										 |  |  |     timebomb.setCountdown(0); | 
					
						
							| 
									
										
										
										
											2020-09-05 10:54:46 +02:00
										 |  |  |     doc.add(0); | 
					
						
							| 
									
										
										
										
											2023-07-25 14:53:54 +02:00
										 |  |  |     timebomb.setCountdown(2); | 
					
						
							| 
									
										
										
										
											2020-09-05 10:54:46 +02:00
										 |  |  |     doc.shrinkToFit(); | 
					
						
							|  |  |  |     CHECK(doc.overflowed() == true); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2023-10-09 11:58:02 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |   SECTION("returns false when string length doesn't overflow") { | 
					
						
							|  |  |  |     auto maxLength = ArduinoJson::detail::StringNode::maxLength; | 
					
						
							|  |  |  |     CHECK(doc.set(std::string(maxLength, 'a')) == true); | 
					
						
							|  |  |  |     CHECK(doc.overflowed() == false); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   SECTION("returns true when string length overflows") { | 
					
						
							|  |  |  |     auto maxLength = ArduinoJson::detail::StringNode::maxLength; | 
					
						
							|  |  |  |     CHECK(doc.set(std::string(maxLength + 1, 'a')) == false); | 
					
						
							|  |  |  |     CHECK(doc.overflowed() == true); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2020-09-05 10:54:46 +02:00
										 |  |  | } |