Commit bc2a4ed2 authored by Norby, Tom's avatar Norby, Tom

Differentiate between integer and double, rather than all double.

parent 5aa49012
Pipeline #101732 passed with stages
in 55 minutes and 59 seconds
......@@ -221,7 +221,8 @@ class RADIX_PUBLIC JSONParserImpl
// ([Ee][+-]?[0-9]+)?
bool parse_number(value_type& value)
{
value = value_type();
bool is_float = false;
value = value_type();
if (m_po >= m_text.size()) return false;
size_t len = 0;
......@@ -263,7 +264,7 @@ class RADIX_PUBLIC JSONParserImpl
{
try
{
value = value_type(std::stod(std::string(&m_text[m_po - len], len)));
value = value_type(std::stoi(std::string(&m_text[m_po - len], len)));
return true;
}
catch (...)
......@@ -276,6 +277,7 @@ class RADIX_PUBLIC JSONParserImpl
// (\.[0-9]+)?
if (ch == '.')
{
is_float = true;
m_po++;
m_col++;
len++;
......@@ -302,6 +304,7 @@ class RADIX_PUBLIC JSONParserImpl
// ([Ee][+-]?[0-9]+)?
if (ch == 'E' || ch == 'e')
{
is_float = true;
m_po++;
m_col++;
len++;
......@@ -340,7 +343,12 @@ class RADIX_PUBLIC JSONParserImpl
try
{
value = value_type(std::stod(std::string(&m_text[m_po - len], len)));
double v = std::stod(std::string(&m_text[m_po - len], len));
if (is_float)
value = value_type(v);
else
value = value_type(int(v >= 0 ? v + .5 : v - .5));
return true;
}
catch (...)
......
......@@ -12,6 +12,8 @@ using namespace radix;
// bool parse_from_stream(std::istream& in_stream)
// bool parse_from_file(std::string fn)
double allowedDelta = 1e-4;
TEST(JSONParser, parse_from_file)
{
{
......@@ -66,13 +68,14 @@ TEST(JSONParser, parse_from_file)
TEST(JSONParser, parse_from_stream)
{
JSONParser jp;
std::string errorMsg;
std::stringstream ss("");
// check code paths in JSONParser class
{
JSONParser jp;
std::string errorMsg;
// trailing junk
std::stringstream ss("{}\r\nfoo");
ss.str("{}\r\nfoo");
EXPECT_FALSE(jp.parse_from_stream(ss));
errorMsg = jp.last_error();
EXPECT_EQ("unexpected trailing character(s) at line 2 column 1", errorMsg);
......@@ -233,6 +236,83 @@ TEST(JSONParser, parse_from_stream)
errorMsg = jp.last_error();
EXPECT_EQ("string missing closing quote at line 1 column 5", errorMsg);
}
// test numbers
{
int iVal;
double dVal;
// integer
ss.str("123");
EXPECT_TRUE(jp.parse_from_stream(ss));
iVal = jp.root().to_int();
EXPECT_EQ(123, iVal);
EXPECT_EQ(Value::TYPE_INTEGER, jp.root().type());
// float
ss.str("123.4");
EXPECT_TRUE(jp.parse_from_stream(ss));
dVal = jp.root().to_double();
EXPECT_NEAR(123.4, dVal, allowedDelta);
EXPECT_EQ(Value::TYPE_DOUBLE, jp.root().type());
// scientific notation
ss.str("123e45");
EXPECT_TRUE(jp.parse_from_stream(ss));
dVal = jp.root().to_double();
EXPECT_NEAR(123e45, dVal, allowedDelta);
EXPECT_EQ(Value::TYPE_DOUBLE, jp.root().type());
// Zeros
ss.str("0");
EXPECT_TRUE(jp.parse_from_stream(ss));
iVal = jp.root().to_int();
EXPECT_EQ(0, iVal);
EXPECT_EQ(Value::TYPE_INTEGER, jp.root().type());
ss.str("-0");
EXPECT_TRUE(jp.parse_from_stream(ss));
iVal = jp.root().to_int();
EXPECT_EQ(0, iVal);
EXPECT_EQ(Value::TYPE_INTEGER, jp.root().type());
ss.str("0.0");
EXPECT_TRUE(jp.parse_from_stream(ss));
dVal = jp.root().to_double();
EXPECT_NEAR(0, dVal, allowedDelta);
EXPECT_EQ(Value::TYPE_DOUBLE, jp.root().type());
ss.str("-0.0e-1");
EXPECT_TRUE(jp.parse_from_stream(ss));
dVal = jp.root().to_double();
EXPECT_NEAR(0, dVal, allowedDelta);
EXPECT_EQ(Value::TYPE_DOUBLE, jp.root().type());
// Negative
ss.str("-123");
EXPECT_TRUE(jp.parse_from_stream(ss));
iVal = jp.root().to_int();
EXPECT_EQ(-123, iVal);
EXPECT_EQ(Value::TYPE_INTEGER, jp.root().type());
ss.str("-123.4");
EXPECT_TRUE(jp.parse_from_stream(ss));
dVal = jp.root().to_double();
EXPECT_NEAR(-123.4, dVal, allowedDelta);
EXPECT_EQ(Value::TYPE_DOUBLE, jp.root().type());
ss.str("-123e45");
EXPECT_TRUE(jp.parse_from_stream(ss));
dVal = jp.root().to_double();
EXPECT_NEAR(-123e45, dVal, allowedDelta);
EXPECT_EQ(Value::TYPE_DOUBLE, jp.root().type());
ss.str("-1234e-5");
EXPECT_TRUE(jp.parse_from_stream(ss));
dVal = jp.root().to_double();
EXPECT_NEAR(-0.01234, dVal, allowedDelta);
EXPECT_EQ(Value::TYPE_DOUBLE, jp.root().type());
}
}
bool parse_and_test(const std::string& file)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment