souffle  2.0.2-371-g6315b36
ast_print_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 ast_print_test.cpp
12  *
13  * Tests souffle's AST program.
14  *
15  ***********************************************************************/
16 
17 #include "tests/test.h"
18 
19 #include "AggregateOp.h"
20 #include "ast/Aggregator.h"
21 #include "ast/Argument.h"
22 #include "ast/Atom.h"
23 #include "ast/Clause.h"
24 #include "ast/Counter.h"
25 #include "ast/Literal.h"
26 #include "ast/NilConstant.h"
27 #include "ast/Node.h"
28 #include "ast/NumericConstant.h"
29 #include "ast/Program.h"
30 #include "ast/StringConstant.h"
31 #include "ast/TranslationUnit.h"
32 #include "ast/UnnamedVariable.h"
33 #include "ast/Variable.h"
34 #include "parser/ParserDriver.h"
35 #include "reports/DebugReport.h"
36 #include "reports/ErrorReport.h"
37 #include <algorithm>
38 #include <iosfwd>
39 #include <memory>
40 #include <string>
41 #include <utility>
42 #include <vector>
43 
44 namespace souffle::ast::test {
45 
46 inline Own<TranslationUnit> makeATU(std::string program = ".decl A,B,C(x:number)") {
47  ErrorReport e;
48  DebugReport d;
49  return ParserDriver::parseTranslationUnit(program, e, d);
50 }
51 
53  std::stringstream ss;
54  ss << tu->getProgram();
55  return makeATU(ss.str());
56 }
57 
58 inline Own<Clause> makeClauseA(Own<Argument> headArgument) {
59  auto headAtom = mk<Atom>("A");
60  headAtom->addArgument(std::move(headArgument));
61  auto clause = mk<Clause>();
62  clause->setHead(std::move(headAtom));
63  return clause;
64 }
65 
66 TEST(AstPrint, NilConstant) {
67  auto testArgument = mk<NilConstant>();
68 
69  auto tu1 = makeATU();
70  tu1->getProgram().addClause(makeClauseA(std::move(testArgument)));
71  auto tu2 = makePrintedATU(tu1);
72  EXPECT_EQ(tu1->getProgram(), tu2->getProgram());
73 }
74 
75 TEST(AstPrint, NumberConstant) {
76  auto testArgument = mk<NumericConstant>("2");
77 
78  EXPECT_EQ(testArgument, testArgument);
79 
80  auto tu1 = makeATU();
81  tu1->getProgram().addClause(makeClauseA(std::move(testArgument)));
82  auto tu2 = makePrintedATU(tu1);
83  EXPECT_EQ(tu1->getProgram(), tu2->getProgram());
84 }
85 
86 TEST(AstPrint, StringConstant) {
87  ErrorReport e;
88  DebugReport d;
89  auto testArgument = mk<StringConstant>("test string");
90 
91  auto tu1 = ParserDriver::parseTranslationUnit(".decl A,B,C(x:number)", e, d);
92  tu1->getProgram().addClause(makeClauseA(std::move(testArgument)));
93  auto tu2 = makePrintedATU(tu1);
94  EXPECT_EQ(tu1->getProgram(), tu2->getProgram());
95 }
96 
97 TEST(AstPrint, Variable) {
98  auto testArgument = mk<Variable>("testVar");
99 
100  auto tu1 = makeATU();
101  tu1->getProgram().addClause(makeClauseA(std::move(testArgument)));
102  auto tu2 = makePrintedATU(tu1);
103  EXPECT_EQ(tu1->getProgram(), tu2->getProgram());
104 }
105 
106 TEST(AstPrint, UnnamedVariable) {
107  auto testArgument = mk<UnnamedVariable>();
108 
109  auto tu1 = makeATU();
110  tu1->getProgram().addClause(makeClauseA(std::move(testArgument)));
111  auto tu2 = makePrintedATU(tu1);
112  EXPECT_EQ(tu1->getProgram(), tu2->getProgram());
113 }
114 
115 TEST(AstPrint, Counter) {
116  auto testArgument = mk<Counter>();
117 
118  auto tu1 = makeATU();
119  tu1->getProgram().addClause(makeClauseA(std::move(testArgument)));
120  auto tu2 = makePrintedATU(tu1);
121  EXPECT_EQ(tu1->getProgram(), tu2->getProgram());
122 }
123 
124 TEST(AstPrint, AggregatorMin) {
125  auto atom = mk<Atom>("B");
126  atom->addArgument(mk<Variable>("x"));
127  auto min = mk<Aggregator>(AggregateOp::MIN, mk<Variable>("x"));
128 
129  VecOwn<Literal> body;
130  body.push_back(mk<Atom>("B"));
131 
132  min->setBody(std::move(body));
133 
134  auto tu1 = makeATU();
135  auto& prog1 = tu1->getProgram();
136  prog1.addClause(makeClauseA(std::move(min)));
137  auto tu2 = makePrintedATU(tu1);
138  EXPECT_EQ(tu1->getProgram(), tu2->getProgram());
139 }
140 
141 TEST(AstPrint, AggregatorMax) {
142  auto atom = mk<Atom>("B");
143  atom->addArgument(mk<Variable>("x"));
144  auto max = mk<Aggregator>(AggregateOp::MAX, mk<Variable>("x"));
145 
146  VecOwn<Literal> body;
147  body.push_back(std::move(atom));
148  max->setBody(std::move(body));
149 
150  auto tu1 = makeATU();
151  auto& prog1 = tu1->getProgram();
152  prog1.addClause(makeClauseA(std::move(max)));
153  auto tu2 = makePrintedATU(tu1);
154  EXPECT_EQ(tu1->getProgram(), tu2->getProgram());
155 }
156 
157 TEST(AstPrint, AggregatorCount) {
158  auto atom = mk<Atom>("B");
159  atom->addArgument(mk<Variable>("x"));
160  auto count = mk<Aggregator>(AggregateOp::COUNT);
161 
162  VecOwn<Literal> body;
163  body.push_back(std::move(atom));
164  count->setBody(std::move(body));
165 
166  auto tu1 = makeATU();
167  auto& prog1 = tu1->getProgram();
168  prog1.addClause(makeClauseA(std::move(count)));
169  auto tu2 = makePrintedATU(tu1);
170  EXPECT_EQ(tu1->getProgram(), tu2->getProgram());
171 }
172 
173 TEST(AstPrint, AggregatorSum) {
174  auto atom = mk<Atom>("B");
175  atom->addArgument(mk<Variable>("x"));
176  auto sum = mk<Aggregator>(AggregateOp::SUM, mk<Variable>("x"));
177 
178  VecOwn<Literal> body;
179  body.push_back(std::move(atom));
180  sum->setBody(std::move(body));
181 
182  auto tu1 = makeATU();
183  auto& prog1 = tu1->getProgram();
184  prog1.addClause(makeClauseA(std::move(sum)));
185  auto tu2 = makePrintedATU(tu1);
186  EXPECT_EQ(tu1->getProgram(), tu2->getProgram());
187 }
188 
189 } // namespace souffle::ast::test
souffle::ast::test
Definition: ast_parser_utils_test.cpp:38
souffle::AggregateOp::MIN
@ MIN
souffle::ParserDriver::parseTranslationUnit
static Own< ast::TranslationUnit > parseTranslationUnit(const std::string &filename, FILE *in, ErrorReport &errorReport, DebugReport &debugReport)
Definition: ParserDriver.cpp:84
TranslationUnit.h
NilConstant.h
UnnamedVariable.h
DebugReport.h
AggregateOp.h
souffle::ast::test::TEST
TEST(RuleBody, Basic)
Definition: ast_parser_utils_test.cpp:46
souffle::ast::test::makeATU
Own< TranslationUnit > makeATU(std::string program=".decl A,B,C(x:number)")
Definition: ast_print_test.cpp:52
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::Own
std::unique_ptr< A > Own
Definition: ContainerUtil.h:42
souffle::ast::NilConstant
Defines the nil constant.
Definition: NilConstant.h:36
souffle::ast::analysis::Variable
A variable to be utilized within constraints to be handled by the constraint solver.
Definition: ConstraintSystem.h:41
souffle::ast::Counter
counter functor (incrementing a value after each invocation)
Definition: Counter.h:34
StringConstant.h
Argument.h
souffle::AggregateOp::MAX
@ MAX
souffle::DebugReport
Class representing a HTML report, consisting of a list of sections.
Definition: DebugReport.h:87
Atom.h
souffle::ast::test::makeClauseA
Own< Clause > makeClauseA(Own< Argument > headArgument)
Definition: ast_print_test.cpp:64
Literal.h
souffle::test::count
int count(const C &c)
Definition: table_test.cpp:40
test.h
Node.h
souffle::AggregateOp::COUNT
@ COUNT
souffle::AggregateOp::SUM
@ SUM
Aggregator.h
souffle::ast::test::makePrintedATU
Own< TranslationUnit > makePrintedATU(Own< TranslationUnit > &tu)
Definition: ast_print_test.cpp:58
Program.h
Clause.h
d
else d
Definition: htmlJsChartistMin.h:15
souffle::ast::UnnamedVariable
Unnamed variable class.
Definition: UnnamedVariable.h:34
Counter.h
ParserDriver.h
souffle::ErrorReport
Definition: ErrorReport.h:152
souffle::ast::StringConstant
String constant class.
Definition: StringConstant.h:37
souffle::VecOwn
std::vector< Own< A > > VecOwn
Definition: ContainerUtil.h:45
ErrorReport.h
NumericConstant.h
Variable.h
souffle::profile::ss
class souffle::profile::Tui ss
Definition: Tui.h:336