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
14using namespace mlir;
15using namespace presburger;
16
17static 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, dividend: dividends[i], divisor: divisors[i]);
23 return repr;
24}
25
26static 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
38TEST(UtilsTest, ParseAndCompareDivisionReprTest) {
39 auto merge = [](unsigned i, unsigned j) -> bool { return true; };
40 DivisionRepr a = parseDivisionRepr(numVars: 1, numDivs: 1, dividends: {{MPInt(1), MPInt(2)}}, divisors: {MPInt(2)}),
41 b = parseDivisionRepr(numVars: 1, numDivs: 1, dividends: {{MPInt(1), MPInt(2)}}, divisors: {MPInt(2)}),
42 c = parseDivisionRepr(numVars: 2, numDivs: 2,
43 dividends: {{MPInt(0), MPInt(1), MPInt(2)},
44 {MPInt(0), MPInt(1), MPInt(2)}},
45 divisors: {MPInt(2), MPInt(2)});
46 c.removeDuplicateDivs(merge);
47 checkEqual(a, b);
48 checkEqual(a, b&: c);
49}
50
51TEST(UtilsTest, DivisionReprNormalizeTest) {
52 auto merge = [](unsigned i, unsigned j) -> bool { return true; };
53 DivisionRepr a = parseDivisionRepr(numVars: 2, numDivs: 1, dividends: {{MPInt(1), MPInt(2), MPInt(-1)}},
54 divisors: {MPInt(2)}),
55 b = parseDivisionRepr(numVars: 2, numDivs: 1, dividends: {{MPInt(16), MPInt(32), MPInt(-16)}},
56 divisors: {MPInt(32)}),
57 c = parseDivisionRepr(numVars: 1, numDivs: 1, dividends: {{MPInt(12), MPInt(-4)}},
58 divisors: {MPInt(8)}),
59 d = parseDivisionRepr(numVars: 2, numDivs: 2,
60 dividends: {{MPInt(1), MPInt(2), MPInt(-1)},
61 {MPInt(4), MPInt(8), MPInt(-4)}},
62 divisors: {MPInt(2), MPInt(8)});
63 b.removeDuplicateDivs(merge);
64 c.removeDuplicateDivs(merge);
65 d.removeDuplicateDivs(merge);
66 checkEqual(a, b);
67 checkEqual(a&: c, b&: d);
68}
69
70TEST(UtilsTest, convolution) {
71 std::vector<Fraction> aVals({1, 2, 3, 4});
72 std::vector<Fraction> bVals({7, 3, 1, 6});
73 ArrayRef<Fraction> a(aVals);
74 ArrayRef<Fraction> b(bVals);
75
76 std::vector<Fraction> conv = multiplyPolynomials(a, b);
77
78 EXPECT_EQ(conv, std::vector<Fraction>({7, 17, 28, 45, 27, 22, 24}));
79
80 aVals = {3, 6, 0, 2, 5};
81 bVals = {2, 0, 6};
82 a = aVals;
83 b = bVals;
84
85 conv = multiplyPolynomials(a, b);
86 EXPECT_EQ(conv, std::vector<Fraction>({6, 12, 18, 40, 10, 12, 30}));
87}
88

source code of mlir/unittests/Analysis/Presburger/UtilsTest.cpp