Skip to content

Commit 718eadc

Browse files
committed
Fix poorly organized iterator test suite
1 parent 95797eb commit 718eadc

2 files changed

Lines changed: 35 additions & 19 deletions

File tree

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,17 @@ However, `std::ifstream` may also be used as well as in-memory sources via `std:
147147
**Note**: Currently CSV guessing only works for memory-mapped files. The CSV dialect
148148
must be manually defined for other sources.
149149

150+
**Note on Iterator Type**: `CSVReader::iterator` is an **input iterator**, not a forward iterator.
151+
This design enables streaming large CSV files without loading them entirely into memory.
152+
While some algorithms requiring forward iterators (e.g., `std::max_element`) may work in practice,
153+
only input iterator algorithms are guaranteed to work reliably. If you need guaranteed forward-iterator
154+
or random-access support, collect rows into a container first:
155+
```cpp
156+
std::vector<CSVRow> rows;
157+
for (auto& row : reader) rows.push_back(row);
158+
// Now use std::max_element or other forward-iterator algorithms reliably
159+
```
160+
150161
```cpp
151162
CSVFormat format;
152163
// custom formatting options go here

tests/test_csv_iterator.cpp

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -108,27 +108,32 @@ TEST_CASE("Basic CSVReader Iterator Test", "[read_ints_iter]") {
108108

109109
//! [CSVReader Iterator 2]
110110
TEST_CASE("CSVReader Iterator + std::max_elem", "[iter_max_elem]") {
111-
// The first is such that each value in the ith row is the number i
112-
// There are 100 rows
113-
// The second file is a database of California state employee salaries
114-
CSVReader r1("./tests/data/fake_data/ints.csv"),
115-
r2("./tests/data/real_data/2015_StateDepartment.csv");
116-
117-
// Find largest number
118-
auto int_finder = [](CSVRow& left, CSVRow& right) {
119-
return (left["A"].get<int>() < right["A"].get<int>());
120-
};
111+
SECTION("Find Max Element with std::max_element") {
112+
// The first is such that each value in the ith row is the number i
113+
// There are 100 rows
114+
CSVReader r1("./tests/data/fake_data/ints.csv");
115+
116+
// Find largest number
117+
auto int_finder = [](CSVRow& left, CSVRow& right) {
118+
return (left["A"].get<int>() < right["A"].get<int>());
119+
};
120+
121+
auto max_int = std::max_element(r1.begin(), r1.end(), int_finder);
122+
REQUIRE((*max_int)["A"] == 100);
123+
}
121124

122-
auto max_int = std::max_element(r1.begin(), r2.end(), int_finder);
125+
SECTION ("Find Max Element with std::max_element - Large File") {
126+
// The second file is a database of California state employee salaries
127+
CSVReader r2("./tests/data/real_data/2015_StateDepartment.csv");
128+
129+
// Find highest salary
130+
auto wage_finder = [](CSVRow& left, CSVRow& right) {
131+
return (left["Total Wages"].get<double>() < right["Total Wages"].get<double>());
132+
};
123133

124-
// Find highest salary
125-
auto wage_finder = [](CSVRow& left, CSVRow& right) {
126-
return (left["Total Wages"].get<double>() < right["Total Wages"].get<double>());
127-
};
134+
auto max_wage = std::max_element(r2.begin(), r2.end(), wage_finder);
128135

129-
auto max_wage = std::max_element(r2.begin(), r2.end(), wage_finder);
130-
131-
REQUIRE((*max_int)["A"] == 100);
132-
REQUIRE((*max_wage)["Total Wages"] == "812064.87");
136+
REQUIRE((*max_wage)["Total Wages"] == "812064.87");
137+
}
133138
}
134139
//! [CSVReader Iterator 2]

0 commit comments

Comments
 (0)