diff options
author | Dominick Allen <djallen@librehumanitas.org> | 2024-11-02 20:45:02 -0500 |
---|---|---|
committer | Dominick Allen <djallen@librehumanitas.org> | 2024-11-02 20:45:02 -0500 |
commit | e8422002f84dc4313894a5b3136c44a9005081fd (patch) | |
tree | b41633d9f759306fe9c7c01e209780222b47f5df /test/test_vector.cpp | |
parent | 6c7fd1db481ff10a16ecab958c6542784fa60b9c (diff) |
Allocator deallocate is void rather than returning FudStatus.
Diffstat (limited to 'test/test_vector.cpp')
-rw-r--r-- | test/test_vector.cpp | 53 |
1 files changed, 50 insertions, 3 deletions
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 |