| 
									
										
										
										
											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
 | 
					
						
							| 
									
										
										
										
											2019-08-11 10:47:19 +02:00
										 |  |  | // MIT License
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #define ARDUINOJSON_ENABLE_PROGMEM 1
 | 
					
						
							|  |  |  | #include <ArduinoJson.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <catch.hpp>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TEST_CASE("Flash strings") { | 
					
						
							| 
									
										
										
										
											2023-07-17 18:15:13 +02:00
										 |  |  |   JsonDocument doc; | 
					
						
							| 
									
										
										
										
											2019-08-11 10:47:19 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |   SECTION("deserializeJson()") { | 
					
						
							|  |  |  |     DeserializationError err = deserializeJson(doc, F("{'hello':'world'}")); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     REQUIRE(err == DeserializationError::Ok); | 
					
						
							|  |  |  |     REQUIRE(doc["hello"] == "world"); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   SECTION("JsonDocument::operator[]") { | 
					
						
							|  |  |  |     doc[F("hello")] = F("world"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     REQUIRE(doc["hello"] == "world"); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   SECTION("JsonDocument::add()") { | 
					
						
							|  |  |  |     doc.add(F("world")); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     REQUIRE(doc[0] == "world"); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   SECTION("JsonVariant::set()") { | 
					
						
							|  |  |  |     JsonVariant var = doc.to<JsonVariant>(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     var.set(F("world")); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     REQUIRE(var == "world"); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   SECTION("MemberProxy::operator==") { | 
					
						
							|  |  |  |     doc["hello"] = "world"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     REQUIRE(doc["hello"] == F("world")); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   SECTION("ElementProxy::operator==") { | 
					
						
							|  |  |  |     doc.add("world"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     REQUIRE(doc[0] == F("world")); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2019-08-21 09:31:53 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-30 10:02:21 +01:00
										 |  |  | TEST_CASE("parseNumber()") {  // tables are in Flash
 | 
					
						
							| 
									
										
										
										
											2023-02-14 10:04:48 +01:00
										 |  |  |   using ArduinoJson::detail::parseNumber; | 
					
						
							| 
									
										
										
										
											2021-12-30 10:02:21 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |   CHECK(parseNumber<float>("1") == 1.f); | 
					
						
							|  |  |  |   CHECK(parseNumber<float>("1.23") == 1.23f); | 
					
						
							|  |  |  |   CHECK(parseNumber<float>("-1.23e34") == -1.23e34f); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-21 09:31:53 +02:00
										 |  |  | TEST_CASE("strlen_P") { | 
					
						
							| 
									
										
										
										
											2020-09-14 09:30:58 +02:00
										 |  |  |   CHECK(strlen_P(PSTR("")) == 0); | 
					
						
							|  |  |  |   CHECK(strlen_P(PSTR("a")) == 1); | 
					
						
							|  |  |  |   CHECK(strlen_P(PSTR("ac")) == 2); | 
					
						
							| 
									
										
										
										
											2019-08-21 09:31:53 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TEST_CASE("strncmp_P") { | 
					
						
							| 
									
										
										
										
											2020-09-14 09:30:58 +02:00
										 |  |  |   CHECK(strncmp_P("a", PSTR("b"), 0) == 0); | 
					
						
							|  |  |  |   CHECK(strncmp_P("a", PSTR("b"), 1) == -1); | 
					
						
							|  |  |  |   CHECK(strncmp_P("b", PSTR("a"), 1) == 1); | 
					
						
							|  |  |  |   CHECK(strncmp_P("a", PSTR("a"), 0) == 0); | 
					
						
							|  |  |  |   CHECK(strncmp_P("a", PSTR("b"), 2) == -1); | 
					
						
							|  |  |  |   CHECK(strncmp_P("b", PSTR("a"), 2) == 1); | 
					
						
							|  |  |  |   CHECK(strncmp_P("a", PSTR("a"), 2) == 0); | 
					
						
							| 
									
										
										
										
											2019-08-21 09:31:53 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TEST_CASE("strcmp_P") { | 
					
						
							| 
									
										
										
										
											2020-09-14 09:30:58 +02:00
										 |  |  |   CHECK(strcmp_P("a", PSTR("b")) == -1); | 
					
						
							|  |  |  |   CHECK(strcmp_P("b", PSTR("a")) == 1); | 
					
						
							|  |  |  |   CHECK(strcmp_P("a", PSTR("a")) == 0); | 
					
						
							|  |  |  |   CHECK(strcmp_P("aa", PSTR("ab")) == -1); | 
					
						
							|  |  |  |   CHECK(strcmp_P("ab", PSTR("aa")) == 1); | 
					
						
							|  |  |  |   CHECK(strcmp_P("aa", PSTR("aa")) == 0); | 
					
						
							| 
									
										
										
										
											2019-08-21 09:31:53 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TEST_CASE("memcpy_P") { | 
					
						
							|  |  |  |   char dst[4]; | 
					
						
							| 
									
										
										
										
											2020-09-14 09:30:58 +02:00
										 |  |  |   CHECK(memcpy_P(dst, PSTR("ABC"), 4) == dst); | 
					
						
							| 
									
										
										
										
											2019-08-21 09:31:53 +02:00
										 |  |  |   CHECK(dst[0] == 'A'); | 
					
						
							|  |  |  |   CHECK(dst[1] == 'B'); | 
					
						
							|  |  |  |   CHECK(dst[2] == 'C'); | 
					
						
							|  |  |  |   CHECK(dst[3] == 0); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2019-10-14 12:02:26 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-25 11:39:04 +02:00
										 |  |  | TEST_CASE("BoundedReader<const __FlashStringHelper*>") { | 
					
						
							| 
									
										
										
										
											2023-02-14 10:04:48 +01:00
										 |  |  |   using namespace ArduinoJson::detail; | 
					
						
							| 
									
										
										
										
											2019-10-14 12:02:26 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |   SECTION("read") { | 
					
						
							| 
									
										
										
										
											2019-10-25 11:39:04 +02:00
										 |  |  |     BoundedReader<const __FlashStringHelper*> reader(F("\x01\xFF"), 2); | 
					
						
							| 
									
										
										
										
											2019-10-14 12:02:26 +02:00
										 |  |  |     REQUIRE(reader.read() == 0x01); | 
					
						
							|  |  |  |     REQUIRE(reader.read() == 0xFF); | 
					
						
							|  |  |  |     REQUIRE(reader.read() == -1); | 
					
						
							|  |  |  |     REQUIRE(reader.read() == -1); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   SECTION("readBytes() all at once") { | 
					
						
							| 
									
										
										
										
											2019-10-25 11:39:04 +02:00
										 |  |  |     BoundedReader<const __FlashStringHelper*> reader(F("ABCD"), 3); | 
					
						
							| 
									
										
										
										
											2019-10-14 12:02:26 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     char buffer[8] = "abcd"; | 
					
						
							|  |  |  |     REQUIRE(reader.readBytes(buffer, 4) == 3); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     REQUIRE(buffer[0] == 'A'); | 
					
						
							|  |  |  |     REQUIRE(buffer[1] == 'B'); | 
					
						
							|  |  |  |     REQUIRE(buffer[2] == 'C'); | 
					
						
							|  |  |  |     REQUIRE(buffer[3] == 'd'); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   SECTION("readBytes() in two parts") { | 
					
						
							| 
									
										
										
										
											2019-10-25 11:39:04 +02:00
										 |  |  |     BoundedReader<const __FlashStringHelper*> reader(F("ABCDEF"), 6); | 
					
						
							| 
									
										
										
										
											2019-10-14 12:02:26 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     char buffer[8] = "abcdefg"; | 
					
						
							|  |  |  |     REQUIRE(reader.readBytes(buffer, 4) == 4); | 
					
						
							|  |  |  |     REQUIRE(reader.readBytes(buffer + 4, 4) == 2); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     REQUIRE(buffer[0] == 'A'); | 
					
						
							|  |  |  |     REQUIRE(buffer[1] == 'B'); | 
					
						
							|  |  |  |     REQUIRE(buffer[2] == 'C'); | 
					
						
							|  |  |  |     REQUIRE(buffer[3] == 'D'); | 
					
						
							|  |  |  |     REQUIRE(buffer[4] == 'E'); | 
					
						
							|  |  |  |     REQUIRE(buffer[5] == 'F'); | 
					
						
							|  |  |  |     REQUIRE(buffer[6] == 'g'); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-25 11:39:04 +02:00
										 |  |  | TEST_CASE("Reader<const __FlashStringHelper*>") { | 
					
						
							| 
									
										
										
										
											2023-02-14 10:04:48 +01:00
										 |  |  |   using namespace ArduinoJson::detail; | 
					
						
							| 
									
										
										
										
											2019-10-14 12:02:26 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |   SECTION("read()") { | 
					
						
							| 
									
										
										
										
											2019-10-25 11:39:04 +02:00
										 |  |  |     Reader<const __FlashStringHelper*> reader(F("\x01\xFF\x00\x12")); | 
					
						
							| 
									
										
										
										
											2019-10-14 12:02:26 +02:00
										 |  |  |     REQUIRE(reader.read() == 0x01); | 
					
						
							|  |  |  |     REQUIRE(reader.read() == 0xFF); | 
					
						
							|  |  |  |     REQUIRE(reader.read() == 0); | 
					
						
							|  |  |  |     REQUIRE(reader.read() == 0x12); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   SECTION("readBytes() all at once") { | 
					
						
							| 
									
										
										
										
											2019-10-25 11:39:04 +02:00
										 |  |  |     Reader<const __FlashStringHelper*> reader(F("ABCD")); | 
					
						
							| 
									
										
										
										
											2019-10-14 12:02:26 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     char buffer[8] = "abcd"; | 
					
						
							|  |  |  |     REQUIRE(reader.readBytes(buffer, 3) == 3); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     REQUIRE(buffer[0] == 'A'); | 
					
						
							|  |  |  |     REQUIRE(buffer[1] == 'B'); | 
					
						
							|  |  |  |     REQUIRE(buffer[2] == 'C'); | 
					
						
							|  |  |  |     REQUIRE(buffer[3] == 'd'); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   SECTION("readBytes() in two parts") { | 
					
						
							| 
									
										
										
										
											2019-10-25 11:39:04 +02:00
										 |  |  |     Reader<const __FlashStringHelper*> reader(F("ABCDEF")); | 
					
						
							| 
									
										
										
										
											2019-10-14 12:02:26 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     char buffer[8] = "abcdefg"; | 
					
						
							|  |  |  |     REQUIRE(reader.readBytes(buffer, 4) == 4); | 
					
						
							|  |  |  |     REQUIRE(reader.readBytes(buffer + 4, 2) == 2); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     REQUIRE(buffer[0] == 'A'); | 
					
						
							|  |  |  |     REQUIRE(buffer[1] == 'B'); | 
					
						
							|  |  |  |     REQUIRE(buffer[2] == 'C'); | 
					
						
							|  |  |  |     REQUIRE(buffer[3] == 'D'); | 
					
						
							|  |  |  |     REQUIRE(buffer[4] == 'E'); | 
					
						
							|  |  |  |     REQUIRE(buffer[5] == 'F'); | 
					
						
							|  |  |  |     REQUIRE(buffer[6] == 'g'); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2020-09-18 09:37:59 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | static void testStringification(DeserializationError error, | 
					
						
							|  |  |  |                                 std::string expected) { | 
					
						
							|  |  |  |   const __FlashStringHelper* s = error.f_str(); | 
					
						
							|  |  |  |   CHECK(reinterpret_cast<const char*>(convertFlashToPtr(s)) == expected); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #define TEST_STRINGIFICATION(symbol) \
 | 
					
						
							|  |  |  |   testStringification(DeserializationError::symbol, #symbol) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TEST_CASE("DeserializationError::f_str()") { | 
					
						
							|  |  |  |   TEST_STRINGIFICATION(Ok); | 
					
						
							|  |  |  |   TEST_STRINGIFICATION(EmptyInput); | 
					
						
							|  |  |  |   TEST_STRINGIFICATION(IncompleteInput); | 
					
						
							|  |  |  |   TEST_STRINGIFICATION(InvalidInput); | 
					
						
							|  |  |  |   TEST_STRINGIFICATION(NoMemory); | 
					
						
							|  |  |  |   TEST_STRINGIFICATION(TooDeep); | 
					
						
							|  |  |  | } |