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 <gmock/gmock.h>
11 #include <gtest/gtest.h>
12 
13 namespace mlir {
14 
15 TEST(MatrixTest, ReadWrite) {
16   Matrix mat(5, 5);
17   for (unsigned row = 0; row < 5; ++row)
18     for (unsigned col = 0; col < 5; ++col)
19       mat(row, col) = 10 * row + col;
20   for (unsigned row = 0; row < 5; ++row)
21     for (unsigned col = 0; col < 5; ++col)
22       EXPECT_EQ(mat(row, col), int(10 * row + col));
23 }
24 
25 TEST(MatrixTest, SwapColumns) {
26   Matrix mat(5, 5);
27   for (unsigned row = 0; row < 5; ++row)
28     for (unsigned col = 0; col < 5; ++col)
29       mat(row, col) = col == 3 ? 1 : 0;
30   mat.swapColumns(3, 1);
31   for (unsigned row = 0; row < 5; ++row)
32     for (unsigned col = 0; col < 5; ++col)
33       EXPECT_EQ(mat(row, col), col == 1 ? 1 : 0);
34 
35   // swap around all the other columns, swap (1, 3) twice for no effect.
36   mat.swapColumns(3, 1);
37   mat.swapColumns(2, 4);
38   mat.swapColumns(1, 3);
39   mat.swapColumns(0, 4);
40   mat.swapColumns(2, 2);
41 
42   for (unsigned row = 0; row < 5; ++row)
43     for (unsigned col = 0; col < 5; ++col)
44       EXPECT_EQ(mat(row, col), col == 1 ? 1 : 0);
45 }
46 
47 TEST(MatrixTest, SwapRows) {
48   Matrix mat(5, 5);
49   for (unsigned row = 0; row < 5; ++row)
50     for (unsigned col = 0; col < 5; ++col)
51       mat(row, col) = row == 2 ? 1 : 0;
52   mat.swapRows(2, 0);
53   for (unsigned row = 0; row < 5; ++row)
54     for (unsigned col = 0; col < 5; ++col)
55       EXPECT_EQ(mat(row, col), row == 0 ? 1 : 0);
56 
57   // swap around all the other rows, swap (2, 0) twice for no effect.
58   mat.swapRows(3, 4);
59   mat.swapRows(1, 4);
60   mat.swapRows(2, 0);
61   mat.swapRows(1, 1);
62   mat.swapRows(0, 2);
63 
64   for (unsigned row = 0; row < 5; ++row)
65     for (unsigned col = 0; col < 5; ++col)
66       EXPECT_EQ(mat(row, col), row == 0 ? 1 : 0);
67 }
68 
69 TEST(MatrixTest, resizeVertically) {
70   Matrix mat(5, 5);
71   EXPECT_EQ(mat.getNumRows(), 5u);
72   EXPECT_EQ(mat.getNumColumns(), 5u);
73   for (unsigned row = 0; row < 5; ++row)
74     for (unsigned col = 0; col < 5; ++col)
75       mat(row, col) = 10 * row + col;
76 
77   mat.resizeVertically(3);
78   ASSERT_TRUE(mat.hasConsistentState());
79   EXPECT_EQ(mat.getNumRows(), 3u);
80   EXPECT_EQ(mat.getNumColumns(), 5u);
81   for (unsigned row = 0; row < 3; ++row)
82     for (unsigned col = 0; col < 5; ++col)
83       EXPECT_EQ(mat(row, col), int(10 * row + col));
84 
85   mat.resizeVertically(5);
86   ASSERT_TRUE(mat.hasConsistentState());
87   EXPECT_EQ(mat.getNumRows(), 5u);
88   EXPECT_EQ(mat.getNumColumns(), 5u);
89   for (unsigned row = 0; row < 5; ++row)
90     for (unsigned col = 0; col < 5; ++col)
91       EXPECT_EQ(mat(row, col), row >= 3 ? 0 : int(10 * row + col));
92 }
93 
94 TEST(MatrixTest, insertColumns) {
95   Matrix mat(5, 5, 5, 10);
96   EXPECT_EQ(mat.getNumRows(), 5u);
97   EXPECT_EQ(mat.getNumColumns(), 5u);
98   for (unsigned row = 0; row < 5; ++row)
99     for (unsigned col = 0; col < 5; ++col)
100       mat(row, col) = 10 * row + col;
101 
102   mat.insertColumns(3, 100);
103   ASSERT_TRUE(mat.hasConsistentState());
104   EXPECT_EQ(mat.getNumRows(), 5u);
105   EXPECT_EQ(mat.getNumColumns(), 105u);
106   for (unsigned row = 0; row < 5; ++row) {
107     for (unsigned col = 0; col < 105; ++col) {
108       if (col < 3)
109         EXPECT_EQ(mat(row, col), int(10 * row + col));
110       else if (3 <= col && col <= 102)
111         EXPECT_EQ(mat(row, col), 0);
112       else
113         EXPECT_EQ(mat(row, col), int(10 * row + col - 100));
114     }
115   }
116 
117   mat.removeColumns(3, 100);
118   ASSERT_TRUE(mat.hasConsistentState());
119   mat.insertColumns(0, 0);
120   ASSERT_TRUE(mat.hasConsistentState());
121   mat.insertColumn(5);
122   ASSERT_TRUE(mat.hasConsistentState());
123 
124   EXPECT_EQ(mat.getNumRows(), 5u);
125   EXPECT_EQ(mat.getNumColumns(), 6u);
126   for (unsigned row = 0; row < 5; ++row)
127     for (unsigned col = 0; col < 6; ++col)
128       EXPECT_EQ(mat(row, col), col == 5 ? 0 : 10 * row + col);
129 }
130 
131 TEST(MatrixTest, insertRows) {
132   Matrix mat(5, 5, 5, 10);
133   ASSERT_TRUE(mat.hasConsistentState());
134   EXPECT_EQ(mat.getNumRows(), 5u);
135   EXPECT_EQ(mat.getNumColumns(), 5u);
136   for (unsigned row = 0; row < 5; ++row)
137     for (unsigned col = 0; col < 5; ++col)
138       mat(row, col) = 10 * row + col;
139 
140   mat.insertRows(3, 100);
141   ASSERT_TRUE(mat.hasConsistentState());
142   EXPECT_EQ(mat.getNumRows(), 105u);
143   EXPECT_EQ(mat.getNumColumns(), 5u);
144   for (unsigned row = 0; row < 105; ++row) {
145     for (unsigned col = 0; col < 5; ++col) {
146       if (row < 3)
147         EXPECT_EQ(mat(row, col), int(10 * row + col));
148       else if (3 <= row && row <= 102)
149         EXPECT_EQ(mat(row, col), 0);
150       else
151         EXPECT_EQ(mat(row, col), int(10 * (row - 100) + col));
152     }
153   }
154 
155   mat.removeRows(3, 100);
156   ASSERT_TRUE(mat.hasConsistentState());
157   mat.insertRows(0, 0);
158   ASSERT_TRUE(mat.hasConsistentState());
159   mat.insertRow(5);
160   ASSERT_TRUE(mat.hasConsistentState());
161 
162   EXPECT_EQ(mat.getNumRows(), 6u);
163   EXPECT_EQ(mat.getNumColumns(), 5u);
164   for (unsigned row = 0; row < 6; ++row)
165     for (unsigned col = 0; col < 5; ++col)
166       EXPECT_EQ(mat(row, col), row == 5 ? 0 : 10 * row + col);
167 }
168 
169 TEST(MatrixTest, resize) {
170   Matrix mat(5, 5);
171   EXPECT_EQ(mat.getNumRows(), 5u);
172   EXPECT_EQ(mat.getNumColumns(), 5u);
173   for (unsigned row = 0; row < 5; ++row)
174     for (unsigned col = 0; col < 5; ++col)
175       mat(row, col) = 10 * row + col;
176 
177   mat.resize(3, 3);
178   ASSERT_TRUE(mat.hasConsistentState());
179   EXPECT_EQ(mat.getNumRows(), 3u);
180   EXPECT_EQ(mat.getNumColumns(), 3u);
181   for (unsigned row = 0; row < 3; ++row)
182     for (unsigned col = 0; col < 3; ++col)
183       EXPECT_EQ(mat(row, col), int(10 * row + col));
184 
185   mat.resize(7, 7);
186   ASSERT_TRUE(mat.hasConsistentState());
187   EXPECT_EQ(mat.getNumRows(), 7u);
188   EXPECT_EQ(mat.getNumColumns(), 7u);
189   for (unsigned row = 0; row < 7; ++row)
190     for (unsigned col = 0; col < 7; ++col)
191       EXPECT_EQ(mat(row, col), row >= 3 || col >= 3 ? 0 : int(10 * row + col));
192 }
193 
194 } // namespace mlir
195