summaryrefslogtreecommitdiff
path: root/test/test_vector.cpp
blob: b4fd83cdbed1ef7d87907c4623bd5c32a93a3752 (plain)
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
/*
 * 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 "gtest/gtest.h"

namespace fud {

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)
{
    auto& counter = NonTrivial::counter;
    counter = 0;
    Vector<NonTrivial> nonTrivialVector{};
    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();
    ASSERT_EQ(nonTrivialVector.reserve(SIZE_MAX / sizeof(NonTrivial)), 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());
}

} // namespace fud