From d5a174a6d4f8be5e7cffe7c2adbb8db23b578f56 Mon Sep 17 00:00:00 2001 From: Dominick Allen Date: Thu, 2 Jan 2025 18:49:56 -0600 Subject: Fixing errors in Vector. --- test/test_common.cpp | 16 +++++++++-- test/test_common.hpp | 7 +++++ test/test_csv.cpp | 77 +++++++++++++++++++++++++++++++++++++++++++++++++--- test/test_file.cpp | 11 ++++++-- 4 files changed, 102 insertions(+), 9 deletions(-) (limited to 'test') diff --git a/test/test_common.cpp b/test/test_common.cpp index f272dad..03c5dff 100644 --- a/test/test_common.cpp +++ b/test/test_common.cpp @@ -27,28 +27,35 @@ namespace fud { std::byte* MockFudAlloc::operator()(size_t size) { + // NOLINTNEXTLINE(cppcoreguidelines-no-malloc) return static_cast(malloc(size)); } void MockFudDealloc::operator()(std::byte* pointer) { + // NOLINTNEXTLINE(cppcoreguidelines-no-malloc) free(pointer); } +// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables) MockFudAlloc globalDefaultMockAlloc{}; +// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables) MockFudDealloc globalDefaultMockDealloc{}; +// NOLINTNEXTLINE(readability-make-member-function-const) std::byte* MockFudAllocator::allocate(size_t size) { return (*m_allocator)(size); } +// NOLINTNEXTLINE(readability-make-member-function-const) void MockFudAllocator::deallocate(std::byte* pointer) { - return (*m_deallocator)(pointer); + (*m_deallocator)(pointer); } +// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables) MockFudAllocator globalMockFudAlloc{}; std::byte* fudAlloc(size_t size) @@ -58,7 +65,7 @@ std::byte* fudAlloc(size_t size) void fudFree(std::byte* ptr) { - return globalMockFudAlloc.deallocate(ptr); + globalMockFudAlloc.deallocate(ptr); } int unlink_cb(const char* fpath, const struct stat* sb_unused, int typeflag, struct FTW* ftwbuf) @@ -79,7 +86,9 @@ FudStatus removeRecursive(StringView path) if (!path.utf8Valid()) { return FudStatus::Utf8Invalid; } - if (path.length() < 5) { + + constexpr const size_t minPathLength = 5; + if (path.length() < minPathLength) { return FudStatus::ArgumentInvalid; } if (not path.nullTerminated()) { @@ -96,6 +105,7 @@ FudStatus removeRecursive(StringView path) } constexpr int maxOpenFd = 64; + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) auto status = nftw(reinterpret_cast(path.data()), unlink_cb, maxOpenFd, FTW_DEPTH | FTW_PHYS); if (status == 0) { return FudStatus::Success; diff --git a/test/test_common.hpp b/test/test_common.hpp index 5f6828f..3be26f0 100644 --- a/test/test_common.hpp +++ b/test/test_common.hpp @@ -58,16 +58,22 @@ static_assert(sizeof(FOUR_BYTE) == 4 + 1); // NOLINTEND(cppcoreguidelines-macro-usage) +// NOLINTNEXTLINE(cppcoreguidelines-special-member-functions) struct MockFudAlloc { + virtual ~MockFudAlloc() = default; virtual std::byte* operator()(size_t size); }; +// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables) extern MockFudAlloc globalDefaultMockAlloc; +// NOLINTNEXTLINE(cppcoreguidelines-special-member-functions) struct MockFudDealloc { + virtual ~MockFudDealloc() = default; virtual void operator()(std::byte* pointer); }; +// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables) extern MockFudDealloc globalDefaultMockDealloc; struct MockFudAllocator { @@ -78,6 +84,7 @@ struct MockFudAllocator { MockFudDealloc* m_deallocator{&globalDefaultMockDealloc}; }; +// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables) extern MockFudAllocator globalMockFudAlloc; class String; 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(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) diff --git a/test/test_file.cpp b/test/test_file.cpp index 12cfb98..c1ce4f1 100644 --- a/test/test_file.cpp +++ b/test/test_file.cpp @@ -21,11 +21,11 @@ #include "test_common.hpp" #include "gtest/gtest.h" -#include #include #include #include -#include + +// NOLINTBEGIN(readability-magic-numbers) namespace fud { @@ -121,6 +121,7 @@ TEST(FudBufferedFile, OpenReadWrite) ASSERT_EQ(bufferedFile.file().size().getOkayOr(std::numeric_limits::max()), 0); auto writeResult = bufferedFile.write( + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) reinterpret_cast(testName.data()), testName.size(), NullOpt); @@ -132,6 +133,7 @@ TEST(FudBufferedFile, OpenReadWrite) ASSERT_EQ(bufferedFile.seekStart(), FudStatus::Success); Vector output{Vector::withSize(testName.size()).takeOkay()}; + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) auto readResult = bufferedFile.read(reinterpret_cast(output.data()), testName.size(), NullOpt); ASSERT_EQ(readResult, expected); @@ -142,11 +144,13 @@ TEST(FudBufferedFile, OpenReadWrite) ASSERT_EQ(bufferedFile.seekStart(), FudStatus::Success); expected.bytesDrained = 1; + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) readResult = bufferedFile.read(reinterpret_cast(output.data()), 1, NullOpt); ASSERT_EQ(readResult, expected); EXPECT_EQ(output[0], testName.data()[0]); expected.bytesDrained = testName.size() - 2; + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) readResult = bufferedFile.read(reinterpret_cast(output.data()) + 1, testName.size() - 2, NullOpt); ASSERT_EQ(readResult, expected); EXPECT_EQ( @@ -154,6 +158,7 @@ TEST(FudBufferedFile, OpenReadWrite) compareMem(output.data() + 1, output.size() - 1, testName.data() + 1, testName.size() - 2).takeOkayOr(-1)); expected.bytesDrained = 1; + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) readResult = bufferedFile.read(reinterpret_cast(output.data()), 1, NullOpt); EXPECT_TRUE(readResult.status == FudStatus::Success || readResult.status == FudStatus::Partial); @@ -164,3 +169,5 @@ TEST(FudBufferedFile, OpenReadWrite) } } // namespace fud + +// NOLINTEND(readability-magic-numbers) -- cgit v1.2.3