This repository was archived by the owner on Jan 31, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 360
Expand file tree
/
Copy pathBlockChainTest.cpp
More file actions
236 lines (188 loc) · 6.13 KB
/
BlockChainTest.cpp
File metadata and controls
236 lines (188 loc) · 6.13 KB
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
// Copyright (c) 2020 The Orbit Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <gtest/gtest.h>
#include <string>
#include <utility>
#include "Containers/BlockChain.h"
namespace orbit_containers {
namespace {
class CopyableType {
public:
CopyableType() = default;
explicit CopyableType(std::string value) : value_{std::move(value)} {}
CopyableType(const CopyableType&) = default;
CopyableType& operator=(const CopyableType&) = default;
void set_value(std::string value) { value_ = std::move(value); }
[[nodiscard]] const std::string& value() const { return value_; }
private:
std::string value_;
};
class MovableType {
public:
MovableType() = default;
explicit MovableType(std::string value) : value_{std::move(value)} {}
MovableType(const MovableType&) = delete;
MovableType& operator=(const MovableType&) = delete;
MovableType(MovableType&&) = default;
MovableType& operator=(MovableType&&) = default;
[[nodiscard]] const std::string& value() const { return value_; }
private:
std::string value_;
};
}; // namespace
TEST(BlockChain, CreateAndMove) {
BlockChain<int, 1024> source_chain;
EXPECT_EQ(source_chain.root(), nullptr);
source_chain.emplace_back(1);
source_chain.emplace_back(2);
EXPECT_NE(source_chain.root(), nullptr);
BlockChain<int, 1024> target_chain(std::move(source_chain));
EXPECT_EQ(target_chain.size(), 2);
EXPECT_EQ(target_chain.root()->data()[0], 1);
// Verify the moved-from block chain is still in a valid state, even if it should no longer be
// used
EXPECT_EQ(source_chain.size(), 0); // NOLINT(bugprone-use-after-move)
EXPECT_EQ(source_chain.root(), nullptr); // NOLINT(bugprone-use-after-move)
source_chain.emplace_back(3);
EXPECT_EQ(source_chain.size(), 1); // NOLINT(bugprone-use-after-move)
EXPECT_EQ(source_chain.root()->data()[0], 3); // NOLINT(bugprone-use-after-move)
// Verify both block chains are fully decoupled
target_chain.emplace_back(10);
EXPECT_EQ(source_chain.size(), 1); // NOLINT(bugprone-use-after-move)
EXPECT_EQ(target_chain.size(), 3);
}
TEST(BlockChain, AddCopyableTypes) {
CopyableType v1("hello world");
CopyableType v2("or not");
BlockChain<CopyableType, 1024> chain;
EXPECT_EQ(chain.size(), 0);
chain.emplace_back(v1);
chain.emplace_back(v2);
EXPECT_EQ(chain.size(), 2);
v1.set_value("new v1");
v2.set_value("new v2");
EXPECT_EQ(chain.root()->data()[0].value(), "hello world");
EXPECT_EQ(chain.root()->data()[1].value(), "or not");
// Multi-block test
for (int i = 0; i < 2000; ++i) {
chain.emplace_back(v1);
}
EXPECT_EQ(chain.size(), 2002);
}
TEST(BlockChain, Clear) {
const std::string v1 = "hello world";
const std::string v2 = "or not";
BlockChain<std::string, 1024> chain;
chain.emplace_back(v1);
EXPECT_GT(chain.size(), 0);
chain.clear();
EXPECT_EQ(chain.size(), 0);
chain.emplace_back(v2);
EXPECT_GT(chain.size(), 0);
EXPECT_EQ(chain.root()->data()[0], v2);
// Multi-block test
for (int i = 0; i < 2000; ++i) {
chain.emplace_back(v1);
}
chain.clear();
EXPECT_EQ(chain.size(), 0);
}
TEST(BlockChain, ElementIteration) {
constexpr const int kV1 = 5;
constexpr const int kV2 = 10;
constexpr const int kV3 = 15;
BlockChain<int, 1024> chain;
chain.emplace_back(kV1);
chain.emplace_back(kV2);
chain.emplace_back(kV3);
// Note that only the "++it" operator is supported
auto it = chain.begin();
EXPECT_EQ(*it, kV1);
++it;
EXPECT_EQ(*it, kV2);
++it;
EXPECT_EQ(*it, kV3);
++it;
// ... and also only !=, not ==
EXPECT_FALSE(it != chain.end());
// Test the complete "typical pattern"
int it_count = 0;
for (it = chain.begin(); it != chain.end(); ++it) {
++it_count;
}
EXPECT_EQ(it_count, 3);
// Multi-block test
chain.clear();
for (int i = 0; i < 2000; ++i) {
chain.emplace_back(i);
}
it_count = 0;
for (it = chain.begin(); it != chain.end(); ++it) {
EXPECT_EQ(*it, it_count);
++it_count;
}
auto it_begin = chain.begin();
it = chain.begin();
++it;
while (it != chain.end()) {
EXPECT_TRUE(it != it_begin);
++it;
}
EXPECT_EQ(it_count, 2000);
}
TEST(BlockChain, EmptyIteration) {
BlockChain<std::string, 1024> chain;
auto it = chain.begin();
ASSERT_FALSE(it != chain.end());
}
TEST(BlockChain, AddCopyableTypesN) {
const std::string v1 = "hello world";
BlockChain<std::string, 1024> chain;
chain.push_back_n(v1, 2000);
EXPECT_EQ(chain.size(), 2000);
for (const auto& it : chain) {
EXPECT_EQ(it, v1);
}
}
// "Reset" works like "clear", except that it does not actually free
// any memory - it keeps the block's memory, just setting their
// size to 0
TEST(BlockChain, Reset) {
BlockChain<int, 1024> chain;
chain.push_back_n(5, 1024 * 3);
EXPECT_GT(chain.size(), 0);
const Block<int, 1024>* block_ptr[] = {chain.root(), nullptr, nullptr};
block_ptr[1] = block_ptr[0]->next();
block_ptr[2] = block_ptr[1]->next();
// Tests below rely quite a lot on the internals of BlockChain, but this
// seems the easiest way to actually test re-usage of the block pointers
chain.Reset();
EXPECT_EQ(chain.size(), 0);
EXPECT_EQ(block_ptr[0]->size(), 0);
EXPECT_EQ(block_ptr[1]->size(), 0);
EXPECT_EQ(block_ptr[2]->size(), 0);
chain.push_back_n(10, 1024);
EXPECT_GT(chain.size(), 0);
EXPECT_EQ(chain.root()->data()[0], 10);
EXPECT_EQ(chain.root(), block_ptr[0]);
EXPECT_EQ(chain.root()->next(), block_ptr[1]);
EXPECT_EQ(block_ptr[1]->size(), 0);
chain.push_back_n(10, 1024);
EXPECT_EQ(chain.root()->next(), block_ptr[1]);
EXPECT_EQ(block_ptr[1]->size(), 1024);
EXPECT_EQ(block_ptr[2]->size(), 0);
chain.push_back_n(10, 1024);
EXPECT_EQ(chain.root()->next()->next(), block_ptr[2]);
EXPECT_EQ(block_ptr[2]->size(), 1024);
}
TEST(BlockChain, MovableType) {
BlockChain<MovableType, 1024> chain;
EXPECT_EQ(chain.size(), 0);
chain.emplace_back(MovableType("v1"));
chain.emplace_back(MovableType("v2"));
EXPECT_EQ(chain.size(), 2);
EXPECT_EQ(chain.root()->data()[0].value(), "v1");
EXPECT_EQ(chain.root()->data()[1].value(), "v2");
}
} // namespace orbit_containers