-
Notifications
You must be signed in to change notification settings - Fork 10.4k
/
Copy pathEncodedSequenceTest.cpp
203 lines (161 loc) · 4.29 KB
/
EncodedSequenceTest.cpp
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
//===--- EncodedSequenceTest.cpp ------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
#include "swift/Basic/EncodedSequence.h"
#include "gtest/gtest.h"
using namespace swift;
namespace {
struct Element {
unsigned X, Y, Z;
static Element decode(const EncodedSequenceBase::Chunk *&ptr) {
auto x = EncodedSequenceBase::decodeIndex(ptr);
auto y = EncodedSequenceBase::decodeIndex(ptr);
auto z = EncodedSequenceBase::decodeIndex(ptr);
return { x, y, z };
}
void encode(EncodedSequenceBase::Chunk *&ptr) const {
EncodedSequenceBase::encodeIndex(X, ptr);
EncodedSequenceBase::encodeIndex(Y, ptr);
EncodedSequenceBase::encodeIndex(Z, ptr);
}
unsigned getEncodedSize() const {
return EncodedSequenceBase::getEncodedIndexSize(X)
+ EncodedSequenceBase::getEncodedIndexSize(Y)
+ EncodedSequenceBase::getEncodedIndexSize(Z);
}
};
enum class DataSet {
None, Small, Big
};
static const DataSet AllDataSets[] = {
DataSet::None,
DataSet::Small,
DataSet::Big,
};
struct Tester {
SmallVector<Element, 4> Vector;
EncodedSequence<Element> Sequence;
Tester() {
EXPECT_TRUE(Sequence.empty());
}
void push_back(Element elt) {
Vector.push_back(elt);
Sequence.push_back(elt);
EXPECT_FALSE(Sequence.empty());
}
// The small data set should use inline storage on any pointer width.
void push_small() {
push_back({ 5, 75, 13 });
check();
}
// The big data set should use out-of-line storage on any pointer width.
void push_big() {
push_back({ 557, 33, 197 });
check();
push_back({ 234897, 3, 0 });
check();
push_back({ 10123, 34987345, 1387123 });
check();
}
void push(DataSet set) {
switch (set) {
case DataSet::None: return;
case DataSet::Small: return push_small();
case DataSet::Big: return push_big();
}
}
void check() {
auto vi = Vector.begin(), ve = Vector.end();
auto si = Sequence.begin(), se = Sequence.end();
while (true) {
if (vi == ve) {
EXPECT_EQ(se, si);
return;
}
auto velt = *vi;
auto selt = *si;
EXPECT_EQ(velt.X, selt.X);
EXPECT_EQ(velt.Y, selt.Y);
EXPECT_EQ(velt.Z, selt.Z);
auto selt2 = *si;
EXPECT_EQ(selt.X, selt2.X);
EXPECT_EQ(selt.Y, selt2.Y);
EXPECT_EQ(selt.Z, selt2.Z);
++vi;
++si;
}
}
};
} // end anonymous namespace
TEST(EncodedSequenceTest, PushIterate) {
Tester tester;
tester.check();
tester.push_back({ 557, 33, 197 });
tester.check();
tester.push_back({ 234897, 3, 0 });
tester.check();
tester.push_back({ 10123, 34987345, 1387123 });
tester.check();
}
static void testCopyCtor(DataSet srcSet) {
Tester src;
src.push(srcSet);
Tester dest = src;
src.check();
dest.check();
}
TEST(EncodedSequenceTest, CopyCtor) {
for (auto srcSet : AllDataSets)
testCopyCtor(srcSet);
}
static void testMoveCtor(DataSet srcSet) {
Tester src;
src.push(srcSet);
Tester dest = std::move(src);
EXPECT_TRUE(src.Sequence.empty());
src.Vector.clear();
src.check();
dest.check();
}
TEST(EncodedSequenceTest, MoveCtor) {
for (auto srcSet : AllDataSets)
testMoveCtor(srcSet);
}
static void testCopyAssign(DataSet destSet, DataSet srcSet) {
Tester src;
src.push(srcSet);
Tester dest;
dest.push(destSet);
dest = src;
src.check();
dest.check();
}
TEST(EncodedSequenceTest, CopyAssign) {
for (auto destSet : AllDataSets)
for (auto srcSet : AllDataSets)
testCopyAssign(destSet, srcSet);
}
static void testMoveAssign(DataSet destSet, DataSet srcSet) {
Tester src;
src.push(srcSet);
Tester dest;
dest.push(destSet);
dest = std::move(src);
EXPECT_TRUE(src.Sequence.empty());
src.Vector.clear();
src.check();
dest.check();
}
TEST(EncodedSequenceTest, MoveAssign) {
for (auto destSet : AllDataSets)
for (auto srcSet : AllDataSets)
testMoveAssign(destSet, srcSet);
}