1 //===- Utils.cpp - Tests for Utils file ----------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "mlir/Analysis/Presburger/Utils.h"
11 #include <gmock/gmock.h>
12 #include <gtest/gtest.h>
15 using namespace presburger
;
18 parseDivisionRepr(unsigned numVars
, unsigned numDivs
,
19 ArrayRef
<ArrayRef
<DynamicAPInt
>> dividends
,
20 ArrayRef
<DynamicAPInt
> divisors
) {
21 DivisionRepr
repr(numVars
, numDivs
);
22 for (unsigned i
= 0, rows
= dividends
.size(); i
< rows
; ++i
)
23 repr
.setDiv(i
, dividends
[i
], divisors
[i
]);
27 static void checkEqual(DivisionRepr
&a
, DivisionRepr
&b
) {
28 EXPECT_EQ(a
.getNumVars(), b
.getNumVars());
29 EXPECT_EQ(a
.getNumDivs(), b
.getNumDivs());
30 for (unsigned i
= 0, rows
= a
.getNumDivs(); i
< rows
; ++i
) {
31 EXPECT_EQ(a
.hasRepr(i
), b
.hasRepr(i
));
34 EXPECT_TRUE(a
.getDenom(i
) == b
.getDenom(i
));
35 EXPECT_TRUE(a
.getDividend(i
).equals(b
.getDividend(i
)));
39 TEST(UtilsTest
, ParseAndCompareDivisionReprTest
) {
40 auto merge
= [](unsigned i
, unsigned j
) -> bool { return true; };
41 DivisionRepr a
= parseDivisionRepr(1, 1, {{DynamicAPInt(1), DynamicAPInt(2)}},
43 b
= parseDivisionRepr(1, 1, {{DynamicAPInt(1), DynamicAPInt(2)}},
45 c
= parseDivisionRepr(
47 {{DynamicAPInt(0), DynamicAPInt(1), DynamicAPInt(2)},
48 {DynamicAPInt(0), DynamicAPInt(1), DynamicAPInt(2)}},
49 {DynamicAPInt(2), DynamicAPInt(2)});
50 c
.removeDuplicateDivs(merge
);
55 TEST(UtilsTest
, DivisionReprNormalizeTest
) {
56 auto merge
= [](unsigned i
, unsigned j
) -> bool { return true; };
57 DivisionRepr a
= parseDivisionRepr(
58 2, 1, {{DynamicAPInt(1), DynamicAPInt(2), DynamicAPInt(-1)}},
60 b
= parseDivisionRepr(
62 {{DynamicAPInt(16), DynamicAPInt(32), DynamicAPInt(-16)}},
64 c
= parseDivisionRepr(1, 1,
65 {{DynamicAPInt(12), DynamicAPInt(-4)}},
67 d
= parseDivisionRepr(
69 {{DynamicAPInt(1), DynamicAPInt(2), DynamicAPInt(-1)},
70 {DynamicAPInt(4), DynamicAPInt(8), DynamicAPInt(-4)}},
71 {DynamicAPInt(2), DynamicAPInt(8)});
72 b
.removeDuplicateDivs(merge
);
73 c
.removeDuplicateDivs(merge
);
74 d
.removeDuplicateDivs(merge
);
79 TEST(UtilsTest
, convolution
) {
80 std::vector
<Fraction
> aVals({1, 2, 3, 4});
81 std::vector
<Fraction
> bVals({7, 3, 1, 6});
82 ArrayRef
<Fraction
> a(aVals
);
83 ArrayRef
<Fraction
> b(bVals
);
85 std::vector
<Fraction
> conv
= multiplyPolynomials(a
, b
);
87 EXPECT_EQ(conv
, std::vector
<Fraction
>({7, 17, 28, 45, 27, 22, 24}));
89 aVals
= {3, 6, 0, 2, 5};
94 conv
= multiplyPolynomials(a
, b
);
95 EXPECT_EQ(conv
, std::vector
<Fraction
>({6, 12, 18, 40, 10, 12, 30}));