|
| 1 | +#include <catch2/catch_all.hpp> |
| 2 | +#include <csv.hpp> |
| 3 | + |
| 4 | +#ifdef CSV_HAS_CXX20 |
| 5 | +#include <ranges> |
| 6 | + |
| 7 | +TEST_CASE("CSVReader C++20 Ranges Compatibility", "[ranges][cxx20]") { |
| 8 | + SECTION("CSVReader works with std::ranges::distance") { |
| 9 | + std::stringstream ss("A,B,C\n1,2,3\n4,5,6\n7,8,9"); |
| 10 | + csv::CSVReader reader(ss); |
| 11 | + |
| 12 | + auto count = std::ranges::distance(reader); |
| 13 | + REQUIRE(count == 3); |
| 14 | + } |
| 15 | + |
| 16 | + SECTION("CSVReader works with std::views") { |
| 17 | + std::stringstream ss("A,B,C\n1,2,3\n4,5,6\n7,8,9\n10,11,12"); |
| 18 | + csv::CSVReader reader(ss); |
| 19 | + |
| 20 | + auto filtered = reader | |
| 21 | + std::views::filter([](const csv::CSVRow &row) { |
| 22 | + return !row.empty() && row[0].get<int>() > 5; |
| 23 | + }); |
| 24 | + |
| 25 | + int filtered_count = 0; |
| 26 | + for (const auto &row : filtered) { |
| 27 | + filtered_count++; |
| 28 | + int val = row[0].get<int>(); |
| 29 | + REQUIRE(val > 5); |
| 30 | + } |
| 31 | + REQUIRE(filtered_count == 2); // rows with 7 and 10 |
| 32 | + } |
| 33 | + |
| 34 | + SECTION("CSVReader iterator satisfies input_range requirements") { |
| 35 | + std::stringstream ss("A,B\n1,2\n3,4"); |
| 36 | + csv::CSVReader reader(ss); |
| 37 | + |
| 38 | + auto it = reader.begin(); |
| 39 | + auto end = reader.end(); |
| 40 | + |
| 41 | + static_assert(std::input_iterator<decltype(it)>); |
| 42 | + static_assert(std::ranges::range<csv::CSVReader>); |
| 43 | + static_assert(std::ranges::input_range<csv::CSVReader>); |
| 44 | + static_assert(std::sentinel_for<decltype(end), decltype(it)>); |
| 45 | + |
| 46 | + REQUIRE(it != end); |
| 47 | + auto row = *it; |
| 48 | + REQUIRE(row.size() == 2); |
| 49 | + |
| 50 | + ++it; |
| 51 | + REQUIRE(it != end); |
| 52 | + |
| 53 | + ++it; |
| 54 | + REQUIRE(it == end); |
| 55 | + } |
| 56 | +} |
| 57 | +#endif |
0 commit comments