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