forked from bblanchon/ArduinoJson
Added custom implementation of strtol()
(issue #465)
`char` is now treated as an integral type (issue #337, #370)
This commit is contained in:
@ -1,37 +0,0 @@
|
||||
// Copyright Benoit Blanchon 2014-2017
|
||||
// MIT License
|
||||
//
|
||||
// Arduino JSON library
|
||||
// https://github.com/bblanchon/ArduinoJson
|
||||
// If you like this project, please add a star!
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
#include <limits.h> // for LONG_MAX
|
||||
|
||||
#define ARDUINOJSON_USE_LONG_LONG 0
|
||||
#define ARDUINOJSON_USE_INT64 0
|
||||
#include <ArduinoJson.h>
|
||||
|
||||
#define SUITE Issue90
|
||||
|
||||
static const char* superLong =
|
||||
"12345678901234567890123456789012345678901234567890123456789012345678901234"
|
||||
"5678901234567890123456789012345678901234567890123456789012345678901234567";
|
||||
|
||||
static const JsonVariant variant = RawJson(superLong);
|
||||
|
||||
TEST(SUITE, IsNotALong) {
|
||||
ASSERT_FALSE(variant.is<long>());
|
||||
}
|
||||
|
||||
TEST(SUITE, AsLong) {
|
||||
ASSERT_EQ(LONG_MAX, variant.as<long>());
|
||||
}
|
||||
|
||||
TEST(SUITE, IsAString) {
|
||||
ASSERT_FALSE(variant.is<const char*>());
|
||||
}
|
||||
|
||||
TEST(SUITE, AsString) {
|
||||
ASSERT_STREQ(superLong, variant.as<const char*>());
|
||||
}
|
@ -63,6 +63,9 @@ TEST_F(JsonVariant_Storage_Tests, Double) {
|
||||
TEST_F(JsonVariant_Storage_Tests, Float) {
|
||||
testNumericType<float>();
|
||||
}
|
||||
TEST_F(JsonVariant_Storage_Tests, Char) {
|
||||
testNumericType<char>();
|
||||
}
|
||||
TEST_F(JsonVariant_Storage_Tests, SChar) {
|
||||
testNumericType<signed char>();
|
||||
}
|
||||
|
@ -18,6 +18,10 @@ struct Polyfills_IsFloat_Tests : testing::Test {
|
||||
};
|
||||
#define TEST_(X) TEST_F(Polyfills_IsFloat_Tests, X)
|
||||
|
||||
TEST_(Null) {
|
||||
check(false, NULL);
|
||||
}
|
||||
|
||||
TEST_(NoExponent) {
|
||||
check(true, "3.14");
|
||||
check(true, "-3.14");
|
||||
|
45
test/Polyfills_IsInteger_Tests.cpp
Normal file
45
test/Polyfills_IsInteger_Tests.cpp
Normal file
@ -0,0 +1,45 @@
|
||||
// Copyright Benoit Blanchon 2014-2017
|
||||
// MIT License
|
||||
//
|
||||
// Arduino JSON library
|
||||
// https://github.com/bblanchon/ArduinoJson
|
||||
// If you like this project, please add a star!
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
#include <ArduinoJson/Polyfills/isInteger.hpp>
|
||||
|
||||
using namespace ArduinoJson::Polyfills;
|
||||
|
||||
struct Polyfills_IsInteger_Tests : testing::Test {
|
||||
void check(bool expected, const char* input) {
|
||||
bool actual = isInteger(input);
|
||||
EXPECT_EQ(expected, actual) << input;
|
||||
}
|
||||
};
|
||||
#define TEST_(X) TEST_F(Polyfills_IsInteger_Tests, X)
|
||||
|
||||
TEST_(Null) {
|
||||
check(false, NULL);
|
||||
}
|
||||
|
||||
TEST_(FloatNotInteger) {
|
||||
check(false, "3.14");
|
||||
check(false, "-3.14");
|
||||
check(false, "+3.14");
|
||||
}
|
||||
|
||||
TEST_(Spaces) {
|
||||
check(false, "42 ");
|
||||
check(false, " 42");
|
||||
}
|
||||
|
||||
TEST_(Valid) {
|
||||
check(true, "42");
|
||||
check(true, "-42");
|
||||
check(true, "+42");
|
||||
}
|
||||
|
||||
TEST_(ExtraSign) {
|
||||
check(false, "--42");
|
||||
check(false, "++42");
|
||||
}
|
@ -54,6 +54,14 @@ struct Polyfills_ParseFloat_Double_Tests : testing::Test {
|
||||
};
|
||||
#define TEST_DOUBLE(X) TEST_F(Polyfills_ParseFloat_Double_Tests, X)
|
||||
|
||||
TEST_DOUBLE(Null) {
|
||||
check(NULL, 0);
|
||||
}
|
||||
|
||||
TEST_FLOAT(Null) {
|
||||
check(NULL, 0);
|
||||
}
|
||||
|
||||
TEST_DOUBLE(Short_NoExponent) {
|
||||
check("3.14", 3.14);
|
||||
check("-3.14", -3.14);
|
||||
|
79
test/Polyfills_ParseInteger_Tests.cpp
Normal file
79
test/Polyfills_ParseInteger_Tests.cpp
Normal file
@ -0,0 +1,79 @@
|
||||
// Copyright Benoit Blanchon 2014-2017
|
||||
// MIT License
|
||||
//
|
||||
// Arduino JSON library
|
||||
// https://github.com/bblanchon/ArduinoJson
|
||||
// If you like this project, please add a star!
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
#include <stdint.h>
|
||||
#include <ArduinoJson/Polyfills/parseInteger.hpp>
|
||||
|
||||
using namespace ArduinoJson::Polyfills;
|
||||
|
||||
struct Polyfills_ParseInteger_Tests : testing::Test {
|
||||
template <typename T>
|
||||
void check(const char* input, T expected) {
|
||||
T actual = parseInteger<T>(input);
|
||||
EXPECT_EQ(expected, actual) << input;
|
||||
}
|
||||
};
|
||||
|
||||
#define TEST_(X) TEST_F(Polyfills_ParseInteger_Tests, X)
|
||||
|
||||
TEST_(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>(" 42", 0);
|
||||
check<int8_t>("x42", 0);
|
||||
check<int8_t>("128", -128);
|
||||
check<int8_t>("-129", 127);
|
||||
check<int8_t>(NULL, 0);
|
||||
}
|
||||
|
||||
TEST_(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>(" 42", 0);
|
||||
check<int16_t>("x42", 0);
|
||||
check<int16_t>("-32769", 32767);
|
||||
check<int16_t>("32768", -32768);
|
||||
check<int16_t>(NULL, 0);
|
||||
}
|
||||
|
||||
TEST_(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>(" 42", 0);
|
||||
check<int32_t>("x42", 0);
|
||||
check<int32_t>("-2147483649", 2147483647);
|
||||
check<int32_t>("2147483648", (-2147483647 - 1));
|
||||
}
|
||||
|
||||
TEST_(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>(" 42", 0);
|
||||
check<uint8_t>("x42", 0);
|
||||
check<uint8_t>("-1", 255);
|
||||
check<uint8_t>("256", 0);
|
||||
}
|
||||
|
||||
TEST_(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);
|
||||
}
|
Reference in New Issue
Block a user