#include "llvm/Analysis/ConstraintSystem.h"
#include "gtest/gtest.h"
using namespace llvm;
namespace {
TEST(ConstraintSolverTest, TestSolutionChecks) {
{
ConstraintSystem CS;
CS.addVariableRow({10, 1, 1});
CS.addVariableRow({-5, -1, 0});
CS.addVariableRow({-6, 0, -1});
CS.addVariableRow({10, 1, 0});
CS.addVariableRow({10, 0, 1});
EXPECT_FALSE(CS.mayHaveSolution());
}
{
ConstraintSystem CS;
CS.addVariableRow({10, 1, 1});
CS.addVariableRow({-2, -1, 0});
CS.addVariableRow({-3, 0, -1});
CS.addVariableRow({10, 1, 0});
CS.addVariableRow({10, 0, 1});
EXPECT_TRUE(CS.mayHaveSolution());
}
{
ConstraintSystem CS;
CS.addVariableRow({10, 1, 1});
CS.addVariableRow({-10, -1, 0});
CS.addVariableRow({-10, 0, -1});
EXPECT_FALSE(CS.mayHaveSolution());
}
{
ConstraintSystem CS;
CS.addVariableRow({-20, -1, -1});
CS.addVariableRow({-10, -1, 0});
CS.addVariableRow({-10, 0, -1});
EXPECT_TRUE(CS.mayHaveSolution());
}
{
ConstraintSystem CS;
CS.addVariableRow({10, 2, 1, 3});
CS.addVariableRow({-10, -2, -1, 0});
CS.addVariableRow({-1, 0, 0, -1});
EXPECT_FALSE(CS.mayHaveSolution());
}
{
ConstraintSystem CS;
CS.addVariableRow({10, 2, 1, 3});
CS.addVariableRow({-10, -2, -1, 0});
EXPECT_TRUE(CS.mayHaveSolution());
}
}
TEST(ConstraintSolverTest, IsConditionImplied) {
{
ConstraintSystem CS;
CS.addVariableRow({5, 1, 0});
CS.addVariableRow({3, 0, 1});
EXPECT_FALSE(CS.isConditionImplied({6, 1, 1}));
EXPECT_FALSE(CS.isConditionImplied({7, 1, 1}));
EXPECT_TRUE(CS.isConditionImplied({8, 1, 1}));
EXPECT_FALSE(CS.isConditionImplied({12, 2, 1}));
EXPECT_TRUE(CS.isConditionImplied({13, 2, 1}));
EXPECT_FALSE(CS.isConditionImplied({12, 2, 1}));
EXPECT_TRUE(CS.isConditionImplied({13, 2, 1}));
EXPECT_FALSE(CS.isConditionImplied({0, 1, -1}));
EXPECT_FALSE(CS.isConditionImplied({0, -1, 1}));
}
{
ConstraintSystem CS;
CS.addVariableRow({0, 1, -1});
EXPECT_TRUE(CS.isConditionImplied({0, 1, -1}));
EXPECT_FALSE(CS.isConditionImplied({0, -1, 1}));
EXPECT_TRUE(CS.isConditionImplied({10, 1, -1}));
EXPECT_FALSE(CS.isConditionImplied({-10, 1, -1}));
}
{
ConstraintSystem CS;
CS.addVariableRow({0, 1, -1, 0});
CS.addVariableRow({0, 0, 1, -1});
EXPECT_FALSE(CS.isConditionImplied({0, 0, -1, 1}));
EXPECT_TRUE(CS.isConditionImplied({0, 1, 0, -1}));
}
}
TEST(ConstraintSolverTest, IsConditionImpliedOverflow) {
ConstraintSystem CS;
int64_t Limit = std::numeric_limits<int64_t>::max();
CS.addVariableRow({Limit - 1, Limit - 2, Limit - 3});
EXPECT_FALSE(CS.isConditionImplied({Limit - 1, Limit - 2, Limit - 3}));
}
}