souffle  2.0.2-371-g6315b36
matching_test.cpp
Go to the documentation of this file.
1 /*
2  * Souffle - A Datalog Compiler
3  * Copyright (c) 2013, 2015, Oracle and/or its affiliates. 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 matching_test.cpp
12  *
13  * Test cases for the computation of optimal indices.
14  *
15  ***********************************************************************/
16 
17 #include "tests/test.h"
18 
19 #include "ram/analysis/Index.h"
20 #include <cstddef>
21 #include <cstdint>
22 #include <string>
23 
24 namespace souffle::ram {
25 using namespace analysis;
26 
27 class TestAutoIndex : public MinIndexSelection {
28 public:
29  TestAutoIndex() : MinIndexSelection() {}
30  /** returns number of unique matchings */
31  int getNumMatchings() {
32  return matching.getNumMatchings();
33  }
34 };
35 
37 
38 SearchSignature setBits(size_t arity, uint64_t mask) {
39  SearchSignature search(arity);
40  for (size_t i = 0; i < arity; ++i) {
41  if (mask % 2) {
43  }
44  mask /= 2;
45  }
46  return search;
47 }
48 
49 TEST(Matching, StaticTest_1) {
50  TestAutoIndex order;
51  Nodes nodes;
52  size_t arity = 5;
53 
54  uint64_t patterns[] = {1, 3, 5, 7, 15, 23, 31};
55  for (auto pattern : patterns) {
56  SearchSignature search = setBits(arity, pattern);
57  order.addSearch(search);
58  nodes.insert(search);
59  }
60 
61  order.solve();
62  int num = order.getNumMatchings();
63 
64  EXPECT_EQ(num, 5);
65 }
66 
67 TEST(Matching, StaticTest_2) {
68  TestAutoIndex order;
69  Nodes nodes;
70 
71  size_t arity = 7;
72 
73  uint64_t patterns[] = {7, 11, 23, 32, 33, 39, 49, 53, 104, 121};
74  for (auto pattern : patterns) {
75  SearchSignature search = setBits(arity, pattern);
76  order.addSearch(search);
77  nodes.insert(search);
78  }
79 
80  order.solve();
81  int num = order.getNumMatchings();
82 
83  EXPECT_EQ(num, 5);
84 }
85 
86 TEST(Matching, TestOver64BitSignature) {
87  TestAutoIndex order;
88  Nodes nodes;
89 
90  size_t arity = 100;
91  SearchSignature first(arity);
93  first[75] = AttributeConstraint::Equal;
94  first[50] = AttributeConstraint::Equal;
95  first[25] = AttributeConstraint::Equal;
96  first[0] = AttributeConstraint::Equal;
97 
98  SearchSignature second(arity);
99  second[99] = AttributeConstraint::Equal;
100  second[75] = AttributeConstraint::Equal;
101  second[50] = AttributeConstraint::Equal;
102 
103  SearchSignature third(arity);
104  third[99] = AttributeConstraint::Equal;
105  third[75] = AttributeConstraint::Equal;
106 
107  SearchSignature fourth(arity);
108  fourth[99] = AttributeConstraint::Equal;
109 
110  SearchSignature fifth(arity);
111  fifth[25] = AttributeConstraint::Equal;
112  fifth[0] = AttributeConstraint::Equal;
113 
114  nodes.insert(first);
115  nodes.insert(second);
116  nodes.insert(third);
117  nodes.insert(fourth);
118  nodes.insert(fifth);
119 
120  order.addSearch(first);
121  order.addSearch(second);
122  order.addSearch(third);
123  order.addSearch(fourth);
124  order.addSearch(fifth);
125 
126  order.solve();
127  int num = order.getNumMatchings();
128 
129  EXPECT_EQ(num, 3);
130 }
131 
132 } // namespace souffle::ram
souffle::ram::analysis::SearchSignature
search signature of a RAM operation; each bit represents an attribute of a relation.
Definition: Index.h:52
souffle::ram::TEST
TEST(Matching, StaticTest_1)
Definition: matching_test.cpp:55
Index.h
EXPECT_EQ
#define EXPECT_EQ(a, b)
Definition: test.h:191
souffle::ram::analysis::MinIndexSelection::SearchSet
std::set< SearchSignature, SearchComparator > SearchSet
Definition: Index.h:231
souffle::ram
Definition: AstToRamTranslator.h:54
souffle::ram::Nodes
MinIndexSelection::SearchSet Nodes
Definition: matching_test.cpp:42
souffle::ram::setBits
SearchSignature setBits(size_t arity, uint64_t mask)
Definition: matching_test.cpp:44
i
size_t i
Definition: json11.h:663
test.h
souffle::ram::analysis::AttributeConstraint::Equal
@ Equal