souffle  2.0.2-371-g6315b36
ram_condition_equal_clone_test.cpp
Go to the documentation of this file.
1 /*
2  * Souffle - A Datalog Compiler
3  * Copyright (c) 2019, 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 ram_condition_equal_clone_test.cpp
12  *
13  * Tests equal and clone function of Condition classes.
14  *
15  ***********************************************************************/
16 
17 #include "tests/test.h"
18 
19 #include "RelationTag.h"
20 #include "ram/Conjunction.h"
21 #include "ram/Constraint.h"
22 #include "ram/EmptinessCheck.h"
23 #include "ram/ExistenceCheck.h"
24 #include "ram/Expression.h"
25 #include "ram/False.h"
26 #include "ram/Negation.h"
28 #include "ram/Relation.h"
29 #include "ram/SignedConstant.h"
30 #include "ram/True.h"
31 #include "ram/TupleElement.h"
33 #include <memory>
34 #include <string>
35 #include <utility>
36 #include <vector>
37 
38 namespace souffle::ram {
39 
40 namespace test {
41 
42 TEST(True, CloneAndEquals) {
43  True a;
44  True b;
45  EXPECT_EQ(a, b);
46  EXPECT_NE(&a, &b);
47 
48  True* c = a.clone();
49  EXPECT_EQ(a, *c);
50  EXPECT_NE(&a, c);
51  delete c;
52 }
53 
54 TEST(False, CloneAndEquals) {
55  False a;
56  False b;
57  EXPECT_EQ(a, b);
58  EXPECT_NE(&a, &b);
59 
60  False* c = a.clone();
61  EXPECT_EQ(a, *c);
62  EXPECT_NE(&a, c);
63  delete c;
64 }
65 
66 TEST(Conjunction, CloneAndEquals) {
67  // true /\ false
68  auto a = mk<Conjunction>(mk<True>(), mk<False>());
69  auto b = mk<Conjunction>(mk<True>(), mk<False>());
70  EXPECT_EQ(*a, *b);
71  EXPECT_NE(a, b);
72 
73  Own<Conjunction> c(a->clone());
74  EXPECT_EQ(*a, *c);
75  EXPECT_NE(a, c);
76 
77  // true /\ (false /\ true)
78  auto d = mk<Conjunction>(mk<True>(), mk<Conjunction>(mk<False>(), mk<True>()));
79  auto e = mk<Conjunction>(mk<True>(), mk<Conjunction>(mk<False>(), mk<True>()));
80  EXPECT_EQ(*d, *e);
81  EXPECT_NE(d, e);
82 
83  Own<Conjunction> f(d->clone());
84  EXPECT_EQ(*d, *f);
85  EXPECT_NE(d, f);
86 
87  // (true /\ false) /\ (true /\ (false /\ true))
88  auto a_conj_d = mk<Conjunction>(std::move(a), std::move(d));
89  auto b_conj_e = mk<Conjunction>(std::move(b), std::move(e));
90  EXPECT_EQ(*a_conj_d, *b_conj_e);
91  EXPECT_NE(a_conj_d, b_conj_e);
92 
93  auto c_conj_f = mk<Conjunction>(std::move(c), std::move(f));
94  EXPECT_EQ(*c_conj_f, *a_conj_d);
95  EXPECT_EQ(*c_conj_f, *b_conj_e);
96  EXPECT_NE(c_conj_f, a_conj_d);
97  EXPECT_NE(c_conj_f, b_conj_e);
98 
99  Own<Conjunction> a_conj_d_copy(a_conj_d->clone());
100  EXPECT_EQ(*a_conj_d, *a_conj_d_copy);
101  EXPECT_NE(a_conj_d, a_conj_d_copy);
102 }
103 
104 TEST(Negation, CloneAndEquals) {
105  auto a = mk<True>();
106  auto neg_a = mk<Negation>(std::move(a));
107  auto b = mk<True>();
108  auto neg_b = mk<Negation>(std::move(b));
109  EXPECT_EQ(*neg_a, *neg_b);
110  EXPECT_NE(neg_a, neg_b);
111 
112  auto c = mk<False>();
113  auto neg_neg_c = mk<Negation>(mk<Negation>(std::move(c)));
114  auto d = mk<False>();
115  auto neg_neg_d = mk<Negation>(mk<Negation>(std::move(d)));
116  EXPECT_EQ(*neg_neg_c, *neg_neg_d);
117  EXPECT_NE(neg_neg_c, neg_neg_d);
118 }
119 
120 TEST(Constraint, CloneAndEquals) {
121  // constraint t0.1 = t1.0
122  Own<Expression> a_lhs(new TupleElement(0, 1));
123  Own<Expression> a_rhs(new TupleElement(1, 0));
124  Own<Constraint> a(new Constraint(BinaryConstraintOp::EQ, std::move(a_lhs), std::move(a_rhs)));
125  Own<Expression> b_lhs(new TupleElement(0, 1));
126  Own<Expression> b_rhs(new TupleElement(1, 0));
127  Own<Constraint> b(new Constraint(BinaryConstraintOp::EQ, std::move(b_lhs), std::move(b_rhs)));
128  EXPECT_EQ(*a, *b);
129  EXPECT_NE(a, b);
130 
131  Own<Constraint> c(a->clone());
132  EXPECT_EQ(*a, *c);
133  EXPECT_EQ(*b, *c);
134  EXPECT_NE(a, c);
135  EXPECT_NE(b, c);
136 
137  // constraint t2.0 >= 5
138  Own<Expression> d_lhs(new TupleElement(2, 0));
139  Own<Expression> d_rhs(new SignedConstant(5));
140  Own<Constraint> d(new Constraint(BinaryConstraintOp::EQ, std::move(d_lhs), std::move(d_rhs)));
141  Own<Expression> e_lhs(new TupleElement(2, 0));
142  Own<Expression> e_rhs(new SignedConstant(5));
143  Own<Constraint> e(new Constraint(BinaryConstraintOp::EQ, std::move(e_lhs), std::move(e_rhs)));
144  EXPECT_EQ(*d, *e);
145  EXPECT_NE(d, e);
146 
147  Own<Constraint> f(d->clone());
148  EXPECT_EQ(*d, *f);
149  EXPECT_EQ(*e, *f);
150  EXPECT_NE(d, f);
151  EXPECT_NE(e, f);
152 }
153 
154 TEST(ExistenceCheck, CloneAndEquals) {
155  // N(1) in relation N(x:number)
156  Relation N("N", 1, 1, {"x"}, {"i"}, RelationRepresentation::DEFAULT);
157  VecOwn<Expression> tuple_a;
158  tuple_a.emplace_back(new SignedConstant(1));
159  ExistenceCheck a("N", std::move(tuple_a));
160  VecOwn<Expression> tuple_b;
161  tuple_b.emplace_back(new SignedConstant(1));
162  ExistenceCheck b("N", std::move(tuple_b));
163  EXPECT_EQ(a, b);
164  EXPECT_NE(&a, &b);
165 
167  EXPECT_EQ(a, *c);
168  EXPECT_EQ(b, *c);
169  EXPECT_NE(&a, c);
170  EXPECT_NE(&b, c);
171 
172  delete c;
173 
174  // edge(1,2) in relation edge(x:number,y:number)
175  Relation edge("edge", 2, 1, {"x", "y"}, {"i", "i"}, RelationRepresentation::BRIE);
176  VecOwn<Expression> tuple_d;
177  tuple_d.emplace_back(new SignedConstant(1));
178  tuple_d.emplace_back(new SignedConstant(2));
179  ExistenceCheck d("edge", std::move(tuple_d));
180  VecOwn<Expression> tuple_e;
181  tuple_e.emplace_back(new SignedConstant(1));
182  tuple_e.emplace_back(new SignedConstant(2));
183  ExistenceCheck e("edge", std::move(tuple_e));
184  EXPECT_EQ(d, e);
185  EXPECT_NE(&d, &e);
186 
187  ExistenceCheck* f = d.clone();
188  EXPECT_EQ(d, *f);
189  EXPECT_EQ(e, *f);
190  EXPECT_NE(&d, f);
191  EXPECT_NE(&e, f);
192 
193  delete f;
194 }
195 
196 TEST(RamProvenanceExistCheck, CloneAndEquals) {
197  Relation N("N", 1, 1, {"x"}, {"i"}, RelationRepresentation::DEFAULT);
198  VecOwn<Expression> tuple_a;
199  tuple_a.emplace_back(new SignedConstant(1));
200  ExistenceCheck a("N", std::move(tuple_a));
201  VecOwn<Expression> tuple_b;
202  tuple_b.emplace_back(new SignedConstant(1));
203  ExistenceCheck b("N", std::move(tuple_b));
204  EXPECT_EQ(a, b);
205  EXPECT_NE(&a, &b);
206 
207  ExistenceCheck* c = a.clone();
208  EXPECT_EQ(a, *c);
209  EXPECT_EQ(b, *c);
210  EXPECT_NE(&a, c);
211  EXPECT_NE(&b, c);
212 
213  delete c;
214 
215  // address(state:symbol, postCode:number, street:symbol)
216  Relation address("address", 3, 1, {"state", "postCode", "street"}, {"s", "i", "s"},
218  VecOwn<Expression> tuple_d;
219  tuple_d.emplace_back(new SignedConstant(0));
220  tuple_d.emplace_back(new SignedConstant(2000));
221  tuple_d.emplace_back(new SignedConstant(0));
222  ProvenanceExistenceCheck d("address", std::move(tuple_d));
223  VecOwn<Expression> tuple_e;
224  tuple_e.emplace_back(new SignedConstant(0));
225  tuple_e.emplace_back(new SignedConstant(2000));
226  tuple_e.emplace_back(new SignedConstant(0));
227  ProvenanceExistenceCheck e("address", std::move(tuple_e));
228  EXPECT_EQ(d, e);
229  EXPECT_NE(&d, &e);
230 
231  ProvenanceExistenceCheck* f = d.clone();
232  EXPECT_EQ(d, *f);
233  EXPECT_EQ(e, *f);
234  EXPECT_NE(&d, f);
235  EXPECT_NE(&e, f);
236 
237  delete f;
238 }
239 
240 TEST(EmptinessCheck, CloneAndEquals) {
241  // Check A(x:number)
242  Relation A("A", 1, 1, {"x"}, {"i"}, RelationRepresentation::DEFAULT);
243  EmptinessCheck a("A");
244  EmptinessCheck b("A");
245  EXPECT_EQ(a, b);
246  EXPECT_NE(&a, &b);
247  EmptinessCheck* c = a.clone();
248  EXPECT_EQ(a, *c);
249  EXPECT_EQ(b, *c);
250  EXPECT_NE(&a, c);
251  EXPECT_NE(&b, c);
252  delete c;
253 }
254 
255 } // end namespace test
256 } // namespace souffle::ram
BinaryConstraintOps.h
souffle::RelationRepresentation::BRIE
@ BRIE
Constraint.h
Negation.h
souffle::ram::ExistenceCheck::clone
ExistenceCheck * clone() const override
Create a clone (i.e.
Definition: ExistenceCheck.h:53
EXPECT_EQ
#define EXPECT_EQ(a, b)
Definition: test.h:191
e
l j a showGridBackground &&c b raw series this eventEmitter e
Definition: htmlJsChartistMin.h:15
False.h
souffle::Own
std::unique_ptr< A > Own
Definition: ContainerUtil.h:42
souffle::BinaryConstraintOp::EQ
@ EQ
souffle::ram::SignedConstant
Represents a signed constant.
Definition: SignedConstant.h:40
EmptinessCheck.h
souffle::ram::ExistenceCheck
Existence check for a tuple(-pattern) in a relation.
Definition: ExistenceCheck.h:49
True.h
souffle::ram::TupleElement
Access element from the current tuple in a tuple environment.
Definition: TupleElement.h:42
souffle::ram::False
Definition: False.h:38
souffle::ram
Definition: AstToRamTranslator.h:54
souffle::ram::Conjunction
A conjunction of conditions.
Definition: Conjunction.h:54
ProvenanceExistenceCheck.h
RelationTag.h
souffle::ram::Relation
An abstract class for performing indexed operations.
Definition: Relation.h:40
Relation.h
ExistenceCheck.h
test.h
souffle::ram::True::clone
True * clone() const override
Create a clone (i.e.
Definition: True.h:40
EXPECT_NE
#define EXPECT_NE(a, b)
Definition: test.h:195
souffle::ram::test::TEST
TEST(True, CloneAndEquals)
Definition: ram_condition_equal_clone_test.cpp:54
souffle::ram::Constraint
Evaluates a binary constraint with respect to two Expressions.
Definition: Constraint.h:55
TupleElement.h
souffle::ram::Negation
Negates a given condition.
Definition: Negation.h:49
b
l j a showGridBackground &&c b raw series this eventEmitter b
Definition: htmlJsChartistMin.h:15
souffle::RelationRepresentation::DEFAULT
@ DEFAULT
SignedConstant.h
souffle::ram::False::clone
False * clone() const override
Create a clone (i.e.
Definition: False.h:40
d
else d
Definition: htmlJsChartistMin.h:15
Expression.h
souffle::ram::True
False value condition.
Definition: True.h:38
souffle::VecOwn
std::vector< Own< A > > VecOwn
Definition: ContainerUtil.h:45
Conjunction.h