From e8422002f84dc4313894a5b3136c44a9005081fd Mon Sep 17 00:00:00 2001 From: Dominick Allen Date: Sat, 2 Nov 2024 20:45:02 -0500 Subject: Allocator deallocate is void rather than returning FudStatus. --- test/test_vector.cpp | 53 +++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 50 insertions(+), 3 deletions(-) (limited to 'test/test_vector.cpp') 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 +struct TestLinearAllocator : public Allocator { + virtual ~TestLinearAllocator() override = default; + + virtual Result 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{m_backing.data() + allocIndex}; + } + + virtual void deallocate(std::byte* pointer, size_t bytes) override + { + static_cast(pointer); + static_cast(bytes); + } + + virtual bool isEqual(const Allocator& rhs) const override { + return &rhs == static_cast(this); + } + + Array m_backing{Array::constFill({})}; + size_t m_next{0}; +}; + + TEST(VectorTest, TrivialVector) { Vector 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 testLinearAllocator{}; auto& counter = NonTrivial::counter; counter = 0; - Vector nonTrivialVector{}; + Vector 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>::withSizeFallible(10, [](auto& vec) { + return Vector::initializeWithSize(vec, 100, &globalNullAllocator); + })}; + EXPECT_TRUE(intVectorVectorResult.isError()); + EXPECT_EQ(intVectorVectorResult.getErrorOr(FudStatus::Success), FudStatus::AllocFailure); + // Result>, FudStatus> +} + } // namespace fud -- cgit v1.2.3