From 8249b28bea29e8ce17eac12776a60ec3f9e47176 Mon Sep 17 00:00:00 2001 From: Dominick Allen Date: Thu, 17 Oct 2024 19:42:29 -0500 Subject: Rename InvalidInput to ArgumentInvalid. --- test/CMakeLists.txt | 6 ++++-- test/test_allocator.cpp | 54 +++++++++++++++++++++++++++++++++++++++++++++++++ test/test_assert.cpp | 2 +- test/test_common.cpp | 29 +++++++++++++++++++++----- test/test_common.hpp | 28 ++++++++++++++++++++++--- test/test_directory.cpp | 6 +++--- test/test_result.cpp | 4 ++-- test/test_span.cpp | 52 +++++++++++++++++++++++++++++++++++++++++++++++ 8 files changed, 165 insertions(+), 16 deletions(-) create mode 100644 test/test_allocator.cpp create mode 100644 test/test_span.cpp (limited to 'test') diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index a20e991..a2fcf7b 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -57,11 +57,13 @@ function(fud_add_test test_name) endfunction() fud_add_test(test_fud SOURCES test_fud.cpp) +fud_add_test(test_allocator SOURCES test_allocator.cpp) fud_add_test(test_assert SOURCES test_assert.cpp) +fud_add_test(test_directory SOURCES test_directory.cpp) fud_add_test(test_result SOURCES test_result.cpp) -fud_add_test(test_string SOURCES test_string.cpp) +fud_add_test(test_span SOURCES test_span.cpp) fud_add_test(test_sqlite SOURCES test_sqlite.cpp) -fud_add_test(test_directory SOURCES test_directory.cpp) +fud_add_test(test_string SOURCES test_string.cpp) # fud_add_test(test_deserialize_number SOURCES test_deserialize_number.cpp) # fud_add_test(test_ext_algorithm SOURCES test_algorithm.cpp) # fud_add_test(test_ext_array SOURCES diff --git a/test/test_allocator.cpp b/test/test_allocator.cpp new file mode 100644 index 0000000..d93df1e --- /dev/null +++ b/test/test_allocator.cpp @@ -0,0 +1,54 @@ +/* + * libfud + * Copyright 2024 Dominick Allen + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "fud_allocator.hpp" +#include "test_common.hpp" + +#include "gtest/gtest.h" + +namespace fud { + +struct FailAllocator : public MockFudAlloc { + virtual void* operator()(size_t size) override { + static_cast(size); + return nullptr; + } +}; + +TEST(AllocatorTest, TestFudAllocator) +{ + FudAllocator fudAllocator{}; + ASSERT_NE(&fudAllocator, &globalFudAllocator); + ASSERT_FALSE(fudAllocator.isEqual(globalFudAllocator)); + + auto allocResult{fudAllocator.allocate(1)}; + ASSERT_TRUE(allocResult.isOkay()); + auto* mem = allocResult.getOkay(); + ASSERT_NE(mem, nullptr); + ASSERT_EQ(fudAllocator.deallocate(mem, 0), FudStatus::ArgumentInvalid); + ASSERT_EQ(fudAllocator.deallocate(mem, 1), FudStatus::Success); + mem = nullptr; + ASSERT_EQ(fudAllocator.deallocate(mem, 1), FudStatus::ArgumentInvalid); + + FailAllocator failAllocator{}; + globalMockFudAlloc.m_allocator = &failAllocator; + + allocResult = fudAllocator.allocate(1); + ASSERT_TRUE(allocResult.isError()); +} + +} // namespace fud diff --git a/test/test_assert.cpp b/test/test_assert.cpp index 41cd20f..1f95388 100644 --- a/test/test_assert.cpp +++ b/test/test_assert.cpp @@ -21,7 +21,7 @@ namespace fud { -TEST(FudTest, FudFud) +TEST(AssertTest, AssertFud) { EXPECT_EXIT(fudAssert(false), ::testing::KilledBySignal(SIGABRT), ".*"); } diff --git a/test/test_common.cpp b/test/test_common.cpp index 5a26e09..d3e1704 100644 --- a/test/test_common.cpp +++ b/test/test_common.cpp @@ -16,21 +16,40 @@ */ #include "test_common.hpp" -#include "fud_allocator.hpp" #include namespace fud { -void* fudAlloc(size_t size) { +void* MockFudAlloc::operator()(size_t size) +{ return malloc(size); } -void* fudRealloc(void* ptr, size_t size) { - return realloc(ptr, size); +void MockFudDealloc::operator()(void* pointer) +{ + return free(pointer); +} + +MockFudAlloc globalDefaultMockAlloc{}; + +MockFudDealloc globalDefaultMockDealloc{}; + +void* MockFudAllocator::allocate(size_t size) { + return (*m_allocator)(size); +} + +void MockFudAllocator::deallocate(void* pointer) { + return (*m_deallocator)(pointer); +} + +MockFudAllocator globalMockFudAlloc{}; + +void* fudAlloc(size_t size) { + return globalMockFudAlloc.allocate(size); } void fudFree(void* ptr) { - return free(ptr); + return globalMockFudAlloc.deallocate(ptr); } } // namespace fud diff --git a/test/test_common.hpp b/test/test_common.hpp index fa6cf09..05f86db 100644 --- a/test/test_common.hpp +++ b/test/test_common.hpp @@ -15,13 +15,13 @@ * limitations under the License. */ -#ifndef EXT_TEST_COMMON_HPP -#define EXT_TEST_COMMON_HPP +#ifndef FUD_TEST_COMMON_HPP +#define FUD_TEST_COMMON_HPP #include #include -namespace ext_lib { +namespace fud { // NOLINTBEGIN(cppcoreguidelines-macro-usage) #define MULTI_BYTE_LITERAL "test今日素敵はですねƩ®😀z" @@ -38,6 +38,28 @@ static_assert(sizeof(FOUR_BYTE) == 4 + 1); // NOLINTEND(cppcoreguidelines-macro-usage) constexpr size_t charSetSize = sizeof(CHARACTER_SET) - 1; +struct MockFudAlloc { + virtual void* operator()(size_t size); +}; + +extern MockFudAlloc globalDefaultMockAlloc; + +struct MockFudDealloc { + virtual void operator()(void* pointer); +}; + +extern MockFudDealloc globalDefaultMockDealloc; + +struct MockFudAllocator { + void* allocate(size_t size); + void deallocate(void* pointer); + + MockFudAlloc* m_allocator{&globalDefaultMockAlloc}; + MockFudDealloc* m_deallocator{&globalDefaultMockDealloc};; +}; + +extern MockFudAllocator globalMockFudAlloc; + } // namespace ext_lib #endif diff --git a/test/test_directory.cpp b/test/test_directory.cpp index c2af281..2f69dab 100644 --- a/test/test_directory.cpp +++ b/test/test_directory.cpp @@ -49,16 +49,16 @@ FudStatus removeRecursive(const String& path) return FudStatus::Utf8Invalid; } if (path.length() < 5) { - return FudStatus::InvalidInput; + return FudStatus::ArgumentInvalid; } auto prefix{String::makeFromCString("/tmp/").takeOkay()}; auto diffResult = compareMem(path.data(), path.length(), prefix.data(), prefix.length()); if (diffResult.isError()) { - return FudStatus::InvalidInput; + return FudStatus::ArgumentInvalid; } auto diff = diffResult.getOkay(); if (diff != 0) { - return FudStatus::InvalidInput; + return FudStatus::ArgumentInvalid; } constexpr int maxOpenFd = 64; auto status = nftw(path.c_str(), unlink_cb, maxOpenFd, FTW_DEPTH | FTW_PHYS); diff --git a/test/test_result.cpp b/test/test_result.cpp index 172323d..5eadd8c 100644 --- a/test/test_result.cpp +++ b/test/test_result.cpp @@ -43,10 +43,10 @@ TEST(ResultTest, ErrResult) ASSERT_EQ(err, FudStatus::Failure); - const auto status = FudStatus::InvalidInput; + const auto status = FudStatus::ArgumentInvalid; GResult err2 = GResult::error(status); ASSERT_TRUE(err2.isError()); - ASSERT_EQ(err2.getError(), FudStatus::InvalidInput); + ASSERT_EQ(err2.getError(), FudStatus::ArgumentInvalid); } } // namespace fud diff --git a/test/test_span.cpp b/test/test_span.cpp new file mode 100644 index 0000000..28d0cc5 --- /dev/null +++ b/test/test_span.cpp @@ -0,0 +1,52 @@ +/* + * libfud + * Copyright 2024 Dominick Allen + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "fud_array.hpp" +#include "fud_span.hpp" + +#include "gtest/gtest.h" + +namespace fud { + +TEST(SpanTest, MutableSpanTest) +{ + Array data{1, 2, 3, 4}; + auto span = Span::make(data); + for (auto& elt : span) { + elt *= 2; + } + + const Array expected{2, 4, 6, 8}; + EXPECT_EQ(data, expected); + + const int* innerImmutable = span.data(); + EXPECT_EQ(*innerImmutable, expected[0]); +} + +TEST(SpanTest, ConstSpanTest) +{ + const Array data{1, 2, 3, 4}; + auto span = Span::make(data); + + int sum = 0; + for (const auto& elt : span) { + sum += elt; + } + EXPECT_EQ(sum, 4 * 5 / 2); +} + +} // namespace fud -- cgit v1.2.3