souffle  2.0.2-371-g6315b36
ram_expression_equal_clone_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 ram_expression_equal_clone_test.cpp
12  *
13  * Tests equal and clone function of Expression classes.
14  *
15  ***********************************************************************/
16 
17 #include "tests/test.h"
18 
19 #include "FunctorOps.h"
20 #include "ram/AutoIncrement.h"
21 #include "ram/Expression.h"
22 #include "ram/IntrinsicOperator.h"
23 #include "ram/PackRecord.h"
24 #include "ram/SignedConstant.h"
25 #include "ram/SubroutineArgument.h"
26 #include "ram/TupleElement.h"
27 #include "ram/UndefValue.h"
29 #include "souffle/TypeAttribute.h"
30 #include <memory>
31 #include <string>
32 #include <utility>
33 #include <vector>
34 
35 namespace souffle::ram {
36 
37 namespace test {
38 
39 TEST(IntrinsicOperator, CloneAndEquals) {
40  // ADD(number(1), number(2))
41  VecOwn<Expression> a_args;
42  a_args.emplace_back(new SignedConstant(1));
43  a_args.emplace_back(new SignedConstant(2));
44  IntrinsicOperator a(FunctorOp::ADD, std::move(a_args));
45 
46  VecOwn<Expression> b_args;
47  b_args.emplace_back(new SignedConstant(1));
48  b_args.emplace_back(new SignedConstant(2));
49  IntrinsicOperator b(FunctorOp::ADD, std::move(b_args));
50  EXPECT_EQ(a, b);
51  EXPECT_NE(&a, &b);
52 
53  IntrinsicOperator* aClone = a.clone();
54  EXPECT_EQ(a, *aClone);
55  EXPECT_NE(&a, aClone);
56  delete aClone;
57 
58  // NEG(number(1))
59  VecOwn<Expression> d_args;
60  d_args.emplace_back(new SignedConstant(1));
61  IntrinsicOperator d(FunctorOp::NEG, std::move(d_args));
62 
63  VecOwn<Expression> e_args;
64  e_args.emplace_back(new SignedConstant(1));
65  IntrinsicOperator e(FunctorOp::NEG, std::move(e_args));
66  EXPECT_EQ(d, e);
67  EXPECT_NE(&d, &e);
68 
69  IntrinsicOperator* dClone = d.clone();
70  EXPECT_EQ(d, *dClone);
71  EXPECT_NE(&d, dClone);
72  delete dClone;
73 }
74 
75 TEST(UserDefinedOperator, CloneAndEquals) {
76  // define binary functor NE check if two Expressions are not equal
77  // NE(number(1), number(10))
78  VecOwn<Expression> a_args;
79  a_args.emplace_back(new SignedConstant(1));
80  a_args.emplace_back(new SignedConstant(10));
82  std::move(a_args));
83 
84  VecOwn<Expression> b_args;
85  b_args.emplace_back(new SignedConstant(1));
86  b_args.emplace_back(new SignedConstant(10));
88  std::move(b_args));
89  EXPECT_EQ(a, b);
90  EXPECT_NE(&a, &b);
91 
92  UserDefinedOperator* aClone = a.clone();
93  EXPECT_EQ(a, *aClone);
94  EXPECT_NE(&a, aClone);
95  delete aClone;
96 }
97 
98 TEST(TupleElement, CloneAndEquals) {
99  // t0.1
100  TupleElement a(0, 1);
101  TupleElement b(0, 1);
102  EXPECT_EQ(a, b);
103  EXPECT_NE(&a, &b);
104 
105  TupleElement* aClone = a.clone();
106  EXPECT_EQ(a, *aClone);
107  EXPECT_NE(&a, aClone);
108  delete aClone;
109 }
110 
111 TEST(SignedConstant, CloneAndEquals) {
112  // number(5)
113  SignedConstant a(5);
114  SignedConstant b(5);
115  EXPECT_EQ(a, b);
116  EXPECT_NE(&a, &b);
117 
118  SignedConstant* aClone = a.clone();
119  EXPECT_EQ(a, *aClone);
120  EXPECT_NE(&a, aClone);
121  delete aClone;
122 }
123 
124 TEST(AutoIncrement, CloneAndEquals) {
125  AutoIncrement a;
127  EXPECT_EQ(a, b);
128  EXPECT_NE(&a, &b);
129 
130  AutoIncrement* aClone = a.clone();
131  EXPECT_EQ(a, *aClone);
132  EXPECT_NE(&a, aClone);
133  delete aClone;
134 }
135 
136 TEST(UndefValue, CloneAndEquals) {
137  UndefValue a;
138  UndefValue b;
139  EXPECT_EQ(a, b);
140  EXPECT_NE(&a, &b);
141 
142  UndefValue* aClone = a.clone();
143  EXPECT_EQ(a, *aClone);
144  EXPECT_NE(&a, aClone);
145  delete aClone;
146 }
147 
148 TEST(PackRecord, CloneAndEquals) {
149  // {number{10), number(5), ⊥, ⊥}
150  VecOwn<Expression> a_args;
151  a_args.emplace_back(new SignedConstant(10));
152  a_args.emplace_back(new SignedConstant(5));
153  a_args.emplace_back(new UndefValue);
154  a_args.emplace_back(new UndefValue);
155  PackRecord a(std::move(a_args));
156 
157  VecOwn<Expression> b_args;
158  b_args.emplace_back(new SignedConstant(10));
159  b_args.emplace_back(new SignedConstant(5));
160  b_args.emplace_back(new UndefValue);
161  b_args.emplace_back(new UndefValue);
162  PackRecord b(std::move(b_args));
163 
164  EXPECT_EQ(a, b);
165  EXPECT_NE(&a, &b);
166 
167  PackRecord* aClone = a.clone();
168  EXPECT_EQ(a, *aClone);
169  EXPECT_NE(&a, aClone);
170  delete aClone;
171 
172  // {⊥, {argument(1), number(0)}, t1.3}
173  VecOwn<Expression> d_args;
174  d_args.emplace_back(new UndefValue);
175  VecOwn<Expression> d_record;
176  d_record.emplace_back(new SubroutineArgument(1));
177  d_record.emplace_back(new SignedConstant(5));
178  d_args.emplace_back(new PackRecord(std::move(d_record)));
179  d_args.emplace_back(new TupleElement(1, 3));
180  PackRecord d(std::move(d_args));
181 
182  VecOwn<Expression> e_args;
183  e_args.emplace_back(new UndefValue);
184  VecOwn<Expression> e_record;
185  e_record.emplace_back(new SubroutineArgument(1));
186  e_record.emplace_back(new SignedConstant(5));
187  e_args.emplace_back(new PackRecord(std::move(e_record)));
188  e_args.emplace_back(new TupleElement(1, 3));
189  PackRecord e(std::move(e_args));
190 
191  EXPECT_EQ(d, e);
192  EXPECT_NE(&d, &e);
193 
194  PackRecord* dClone = d.clone();
195  EXPECT_EQ(d, *dClone);
196  EXPECT_NE(&d, dClone);
197  delete dClone;
198 }
199 
200 TEST(RamSubrountineArgument, CloneAndEquals) {
201  SubroutineArgument a(2);
203  EXPECT_EQ(a, b);
204  EXPECT_NE(&a, &b);
205 
206  SubroutineArgument* aClone = a.clone();
207  EXPECT_EQ(a, *aClone);
208  EXPECT_NE(&a, aClone);
209  delete aClone;
210 }
211 } // end namespace test
212 } // namespace souffle::ram
souffle::ram::UserDefinedOperator::clone
UserDefinedOperator * clone() const override
Create a clone (i.e.
Definition: UserDefinedOperator.h:72
UserDefinedOperator.h
souffle::ram::AutoIncrement
Increment a counter and return its value.
Definition: AutoIncrement.h:36
souffle::ram::UserDefinedOperator
Operator that represents an extrinsic (user-defined) functor.
Definition: UserDefinedOperator.h:43
PackRecord.h
souffle::ram::UndefValue::clone
UndefValue * clone() const override
Create a clone (i.e.
Definition: UndefValue.h:38
souffle::ram::PackRecord
Packs a record's arguments into a reference.
Definition: PackRecord.h:42
souffle::ram::SignedConstant::clone
SignedConstant * clone() const override
Create clone.
Definition: SignedConstant.h:50
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
souffle::ram::SignedConstant
Represents a signed constant.
Definition: SignedConstant.h:40
SubroutineArgument.h
souffle::FunctorOp::NEG
@ NEG
souffle::ram::TupleElement
Access element from the current tuple in a tuple environment.
Definition: TupleElement.h:42
souffle::ram
Definition: AstToRamTranslator.h:54
souffle::TypeAttribute::Signed
@ Signed
souffle::ram::IntrinsicOperator::clone
IntrinsicOperator * clone() const override
Create a clone (i.e.
Definition: IntrinsicOperator.h:55
AutoIncrement.h
test.h
souffle::ram::PackRecord::clone
PackRecord * clone() const override
Create a clone (i.e.
Definition: PackRecord.h:63
EXPECT_NE
#define EXPECT_NE(a, b)
Definition: test.h:195
souffle::ram::IntrinsicOperator
Operator that represents an intrinsic (built-in) functor.
Definition: IntrinsicOperator.h:42
souffle::ram::test::TEST
TEST(True, CloneAndEquals)
Definition: ram_condition_equal_clone_test.cpp:54
TupleElement.h
b
l j a showGridBackground &&c b raw series this eventEmitter b
Definition: htmlJsChartistMin.h:15
souffle::ram::AutoIncrement::clone
AutoIncrement * clone() const override
Create a clone (i.e.
Definition: AutoIncrement.h:38
SignedConstant.h
d
else d
Definition: htmlJsChartistMin.h:15
souffle::ram::UndefValue
An undefined expression.
Definition: UndefValue.h:36
FunctorOps.h
Expression.h
UndefValue.h
souffle::ram::SubroutineArgument
Access argument of a subroutine.
Definition: SubroutineArgument.h:40
souffle::FunctorOp::ADD
@ ADD
Binary Functor Operators.
souffle::VecOwn
std::vector< Own< A > > VecOwn
Definition: ContainerUtil.h:45
IntrinsicOperator.h
TypeAttribute.h