1 //===- IntegerRelationTest.cpp - Tests for IntegerRelation class ----------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "mlir/Analysis/Presburger/IntegerRelation.h"
10 #include "./Utils.h"
11 
12 #include <gmock/gmock.h>
13 #include <gtest/gtest.h>
14 
15 using namespace mlir;
16 using namespace presburger;
17 
18 static IntegerRelation parseRelationFromSet(StringRef set, unsigned numDomain) {
19   IntegerRelation rel = parsePoly(set);
20 
21   rel.convertIdKind(IdKind::SetDim, 0, numDomain, IdKind::Domain);
22 
23   return rel;
24 }
25 
26 TEST(IntegerRelationTest, getDomainAndRangeSet) {
27   IntegerRelation rel = parseRelationFromSet(
28       "(x, xr)[N] : (xr - x - 10 == 0, xr >= 0, N - xr >= 0)", 1);
29 
30   IntegerPolyhedron domainSet = rel.getDomainSet();
31 
32   IntegerPolyhedron expectedDomainSet =
33       parsePoly("(x)[N] : (x + 10 >= 0, N - x - 10 >= 0)");
34 
35   EXPECT_TRUE(domainSet.isEqual(expectedDomainSet));
36 
37   IntegerPolyhedron rangeSet = rel.getRangeSet();
38 
39   IntegerPolyhedron expectedRangeSet =
40       parsePoly("(x)[N] : (x >= 0, N - x >= 0)");
41 
42   EXPECT_TRUE(rangeSet.isEqual(expectedRangeSet));
43 }
44 
45 TEST(IntegerRelationTest, inverse) {
46   IntegerRelation rel =
47       parseRelationFromSet("(x, y, z)[N, M] : (z - x - y == 0, x >= 0, N - x "
48                            ">= 0, y >= 0, M - y >= 0)",
49                            2);
50 
51   IntegerRelation inverseRel =
52       parseRelationFromSet("(z, x, y)[N, M]  : (x >= 0, N - x >= 0, y >= 0, M "
53                            "- y >= 0, x + y - z == 0)",
54                            1);
55 
56   rel.inverse();
57 
58   EXPECT_TRUE(rel.isEqual(inverseRel));
59 }
60