souffle
2.0.2-371-g6315b36
ast
tests
ast_parser_utils_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 ast_parser_utils_test.cpp
12
*
13
* Tests souffle's parser utils.
14
*
15
***********************************************************************/
16
17
#include "
tests/test.h
"
18
19
#include "
ast/Atom.h
"
20
#include "
ast/Clause.h
"
21
#include "
ast/Node.h
"
22
#include "
parser/ParserUtils.h
"
23
#include "
souffle/utility/ContainerUtil.h
"
24
#include "
souffle/utility/StringUtil.h
"
25
#include <memory>
26
#include <string>
27
#include <utility>
28
#include <vector>
29
30
namespace
souffle::ast
{
31
32
namespace
test {
33
34
TEST
(RuleBody, Basic) {
35
RuleBody body;
36
37
// start with an A
38
auto
a =
RuleBody::atom
(mk<Atom>(
"A"
));
39
EXPECT_EQ
(
"A()"
,
toString
(a));
40
41
a.conjunct(
RuleBody::atom
(mk<Atom>(
"B"
)));
42
EXPECT_EQ
(
"A(),B()"
,
toString
(a));
43
44
a.disjunct(
RuleBody::atom
(mk<Atom>(
"C"
)));
45
EXPECT_EQ
(
"A(),B();C()"
,
toString
(a));
46
}
47
48
TEST
(
RuleBody
,
Negation
) {
49
RuleBody
body =
RuleBody::getTrue
();
50
51
RuleBody
AB =
RuleBody::getTrue
();
52
AB.
conjunct
(
RuleBody::atom
(mk<Atom>(
"A"
)));
53
AB.
conjunct
(
RuleBody::atom
(mk<Atom>(
"B"
)));
54
EXPECT_EQ
(
"A(),B()"
,
toString
(AB));
55
56
RuleBody
CD =
RuleBody::getTrue
();
57
CD.
conjunct
(
RuleBody::atom
(mk<Atom>(
"C"
)));
58
CD.
conjunct
(
RuleBody::atom
(mk<Atom>(
"D"
)));
59
EXPECT_EQ
(
"C(),D()"
,
toString
(CD));
60
61
RuleBody
EF =
RuleBody::getTrue
();
62
EF.
conjunct
(
RuleBody::atom
(mk<Atom>(
"E"
)));
63
EF.
conjunct
(
RuleBody::atom
(mk<Atom>(
"F"
)));
64
EXPECT_EQ
(
"E(),F()"
,
toString
(EF));
65
66
RuleBody
full =
RuleBody::getFalse
();
67
full.
disjunct
(std::move(AB));
68
full.
disjunct
(std::move(CD));
69
full.
disjunct
(std::move(EF));
70
EXPECT_EQ
(
"A(),B();C(),D();E(),F()"
,
toString
(full));
71
72
full = full.
negated
();
73
EXPECT_EQ
(
74
"!A(),!C(),!E();!A(),!C(),!F();!A(),!D(),!E();!A(),!D(),!F();!B(),!C(),!E();!B(),!C(),!F();!B(),!"
75
"D(),!E();!B(),!D(),!F()"
,
76
toString
(full));
77
78
full = full.
negated
();
79
EXPECT_EQ
(
"A(),B();C(),D();E(),F()"
,
toString
(full));
80
}
81
82
TEST
(
RuleBody
, ClauseBodyExtraction) {
83
RuleBody
body =
RuleBody::getTrue
();
84
85
RuleBody
AB =
RuleBody::getTrue
();
86
AB.
conjunct
(
RuleBody::atom
(mk<Atom>(
"A"
)));
87
AB.
conjunct
(
RuleBody::atom
(mk<Atom>(
"B"
)));
88
EXPECT_EQ
(
"A(),B()"
,
toString
(AB));
89
90
RuleBody
CD =
RuleBody::getTrue
();
91
CD.
conjunct
(
RuleBody::atom
(mk<Atom>(
"C"
)));
92
CD.
conjunct
(
RuleBody::atom
(mk<Atom>(
"D"
)));
93
EXPECT_EQ
(
"C(),D()"
,
toString
(CD));
94
95
RuleBody
EF =
RuleBody::getTrue
();
96
EF.
conjunct
(
RuleBody::atom
(mk<Atom>(
"E"
)));
97
EF.
conjunct
(
RuleBody::atom
(mk<Atom>(
"F"
)));
98
EXPECT_EQ
(
"E(),F()"
,
toString
(EF));
99
100
RuleBody
full =
RuleBody::getFalse
();
101
full.
disjunct
(std::move(AB));
102
full.
disjunct
(std::move(CD));
103
full.
disjunct
(std::move(EF));
104
EXPECT_EQ
(
"A(),B();C(),D();E(),F()"
,
toString
(full));
105
106
// extract the clause
107
auto
list = full.
toClauseBodies
();
108
EXPECT_EQ
(3, list.size());
109
110
EXPECT_EQ
(
" :- \n A(),\n B()."
,
toString
(*list[0]));
111
EXPECT_EQ
(
" :- \n C(),\n D()."
,
toString
(*list[1]));
112
EXPECT_EQ
(
" :- \n E(),\n F()."
,
toString
(*list[2]));
113
}
114
115
}
// end namespace test
116
}
// namespace souffle::ast
souffle::RuleBody::disjunct
void disjunct(RuleBody other)
Definition:
ParserUtils.cpp:89
souffle::RuleBody::toClauseBodies
VecOwn< ast::Clause > toClauseBodies() const
Definition:
ParserUtils.cpp:96
souffle::RuleBody::conjunct
void conjunct(RuleBody other)
Definition:
ParserUtils.cpp:58
souffle::ast::test::TEST
TEST(RuleBody, Basic)
Definition:
ast_parser_utils_test.cpp:46
souffle::RuleBody::negated
RuleBody negated() const
Definition:
ParserUtils.cpp:42
EXPECT_EQ
#define EXPECT_EQ(a, b)
Definition:
test.h:191
souffle::RuleBody::getFalse
static RuleBody getFalse()
Definition:
ParserUtils.cpp:135
ParserUtils.h
souffle::toString
const std::string & toString(const std::string &str)
A generic function converting strings into strings (trivial case).
Definition:
StringUtil.h:234
ContainerUtil.h
souffle::RuleBody::atom
static RuleBody atom(Own< ast::Atom > atom)
Definition:
ParserUtils.cpp:139
souffle::ast::Negation
Negation of an atom negated atom.
Definition:
Negation.h:50
StringUtil.h
Atom.h
test.h
Node.h
souffle::RuleBody::getTrue
static RuleBody getTrue()
Definition:
ParserUtils.cpp:129
Clause.h
souffle::RuleBody
Definition:
ParserUtils.h:36
souffle::ast
Definition:
Aggregator.h:35
Generated by
1.8.17