1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
|
/*
* 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_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{};
ASSERT_EQ(intVector.size(), 0);
ASSERT_EQ(intVector.capacity(), 0);
ASSERT_TRUE(intVector.ref(0).isError());
ASSERT_EQ(intVector.resize(10), FudStatus::Success);
ASSERT_EQ(intVector.size(), 10);
ASSERT_EQ(intVector.capacity(), 10);
ASSERT_TRUE(intVector.ref(0).isOkay());
ASSERT_EQ(intVector.ref(0).getOkay(), 0);
intVector.get(0).takeOkay().get() = 10;
ASSERT_EQ(intVector.ref(0).getOkay(), 10);
}
struct NonTrivial {
static thread_local int counter;
NonTrivial()
{
counter++;
}
explicit NonTrivial(int val) : value{val}
{
counter++;
}
NonTrivial(const NonTrivial&) = delete;
NonTrivial(NonTrivial&& rhs) : value{rhs.value}, destroyed{rhs.destroyed}
{
rhs.destroyed = true;
}
~NonTrivial()
{
if (!destroyed) {
counter--;
destroyed = true;
}
}
NonTrivial& operator=(const NonTrivial& rhs) = delete;
NonTrivial& operator=(NonTrivial&& rhs)
{
value = rhs.value;
destroyed = rhs.destroyed;
rhs.destroyed = true;
return *this;
}
int value{0};
bool destroyed{false};
};
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{testLinearAllocator};
ASSERT_EQ(nonTrivialVector.size(), 0);
ASSERT_EQ(nonTrivialVector.capacity(), 0);
ASSERT_TRUE(nonTrivialVector.ref(0).isError());
ASSERT_EQ(counter, 0);
ASSERT_EQ(nonTrivialVector.resize(10), FudStatus::Success);
ASSERT_EQ(nonTrivialVector.size(), 10);
ASSERT_EQ(nonTrivialVector.capacity(), 10);
ASSERT_EQ(counter, 10);
ASSERT_TRUE(nonTrivialVector.ref(0).isOkay());
ASSERT_EQ(nonTrivialVector.ref(0).getOkay().get().value, 0);
nonTrivialVector.get(0).takeOkay().get().value = 10;
ASSERT_EQ(nonTrivialVector.ref(0).getOkay().get().value, 10);
ASSERT_EQ(nonTrivialVector.pushBack(NonTrivial{42}), FudStatus::Success);
ASSERT_EQ(nonTrivialVector.size(), 11);
ASSERT_GE(nonTrivialVector.capacity(), 11);
ASSERT_EQ(counter, 11);
auto capacity = nonTrivialVector.capacity();
constexpr auto FAIL_RESERVE_SIZE = 0x10000000000 / sizeof(NonTrivial);
ASSERT_EQ(nonTrivialVector.reserve(FAIL_RESERVE_SIZE), FudStatus::AllocFailure);
ASSERT_EQ(nonTrivialVector.capacity(), capacity);
{
auto popResult{nonTrivialVector.popBack()};
ASSERT_TRUE(popResult.isOkay());
auto value{popResult.takeOkay()};
ASSERT_EQ(value.value, 42);
ASSERT_EQ(counter, 11);
}
ASSERT_EQ(counter, 10);
ASSERT_EQ(nonTrivialVector.eraseBack(), FudStatus::Success);
ASSERT_EQ(counter, 9);
int val = 1;
for (auto& element : nonTrivialVector) {
element.value = val;
val++;
}
ASSERT_EQ(nonTrivialVector.insert(3, NonTrivial{13}), FudStatus::Success);
for (size_t idx = 0; idx < 3; ++idx) {
ASSERT_EQ(nonTrivialVector[idx].value, idx + 1);
}
ASSERT_EQ(counter, 10);
ASSERT_EQ(nonTrivialVector[3].value, 13);
for (size_t idx = 4; idx < nonTrivialVector.size(); ++idx) {
ASSERT_EQ(nonTrivialVector[idx].value, idx);
}
ASSERT_EQ(counter, nonTrivialVector.size());
ASSERT_EQ(nonTrivialVector.erase(3), FudStatus::Success);
for (size_t idx = 0; idx < nonTrivialVector.size(); ++idx) {
EXPECT_EQ(nonTrivialVector[idx].value, idx + 1);
}
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
|