souffle  2.0.2-371-g6315b36
record_table_test.cpp
Go to the documentation of this file.
1 /*
2  * Souffle - A Datalog Compiler
3  * Copyright (c) 2020, The Souffle Developers. All rights reserved
4  * Licensed under the Universal Permissive License v 1.0 as shown at:
5  * - https://opensource.org/licenses/UPL
6  * - <souffle root>/licenses/SOUFFLE-UPL.txt
7  */
8 
9 /************************************************************************
10  *
11  * @file record_table_test.cpp
12  *
13  * Tests the record table.
14  *
15  ***********************************************************************/
16 
17 #include "tests/test.h"
18 
19 #include "souffle/RamTypes.h"
20 #include "souffle/RecordTable.h"
21 #include <functional>
22 #include <iostream>
23 #include <limits>
24 #include <random>
25 #include <string>
26 #include <vector>
27 
28 #include <cstddef>
29 
30 namespace souffle::test {
31 
32 #define NUMBER_OF_TESTS 100
33 
34 TEST(Pack, Tuple) {
35  RecordTable recordTable;
36  const Tuple<RamDomain, 3> tuple = {{1, 2, 3}};
37 
38  RamDomain ref = pack(recordTable, tuple);
39 
40  const RamDomain* ptr = recordTable.unpack(ref, 3);
41 
42  for (size_t i = 0; i < 3; ++i) {
43  EXPECT_EQ(tuple[i], ptr[i]);
44  }
45 }
46 
47 // Generate random tuples
48 // pack them all
49 // unpack and test for equality
50 TEST(PackUnpack, Tuple) {
51  constexpr size_t tupleSize = 3;
52  using tupleType = Tuple<RamDomain, tupleSize>;
53 
54  RecordTable recordTable;
55 
56  // Setup random number generation
57  std::default_random_engine randomGenerator(3);
58  std::uniform_int_distribution<RamDomain> distribution(
59  std::numeric_limits<RamDomain>::lowest(), std::numeric_limits<RamDomain>::max());
60 
61  auto random = std::bind(distribution, randomGenerator);
62 
63  // Tuples that will be packed
64  std::vector<tupleType> toPack(NUMBER_OF_TESTS);
65 
66  // Tuple reference after they are packed.
67  std::vector<RamDomain> tupleRef(NUMBER_OF_TESTS);
68 
69  // Generate and pack the tuples
70  for (size_t i = 0; i < NUMBER_OF_TESTS; ++i) {
71  toPack[i] = {{random(), random(), random()}};
72  tupleRef[i] = pack(recordTable, toPack[i]);
73  }
74 
75  // unpack and test
76  for (size_t i = 0; i < NUMBER_OF_TESTS; ++i) {
77  auto unpacked = recordTable.unpack(tupleRef[i], tupleSize);
78  tupleType cmp = {unpacked[0], unpacked[1], unpacked[2]};
79  EXPECT_EQ(toPack[i], cmp);
80  }
81 }
82 
83 // Generate random vectors
84 // pack them all
85 // unpack and test for equality
86 TEST(PackUnpack, Vector) {
87  constexpr size_t vectorSize = 10;
88 
89  RecordTable recordTable;
90 
91  // Tuples that will be packed
92  std::vector<std::vector<RamDomain>> toPack(NUMBER_OF_TESTS);
93 
94  // Tuple reference after they are packed.
95  std::vector<RamDomain> tupleRef(NUMBER_OF_TESTS);
96 
97  // Generate and pack the tuples
98  for (size_t i = 0; i < NUMBER_OF_TESTS; ++i) {
99  toPack[i] = testutil::generateRandomVector<RamDomain>(10);
100  tupleRef[i] = recordTable.pack(toPack[i].data(), vectorSize);
101  std::cerr << "Ref: " << tupleRef[i] << std::endl;
102  }
103 
104  // unpack and test
105  for (size_t i = 0; i < NUMBER_OF_TESTS; ++i) {
106  const RamDomain* unpacked{recordTable.unpack(tupleRef[i], vectorSize)};
107  for (size_t j = 0; j < vectorSize; ++j) {
108  EXPECT_EQ(toPack[i][j], unpacked[j]);
109  }
110  }
111 }
112 
113 } // namespace souffle::test
souffle::RamDomain
int32_t RamDomain
Definition: RamTypes.h:56
EXPECT_EQ
#define EXPECT_EQ(a, b)
Definition: test.h:191
j
var j
Definition: htmlJsChartistMin.h:15
NUMBER_OF_TESTS
#define NUMBER_OF_TESTS
Definition: record_table_test.cpp:38
i
size_t i
Definition: json11.h:663
souffle::test
Definition: binary_relation_test.cpp:43
souffle::pack
RamDomain pack(RecordTable &recordTab, Tuple< RamDomain, Arity > const &tuple)
helper to convert tuple to record reference for the synthesiser
Definition: RecordTable.h:153
test.h
souffle::Tuple
std::array< A, N > Tuple
Definition: RamTypes.h:36
RecordTable.h
RamTypes.h
TCB_SPAN_NAMESPACE_NAME::detail::data
constexpr auto data(C &c) -> decltype(c.data())
Definition: span.h:210
souffle::tuple
Defines a tuple for the OO interface such that relations with varying columns can be accessed.
Definition: SouffleInterface.h:443
souffle::test::TEST
TEST(EqRelTest, Scoping)
Definition: binary_relation_test.cpp:51