Skip to content

Commit d51f9e4

Browse files
committed
Fix off-by-one n_rows() when using iterators (#173)
Reported and diagnosed accurately by @sjoubert
1 parent 0abcfad commit d51f9e4

File tree

2 files changed

+24
-7
lines changed

2 files changed

+24
-7
lines changed

include/internal/csv_reader_iterator.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ namespace csv {
1515
if (this->records->empty()) return this->end();
1616
}
1717

18+
this->_n_rows++;
1819
CSVReader::iterator ret(this, this->records->pop_front());
1920
return ret;
2021
}

tests/test_read_csv_file.cpp

+23-7
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,29 @@ TEST_CASE("Prevent Column Names From Being Overwritten", "[csv_col_names_overwri
4444

4545
// get_file_info()
4646
TEST_CASE("get_file_info() Test", "[test_file_info]") {
47-
CSVFileInfo info = get_file_info(
48-
"./tests/data/real_data/2009PowerStatus.txt");
49-
50-
REQUIRE(info.delim == '|');
51-
REQUIRE(info.n_rows == 37960); // Can confirm with Excel
52-
REQUIRE(info.n_cols == 3);
53-
REQUIRE(info.col_names == vector<string>({"ReportDt", "Unit", "Power"}));
47+
SECTION("ints.csv") {
48+
CSVReader reader("./tests/data/fake_data/ints.csv");
49+
CSVRow row;
50+
while (reader.read_row(row)) {
51+
std::cout << row[0] << " " << reader.n_rows() << std::endl;
52+
}
53+
54+
CSVFileInfo info = get_file_info(
55+
"./tests/data/fake_data/ints.csv");
56+
57+
REQUIRE(info.delim == ',');
58+
REQUIRE(info.n_rows == 100);
59+
}
60+
61+
SECTION("2009PowerStatus.txt") {
62+
CSVFileInfo info = get_file_info(
63+
"./tests/data/real_data/2009PowerStatus.txt");
64+
65+
REQUIRE(info.delim == '|');
66+
REQUIRE(info.n_rows == 37960); // Can confirm with Excel
67+
REQUIRE(info.n_cols == 3);
68+
REQUIRE(info.col_names == vector<string>({ "ReportDt", "Unit", "Power" }));
69+
}
5470
}
5571

5672
TEST_CASE("Non-Existent CSV", "[read_ghost_csv]") {

0 commit comments

Comments
 (0)