diff options
Diffstat (limited to 'test')
-rw-r--r-- | test/test_allocator.cpp | 6 | ||||
-rw-r--r-- | test/test_common.cpp | 2 | ||||
-rw-r--r-- | test/test_utf8.cpp | 3 | ||||
-rw-r--r-- | test/test_vector.cpp | 53 |
4 files changed, 55 insertions, 9 deletions
diff --git a/test/test_allocator.cpp b/test/test_allocator.cpp index 6382463..fa32a96 100644 --- a/test/test_allocator.cpp +++ b/test/test_allocator.cpp @@ -39,10 +39,10 @@ TEST(AllocatorTest, TestFudAllocator) 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); + fudAllocator.deallocate(mem, 0); + fudAllocator.deallocate(mem, 1); mem = nullptr; - ASSERT_EQ(fudAllocator.deallocate(mem, 1), FudStatus::ArgumentInvalid); + fudAllocator.deallocate(mem, 1); FailAllocator failAllocator{}; globalMockFudAlloc.m_allocator = &failAllocator; diff --git a/test/test_common.cpp b/test/test_common.cpp index 778b4b5..07a0088 100644 --- a/test/test_common.cpp +++ b/test/test_common.cpp @@ -32,7 +32,7 @@ std::byte* MockFudAlloc::operator()(size_t size) void MockFudDealloc::operator()(std::byte* pointer) { - return free(pointer); + free(pointer); } MockFudAlloc globalDefaultMockAlloc{}; diff --git a/test/test_utf8.cpp b/test/test_utf8.cpp index 69a1643..e5cb89a 100644 --- a/test/test_utf8.cpp +++ b/test/test_utf8.cpp @@ -147,11 +147,10 @@ TEST(Utf8Test, Utf8MultiByte) return RetType::okay(data); } - virtual FudStatus deallocate(std::byte* pointer, size_t bytes) override final + virtual void deallocate(std::byte* pointer, size_t bytes) override final { static_cast<void>(pointer); static_cast<void>(bytes); - return FudStatus::Success; } virtual bool isEqual(const Allocator& rhs) const override final diff --git a/test/test_vector.cpp b/test/test_vector.cpp index cadeaa6..ba0272e 100644 --- a/test/test_vector.cpp +++ b/test/test_vector.cpp @@ -16,11 +16,44 @@ */ #include "fud_vector.hpp" +#include "fud_array.hpp" #include "gtest/gtest.h" namespace fud { +template <size_t Size> +struct TestLinearAllocator : public Allocator { + virtual ~TestLinearAllocator() override = default; + + virtual Result<std::byte*, FudStatus> allocate(size_t bytes, size_t alignment = alignof(std::max_align_t)) override + { + auto allocIndex = m_next; + if (allocIndex % alignment != 0) { + allocIndex += alignment - allocIndex % alignment; + } + if ((allocIndex + bytes) > Size) { + return FudError{FudStatus::AllocFailure}; + } + m_next = allocIndex + bytes; + return Okay<std::byte*>{m_backing.data() + allocIndex}; + } + + virtual void deallocate(std::byte* pointer, size_t bytes) override + { + static_cast<void>(pointer); + static_cast<void>(bytes); + } + + virtual bool isEqual(const Allocator& rhs) const override { + return &rhs == static_cast<const Allocator*>(this); + } + + Array<std::byte, Size> m_backing{Array<std::byte, Size>::constFill({})}; + size_t m_next{0}; +}; + + TEST(VectorTest, TrivialVector) { Vector<int> intVector{}; @@ -61,7 +94,8 @@ struct NonTrivial { } } NonTrivial& operator=(const NonTrivial& rhs) = delete; - NonTrivial& operator=(NonTrivial&& rhs) { + NonTrivial& operator=(NonTrivial&& rhs) + { value = rhs.value; destroyed = rhs.destroyed; rhs.destroyed = true; @@ -75,9 +109,11 @@ int thread_local NonTrivial::counter = 0; TEST(VectorTest, NonTrivialVector) { + constexpr size_t testAllocSize = sizeof(NonTrivial) * 30; + TestLinearAllocator<testAllocSize> testLinearAllocator{}; auto& counter = NonTrivial::counter; counter = 0; - Vector<NonTrivial> nonTrivialVector{}; + Vector<NonTrivial> nonTrivialVector{testLinearAllocator}; ASSERT_EQ(nonTrivialVector.size(), 0); ASSERT_EQ(nonTrivialVector.capacity(), 0); ASSERT_TRUE(nonTrivialVector.ref(0).isError()); @@ -114,7 +150,7 @@ TEST(VectorTest, NonTrivialVector) ASSERT_EQ(counter, 9); int val = 1; - for (auto& element: nonTrivialVector) { + for (auto& element : nonTrivialVector) { element.value = val; val++; } @@ -136,4 +172,15 @@ TEST(VectorTest, NonTrivialVector) ASSERT_EQ(counter, nonTrivialVector.size()); } +TEST(VectorTest, NestedVector) +{ + struct FallibleObject {}; + auto intVectorVectorResult{Vector<Vector<int>>::withSizeFallible(10, [](auto& vec) { + return Vector<int>::initializeWithSize(vec, 100, &globalNullAllocator); + })}; + EXPECT_TRUE(intVectorVectorResult.isError()); + EXPECT_EQ(intVectorVectorResult.getErrorOr(FudStatus::Success), FudStatus::AllocFailure); + // Result<Vector<Vector<FallibleObject>>, FudStatus> +} + } // namespace fud |