#include "Parser.h"
#include "Utils.h"
#include "mlir/Analysis/Presburger/PresburgerRelation.h"
#include "mlir/IR/MLIRContext.h"
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <optional>
using namespace mlir;
using namespace presburger;
static void testUnionAtPoints(const PresburgerSet &s, const PresburgerSet &t,
ArrayRef<SmallVector<int64_t, 4>> points) {
PresburgerSet unionSet = s.unionSet(t);
for (const SmallVector<int64_t, 4> &point : points) {
bool inS = s.containsPoint(point);
bool inT = t.containsPoint(point);
bool inUnion = unionSet.containsPoint(point);
EXPECT_EQ(inUnion, inS || inT);
}
}
static void testIntersectAtPoints(const PresburgerSet &s,
const PresburgerSet &t,
ArrayRef<SmallVector<int64_t, 4>> points) {
PresburgerSet intersection = s.intersect(t);
for (const SmallVector<int64_t, 4> &point : points) {
bool inS = s.containsPoint(point);
bool inT = t.containsPoint(point);
bool inIntersection = intersection.containsPoint(point);
EXPECT_EQ(inIntersection, inS && inT);
}
}
static void testSubtractAtPoints(const PresburgerSet &s, const PresburgerSet &t,
ArrayRef<SmallVector<int64_t, 4>> points) {
PresburgerSet diff = s.subtract(t);
for (const SmallVector<int64_t, 4> &point : points) {
bool inS = s.containsPoint(point);
bool inT = t.containsPoint(point);
bool inDiff = diff.containsPoint(point);
if (inT)
EXPECT_FALSE(inDiff);
else
EXPECT_EQ(inDiff, inS);
}
}
static void testComplementAtPoints(const PresburgerSet &s,
ArrayRef<SmallVector<int64_t, 4>> points) {
PresburgerSet complement = s.complement();
complement.complement();
for (const SmallVector<int64_t, 4> &point : points) {
bool inS = s.containsPoint(point);
bool inComplement = complement.containsPoint(point);
if (inS)
EXPECT_FALSE(inComplement);
else
EXPECT_TRUE(inComplement);
}
}
static PresburgerSet makeSetFromPoly(unsigned numDims,
ArrayRef<IntegerPolyhedron> polys) {
PresburgerSet set =
PresburgerSet::getEmpty(PresburgerSpace::getSetSpace(numDims));
for (const IntegerPolyhedron &poly : polys)
set.unionInPlace(poly);
return set;
}
TEST(SetTest, containsPoint) {
PresburgerSet setA = parsePresburgerSet(
{"(x) : (x - 2 >= 0, -x + 8 >= 0)", "(x) : (x - 10 >= 0, -x + 20 >= 0)"});
for (unsigned x = 0; x <= 21; ++x) {
if ((2 <= x && x <= 8) || (10 <= x && x <= 20))
EXPECT_TRUE(setA.containsPoint({x}));
else
EXPECT_FALSE(setA.containsPoint({x}));
}
PresburgerSet setB = parsePresburgerSet(
{"(x,y) : (x + y - 4 >= 0, -x - y + 32 >= 0, "
"x - y - 2 >= 0, -x + y + 16 >= 0)",
"(x,y) : (x - 2 >= 0, y - 2 >= 0, -x + 10 >= 0, -y + 10 >= 0)"});
for (unsigned x = 1; x <= 25; ++x) {
for (unsigned y = -6; y <= 16; ++y) {
if (4 <= x + y && x + y <= 32 && 2 <= x - y && x - y <= 16)
EXPECT_TRUE(setB.containsPoint({x, y}));
else if (2 <= x && x <= 10 && 2 <= y && y <= 10)
EXPECT_TRUE(setB.containsPoint({x, y}));
else
EXPECT_FALSE(setB.containsPoint({x, y}));
}
}
EXPECT_TRUE(
PresburgerSet(parseIntegerPolyhedron("(x) : (x - 2*(x floordiv 2) == 0)"))
.containsPoint({0}));
}
TEST(SetTest, Union) {
PresburgerSet set = parsePresburgerSet(
{"(x) : (x - 2 >= 0, -x + 8 >= 0)", "(x) : (x - 10 >= 0, -x + 20 >= 0)"});
testUnionAtPoints(PresburgerSet::getUniverse(PresburgerSpace::getSetSpace(1)),
set, {{1}, {2}, {8}, {9}, {10}, {20}, {21}});
testUnionAtPoints(PresburgerSet::getEmpty(PresburgerSpace::getSetSpace(1)),
set, {{1}, {2}, {8}, {9}, {10}, {20}, {21}});
testUnionAtPoints(PresburgerSet::getEmpty(PresburgerSpace::getSetSpace(1)),
PresburgerSet::getUniverse(PresburgerSpace::getSetSpace(1)),
{{1}, {2}, {0}, {-1}});
testUnionAtPoints(PresburgerSet::getUniverse(PresburgerSpace::getSetSpace(1)),
PresburgerSet::getEmpty(PresburgerSpace::getSetSpace(1)),
{{1}, {2}, {0}, {-1}});
testUnionAtPoints(PresburgerSet::getEmpty(PresburgerSpace::getSetSpace((1))),
PresburgerSet::getEmpty(PresburgerSpace::getSetSpace((1))),
{{1}, {2}, {0}, {-1}});
}
TEST(SetTest, Intersect) {
PresburgerSet set = parsePresburgerSet(
{"(x) : (x - 2 >= 0, -x + 8 >= 0)", "(x) : (x - 10 >= 0, -x + 20 >= 0)"});
testIntersectAtPoints(
PresburgerSet::getUniverse(PresburgerSpace::getSetSpace((1))), set,
{{1}, {2}, {8}, {9}, {10}, {20}, {21}});
testIntersectAtPoints(
PresburgerSet::getEmpty(PresburgerSpace::getSetSpace((1))), set,
{{1}, {2}, {8}, {9}, {10}, {20}, {21}});
testIntersectAtPoints(
PresburgerSet::getEmpty(PresburgerSpace::getSetSpace((1))),
PresburgerSet::getUniverse(PresburgerSpace::getSetSpace((1))),
{{1}, {2}, {0}, {-1}});
testIntersectAtPoints(
PresburgerSet::getUniverse(PresburgerSpace::getSetSpace((1))),
PresburgerSet::getEmpty(PresburgerSpace::getSetSpace((1))),
{{1}, {2}, {0}, {-1}});
testIntersectAtPoints(
PresburgerSet::getUniverse(PresburgerSpace::getSetSpace((1))),
PresburgerSet::getUniverse(PresburgerSpace::getSetSpace((1))),
{{1}, {2}, {0}, {-1}});
}
TEST(SetTest, Subtract) {
testSubtractAtPoints(
parsePresburgerSet({"(x) : (x - 2 >= 0, -x + 8 >= 0)"}),
parsePresburgerSet({"(x) : (x - 10 >= 0, -x + 20 >= 0)"}),
{{1}, {2}, {8}, {9}, {10}, {20}, {21}});
testSubtractAtPoints(
parsePresburgerSet({"(x) : ()"}),
parsePresburgerSet({"(x) : (x - 2 >= 0, -x + 8 >= 0)",
"(x) : (x - 10 >= 0, -x + 20 >= 0)"}),
{{1}, {2}, {8}, {9}, {10}, {20}, {21}});
testSubtractAtPoints(
parsePresburgerSet({"(x) : (-x >= 0)", "(x) : (x - 3 >= 0, -x + 4 >= 0)",
"(x) : (x - 6 >= 0, -x + 7 >= 0)"}),
parsePresburgerSet({"(x) : (x - 2 >= 0, -x + 3 >= 0)",
"(x) : (x - 5 >= 0, -x + 6 >= 0)"}),
{{0}, {1}, {2}, {3}, {4}, {5}, {6}, {7}, {8}});
testSubtractAtPoints(parsePresburgerSet({"(x, y) : (x - y >= 0)"}),
parsePresburgerSet({"(x, y) : (x + y >= 0)"}),
{{0, 1}, {1, 1}, {1, 0}, {1, -1}, {0, -1}});
testSubtractAtPoints(
parsePresburgerSet({
"(x, y) : (x - 2 >= 0, y - 2 >= 0, -x + 10 >= 0, -y + 10 >= 0)",
}),
parsePresburgerSet({
"(x, y) : (x - 5 >= 0, y + 10 >= 0, -x + 7 >= 0, -y + 100 >= 0)",
}),
{{1, 2}, {2, 2}, {4, 2}, {5, 2}, {7, 2}, {8, 2}, {11, 2},
{1, 1}, {2, 1}, {4, 1}, {5, 1}, {7, 1}, {8, 1}, {11, 1},
{1, 10}, {2, 10}, {4, 10}, {5, 10}, {7, 10}, {8, 10}, {11, 10},
{1, 11}, {2, 11}, {4, 11}, {5, 11}, {7, 11}, {8, 11}, {11, 11}});
testSubtractAtPoints(
parsePresburgerSet(
{"(x, y) : (x - 2 >= 0, y -2 >= 0, -x + 10 >= 0, -y + 10 >= 0)"}),
parsePresburgerSet({
"(x, y) : (x - 5 >= 0, y - 4 >= 0, -x + 7 >= 0, -y + 8 >= 0)",
}),
{{1, 1},
{2, 2},
{10, 10},
{11, 11},
{5, 4},
{7, 4},
{5, 8},
{7, 8},
{4, 4},
{8, 4},
{4, 8},
{8, 8}});
testSubtractAtPoints(
parsePresburgerSet({"(x, y) : (x >= 0, x + y == 0)"}),
parsePresburgerSet({"(x, y) : (-y + 1 >= 0, x + y == 0)"}),
{{0, 0},
{1, -1},
{2, -2},
{-1, 1},
{-2, 2},
{1, 1},
{-1, -1},
{-1, 1},
{1, -1}});
testSubtractAtPoints(parsePresburgerSet({"(x) : (x >= 0, -x + 2 >= 0)"}),
parsePresburgerSet({"(x) : (x - 1 == 0)"}),
{{-1}, {0}, {1}, {2}, {3}});
testSubtractAtPoints(
parsePresburgerSet({
"(x, y) : (x + y - 4 >= 0, -x - y + 32 >= 0, x - y - 2 >= 0, "
"-x + y + 16 >= 0)",
}),
parsePresburgerSet(
{"(x, y) : (x - 2 >= 0, y - 2 >= 0, -x + 10 >= 0, "
"-y + 10 >= 0, x + y - 2 >= 0, -x - y + 30 >= 0, x - y >= 0, "
"-x + y + 10 >= 0)"}),
{{1, 2}, {2, 2}, {3, 2}, {4, 2}, {1, 1}, {2, 1}, {3, 1},
{4, 1}, {2, 0}, {3, 0}, {4, 0}, {5, 0}, {10, 2}, {11, 2},
{10, 1}, {10, 10}, {10, 11}, {10, 9}, {11, 10}, {10, -6}, {11, -6},
{24, 8}, {24, 7}, {17, 15}, {16, 15}});
testSubtractAtPoints(
parsePresburgerSet({"(x) : (-x - 5 >= 0)", "(x) : (x - 3 == 0)",
"(x) : (x - 4 == 0)", "(x) : (x - 5 == 0)"}),
parsePresburgerSet(
{"(x) : (-x - 2 >= 0, x - 10 >= 0, -x >= 0, -x + 10 >= 0, "
"x - 100 >= 0, x - 50 >= 0)",
"(x) : (x - 3 >= 0, -x + 4 >= 0, x + 1 >= 0, "
"x + 7 >= 0, -x + 10 >= 0)",
"(x) : (x - 6 >= 0, -x + 7 >= 0, x + 1 >= 0, x - 3 >= 0, "
"-x + 5 >= 0)"}),
{{-6},
{-5},
{-4},
{-9},
{-10},
{-11},
{0},
{1},
{2},
{3},
{4},
{5},
{6},
{7},
{8}});
}
TEST(SetTest, Complement) {
testComplementAtPoints(
PresburgerSet::getUniverse(PresburgerSpace::getSetSpace((1))),
{{-1}, {-2}, {-8}, {1}, {2}, {8}, {9}, {10}, {20}, {21}});
testComplementAtPoints(
PresburgerSet::getEmpty(PresburgerSpace::getSetSpace((1))),
{{-1}, {-2}, {-8}, {1}, {2}, {8}, {9}, {10}, {20}, {21}});
testComplementAtPoints(parsePresburgerSet({"(x,y) : (x - 2 >= 0, y - 2 >= 0, "
"-x + 10 >= 0, -y + 10 >= 0)"}),
{{1, 1},
{2, 1},
{1, 2},
{2, 2},
{2, 3},
{3, 2},
{10, 10},
{10, 11},
{11, 10},
{2, 10},
{2, 11},
{1, 10}});
}
TEST(SetTest, isEqual) {
PresburgerSet universe =
PresburgerSet::getUniverse(PresburgerSpace::getSetSpace((1)));
PresburgerSet emptySet =
PresburgerSet::getEmpty(PresburgerSpace::getSetSpace((1)));
PresburgerSet set = parsePresburgerSet(
{"(x) : (x - 2 >= 0, -x + 8 >= 0)", "(x) : (x - 10 >= 0, -x + 20 >= 0)"});
EXPECT_FALSE(universe.isEqual(emptySet));
EXPECT_FALSE(emptySet.isEqual(universe));
EXPECT_TRUE(emptySet.isEqual(emptySet));
EXPECT_TRUE(universe.isEqual(universe));
EXPECT_TRUE(universe.unionSet(emptySet).isEqual(universe));
EXPECT_TRUE(universe.unionSet(universe).isEqual(universe));
EXPECT_TRUE(emptySet.unionSet(emptySet).isEqual(emptySet));
EXPECT_FALSE(universe.unionSet(emptySet).isEqual(emptySet));
EXPECT_FALSE(universe.unionSet(universe).isEqual(emptySet));
EXPECT_FALSE(emptySet.unionSet(emptySet).isEqual(universe));
EXPECT_TRUE(set.subtract(set).isEqual(emptySet));
EXPECT_TRUE(set.isEqual(set));
EXPECT_TRUE(set.unionSet(set.complement()).isEqual(universe));
EXPECT_FALSE(set.unionSet(set.complement()).isEqual(set));
EXPECT_FALSE(set.isEqual(set.unionSet(set.complement())));
PresburgerSet square = parsePresburgerSet(
{"(x, y) : (x - 2 >= 0, y - 2 >= 0, -x + 9 >= 0, -y + 9 >= 0)"});
PresburgerSet rect = parsePresburgerSet(
{"(x, y) : (x - 2 >= 0, y - 2 >= 0, -x + 9 >= 0, -y + 8 >= 0)"});
EXPECT_FALSE(square.isEqual(rect));
PresburgerSet universeRect = square.unionSet(square.complement());
PresburgerSet universeSquare = rect.unionSet(rect.complement());
EXPECT_TRUE(universeRect.isEqual(universeSquare));
EXPECT_FALSE(universeRect.isEqual(rect));
EXPECT_FALSE(universeSquare.isEqual(square));
EXPECT_FALSE(rect.complement().isEqual(square.complement()));
}
void expectEqual(const PresburgerSet &s, const PresburgerSet &t) {
EXPECT_TRUE(s.isEqual(t));
}
void expectEqual(const IntegerPolyhedron &s, const IntegerPolyhedron &t) {
EXPECT_TRUE(s.isEqual(t));
}
void expectEmpty(const PresburgerSet &s) { EXPECT_TRUE(s.isIntegerEmpty()); }
TEST(SetTest, divisions) {
PresburgerSet evens{
parseIntegerPolyhedron("(x) : (x - 2 * (x floordiv 2) == 0)")};
PresburgerSet odds{
parseIntegerPolyhedron("(x) : (x - 2 * (x floordiv 2) - 1 == 0)")};
PresburgerSet multiples3{
parseIntegerPolyhedron("(x) : (x - 3 * (x floordiv 3) == 0)")};
PresburgerSet multiples6{
parseIntegerPolyhedron("(x) : (x - 6 * (x floordiv 6) == 0)")};
expectEmpty(PresburgerSet(evens).intersect(PresburgerSet(odds)));
expectEqual(evens.unionSet(odds),
PresburgerSet::getUniverse(PresburgerSpace::getSetSpace((1))));
expectEqual(evens.complement(), odds);
expectEqual(odds.complement(), evens);
expectEqual(multiples3.intersect(evens), multiples6);
PresburgerSet setA{parseIntegerPolyhedron("(x) : (-x >= 0)")};
PresburgerSet setB{parseIntegerPolyhedron("(x) : (x floordiv 2 - 4 >= 0)")};
EXPECT_TRUE(setA.subtract(setB).isEqual(setA));
}
void convertSuffixDimsToLocals(IntegerPolyhedron &poly, unsigned numLocals) {
poly.convertVarKind(VarKind::SetDim, poly.getNumDimVars() - numLocals,
poly.getNumDimVars(), VarKind::Local);
}
inline IntegerPolyhedron
parseIntegerPolyhedronAndMakeLocals(StringRef str, unsigned numLocals) {
IntegerPolyhedron poly = parseIntegerPolyhedron(str);
convertSuffixDimsToLocals(poly, numLocals);
return poly;
}
TEST(SetTest, divisionsDefByEq) {
PresburgerSet evens{parseIntegerPolyhedronAndMakeLocals(
"(x, y) : (x - 2 * y == 0)", 1)};
PresburgerSet odds{parseIntegerPolyhedronAndMakeLocals(
"(x, y) : (x - 2 * y - 1 == 0)", 1)};
PresburgerSet multiples3{parseIntegerPolyhedronAndMakeLocals(
"(x, y) : (x - 3 * y == 0)", 1)};
PresburgerSet multiples6{parseIntegerPolyhedronAndMakeLocals(
"(x, y) : (x - 6 * y == 0)", 1)};
expectEmpty(PresburgerSet(evens).intersect(PresburgerSet(odds)));
expectEqual(evens.unionSet(odds),
PresburgerSet::getUniverse(PresburgerSpace::getSetSpace((1))));
expectEqual(evens.complement(), odds);
expectEqual(odds.complement(), evens);
expectEqual(multiples3.intersect(evens), multiples6);
PresburgerSet evensDefByIneq{
parseIntegerPolyhedron("(x) : (x - 2 * (x floordiv 2) == 0)")};
expectEqual(evens, PresburgerSet(evensDefByIneq));
}
TEST(SetTest, divisionNonDivLocals) {
IntegerPolyhedron tetrahedron = parseIntegerPolyhedronAndMakeLocals(
"(x, y, z) : (y >= 0, z - y >= 0, 3000*x - 2998*y "
"- 1000 - z >= 0, -1500*x + 1499*y + 1000 >= 0)",
1);
IntegerPolyhedron triangle =
parseIntegerPolyhedron("(x,y) : (y >= 0, 3000 * x - 2999 * y - 1000 >= "
"0, -3000 * x + 2998 * y + 2000 >= 0)");
EXPECT_TRUE(triangle.containsPoint({1000, 1000}));
EXPECT_FALSE(triangle.containsPoint({1001, 1001}));
expectEqual(triangle, tetrahedron);
convertSuffixDimsToLocals(triangle, 1);
IntegerPolyhedron line = parseIntegerPolyhedron("(x) : (x - 1000 == 0)");
expectEqual(line, triangle);
PresburgerSet triangle2{
parseIntegerPolyhedronAndMakeLocals("(x,y) : (y >= 0, "
"x - 3*y >= 0, "
"2*y - x + 5 >= 0)",
1)};
PresburgerSet zeroToThirteen{
parseIntegerPolyhedron("(x) : (13 - x >= 0, x >= 0)")};
PresburgerSet fifteen{parseIntegerPolyhedron("(x) : (x - 15 == 0)")};
expectEqual(triangle2.subtract(zeroToThirteen), fifteen);
}
TEST(SetTest, subtractDuplicateDivsRegression) {
IntegerPolyhedron setA(PresburgerSpace::getSetSpace(1));
setA.addLocalFloorDiv({1, 0}, 2);
setA.addLocalFloorDiv({1, 0, 0}, 2);
EXPECT_TRUE(setA.isEqual(setA));
}
void expectCoalesce(size_t expectedNumPoly, const PresburgerSet &set) {
PresburgerSet newSet = set.coalesce();
EXPECT_TRUE(set.isEqual(newSet));
EXPECT_TRUE(expectedNumPoly == newSet.getNumDisjuncts());
}
TEST(SetTest, coalesceNoPoly) {
PresburgerSet set = makeSetFromPoly(0, {});
expectCoalesce(0, set);
}
TEST(SetTest, coalesceContainedOneDim) {
PresburgerSet set = parsePresburgerSet(
{"(x) : (x >= 0, -x + 4 >= 0)", "(x) : (x - 1 >= 0, -x + 2 >= 0)"});
expectCoalesce(1, set);
}
TEST(SetTest, coalesceFirstEmpty) {
PresburgerSet set = parsePresburgerSet(
{"(x) : ( x >= 0, -x - 1 >= 0)", "(x) : ( x - 1 >= 0, -x + 2 >= 0)"});
expectCoalesce(1, set);
}
TEST(SetTest, coalesceSecondEmpty) {
PresburgerSet set = parsePresburgerSet(
{"(x) : (x - 1 >= 0, -x + 2 >= 0)", "(x) : (x >= 0, -x - 1 >= 0)"});
expectCoalesce(1, set);
}
TEST(SetTest, coalesceBothEmpty) {
PresburgerSet set = parsePresburgerSet(
{"(x) : (x - 3 >= 0, -x - 1 >= 0)", "(x) : (x >= 0, -x - 1 >= 0)"});
expectCoalesce(0, set);
}
TEST(SetTest, coalesceFirstUniv) {
PresburgerSet set =
parsePresburgerSet({"(x) : ()", "(x) : ( x >= 0, -x + 1 >= 0)"});
expectCoalesce(1, set);
}
TEST(SetTest, coalesceSecondUniv) {
PresburgerSet set =
parsePresburgerSet({"(x) : ( x >= 0, -x + 1 >= 0)", "(x) : ()"});
expectCoalesce(1, set);
}
TEST(SetTest, coalesceBothUniv) {
PresburgerSet set = parsePresburgerSet({"(x) : ()", "(x) : ()"});
expectCoalesce(1, set);
}
TEST(SetTest, coalesceFirstUnivSecondEmpty) {
PresburgerSet set =
parsePresburgerSet({"(x) : ()", "(x) : ( x >= 0, -x - 1 >= 0)"});
expectCoalesce(1, set);
}
TEST(SetTest, coalesceFirstEmptySecondUniv) {
PresburgerSet set =
parsePresburgerSet({"(x) : ( x >= 0, -x - 1 >= 0)", "(x) : ()"});
expectCoalesce(1, set);
}
TEST(SetTest, coalesceCutOneDim) {
PresburgerSet set = parsePresburgerSet({
"(x) : ( x >= 0, -x + 3 >= 0)",
"(x) : ( x - 2 >= 0, -x + 4 >= 0)",
});
expectCoalesce(1, set);
}
TEST(SetTest, coalesceSeparateOneDim) {
PresburgerSet set = parsePresburgerSet(
{"(x) : ( x >= 0, -x + 2 >= 0)", "(x) : ( x - 3 >= 0, -x + 4 >= 0)"});
expectCoalesce(2, set);
}
TEST(SetTest, coalesceAdjEq) {
PresburgerSet set =
parsePresburgerSet({"(x) : ( x == 0)", "(x) : ( x - 1 == 0)"});
expectCoalesce(2, set);
}
TEST(SetTest, coalesceContainedTwoDim) {
PresburgerSet set = parsePresburgerSet({
"(x,y) : (x >= 0, -x + 3 >= 0, y >= 0, -y + 3 >= 0)",
"(x,y) : (x >= 0, -x + 3 >= 0, y - 2 >= 0, -y + 3 >= 0)",
});
expectCoalesce(1, set);
}
TEST(SetTest, coalesceCutTwoDim) {
PresburgerSet set = parsePresburgerSet({
"(x,y) : (x >= 0, -x + 3 >= 0, y >= 0, -y + 2 >= 0)",
"(x,y) : (x >= 0, -x + 3 >= 0, y - 1 >= 0, -y + 3 >= 0)",
});
expectCoalesce(1, set);
}
TEST(SetTest, coalesceEqStickingOut) {
PresburgerSet set = parsePresburgerSet({
"(x,y) : (x >= 0, -x + 2 >= 0, y >= 0, -y + 2 >= 0)",
"(x,y) : (y - 1 == 0, x >= 0, -x + 3 >= 0)",
});
expectCoalesce(2, set);
}
TEST(SetTest, coalesceSeparateTwoDim) {
PresburgerSet set = parsePresburgerSet({
"(x,y) : (x >= 0, -x + 3 >= 0, y >= 0, -y + 1 >= 0)",
"(x,y) : (x >= 0, -x + 3 >= 0, y - 2 >= 0, -y + 3 >= 0)",
});
expectCoalesce(2, set);
}
TEST(SetTest, coalesceContainedEq) {
PresburgerSet set = parsePresburgerSet({
"(x,y) : (x >= 0, -x + 3 >= 0, x - y == 0)",
"(x,y) : (x - 1 >= 0, -x + 2 >= 0, x - y == 0)",
});
expectCoalesce(1, set);
}
TEST(SetTest, coalesceCuttingEq) {
PresburgerSet set = parsePresburgerSet({
"(x,y) : (x + 1 >= 0, -x + 1 >= 0, x - y == 0)",
"(x,y) : (x >= 0, -x + 2 >= 0, x - y == 0)",
});
expectCoalesce(1, set);
}
TEST(SetTest, coalesceSeparateEq) {
PresburgerSet set = parsePresburgerSet({
"(x,y) : (x - 3 >= 0, -x + 4 >= 0, x - y == 0)",
"(x,y) : (x >= 0, -x + 1 >= 0, x - y == 0)",
});
expectCoalesce(2, set);
}
TEST(SetTest, coalesceContainedEqAsIneq) {
PresburgerSet set = parsePresburgerSet({
"(x,y) : (x >= 0, -x + 3 >= 0, x - y >= 0, -x + y >= 0)",
"(x,y) : (x - 1 >= 0, -x + 2 >= 0, x - y == 0)",
});
expectCoalesce(1, set);
}
TEST(SetTest, coalesceContainedEqComplex) {
PresburgerSet set = parsePresburgerSet({
"(x,y) : (x - 2 == 0, x - y == 0)",
"(x,y) : (x - 1 >= 0, -x + 2 >= 0, x - y == 0)",
});
expectCoalesce(1, set);
}
TEST(SetTest, coalesceThreeContained) {
PresburgerSet set = parsePresburgerSet({
"(x) : (x >= 0, -x + 1 >= 0)",
"(x) : (x >= 0, -x + 2 >= 0)",
"(x) : (x >= 0, -x + 3 >= 0)",
});
expectCoalesce(1, set);
}
TEST(SetTest, coalesceDoubleIncrement) {
PresburgerSet set = parsePresburgerSet({
"(x) : (x == 0)",
"(x) : (x - 2 == 0)",
"(x) : (x + 2 == 0)",
"(x) : (x - 2 >= 0, -x + 3 >= 0)",
});
expectCoalesce(3, set);
}
TEST(SetTest, coalesceLastCoalesced) {
PresburgerSet set = parsePresburgerSet({
"(x) : (x == 0)",
"(x) : (x - 1 >= 0, -x + 3 >= 0)",
"(x) : (x + 2 == 0)",
"(x) : (x - 2 >= 0, -x + 4 >= 0)",
});
expectCoalesce(3, set);
}
TEST(SetTest, coalesceDiv) {
PresburgerSet set = parsePresburgerSet({
"(x) : (x floordiv 2 == 0)",
"(x) : (x floordiv 2 - 1 == 0)",
});
expectCoalesce(2, set);
}
TEST(SetTest, coalesceDivOtherContained) {
PresburgerSet set = parsePresburgerSet({
"(x) : (x floordiv 2 == 0)",
"(x) : (x == 0)",
"(x) : (x >= 0, -x + 1 >= 0)",
});
expectCoalesce(2, set);
}
static void
expectComputedVolumeIsValidOverapprox(const PresburgerSet &set,
std::optional<int64_t> trueVolume,
std::optional<int64_t> resultBound) {
expectComputedVolumeIsValidOverapprox(set.computeVolume(), trueVolume,
resultBound);
}
TEST(SetTest, computeVolume) {
PresburgerSet diamond(parseIntegerPolyhedron(
"(x, y) : (x + y >= 0, -x - y + 10 >= 0, x - y >= 0, -x + y + "
"10 >= 0)"));
expectComputedVolumeIsValidOverapprox(diamond,
61ull,
121ull);
PresburgerSet shiftedDiamond(parseIntegerPolyhedron(
"(x, y) : (x + y + 5 >= 0, -x - y + 5 >= 0, x - y + 5 >= 0, -x + y + "
"5 >= 0)"));
expectComputedVolumeIsValidOverapprox(shiftedDiamond,
61ull,
121ull);
PresburgerSet biggerDiamond(parseIntegerPolyhedron(
"(x, y) : (x + y + 5 >= 0, -x - y + 15 >= 0, x - y + 5 >= 0, -x + y + "
"15 >= 0)"));
expectComputedVolumeIsValidOverapprox(biggerDiamond,
221ull,
441ull);
expectComputedVolumeIsValidOverapprox(diamond.unionSet(shiftedDiamond),
104ull,
242ull);
expectComputedVolumeIsValidOverapprox(
diamond.unionSet(shiftedDiamond).unionSet(biggerDiamond),
221ull,
683ull);
PresburgerSet unbounded(
parseIntegerPolyhedron("(x, y) : (2*x - y >= 0, y - 3*x >= 0)"));
expectComputedVolumeIsValidOverapprox(unbounded, {},
{});
expectComputedVolumeIsValidOverapprox(unbounded.unionSet(diamond),
{},
{});
}
void testComputeReprAtPoints(IntegerPolyhedron poly,
ArrayRef<SmallVector<int64_t, 4>> points,
unsigned numToProject) {
poly.convertVarKind(VarKind::SetDim, poly.getNumDimVars() - numToProject,
poly.getNumDimVars(), VarKind::Local);
PresburgerRelation repr = poly.computeReprWithOnlyDivLocals();
EXPECT_TRUE(repr.hasOnlyDivLocals());
EXPECT_TRUE(repr.getSpace().isCompatible(poly.getSpace()));
for (const SmallVector<int64_t, 4> &point : points) {
EXPECT_EQ(poly.containsPointNoLocal(point).has_value(),
repr.containsPoint(point));
}
}
void testComputeRepr(IntegerPolyhedron poly, const PresburgerSet &expected,
unsigned numToProject) {
poly.convertVarKind(VarKind::SetDim, poly.getNumDimVars() - numToProject,
poly.getNumDimVars(), VarKind::Local);
PresburgerRelation repr = poly.computeReprWithOnlyDivLocals();
EXPECT_TRUE(repr.hasOnlyDivLocals());
EXPECT_TRUE(repr.getSpace().isCompatible(poly.getSpace()));
EXPECT_TRUE(repr.isEqual(expected));
}
TEST(SetTest, computeReprWithOnlyDivLocals) {
testComputeReprAtPoints(parseIntegerPolyhedron("(x, y) : (x - 2*y == 0)"),
{{1, 0}, {2, 1}, {3, 0}, {4, 2}, {5, 3}},
0);
testComputeReprAtPoints(parseIntegerPolyhedron("(x, e) : (x - 2*e == 0)"),
{{1}, {2}, {3}, {4}, {5}}, 1);
testComputeReprAtPoints(parseIntegerPolyhedron("(x, y)[z, w] : ()"), {},
1);
testComputeReprAtPoints(
parseIntegerPolyhedron("(x, y)[z, w] : (z - (w floordiv 2) == 0)"), {},
1);
testComputeRepr(parseIntegerPolyhedron("(x, e, f) : (x - 15*e - 21*f == 0)"),
PresburgerSet(parseIntegerPolyhedron(
{"(x) : (x - 3*(x floordiv 3) == 0)"})),
2);
}
TEST(SetTest, subtractOutputSizeRegression) {
PresburgerSet set1 = parsePresburgerSet({"(i) : (i >= 0, 10 - i >= 0)"});
PresburgerSet set2 = parsePresburgerSet({"(i) : (i - 5 >= 0)"});
PresburgerSet set3 = parsePresburgerSet({"(i) : (i >= 0, 4 - i >= 0)"});
PresburgerSet result = set1.subtract(set2);
EXPECT_TRUE(result.isEqual(set3));
EXPECT_EQ(result.getNumDisjuncts(), 1u);
PresburgerSet subtractSelf = set1.subtract(set1);
EXPECT_TRUE(subtractSelf.isIntegerEmpty());
EXPECT_EQ(subtractSelf.getNumDisjuncts(), 0u);
}