xref: /llvm-project/mlir/unittests/Analysis/Presburger/UtilsTest.cpp (revision e63cc56d9d049edbc30528aba7e9dd918ab8f82d)
1 //===- Utils.cpp - Tests for Utils file ----------===//
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/Utils.h"
10 
11 #include <gmock/gmock.h>
12 #include <gtest/gtest.h>
13 
14 using namespace mlir;
15 using namespace presburger;
16 
17 static DivisionRepr parseDivisionRepr(unsigned numVars, unsigned numDivs,
18                                       ArrayRef<ArrayRef<MPInt>> dividends,
19                                       ArrayRef<MPInt> divisors) {
20   DivisionRepr repr(numVars, numDivs);
21   for (unsigned i = 0, rows = dividends.size(); i < rows; ++i)
22     repr.setDiv(i, dividends[i], divisors[i]);
23   return repr;
24 }
25 
26 static void checkEqual(DivisionRepr &a, DivisionRepr &b) {
27   EXPECT_EQ(a.getNumVars(), b.getNumVars());
28   EXPECT_EQ(a.getNumDivs(), b.getNumDivs());
29   for (unsigned i = 0, rows = a.getNumDivs(); i < rows; ++i) {
30     EXPECT_EQ(a.hasRepr(i), b.hasRepr(i));
31     if (!a.hasRepr(i))
32       continue;
33     EXPECT_TRUE(a.getDenom(i) == b.getDenom(i));
34     EXPECT_TRUE(a.getDividend(i).equals(b.getDividend(i)));
35   }
36 }
37 
38 TEST(UtilsTest, ParseAndCompareDivisionReprTest) {
39   auto merge = [this](unsigned i, unsigned j) -> bool { return true; };
40   DivisionRepr a = parseDivisionRepr(1, 1, {{MPInt(1), MPInt(2)}}, {MPInt(2)}),
41                b = parseDivisionRepr(1, 1, {{MPInt(1), MPInt(2)}}, {MPInt(2)}),
42                c = parseDivisionRepr(2, 2,
43                                      {{MPInt(0), MPInt(1), MPInt(2)},
44                                       {MPInt(0), MPInt(1), MPInt(2)}},
45                                      {MPInt(2), MPInt(2)});
46   c.removeDuplicateDivs(merge);
47   checkEqual(a, b);
48   checkEqual(a, c);
49 }
50 
51 TEST(UtilsTest, DivisionReprNormalizeTest) {
52   auto merge = [this](unsigned i, unsigned j) -> bool { return true; };
53   DivisionRepr a = parseDivisionRepr(2, 1, {{MPInt(1), MPInt(2), MPInt(-1)}},
54                                      {MPInt(2)}),
55                b = parseDivisionRepr(2, 1, {{MPInt(16), MPInt(32), MPInt(-16)}},
56                                      {MPInt(32)}),
57                c = parseDivisionRepr(1, 1, {{MPInt(12), MPInt(-4)}},
58                                      {MPInt(8)}),
59                d = parseDivisionRepr(2, 2,
60                                      {{MPInt(1), MPInt(2), MPInt(-1)},
61                                       {MPInt(4), MPInt(8), MPInt(-4)}},
62                                      {MPInt(2), MPInt(8)});
63   b.removeDuplicateDivs(merge);
64   c.removeDuplicateDivs(merge);
65   d.removeDuplicateDivs(merge);
66   checkEqual(a, b);
67   checkEqual(c, d);
68 }
69