From bd072e1025e809fc228a08c16feb2149ec5b6350 Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Sat, 12 Mar 2022 00:45:27 +0100 Subject: [PATCH] kraken: csv: fix comma separation logic Unfortunately, `std::ostream_iterator` puts its separator *after* every element, not *in-between* each element. --- src/csv/write-csv.cc | 8 ++++++-- tests/unit/csv.cc | 4 ++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/csv/write-csv.cc b/src/csv/write-csv.cc index 186947c..ce86b83 100644 --- a/src/csv/write-csv.cc +++ b/src/csv/write-csv.cc @@ -8,8 +8,12 @@ namespace kraken::csv { void write_csv(std::ostream& output, csv_type const& csv) { for (const auto& line : csv) { - std::ranges::copy(line, - std::ostream_iterator{output, ","}); + bool first = true; + for (const auto& atom : line) { + output << (first ? "" : ",") << atom; + first = false; + }; + output << '\n'; } } diff --git a/tests/unit/csv.cc b/tests/unit/csv.cc index 2135081..093430a 100644 --- a/tests/unit/csv.cc +++ b/tests/unit/csv.cc @@ -48,7 +48,7 @@ TEST(write_csv, empty) { ASSERT_EQ(output.str(), expected); } -TEST(write_csv, DISABLED_single_line) { +TEST(write_csv, single_line) { auto const csv = csv_type{ {"a", "b", "c"}, }; @@ -58,7 +58,7 @@ TEST(write_csv, DISABLED_single_line) { ASSERT_EQ(output.str(), expected); } -TEST(write_csv, DISABLED_multiple_lines) { +TEST(write_csv, multiple_lines) { auto const csv = csv_type{ {"a", "b", "c"}, {"1", "2", "3"},