tests and fix for #367

operator>> now works as expected.
This commit is contained in:
Niels Lohmann 2017-05-21 16:36:51 +02:00
parent 16b63d3197
commit 9e507dfad5
No known key found for this signature in database
GPG key ID: 7F3CEA63AE251B69
2 changed files with 121 additions and 8 deletions

View file

@ -8826,8 +8826,10 @@ class basic_json
{ {
// clear stream flags // clear stream flags
is.clear(); is.clear();
// set stream after last processed char // We initially read a lot of characters into the buffer, and we
is.seekg(start_position + static_cast<std::streamoff>(processed_chars - 1)); // may not have processed all of them. Therefore, we need to
// "rewind" the stream after the last processed char.
is.seekg(start_position + static_cast<std::streamoff>(processed_chars));
} }
int get_character() override int get_character() override
@ -8840,20 +8842,19 @@ class basic_json
// store number of bytes in the buffer // store number of bytes in the buffer
fill_size = static_cast<size_t>(is.gcount()); fill_size = static_cast<size_t>(is.gcount());
// the buffer is ready
buffer_pos = 0;
// remember that filling did not yield new input // remember that filling did not yield new input
if (fill_size == 0) if (fill_size == 0)
{ {
eof = true; eof = true;
return std::char_traits<char>::eof();
} }
// the buffer is ready
buffer_pos = 0;
} }
++processed_chars; ++processed_chars;
return eof return buffer[buffer_pos++] & 0xFF;;
? std::char_traits<char>::eof()
: buffer[buffer_pos++] & 0xFF;
} }
std::string read(size_t offset, size_t length) override std::string read(size_t offset, size_t length) override

View file

@ -599,6 +599,118 @@ TEST_CASE("regression tests")
"[json.exception.parse_error.101] parse error at 1: syntax error - unexpected end of input"); "[json.exception.parse_error.101] parse error at 1: syntax error - unexpected end of input");
} }
SECTION("issue #367 - behavior of operator>> should more closely resemble that of built-in overloads")
{
SECTION("(empty)")
{
std::stringstream ss;
json j;
CHECK_THROWS_AS(ss >> j, json::parse_error);
CHECK_THROWS_WITH(ss >> j,
"[json.exception.parse_error.101] parse error at 1: syntax error - unexpected end of input");
}
SECTION("(whitespace)")
{
std::stringstream ss;
ss << " ";
json j;
CHECK_THROWS_AS(ss >> j, json::parse_error);
CHECK_THROWS_WITH(ss >> j,
"[json.exception.parse_error.101] parse error at 1: syntax error - unexpected end of input");
}
SECTION("one value")
{
std::stringstream ss;
ss << "111";
json j;
CHECK_NOTHROW(ss >> j);
CHECK(j == 111);
CHECK_THROWS_AS(ss >> j, json::parse_error);
CHECK_THROWS_WITH(ss >> j,
"[json.exception.parse_error.101] parse error at 1: syntax error - unexpected end of input");
}
SECTION("one value + whitespace")
{
std::stringstream ss;
ss << "222 \t\n";
json j;
CHECK_NOTHROW(ss >> j);
CHECK(j == 222);
CHECK_THROWS_AS(ss >> j, json::parse_error);
CHECK_THROWS_WITH(ss >> j,
"[json.exception.parse_error.101] parse error at 1: syntax error - unexpected end of input");
}
SECTION("whitespace + one value")
{
std::stringstream ss;
ss << "\n\t 333";
json j;
CHECK_NOTHROW(ss >> j);
CHECK(j == 333);
CHECK_THROWS_AS(ss >> j, json::parse_error);
CHECK_THROWS_WITH(ss >> j,
"[json.exception.parse_error.101] parse error at 1: syntax error - unexpected end of input");
}
SECTION("three values")
{
std::stringstream ss;
ss << " 111 \n222\n \n 333";
json j;
CHECK_NOTHROW(ss >> j);
CHECK(j == 111);
CHECK_NOTHROW(ss >> j);
CHECK(j == 222);
CHECK_NOTHROW(ss >> j);
CHECK(j == 333);
CHECK_THROWS_AS(ss >> j, json::parse_error);
CHECK_THROWS_WITH(ss >> j,
"[json.exception.parse_error.101] parse error at 1: syntax error - unexpected end of input");
}
SECTION("literals without whitespace")
{
std::stringstream ss;
ss << "truefalsenull\"\"";
json j;
CHECK_NOTHROW(ss >> j);
CHECK(j == true);
CHECK_NOTHROW(ss >> j);
CHECK(j == false);
CHECK_NOTHROW(ss >> j);
CHECK(j == nullptr);
CHECK_NOTHROW(ss >> j);
CHECK(j == "");
CHECK_THROWS_AS(ss >> j, json::parse_error);
CHECK_THROWS_WITH(ss >> j,
"[json.exception.parse_error.101] parse error at 1: syntax error - unexpected end of input");
}
SECTION("example from #529")
{
std::stringstream ss;
ss << "{\n \"one\" : 1,\n \"two\" : 2\n}\n{\n \"three\" : 3\n}";
json j;
CHECK_NOTHROW(ss >> j);
CHECK(j == json({{"one", 1}, {"two", 2}}));
CHECK_NOTHROW(ss >> j);
CHECK(j == json({{"three", 3}}));
CHECK_THROWS_AS(ss >> j, json::parse_error);
CHECK_THROWS_WITH(ss >> j,
"[json.exception.parse_error.101] parse error at 1: syntax error - unexpected end of input");
}
}
SECTION("issue #389 - Integer-overflow (OSS-Fuzz issue 267)") SECTION("issue #389 - Integer-overflow (OSS-Fuzz issue 267)")
{ {
// original test case // original test case