[llvm] ConstraintSystem: replace data structure with Matrix (PR #98895)

Matt Arsenault via llvm-commits llvm-commits at lists.llvm.org
Mon Jul 15 07:34:41 PDT 2024


================
@@ -0,0 +1,325 @@
+#include "llvm/ADT/Matrix.h"
+#include "llvm/ADT/DynamicAPInt.h"
+#include "gtest/gtest.h"
+
+using namespace llvm;
+
+template <typename T> static SmallVector<T> getDummyValues(size_t NValues) {
+  SmallVector<T> Ret(NValues);
+  for (size_t Idx = 0; Idx < NValues; ++Idx)
+    Ret[Idx] = T(Idx + 1);
+  return Ret;
+}
+
+template <typename T> class MatrixTest : public testing::Test {
+protected:
+  MatrixTest()
+      : ColumnInitMatrix(8), SmallMatrix(2, 2), OtherSmall(3, 3),
+        LargeMatrix(16, 16) {}
+  MatrixStorage<T, 16> ColumnInitMatrix;
+  MatrixStorage<T> SmallMatrix;
+  MatrixStorage<T> OtherSmall;
+  MatrixStorage<T> LargeMatrix;
+  SmallVector<T> getDummyRow(size_t NValues) {
+    return getDummyValues<T>(NValues);
+  }
+};
+
+using MatrixTestTypes = ::testing::Types<int64_t, DynamicAPInt>;
+TYPED_TEST_SUITE(MatrixTest, MatrixTestTypes, );
+
+TYPED_TEST(MatrixTest, Construction) {
+  auto &E = this->ColumnInitMatrix;
+  ASSERT_TRUE(E.empty());
+  ASSERT_EQ(E.getNumCols(), 8u);
+  E.setNumCols(3);
+  ASSERT_EQ(E.getNumCols(), 3u);
+  ASSERT_TRUE(E.empty());
+  ASSERT_EQ(E.getNumRows(), 0u);
+  auto &M = this->SmallMatrix;
+  ASSERT_FALSE(M.empty());
+  ASSERT_EQ(M.size(), 4u);
+  ASSERT_EQ(M.getNumRows(), 2u);
+  ASSERT_EQ(M.getNumCols(), 2u);
+}
+
+TYPED_TEST(MatrixTest, CopyConstruction) {
+  auto &OldMat = this->SmallMatrix;
+  auto V = MatrixView<TypeParam>{OldMat};
+  V[0] = this->getDummyRow(2);
+  V[0].pop_back();
+  V[1] = this->getDummyRow(2);
+  V[1].pop_front();
+  ASSERT_EQ(V.getRowSpan(), 2u);
+  ASSERT_EQ(V.getColSpan(0), 1u);
+  ASSERT_EQ(V.getColSpan(1), 1u);
+  ASSERT_EQ(V[0][0], 1);
+  ASSERT_EQ(V[1][0], 2);
+  ASSERT_EQ(MatrixView<TypeParam>{OldMat}[0][0], 1);
+  ASSERT_EQ(MatrixView<TypeParam>{OldMat}[0][1], 2);
+  ASSERT_EQ(MatrixView<TypeParam>{OldMat}[1][0], 1);
+  ASSERT_EQ(MatrixView<TypeParam>{OldMat}[1][1], 2);
+  MatrixStorage<TypeParam> NewMat{OldMat};
+  MatrixView<TypeParam> C{V, NewMat};
+  ASSERT_EQ(C.getRowSpan(), 2u);
+  ASSERT_EQ(C.getColSpan(0), 1u);
+  ASSERT_EQ(C.getColSpan(1), 1u);
+  ASSERT_EQ(C[0][0], 1);
+  ASSERT_EQ(C[1][0], 2);
+  C.addRow(this->getDummyRow(2));
+  ASSERT_EQ(C[2][0], 1);
+  ASSERT_EQ(C[2][1], 2);
+}
+
+TYPED_TEST(MatrixTest, RowOps) {
+  auto &M = this->SmallMatrix;
+  auto &O = this->OtherSmall;
+  MatrixView<TypeParam> V{M};
+  ASSERT_EQ(M.getNumRows(), 2u);
----------------
arsenm wrote:

Most of these ASSERT_EQ should be EXPECT_EQ 

https://github.com/llvm/llvm-project/pull/98895


More information about the llvm-commits mailing list