Skip to content

Commit

Permalink
split string test
Browse files Browse the repository at this point in the history
  • Loading branch information
danielaparker committed Nov 7, 2024
1 parent 20a4cc0 commit b20c30d
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 55 deletions.
6 changes: 3 additions & 3 deletions include/jsoncons/json_parser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1459,7 +1459,7 @@ class basic_json_parser : public ser_context
if (JSONCONS_UNLIKELY(n < 4))
{
std::ptrdiff_t m = 4 - n;
int matches = std::memcmp(input_ptr_, pat, n*sizeof(char_type));
int matches = std::memcmp(input_ptr_+1, pat+1, (n-1)*sizeof(char_type));
input_ptr_ += n;
position_ += n;
if (matches != 0)
Expand Down Expand Up @@ -1530,7 +1530,7 @@ class basic_json_parser : public ser_context
if (JSONCONS_UNLIKELY(n < 4))
{
std::ptrdiff_t m = 4 - n;
int matches = std::memcmp(input_ptr_, pat, n*sizeof(char_type));
int matches = std::memcmp(input_ptr_+1, pat+1, (n-1)*sizeof(char_type));
input_ptr_ += n;
position_ += n;
if (matches != 0)
Expand Down Expand Up @@ -1601,7 +1601,7 @@ class basic_json_parser : public ser_context
if (JSONCONS_UNLIKELY(n < 5))
{
std::ptrdiff_t m = 5 - n;
int matches = std::memcmp(input_ptr_, pat, n*sizeof(char_type));
int matches = std::memcmp(input_ptr_+1, pat+1, (n-1)*sizeof(char_type));
input_ptr_ += n;
position_ += n;
if (matches != 0)
Expand Down
118 changes: 66 additions & 52 deletions test/corelib/src/json_reader_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,65 +14,79 @@ using namespace jsoncons;

TEST_CASE("test json_reader buffered read")
{
std::string input;

std::string str(stream_source<char>::default_max_buffer_size-7, 'a');
std::string neg_num("-123456789.123456789");

int64_t m = (std::numeric_limits<int64_t>::lowest)();
std::string int_overflow = std::to_string(m);
int_overflow.push_back('0');

/*SECTION("test 1")
SECTION("string with split buffer")
{
//input.push_back('[');
//input.push_back('"');
//input.append(str);
//input.push_back('"');
//input.push_back(',');
input.append(int_overflow);
//input.push_back(']');
std::string str(stream_source<char>::default_max_buffer_size+10, '1');
for (std::size_t i = 0; i < str.size(); i+= 2)
{
str[i] = '0';
}

std::string input;
input.push_back('"');
input.append(str);
input.push_back('"');
std::stringstream is(input);

auto j = json::parse(is);
REQUIRE(j.is_string());
CHECK(j.as<std::string>() == str);
}

SECTION("number with split buffer")
{
std::string str(stream_source<char>::default_max_buffer_size-7, 'a');
std::string neg_num("-123456789.123456789");

std::string input;
input.push_back('[');
input.push_back('"');
input.append(str);
input.push_back('"');
input.push_back(',');
input.append(neg_num);
input.push_back(']');

std::stringstream is(input);

auto j = json::parse(is);
//CHECK(j[1].as<double>() == -123456789.123456789);
}*/

REQUIRE(j.is_array());
REQUIRE(j.size() == 2);
CHECK(j[1].as<double>() == -123456789.123456789);
}

SECTION("false with split buffer")
{
std::string str2;
str2.push_back('[');
str2.push_back('"');
str2.append(stream_source<char>::default_max_buffer_size-8, 'a');
str2.push_back('"');
str2.push_back(',');
str2.append("false");
str2.push_back(']');
std::string str;
str.push_back('[');
str.push_back('"');
str.append(stream_source<char>::default_max_buffer_size-8, 'a');
str.push_back('"');
str.push_back(',');
str.append("false");
str.push_back(']');

std::stringstream is(str2);
std::stringstream is(str);

auto j = json::parse(is);
REQUIRE(j.is_array());
REQUIRE(j.size() == 2);
CHECK_FALSE(j[1].as<bool>());

}

SECTION("true with split buffer")
{
std::string str2;
str2.push_back('[');
str2.push_back('"');
str2.append(stream_source<char>::default_max_buffer_size - 6, 'a');
str2.push_back('"');
str2.push_back(',');
str2.append("true");
str2.push_back(']');
std::string str;
str.push_back('[');
str.push_back('"');
str.append(stream_source<char>::default_max_buffer_size - 6, 'a');
str.push_back('"');
str.push_back(',');
str.append("true");
str.push_back(']');

std::stringstream is(str2);
std::stringstream is(str);

auto j = json::parse(is);
REQUIRE(j.is_array());
Expand All @@ -82,16 +96,16 @@ TEST_CASE("test json_reader buffered read")

SECTION("null with split buffer")
{
std::string str2;
str2.push_back('[');
str2.push_back('"');
str2.append(stream_source<char>::default_max_buffer_size - 5, 'a');
str2.push_back('"');
str2.push_back(',');
str2.append("null");
str2.push_back(']');

std::stringstream is(str2);
std::string str;
str.push_back('[');
str.push_back('"');
str.append(stream_source<char>::default_max_buffer_size - 5, 'a');
str.push_back('"');
str.push_back(',');
str.append("null");
str.push_back(']');

std::stringstream is(str);

auto j = json::parse(is);
REQUIRE(j.is_array());
Expand Down

0 comments on commit b20c30d

Please sign in to comment.