Debian Codegraph @

llvm-project/mlir/unittests/Analysis/Presburger/MatrixTest.cpp

raw
1 //===- MatrixTest.cpp - Tests for Matrix ----------------------------------===//
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/Matrix.h"
10 #include "./Utils.h"
11 #include <gmock/gmock.h>
12 #include <gtest/gtest.h>
13
14 using namespace mlir;
15 using namespace presburger;
16
17 TEST(MatrixTest, ReadWrite) {
18 Matrix mat(5, 5);
19 for (unsigned row = 0; row < 5; ++row)
20 for (unsigned col = 0; col < 5; ++col)
21 mat(row, col) = 10 * row + col;
22 for (unsigned row = 0; row < 5; ++row)
23 for (unsigned col = 0; col < 5; ++col)
24 EXPECT_EQ(mat(row, col), int(10 * row + col));
25 }
26
27 TEST(MatrixTest, SwapColumns) {
28 Matrix mat(5, 5);
29 for (unsigned row = 0; row < 5; ++row)
30 for (unsigned col = 0; col < 5; ++col)
31 mat(row, col) = col == 3 ? 1 : 0;
32 mat.swapColumns(3, 1);
33 for (unsigned row = 0; row < 5; ++row)
34 for (unsigned col = 0; col < 5; ++col)
35 EXPECT_EQ(mat(row, col), col == 1 ? 1 : 0);
36
37 // swap around all the other columns, swap (1, 3) twice for no effect.
38 mat.swapColumns(3, 1);
39 mat.swapColumns(2, 4);
40 mat.swapColumns(1, 3);
41 mat.swapColumns(0, 4);
42 mat.swapColumns(2, 2);
43
44 for (unsigned row = 0; row < 5; ++row)
45 for (unsigned col = 0; col < 5; ++col)
46 EXPECT_EQ(mat(row, col), col == 1 ? 1 : 0);
47 }
48
49 TEST(MatrixTest, SwapRows) {
50 Matrix mat(5, 5);
51 for (unsigned row = 0; row < 5; ++row)
52 for (unsigned col = 0; col < 5; ++col)
53 mat(row, col) = row == 2 ? 1 : 0;
54 mat.swapRows(2, 0);
55 for (unsigned row = 0; row < 5; ++row)
56 for (unsigned col = 0; col < 5; ++col)
57 EXPECT_EQ(mat(row, col), row == 0 ? 1 : 0);
58
59 // swap around all the other rows, swap (2, 0) twice for no effect.
60 mat.swapRows(3, 4);
61 mat.swapRows(1, 4);
62 mat.swapRows(2, 0);
63 mat.swapRows(1, 1);
64 mat.swapRows(0, 2);
65
66 for (unsigned row = 0; row < 5; ++row)
67 for (unsigned col = 0; col < 5; ++col)
68 EXPECT_EQ(mat(row, col), row == 0 ? 1 : 0);
69 }
70
71 TEST(MatrixTest, resizeVertically) {
72 Matrix mat(5, 5);
73 EXPECT_EQ(mat.getNumRows(), 5u);
74 EXPECT_EQ(mat.getNumColumns(), 5u);
75 for (unsigned row = 0; row < 5; ++row)
76 for (unsigned col = 0; col < 5; ++col)
77 mat(row, col) = 10 * row + col;
78
79 mat.resizeVertically(3);
80 ASSERT_TRUE(mat.hasConsistentState());
81 EXPECT_EQ(mat.getNumRows(), 3u);
82 EXPECT_EQ(mat.getNumColumns(), 5u);
83 for (unsigned row = 0; row < 3; ++row)
84 for (unsigned col = 0; col < 5; ++col)
85 EXPECT_EQ(mat(row, col), int(10 * row + col));
86
87 mat.resizeVertically(5);
88 ASSERT_TRUE(mat.hasConsistentState());
89 EXPECT_EQ(mat.getNumRows(), 5u);
90 EXPECT_EQ(mat.getNumColumns(), 5u);
91 for (unsigned row = 0; row < 5; ++row)
92 for (unsigned col = 0; col < 5; ++col)
93 EXPECT_EQ(mat(row, col), row >= 3 ? 0 : int(10 * row + col));
94 }
95
96 TEST(MatrixTest, insertColumns) {
97 Matrix mat(5, 5, 5, 10);
98 EXPECT_EQ(mat.getNumRows(), 5u);
99 EXPECT_EQ(mat.getNumColumns(), 5u);
100 for (unsigned row = 0; row < 5; ++row)
101 for (unsigned col = 0; col < 5; ++col)
102 mat(row, col) = 10 * row + col;
103
104 mat.insertColumns(3, 100);
105 ASSERT_TRUE(mat.hasConsistentState());
106 EXPECT_EQ(mat.getNumRows(), 5u);
107 EXPECT_EQ(mat.getNumColumns(), 105u);
108 for (unsigned row = 0; row < 5; ++row) {
109 for (unsigned col = 0; col < 105; ++col) {
110 if (col < 3)
111 EXPECT_EQ(mat(row, col), int(10 * row + col));
112 else if (3 <= col && col <= 102)
113 EXPECT_EQ(mat(row, col), 0);
114 else
115 EXPECT_EQ(mat(row, col), int(10 * row + col - 100));
116 }
117 }
118
119 mat.removeColumns(3, 100);
120 ASSERT_TRUE(mat.hasConsistentState());
121 mat.insertColumns(0, 0);
122 ASSERT_TRUE(mat.hasConsistentState());
123 mat.insertColumn(5);
124 ASSERT_TRUE(mat.hasConsistentState());
125
126 EXPECT_EQ(mat.getNumRows(), 5u);
127 EXPECT_EQ(mat.getNumColumns(), 6u);
128 for (unsigned row = 0; row < 5; ++row)
129 for (unsigned col = 0; col < 6; ++col)
130 EXPECT_EQ(mat(row, col), col == 5 ? 0 : 10 * row + col);
131 }
132
133 TEST(MatrixTest, insertRows) {
134 Matrix mat(5, 5, 5, 10);
135 ASSERT_TRUE(mat.hasConsistentState());
136 EXPECT_EQ(mat.getNumRows(), 5u);
137 EXPECT_EQ(mat.getNumColumns(), 5u);
138 for (unsigned row = 0; row < 5; ++row)
139 for (unsigned col = 0; col < 5; ++col)
140 mat(row, col) = 10 * row + col;
141
142 mat.insertRows(3, 100);
143 ASSERT_TRUE(mat.hasConsistentState());
144 EXPECT_EQ(mat.getNumRows(), 105u);
145 EXPECT_EQ(mat.getNumColumns(), 5u);
146 for (unsigned row = 0; row < 105; ++row) {
147 for (unsigned col = 0; col < 5; ++col) {
148 if (row < 3)
149 EXPECT_EQ(mat(row, col), int(10 * row + col));
150 else if (3 <= row && row <= 102)
151 EXPECT_EQ(mat(row, col), 0);
152 else
153 EXPECT_EQ(mat(row, col), int(10 * (row - 100) + col));
154 }
155 }
156
157 mat.removeRows(3, 100);
158 ASSERT_TRUE(mat.hasConsistentState());
159 mat.insertRows(0, 0);
160 ASSERT_TRUE(mat.hasConsistentState());
161 mat.insertRow(5);
162 ASSERT_TRUE(mat.hasConsistentState());
163
164 EXPECT_EQ(mat.getNumRows(), 6u);
165 EXPECT_EQ(mat.getNumColumns(), 5u);
166 for (unsigned row = 0; row < 6; ++row)
167 for (unsigned col = 0; col < 5; ++col)
168 EXPECT_EQ(mat(row, col), row == 5 ? 0 : 10 * row + col);
169 }
170
171 TEST(MatrixTest, resize) {
172 Matrix mat(5, 5);
173 EXPECT_EQ(mat.getNumRows(), 5u);
174 EXPECT_EQ(mat.getNumColumns(), 5u);
175 for (unsigned row = 0; row < 5; ++row)
176 for (unsigned col = 0; col < 5; ++col)
177 mat(row, col) = 10 * row + col;
178
179 mat.resize(3, 3);
180 ASSERT_TRUE(mat.hasConsistentState());
181 EXPECT_EQ(mat.getNumRows(), 3u);
182 EXPECT_EQ(mat.getNumColumns(), 3u);
183 for (unsigned row = 0; row < 3; ++row)
184 for (unsigned col = 0; col < 3; ++col)
185 EXPECT_EQ(mat(row, col), int(10 * row + col));
186
187 mat.resize(7, 7);
188 ASSERT_TRUE(mat.hasConsistentState());
189 EXPECT_EQ(mat.getNumRows(), 7u);
190 EXPECT_EQ(mat.getNumColumns(), 7u);
191 for (unsigned row = 0; row < 7; ++row)
192 for (unsigned col = 0; col < 7; ++col)
193 EXPECT_EQ(mat(row, col), row >= 3 || col >= 3 ? 0 : int(10 * row + col));
194 }
195
196 static void checkHermiteNormalForm(const Matrix &mat,
197 const Matrix &hermiteForm) {
198 auto [h, u] = mat.computeHermiteNormalForm();
199
200 for (unsigned row = 0; row < mat.getNumRows(); row++)
201 for (unsigned col = 0; col < mat.getNumColumns(); col++)
202 EXPECT_EQ(h(row, col), hermiteForm(row, col));
203 }
204
205 TEST(MatrixTest, computeHermiteNormalForm) {
206 // TODO: Add a check to test the original statement of hermite normal form
207 // instead of using a precomputed result.
208
209 {
210 // Hermite form of a unimodular matrix is the identity matrix.
211 Matrix mat = makeMatrix(3, 3, {{2, 3, 6}, {3, 2, 3}, {17, 11, 16}});
212 Matrix hermiteForm = makeMatrix(3, 3, {{1, 0, 0}, {0, 1, 0}, {0, 0, 1}});
213 checkHermiteNormalForm(mat, hermiteForm);
214 }
215
216 {
217 // Hermite form of a unimodular is the identity matrix.
218 Matrix mat = makeMatrix(
219 4, 4,
220 {{-6, -1, -19, -20}, {0, 1, 0, 0}, {-5, 0, -15, -16}, {6, 0, 18, 19}});
221 Matrix hermiteForm = makeMatrix(
222 4, 4, {{1, 0, 0, 0}, {0, 1, 0, 0}, {0, 0, 1, 0}, {0, 0, 0, 1}});
223 checkHermiteNormalForm(mat, hermiteForm);
224 }
225
226 {
227 Matrix mat = makeMatrix(
228 4, 4, {{3, 3, 1, 4}, {0, 1, 0, 0}, {0, 0, 19, 16}, {0, 0, 0, 3}});
229 Matrix hermiteForm = makeMatrix(
230 4, 4, {{1, 0, 0, 0}, {0, 1, 0, 0}, {1, 0, 3, 0}, {18, 0, 54, 57}});
231 checkHermiteNormalForm(mat, hermiteForm);
232 }
233
234 {
235 Matrix mat = makeMatrix(
236 4, 4, {{3, 3, 1, 4}, {0, 1, 0, 0}, {0, 0, 19, 16}, {0, 0, 0, 3}});
237 Matrix hermiteForm = makeMatrix(
238 4, 4, {{1, 0, 0, 0}, {0, 1, 0, 0}, {1, 0, 3, 0}, {18, 0, 54, 57}});
239 checkHermiteNormalForm(mat, hermiteForm);
240 }
241
242 {
243 Matrix mat =
244 makeMatrix(3, 5, {{0, 2, 0, 7, 1}, {-1, 0, 0, -3, 0}, {0, 4, 1, 0, 8}});
245 Matrix hermiteForm =
246 makeMatrix(3, 5, {{1, 0, 0, 0, 0}, {0, 1, 0, 0, 0}, {0, 0, 1, 0, 0}});
247 checkHermiteNormalForm(mat, hermiteForm);
248 }
249 }