1 //===- LinearTransformTest.cpp - Tests for LinearTransform ----------------===//
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/LinearTransform.h"
10 #include <gmock/gmock.h>
11 #include <gtest/gtest.h>
14 using namespace presburger
;
16 void testColumnEchelonForm(const IntMatrix
&m
, unsigned expectedRank
) {
17 unsigned lastAllowedNonZeroCol
= 0;
18 std::pair
<unsigned, LinearTransform
> result
=
19 LinearTransform::makeTransformToColumnEchelon(m
);
20 unsigned rank
= result
.first
;
21 EXPECT_EQ(rank
, expectedRank
);
22 LinearTransform transform
= result
.second
;
23 // In column echelon form, each row's last non-zero value can be at most one
24 // column to the right of the last non-zero column among the previous rows.
25 for (unsigned row
= 0, nRows
= m
.getNumRows(); row
< nRows
; ++row
) {
26 SmallVector
<DynamicAPInt
, 8> rowVec
=
27 transform
.preMultiplyWithRow(m
.getRow(row
));
28 for (unsigned col
= lastAllowedNonZeroCol
+ 1, nCols
= m
.getNumColumns();
30 EXPECT_EQ(rowVec
[col
], 0);
31 if (rowVec
[col
] != 0) {
32 llvm::errs() << "Failed at input matrix:\n";
36 if (rowVec
[lastAllowedNonZeroCol
] != 0)
37 lastAllowedNonZeroCol
++;
39 // The final value of lastAllowedNonZeroCol is the index of the first
40 // all-zeros column, so it must be equal to the rank.
41 EXPECT_EQ(lastAllowedNonZeroCol
, rank
);
44 TEST(LinearTransformTest
, transformToColumnEchelonTest
) {
45 // m1, m2, m3 are rank 1 matrices -- the first and second rows are identical.
51 testColumnEchelonForm(m1
, 1u);
58 testColumnEchelonForm(m2
, 1u);
65 testColumnEchelonForm(m3
, 1u);
67 // m4, m5, m6 are rank 2 matrices -- the first and second rows are different.
73 testColumnEchelonForm(m4
, 2u);
80 testColumnEchelonForm(m5
, 2u);
87 testColumnEchelonForm(m5
, 2u);