1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
|
//===- unittests/Analysis/FlowSensitive/SimplifyConstraintsTest.cpp -------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "clang/Analysis/FlowSensitive/SimplifyConstraints.h"
#include "TestingSupport.h"
#include "clang/Analysis/FlowSensitive/Arena.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
namespace {
using namespace clang;
using namespace dataflow;
using ::testing::ElementsAre;
using ::testing::IsEmpty;
class SimplifyConstraintsTest : public ::testing::Test {
protected:
llvm::SetVector<const Formula *> parse(StringRef Lines) {
std::vector<const Formula *> formulas = test::parseFormulas(A, Lines);
llvm::SetVector<const Formula *> Constraints(formulas.begin(),
formulas.end());
return Constraints;
}
llvm::SetVector<const Formula *> simplify(StringRef Lines,
SimplifyConstraintsInfo &Info) {
llvm::SetVector<const Formula *> Constraints = parse(Lines);
simplifyConstraints(Constraints, A, &Info);
return Constraints;
}
Arena A;
};
void printConstraints(const llvm::SetVector<const Formula *> &Constraints,
raw_ostream &OS) {
if (Constraints.empty()) {
OS << "empty";
return;
}
for (const auto *Constraint : Constraints) {
Constraint->print(OS);
OS << "\n";
}
}
std::string
constraintsToString(const llvm::SetVector<const Formula *> &Constraints) {
std::string Str;
llvm::raw_string_ostream OS(Str);
printConstraints(Constraints, OS);
return Str;
}
MATCHER_P(EqualsConstraints, Constraints,
"constraints are: " + constraintsToString(Constraints)) {
if (arg == Constraints)
return true;
if (result_listener->stream()) {
llvm::raw_os_ostream OS(*result_listener->stream());
OS << "constraints are: ";
printConstraints(arg, OS);
}
return false;
}
TEST_F(SimplifyConstraintsTest, TriviallySatisfiable) {
SimplifyConstraintsInfo Info;
EXPECT_THAT(simplify(R"(
V0
)",
Info),
EqualsConstraints(parse("")));
EXPECT_THAT(Info.EquivalentAtoms, IsEmpty());
EXPECT_THAT(Info.TrueAtoms, ElementsAre(Atom(0)));
EXPECT_THAT(Info.FalseAtoms, IsEmpty());
}
TEST_F(SimplifyConstraintsTest, SimpleContradiction) {
SimplifyConstraintsInfo Info;
EXPECT_THAT(simplify(R"(
V0
!V0
)",
Info),
EqualsConstraints(parse("false")));
EXPECT_THAT(Info.EquivalentAtoms, IsEmpty());
EXPECT_THAT(Info.TrueAtoms, IsEmpty());
EXPECT_THAT(Info.FalseAtoms, IsEmpty());
}
TEST_F(SimplifyConstraintsTest, ContradictionThroughEquivalence) {
SimplifyConstraintsInfo Info;
EXPECT_THAT(simplify(R"(
(V0 = V1)
V0
!V1
)",
Info),
EqualsConstraints(parse("false")));
EXPECT_THAT(Info.EquivalentAtoms, IsEmpty());
EXPECT_THAT(Info.TrueAtoms, IsEmpty());
EXPECT_THAT(Info.FalseAtoms, IsEmpty());
}
TEST_F(SimplifyConstraintsTest, EquivalenceChain) {
SimplifyConstraintsInfo Info;
EXPECT_THAT(simplify(R"(
(V0 | V3)
(V1 = V2)
(V2 = V3)
)",
Info),
EqualsConstraints(parse("(V0 | V1)")));
EXPECT_THAT(Info.EquivalentAtoms,
ElementsAre(ElementsAre(Atom(1), Atom(2), Atom(3))));
EXPECT_THAT(Info.TrueAtoms, IsEmpty());
EXPECT_THAT(Info.FalseAtoms, IsEmpty());
}
TEST_F(SimplifyConstraintsTest, TrueAndFalseAtomsSimplifyOtherExpressions) {
SimplifyConstraintsInfo Info;
EXPECT_THAT(simplify(R"(
V0
!V1
(V0 & (V2 => V3))
(V1 | (V4 => V5))
)",
Info),
EqualsConstraints(parse(R"(
(V2 => V3)
(V4 => V5)
)")));
EXPECT_THAT(Info.EquivalentAtoms, IsEmpty());
EXPECT_THAT(Info.TrueAtoms, ElementsAre(Atom(0)));
EXPECT_THAT(Info.FalseAtoms, ElementsAre(Atom(1)));
}
TEST_F(SimplifyConstraintsTest, TrueAtomUnlocksEquivalenceChain) {
SimplifyConstraintsInfo Info;
EXPECT_THAT(simplify(R"(
V0
(V0 & (V1 = V2))
(V0 & (V2 = V3))
)",
Info),
EqualsConstraints(parse("")));
EXPECT_THAT(Info.EquivalentAtoms,
ElementsAre(ElementsAre(Atom(1), Atom(2), Atom(3))));
EXPECT_THAT(Info.TrueAtoms, ElementsAre(Atom(0)));
EXPECT_THAT(Info.FalseAtoms, IsEmpty());
}
TEST_F(SimplifyConstraintsTest, TopLevelAndSplitIntoMultipleConstraints) {
SimplifyConstraintsInfo Info;
EXPECT_THAT(simplify(R"(
((V0 => V1) & (V2 => V3))
)",
Info),
EqualsConstraints(parse(R"(
(V0 => V1)
(V2 => V3)
)")));
EXPECT_THAT(Info.EquivalentAtoms, IsEmpty());
EXPECT_THAT(Info.TrueAtoms, IsEmpty());
EXPECT_THAT(Info.FalseAtoms, IsEmpty());
}
} // namespace
|