diff options
author | Dominick Allen <djallen@librehumanitas.org> | 2025-01-02 18:49:56 -0600 |
---|---|---|
committer | Dominick Allen <djallen@librehumanitas.org> | 2025-01-02 18:49:56 -0600 |
commit | d5a174a6d4f8be5e7cffe7c2adbb8db23b578f56 (patch) | |
tree | 536a10802f418e3e71f3828597439537d9e62f21 /test/test_csv.cpp | |
parent | 908fdf06b41f9084d719a4b517c868b1ad29a9ac (diff) |
Fixing errors in Vector.
Diffstat (limited to 'test/test_csv.cpp')
-rw-r--r-- | test/test_csv.cpp | 77 |
1 files changed, 73 insertions, 4 deletions
diff --git a/test/test_csv.cpp b/test/test_csv.cpp index 90e7ef0..cb93a32 100644 --- a/test/test_csv.cpp +++ b/test/test_csv.cpp @@ -20,14 +20,49 @@ #include "gtest/gtest.h" +//NOLINTBEGIN(readability-magic-numbers) + namespace fud { const StringView happyCsvFilename{u8"fud-happy-test.csv"}; const StringView happyData{ u8"foo,bar,baz\n" - u8"1,Unquoted Text,\"Quoted Text with embedded \"\" quote and embedded newline \n" - u8"see\"\n,,\"Prior two fields are empty\"\n"}; + u8"1,Unquoted Text,\"Quoted Text with embedded \"\" quote and embedded newline \nsee\"\n" + u8",,\"Prior two fields are empty\"\n"}; + +Csv expectedHappyCsv() { + Csv csv{Csv::makeDefault()}; + csv.numLines = 3; + csv.numColumns = 3; + + auto resizeStatus = csv.entries.resize(csv.numLines * csv.numColumns); + fudAssert(resizeStatus == FudStatus::Success); + csv.entries[0] = StringView{u8"foo"}; + csv.entries[1] = StringView{u8"bar"}; + csv.entries[2] = StringView{u8"baz"}; + csv.entries[3] = StringView{u8"1"}; + csv.entries[4] = StringView{u8"Unquoted Text"}; + csv.entries[5] = StringView{u8"Quoted Text with embedded \" quote and embedded newline \nsee"}; + csv.entries[6] = StringView{}; + csv.entries[7] = StringView{}; + csv.entries[8] = StringView{u8"Prior two fields are empty"}; + + for (const auto& entry : csv.entries) { + if (entry.length() > 0) { + auto extendStatus = csv.buffer.extend(entry.asSpan()); + fudAssert(extendStatus == FudStatus::Success); + } + } + + size_t totalSize = 0; + for (auto& entry : csv.entries) { + entry.m_data = csv.buffer.data() + totalSize; + totalSize += entry.length(); + } + + return csv; +} auto writeHappyCsv() -> FudStatus { @@ -44,6 +79,7 @@ auto writeHappyCsv() -> FudStatus } auto file{fileResult.takeOkay()}; + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) auto writeResult = file.write(reinterpret_cast<const std::byte*>(happyData.data()), happyData.length()); if (writeResult.status != FudStatus::Success) { return writeResult.status; @@ -62,13 +98,46 @@ TEST(FudCsv, ParseCsvFromFilename) ASSERT_EQ(writeHappyCsv(), FudStatus::Success); - debugPrint(u8"Wrote happy data:\n-----\n{}\n-----\n", happyData); - auto parseStatus = Csv::parseFromFilenameUnbuffered(csv, happyCsvFilename); if (parseStatus != FudStatus::Success) { debugPrint(u8"Error parsing file: {}\n", FudStatusToString(parseStatus)); } ASSERT_EQ(parseStatus, FudStatus::Success); + + auto expectedCsv{expectedHappyCsv()}; + ASSERT_EQ(expectedCsv.numLines, csv.numLines); + ASSERT_EQ(expectedCsv.numColumns, csv.numColumns); + ASSERT_EQ(expectedCsv.buffer.size(), csv.buffer.size()); + ASSERT_EQ(expectedCsv.entries.size(), csv.entries.size()); + EXPECT_EQ(0, compareMem(expectedCsv.buffer.data(), expectedCsv.buffer.size(), csv.buffer.data(), csv.buffer.size()).takeOkayOr(-1)); + for (size_t line = 0; line < csv.numLines; ++line) + { + for (size_t column = 0; column < csv.numColumns; ++column) + { + auto expectedEntryResult = expectedCsv.entry(line, column); + ASSERT_TRUE(expectedEntryResult.isOkay()); + auto expectedEntryOpt{expectedEntryResult.takeOkay()}; + + auto csvEntryResult = csv.entry(line, column); + EXPECT_TRUE(csvEntryResult.isOkay()); + if (csvEntryResult.isError()) { + continue; + } + auto csvEntryOpt{csvEntryResult.takeOkay()}; + + EXPECT_EQ(expectedEntryOpt.hasValue(), csvEntryOpt.hasValue()); + if (expectedEntryOpt.isNone()) { + continue; + } + auto expectedEntry{expectedEntryOpt.value()}; + auto csvEntry{csvEntryOpt.value()}; + + EXPECT_EQ(expectedEntry.length(), csvEntry.length()); + EXPECT_EQ(0, compareMem(expectedEntry.data(), expectedEntry.length(), csvEntry.data(), csvEntry.length()).takeOkayOr(-1)); + } + } } } // namespace fud + +//NOLINTEND(readability-magic-numbers) |