mirror of
				https://github.com/eledio-devices/thirdparty-ArduinoJson.git
				synced 2025-10-31 16:14:11 +01:00 
			
		
		
		
	
		
			
				
	
	
		
			80 lines
		
	
	
		
			2.1 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
			
		
		
	
	
			80 lines
		
	
	
		
			2.1 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
| // ArduinoJson - arduinojson.org
 | |
| // Copyright Benoit Blanchon 2014-2018
 | |
| // MIT License
 | |
| 
 | |
| #include <stdint.h>
 | |
| #include <ArduinoJson/Polyfills/parseInteger.hpp>
 | |
| #include <catch.hpp>
 | |
| 
 | |
| using namespace ArduinoJson::Internals;
 | |
| 
 | |
| template <typename T>
 | |
| void check(const char* input, T expected) {
 | |
|   CAPTURE(input);
 | |
|   T actual = parseInteger<T>(input);
 | |
|   REQUIRE(expected == actual);
 | |
| }
 | |
| 
 | |
| TEST_CASE("parseInteger<int8_t>()") {
 | |
|   check<int8_t>("-128", -128);
 | |
|   check<int8_t>("127", 127);
 | |
|   check<int8_t>("+127", 127);
 | |
|   check<int8_t>("3.14", 3);
 | |
|   check<int8_t>("x42", 0);
 | |
|   check<int8_t>("128", -128);
 | |
|   check<int8_t>("-129", 127);
 | |
|   check<int8_t>(NULL, 0);
 | |
|   check<int8_t>("true", 1);
 | |
|   check<int8_t>("false", 0);
 | |
| }
 | |
| 
 | |
| TEST_CASE("parseInteger<int16_t>()") {
 | |
|   check<int16_t>("-32768", -32768);
 | |
|   check<int16_t>("32767", 32767);
 | |
|   check<int16_t>("+32767", 32767);
 | |
|   check<int16_t>("3.14", 3);
 | |
|   check<int16_t>("x42", 0);
 | |
|   check<int16_t>("-32769", 32767);
 | |
|   check<int16_t>("32768", -32768);
 | |
|   check<int16_t>(NULL, 0);
 | |
|   check<int16_t>("true", 1);
 | |
|   check<int16_t>("false", 0);
 | |
| }
 | |
| 
 | |
| TEST_CASE("parseInteger<int32_t>()") {
 | |
|   check<int32_t>("-2147483648", (-2147483647 - 1));
 | |
|   check<int32_t>("2147483647", 2147483647);
 | |
|   check<int32_t>("+2147483647", 2147483647);
 | |
|   check<int32_t>("3.14", 3);
 | |
|   check<int32_t>("x42", 0);
 | |
|   check<int32_t>("-2147483649", 2147483647);
 | |
|   check<int32_t>("2147483648", (-2147483647 - 1));
 | |
|   check<int32_t>("true", 1);
 | |
|   check<int32_t>("false", 0);
 | |
| }
 | |
| 
 | |
| TEST_CASE("parseInteger<uint8_t>()") {
 | |
|   check<uint8_t>("0", 0);
 | |
|   check<uint8_t>("255", 255);
 | |
|   check<uint8_t>("+255", 255);
 | |
|   check<uint8_t>("3.14", 3);
 | |
|   check<uint8_t>("x42", 0);
 | |
|   check<uint8_t>("-1", 255);
 | |
|   check<uint8_t>("256", 0);
 | |
|   check<uint8_t>("true", 1);
 | |
|   check<uint8_t>("false", 0);
 | |
| }
 | |
| 
 | |
| TEST_CASE("parseInteger<uint16_t>()") {
 | |
|   check<uint16_t>("0", 0);
 | |
|   check<uint16_t>("65535", 65535);
 | |
|   check<uint16_t>("+65535", 65535);
 | |
|   check<uint16_t>("3.14", 3);
 | |
|   // check<uint16_t>(" 42", 0);
 | |
|   check<uint16_t>("x42", 0);
 | |
|   check<uint16_t>("-1", 65535);
 | |
|   check<uint16_t>("65536", 0);
 | |
|   check<uint16_t>("true", 1);
 | |
|   check<uint16_t>("false", 0);
 | |
| }
 |