Skip to content

Commit

Permalink
Fix potential bug with RawRowBuffer (#39)
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentlaucsb authored May 20, 2019
1 parent 0595acb commit a1e1343
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 21 deletions.
6 changes: 6 additions & 0 deletions include/internal/row_buffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,17 @@ namespace csv {
// Save current row in progress
auto new_buff = BufferPtr(new RawRowBuffer());

// Save text
new_buff->buffer = this->buffer.substr(
this->current_end, // Position
(this->buffer.size() - this->current_end) // Count
);

// Save split buffer in progress
for (size_t i = this->current_split_idx; i < this->split_buffer.size(); i++) {
new_buff->split_buffer.push_back(this->split_buffer[i]);
}

new_buff->col_names = this->col_names;

// No need to remove unnecessary bits from this buffer
Expand Down
6 changes: 6 additions & 0 deletions single_include/csv.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3817,11 +3817,17 @@ namespace csv {
// Save current row in progress
auto new_buff = BufferPtr(new RawRowBuffer());

// Save text
new_buff->buffer = this->buffer.substr(
this->current_end, // Position
(this->buffer.size() - this->current_end) // Count
);

// Save split buffer in progress
for (size_t i = this->current_split_idx; i < this->split_buffer.size(); i++) {
new_buff->split_buffer.push_back(this->split_buffer[i]);
}

new_buff->col_names = this->col_names;

// No need to remove unnecessary bits from this buffer
Expand Down
58 changes: 37 additions & 21 deletions tests/test_csv_buffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,42 +5,58 @@
using namespace csv::internals;

TEST_CASE("GiantStringBufferTest", "[test_giant_string_buffer]") {
RawRowBuffer buffer;
BufferPtr buffer = BufferPtr(new RawRowBuffer());

buffer.buffer.append("1234");
std::string first_row = std::string(buffer.get_row());
buffer->buffer.append("1234");
std::string first_row = std::string(buffer->get_row());

buffer.buffer.append("5678");
std::string second_row = std::string(buffer.get_row());
buffer->buffer.append("5678");
std::string second_row = std::string(buffer->get_row());

buffer.reset();
buffer.buffer.append("abcd");
std::string third_row = std::string(buffer.get_row());
buffer = buffer->reset();
buffer->buffer.append("abcd");
std::string third_row = std::string(buffer->get_row());

REQUIRE(first_row == "1234");
REQUIRE(second_row == "5678");
REQUIRE(third_row == "abcd");
}

TEST_CASE("GiantSplitBufferTest", "[test_giant_split_buffer]") {
RawRowBuffer buffer;
auto & splits = buffer.split_buffer;
BufferPtr buffer = BufferPtr(new RawRowBuffer());
auto * splits = &(buffer->split_buffer);

splits.push_back(1);
splits.push_back(2);
splits.push_back(3);
splits->push_back(1);
splits->push_back(2);
splits->push_back(3);

auto pos = buffer.get_splits();
auto pos = buffer->get_splits();
REQUIRE(pos.split_at(0) == 1);
REQUIRE(pos.split_at(1) == 2);
REQUIRE(pos.split_at(2) == 3);
REQUIRE(pos.n_cols == 4);

splits.push_back(4);
splits.push_back(5);

pos = buffer.get_splits();
REQUIRE(pos.split_at(0) == 4);
REQUIRE(pos.split_at(1) == 5);
REQUIRE(pos.n_cols == 3);
SECTION("Two Splits Test") {
splits->push_back(4);
splits->push_back(5);

pos = buffer->get_splits();
REQUIRE(pos.split_at(0) == 4);
REQUIRE(pos.split_at(1) == 5);
REQUIRE(pos.n_cols == 3);
}

SECTION("Reset In Middle Test") {
splits->push_back(1);
buffer = buffer->reset();
splits = &(buffer->split_buffer);
splits->push_back(2);
splits->push_back(3);

auto pos = buffer->get_splits();
REQUIRE(pos.split_at(0) == 1);
REQUIRE(pos.split_at(1) == 2);
REQUIRE(pos.split_at(2) == 3);
REQUIRE(pos.n_cols == 4);
}
}

0 comments on commit a1e1343

Please sign in to comment.