-
Notifications
You must be signed in to change notification settings - Fork 242
Expand file tree
/
Copy pathrecord_table_test.cpp
More file actions
261 lines (202 loc) · 7.76 KB
/
Copy pathrecord_table_test.cpp
File metadata and controls
261 lines (202 loc) · 7.76 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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/*
* Souffle - A Datalog Compiler
* Copyright (c) 2020, The Souffle Developers. All rights reserved
* Licensed under the Universal Permissive License v 1.0 as shown at:
* - https://opensource.org/licenses/UPL
* - <souffle root>/licenses/SOUFFLE-UPL.txt
*/
/************************************************************************
*
* @file record_table_test.cpp
*
* Tests the record table.
*
***********************************************************************/
#include "tests/test.h"
#include "souffle/RamTypes.h"
#include "souffle/RecordTable.h"
#include "souffle/datastructure/RecordTableImpl.h"
#include <algorithm>
#include <functional>
#include <iostream>
#include <limits>
#include <random>
#include <string>
#include <vector>
#include <cstddef>
namespace souffle::test {
#define NUMBER_OF_TESTS 100
TEST(Pack, Tuple) {
SpecializedRecordTable<3> recordTable;
const Tuple<RamDomain, 3> tuple = {{1, 2, 3}};
RamDomain ref = pack(recordTable, tuple);
const RamDomain* ptr = recordTable.unpack(ref, 3);
for (std::size_t i = 0; i < 3; ++i) {
EXPECT_EQ(tuple[i], ptr[i]);
}
}
TEST(Pack, InitList) {
SpecializedRecordTable<3> recordTable;
RamDomain ref = recordTable.pack({1, 2, 3});
const RamDomain* ptr = recordTable.unpack(ref, 3);
EXPECT_EQ(1, ptr[0]);
EXPECT_EQ(2, ptr[1]);
EXPECT_EQ(3, ptr[2]);
}
TEST(Pack, InitListHelper) {
SpecializedRecordTable<3> recordTable;
RamDomain ref = pack(recordTable, {1, 2, 3});
const RamDomain* ptr = recordTable.unpack(ref, 3);
EXPECT_EQ(1, ptr[0]);
EXPECT_EQ(2, ptr[1]);
EXPECT_EQ(3, ptr[2]);
}
TEST(PackADT, Enum) {
EXPECT_EQ(2, packADTEnum(2));
}
TEST(PackADT, EmptyBranch) {
SpecializedRecordTable<0, 2> recordTable;
const RamDomain ref = packADT(recordTable, 3, {});
const RamDomain* adt = recordTable.unpack(ref, 2);
EXPECT_EQ(3, adt[0]);
EXPECT_EQ(recordTable.pack(nullptr, 0), adt[1]);
}
TEST(PackADT, UnaryBranch) {
SpecializedRecordTable<2> recordTable;
const RamDomain ref = packADT(recordTable, 4, {42});
const RamDomain* adt = recordTable.unpack(ref, 2);
EXPECT_EQ(4, adt[0]);
EXPECT_EQ(42, adt[1]);
}
TEST(PackADT, MultiArgumentBranch) {
SpecializedRecordTable<2, 3> recordTable;
const Tuple<RamDomain, 3> arguments = {{10, 20, 30}};
const RamDomain ref = packADT(recordTable, 5, arguments);
const RamDomain* adt = recordTable.unpack(ref, 2);
const RamDomain* unpackedArguments = recordTable.unpack(adt[1], 3);
EXPECT_EQ(5, adt[0]);
EXPECT_EQ(10, unpackedArguments[0]);
EXPECT_EQ(20, unpackedArguments[1]);
EXPECT_EQ(30, unpackedArguments[2]);
}
TEST(PackADT, NestedBranch) {
SpecializedRecordTable<2> recordTable;
const RamDomain inner = packADT(recordTable, 0, {7});
const RamDomain outer = packADT(recordTable, 1, {inner});
const RamDomain* unpackedOuter = recordTable.unpack(outer, 2);
EXPECT_EQ(1, unpackedOuter[0]);
EXPECT_EQ(inner, unpackedOuter[1]);
}
TEST(Enumerate, Empty) {
SpecializedRecordTable<2> recordTable;
std::size_t count = 0;
recordTable.enumerate([&](const RamDomain*, std::size_t, RamDomain) { count += 1; });
EXPECT_EQ(0, count);
}
TEST(Enumerate, Three) {
SpecializedRecordTable<3> recordTable;
RamDomain ref = pack(recordTable, {1, 2, 3});
recordTable.enumerate([&](const RamDomain* t, std::size_t arity, RamDomain idx) {
EXPECT_EQ(3, arity);
EXPECT_EQ(ref, idx);
EXPECT_EQ(1, t[0]);
EXPECT_EQ(2, t[1]);
EXPECT_EQ(3, t[2]);
});
}
// Generate random tuples
// pack them all
// unpack and test for equality
TEMPLATE_TEST(PackUnpack, Tuple, std::size_t TupleSize, TupleSize) {
using tupleType = Tuple<RamDomain, TupleSize>;
constexpr std::size_t tupleSize = TupleSize;
SpecializedRecordTable<3> recordTable;
// Setup random number generation
std::default_random_engine randomGenerator(3);
std::uniform_int_distribution<RamDomain> distribution(
std::numeric_limits<RamDomain>::lowest(), std::numeric_limits<RamDomain>::max());
auto random = std::bind(distribution, randomGenerator);
auto rnd = [&]() { return random(); };
// Tuples that will be packed
std::vector<tupleType> toPack(NUMBER_OF_TESTS);
// Tuple reference after they are packed.
std::vector<RamDomain> tupleRef(NUMBER_OF_TESTS);
// Generate and pack the tuples
for (std::size_t i = 0; i < NUMBER_OF_TESTS; ++i) {
std::generate(toPack[i].begin(), toPack[i].end(), rnd);
tupleRef[i] = pack(recordTable, toPack[i]);
EXPECT_LT(0, tupleRef[i]);
}
// unpack and test
for (std::size_t i = 0; i < NUMBER_OF_TESTS; ++i) {
const RamDomain* unpacked = recordTable.unpack(tupleRef[i], tupleSize);
tupleType cmp;
std::copy_n(unpacked, TupleSize, cmp.begin());
EXPECT_EQ(toPack[i], cmp);
}
}
INSTANTIATE_TEMPLATE_TEST(PackUnpack, Tuple, 0);
INSTANTIATE_TEMPLATE_TEST(PackUnpack, Tuple, 1);
INSTANTIATE_TEMPLATE_TEST(PackUnpack, Tuple, 2);
INSTANTIATE_TEMPLATE_TEST(PackUnpack, Tuple, 3);
INSTANTIATE_TEMPLATE_TEST(PackUnpack, Tuple, 4);
INSTANTIATE_TEMPLATE_TEST(PackUnpack, Tuple, 5);
INSTANTIATE_TEMPLATE_TEST(PackUnpack, Tuple, 6);
INSTANTIATE_TEMPLATE_TEST(PackUnpack, Tuple, 7);
INSTANTIATE_TEMPLATE_TEST(PackUnpack, Tuple, 11);
INSTANTIATE_TEMPLATE_TEST(PackUnpack, Tuple, 23);
INSTANTIATE_TEMPLATE_TEST(PackUnpack, Tuple, 59);
// Generate random vectors
// pack them all
// unpack and test for equality
TEMPLATE_TEST(PackUnpack, Vector, std::size_t VectorSize, VectorSize) {
constexpr std::size_t vectorSize = VectorSize;
// Setup random number generation
std::default_random_engine randomGenerator(3);
std::uniform_int_distribution<RamDomain> distribution(
std::numeric_limits<RamDomain>::lowest(), std::numeric_limits<RamDomain>::max());
auto random = std::bind(distribution, randomGenerator);
auto rnd = [&]() { return random(); };
SpecializedRecordTable<VectorSize> recordTable;
// Tuples that will be packed
std::vector<std::vector<RamDomain>> toPack(NUMBER_OF_TESTS);
// Tuple reference after they are packed.
std::vector<RamDomain> tupleRef(NUMBER_OF_TESTS);
// Generate and pack the tuples
for (std::size_t i = 0; i < NUMBER_OF_TESTS; ++i) {
toPack[i].resize(vectorSize);
std::generate(toPack[i].begin(), toPack[i].end(), rnd);
tupleRef[i] = recordTable.pack(toPack[i].data(), vectorSize);
EXPECT_LT(0, tupleRef[i]);
}
// unpack and test
for (std::size_t i = 0; i < NUMBER_OF_TESTS; ++i) {
const RamDomain* unpacked{recordTable.unpack(tupleRef[i], vectorSize)};
for (std::size_t j = 0; j < vectorSize; ++j) {
EXPECT_EQ(toPack[i][j], unpacked[j]);
}
}
}
// special version of the test for vector of size 0
SPECIALIZE_TEMPLATE_TEST(PackUnpack, Vector, 0) {
SpecializedRecordTable<0> recordTable;
std::vector<RamDomain> toPack(0);
RamDomain tupleRef = recordTable.pack(toPack.data(), 0);
// empty record has reference 1
EXPECT_EQ(tupleRef, 1);
const RamDomain* unpacked{recordTable.unpack(tupleRef, 0)};
// unpacking empty record returns nullptr
EXPECT_EQ(unpacked, nullptr);
}
INSTANTIATE_TEMPLATE_TEST(PackUnpack, Vector, 0);
INSTANTIATE_TEMPLATE_TEST(PackUnpack, Vector, 1);
INSTANTIATE_TEMPLATE_TEST(PackUnpack, Vector, 2);
INSTANTIATE_TEMPLATE_TEST(PackUnpack, Vector, 3);
INSTANTIATE_TEMPLATE_TEST(PackUnpack, Vector, 4);
INSTANTIATE_TEMPLATE_TEST(PackUnpack, Vector, 5);
INSTANTIATE_TEMPLATE_TEST(PackUnpack, Vector, 6);
INSTANTIATE_TEMPLATE_TEST(PackUnpack, Vector, 7);
INSTANTIATE_TEMPLATE_TEST(PackUnpack, Vector, 11);
INSTANTIATE_TEMPLATE_TEST(PackUnpack, Vector, 23);
INSTANTIATE_TEMPLATE_TEST(PackUnpack, Vector, 59);
} // namespace souffle::test