You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

314 lines
11 KiB

  1. #include "gtest/gtest.h"
  2. #include "src/sparse/static_sparse_matrix.h"
  3. #include "src/exceptions/invalid_argument.h"
  4. TEST(StaticSparseMatrixTest, ZeroRowsTest) {
  5. mrmc::sparse::StaticSparseMatrix<int> *ssm = new mrmc::sparse::StaticSparseMatrix<int>(0);
  6. ASSERT_EQ(ssm->getState(), mrmc::sparse::StaticSparseMatrix<int>::MatrixStatus::UnInitialized);
  7. ASSERT_THROW(ssm->initialize(50), mrmc::exceptions::invalid_argument);
  8. ASSERT_EQ(ssm->getState(), mrmc::sparse::StaticSparseMatrix<int>::MatrixStatus::Error);
  9. delete ssm;
  10. }
  11. TEST(StaticSparseMatrixTest, TooManyEntriesTest) {
  12. mrmc::sparse::StaticSparseMatrix<int> *ssm = new mrmc::sparse::StaticSparseMatrix<int>(2);
  13. ASSERT_EQ(ssm->getState(), mrmc::sparse::StaticSparseMatrix<int>::MatrixStatus::UnInitialized);
  14. ASSERT_THROW(ssm->initialize(10), mrmc::exceptions::invalid_argument);
  15. ASSERT_EQ(ssm->getState(), mrmc::sparse::StaticSparseMatrix<int>::MatrixStatus::Error);
  16. delete ssm;
  17. }
  18. TEST(StaticSparseMatrixTest, addNextValueTest) {
  19. mrmc::sparse::StaticSparseMatrix<int> *ssm = new mrmc::sparse::StaticSparseMatrix<int>(5);
  20. ASSERT_EQ(ssm->getState(), mrmc::sparse::StaticSparseMatrix<int>::MatrixStatus::UnInitialized);
  21. ASSERT_NO_THROW(ssm->initialize(1));
  22. ASSERT_EQ(ssm->getState(), mrmc::sparse::StaticSparseMatrix<int>::MatrixStatus::Initialized);
  23. ASSERT_THROW(ssm->addNextValue(-1, 1, 1), mrmc::exceptions::out_of_range);
  24. ASSERT_EQ(ssm->getState(), mrmc::sparse::StaticSparseMatrix<int>::MatrixStatus::Error);
  25. ASSERT_THROW(ssm->addNextValue(1, -1, 1), mrmc::exceptions::out_of_range);
  26. ASSERT_EQ(ssm->getState(), mrmc::sparse::StaticSparseMatrix<int>::MatrixStatus::Error);
  27. ASSERT_THROW(ssm->addNextValue(6, 1, 1), mrmc::exceptions::out_of_range);
  28. ASSERT_EQ(ssm->getState(), mrmc::sparse::StaticSparseMatrix<int>::MatrixStatus::Error);
  29. ASSERT_THROW(ssm->addNextValue(1, 6, 1), mrmc::exceptions::out_of_range);
  30. ASSERT_EQ(ssm->getState(), mrmc::sparse::StaticSparseMatrix<int>::MatrixStatus::Error);
  31. delete ssm;
  32. }
  33. TEST(StaticSparseMatrixTest, finalizeTest) {
  34. mrmc::sparse::StaticSparseMatrix<int> *ssm = new mrmc::sparse::StaticSparseMatrix<int>(5);
  35. ASSERT_EQ(ssm->getState(), mrmc::sparse::StaticSparseMatrix<int>::MatrixStatus::UnInitialized);
  36. ASSERT_NO_THROW(ssm->initialize(5));
  37. ASSERT_EQ(ssm->getState(), mrmc::sparse::StaticSparseMatrix<int>::MatrixStatus::Initialized);
  38. ASSERT_NO_THROW(ssm->addNextValue(1, 2, 1));
  39. ASSERT_NO_THROW(ssm->addNextValue(1, 3, 1));
  40. ASSERT_NO_THROW(ssm->addNextValue(1, 4, 1));
  41. ASSERT_NO_THROW(ssm->addNextValue(1, 5, 1));
  42. ASSERT_EQ(ssm->getState(), mrmc::sparse::StaticSparseMatrix<int>::MatrixStatus::Initialized);
  43. ASSERT_THROW(ssm->finalize(), mrmc::exceptions::invalid_state);
  44. ASSERT_EQ(ssm->getState(), mrmc::sparse::StaticSparseMatrix<int>::MatrixStatus::Error);
  45. delete ssm;
  46. }
  47. TEST(StaticSparseMatrixTest, Test) {
  48. // 25 rows, 50 non zero entries
  49. mrmc::sparse::StaticSparseMatrix<int> *ssm = new mrmc::sparse::StaticSparseMatrix<int>(25);
  50. ASSERT_EQ(ssm->getState(), mrmc::sparse::StaticSparseMatrix<int>::MatrixStatus::UnInitialized);
  51. int values[50] = {
  52. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
  53. 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
  54. 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
  55. 30, 31, 32, 33, 34, 35, 36, 37, 38, 39,
  56. 40, 41, 42, 43, 44, 45, 46, 47, 48, 49
  57. };
  58. int position_row[50] = {
  59. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  60. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  61. 2, 2, 2, 2, /* first row empty, one full row ��� 25 minus the diagonal entry */
  62. 4, 4, /* one empty row, then first and last column */
  63. 13, 13, 13, 13, /* a few empty rows, middle columns */
  64. 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
  65. 24, 24, 24, 24, 24, 24, 24, 24, 24, 24 /* second to last row */
  66. };
  67. int position_col[50] = {
  68. 1, 3, 4, 5, 6, 7, 8, 9, 10,
  69. 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
  70. 21, 22, 23, 24, 25, /* first row empty, one full row a 25 */
  71. 1, 25, /* one empty row, then first and last column */
  72. 16, 17, 18, 19, /* a few empty rows, middle columns */
  73. 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
  74. 14, 15, 16, 17, 18, 19, 20, 21, 22, 23 /* second to last row */
  75. };
  76. ASSERT_NO_THROW(ssm->initialize(50));
  77. ASSERT_EQ(ssm->getState(), mrmc::sparse::StaticSparseMatrix<int>::MatrixStatus::Initialized);
  78. for (int i = 0; i < 50; ++i) {
  79. ASSERT_NO_THROW(ssm->addNextValue(position_row[i], position_col[i], values[i]));
  80. }
  81. ASSERT_EQ(ssm->getState(), mrmc::sparse::StaticSparseMatrix<int>::MatrixStatus::Initialized);
  82. ASSERT_NO_THROW(ssm->finalize());
  83. ASSERT_EQ(ssm->getState(), mrmc::sparse::StaticSparseMatrix<int>::MatrixStatus::ReadReady);
  84. int target;
  85. for (int i = 0; i < 50; ++i) {
  86. ASSERT_TRUE(ssm->getValue(position_row[i], position_col[i], &target));
  87. ASSERT_EQ(values[i], target);
  88. }
  89. // test for a few of the empty rows
  90. for (int row = 15; row < 24; ++row) {
  91. for (int col = 1; col <= 25; ++col) {
  92. target = 1;
  93. if (row != col) {
  94. ASSERT_FALSE(ssm->getValue(row, col, &target));
  95. } else {
  96. ASSERT_TRUE(ssm->getValue(row, col, &target));
  97. }
  98. ASSERT_EQ(0, target);
  99. }
  100. }
  101. ASSERT_EQ(ssm->getState(), mrmc::sparse::StaticSparseMatrix<int>::MatrixStatus::ReadReady);
  102. delete ssm;
  103. }
  104. TEST(StaticSparseMatrixTest, ConversionFromDenseEigen_ColMajor_SparseMatrixTest) {
  105. // 10 rows, 100 non zero entries
  106. mrmc::sparse::StaticSparseMatrix<int> *ssm = new mrmc::sparse::StaticSparseMatrix<int>(10);
  107. ASSERT_EQ(ssm->getState(), mrmc::sparse::StaticSparseMatrix<int>::MatrixStatus::UnInitialized);
  108. Eigen::SparseMatrix<int> esm(10, 10);
  109. for (int row = 0; row < 10; ++row) {
  110. for (int col = 0; col < 10; ++col) {
  111. esm.insert(row, col) = row * 10 + col;
  112. }
  113. }
  114. // make compressed, important for initialize()
  115. esm.makeCompressed();
  116. ASSERT_NO_THROW(ssm->initialize(esm));
  117. ASSERT_NO_THROW(ssm->finalize());
  118. ASSERT_EQ(ssm->getState(), mrmc::sparse::StaticSparseMatrix<int>::MatrixStatus::ReadReady);
  119. int target = -1;
  120. for (int row = 0; row < 10; ++row) {
  121. for (int col = 0; col < 10; ++col) {
  122. ASSERT_TRUE(ssm->getValue(row, col, &target));
  123. ASSERT_EQ(target, row * 10 + col);
  124. }
  125. }
  126. }
  127. TEST(StaticSparseMatrixTest, ConversionFromDenseEigen_RowMajor_SparseMatrixTest) {
  128. // 10 rows, 100 non zero entries
  129. mrmc::sparse::StaticSparseMatrix<int> *ssm = new mrmc::sparse::StaticSparseMatrix<int>(10);
  130. ASSERT_EQ(ssm->getState(), mrmc::sparse::StaticSparseMatrix<int>::MatrixStatus::UnInitialized);
  131. Eigen::SparseMatrix<int, Eigen::RowMajor> esm(10, 10);
  132. for (int row = 0; row < 10; ++row) {
  133. for (int col = 0; col < 10; ++col) {
  134. esm.insert(row, col) = row * 10 + col;
  135. }
  136. }
  137. // make compressed, important for initialize()
  138. esm.makeCompressed();
  139. ASSERT_NO_THROW(ssm->initialize(esm));
  140. ASSERT_NO_THROW(ssm->finalize());
  141. ASSERT_EQ(ssm->getState(), mrmc::sparse::StaticSparseMatrix<int>::MatrixStatus::ReadReady);
  142. int target = -1;
  143. for (int row = 0; row < 10; ++row) {
  144. for (int col = 0; col < 10; ++col) {
  145. ASSERT_TRUE(ssm->getValue(row, col, &target));
  146. ASSERT_EQ(target, row * 10 + col);
  147. }
  148. }
  149. }
  150. TEST(StaticSparseMatrixTest, ConversionFromSparseEigen_ColMajor_SparseMatrixTest) {
  151. // 10 rows, 15 non zero entries
  152. mrmc::sparse::StaticSparseMatrix<int> *ssm = new mrmc::sparse::StaticSparseMatrix<int>(10);
  153. ASSERT_EQ(ssm->getState(), mrmc::sparse::StaticSparseMatrix<int>::MatrixStatus::UnInitialized);
  154. Eigen::SparseMatrix<int> esm(10, 10);
  155. typedef Eigen::Triplet<int> IntTriplet;
  156. std::vector<IntTriplet> tripletList;
  157. tripletList.reserve(15);
  158. tripletList.push_back(IntTriplet(1, 0, 0));
  159. tripletList.push_back(IntTriplet(1, 1, 1));
  160. tripletList.push_back(IntTriplet(1, 2, 2));
  161. tripletList.push_back(IntTriplet(1, 3, 3));
  162. tripletList.push_back(IntTriplet(1, 4, 4));
  163. tripletList.push_back(IntTriplet(1, 5, 5));
  164. tripletList.push_back(IntTriplet(1, 6, 6));
  165. tripletList.push_back(IntTriplet(1, 7, 7));
  166. tripletList.push_back(IntTriplet(1, 8, 8));
  167. tripletList.push_back(IntTriplet(1, 9, 9));
  168. tripletList.push_back(IntTriplet(4, 3, 10));
  169. tripletList.push_back(IntTriplet(4, 6, 11));
  170. tripletList.push_back(IntTriplet(4, 9, 12));
  171. tripletList.push_back(IntTriplet(6, 0, 13));
  172. tripletList.push_back(IntTriplet(8, 9, 14));
  173. esm.setFromTriplets(tripletList.begin(), tripletList.end());
  174. // make compressed, important for initialize()
  175. esm.makeCompressed();
  176. ASSERT_NO_THROW(ssm->initialize(esm));
  177. ASSERT_NO_THROW(ssm->finalize());
  178. ASSERT_EQ(ssm->getState(), mrmc::sparse::StaticSparseMatrix<int>::MatrixStatus::ReadReady);
  179. int target = -1;
  180. for (auto &coeff: tripletList) {
  181. ASSERT_TRUE(ssm->getValue(coeff.row(), coeff.col(), &target));
  182. ASSERT_EQ(target, coeff.value());
  183. }
  184. }
  185. TEST(StaticSparseMatrixTest, ConversionFromSparseEigen_RowMajor_SparseMatrixTest) {
  186. // 10 rows, 15 non zero entries
  187. mrmc::sparse::StaticSparseMatrix<int> *ssm = new mrmc::sparse::StaticSparseMatrix<int>(10);
  188. ASSERT_EQ(ssm->getState(), mrmc::sparse::StaticSparseMatrix<int>::MatrixStatus::UnInitialized);
  189. Eigen::SparseMatrix<int, Eigen::RowMajor> esm(10, 10);
  190. typedef Eigen::Triplet<int> IntTriplet;
  191. std::vector<IntTriplet> tripletList;
  192. tripletList.reserve(15);
  193. tripletList.push_back(IntTriplet(1, 0, 0));
  194. tripletList.push_back(IntTriplet(1, 1, 1));
  195. tripletList.push_back(IntTriplet(1, 2, 2));
  196. tripletList.push_back(IntTriplet(1, 3, 3));
  197. tripletList.push_back(IntTriplet(1, 4, 4));
  198. tripletList.push_back(IntTriplet(1, 5, 5));
  199. tripletList.push_back(IntTriplet(1, 6, 6));
  200. tripletList.push_back(IntTriplet(1, 7, 7));
  201. tripletList.push_back(IntTriplet(1, 8, 8));
  202. tripletList.push_back(IntTriplet(1, 9, 9));
  203. tripletList.push_back(IntTriplet(4, 3, 10));
  204. tripletList.push_back(IntTriplet(4, 6, 11));
  205. tripletList.push_back(IntTriplet(4, 9, 12));
  206. tripletList.push_back(IntTriplet(6, 0, 13));
  207. tripletList.push_back(IntTriplet(8, 9, 14));
  208. esm.setFromTriplets(tripletList.begin(), tripletList.end());
  209. // make compressed, important for initialize()
  210. esm.makeCompressed();
  211. ASSERT_NO_THROW(ssm->initialize(esm));
  212. ASSERT_NO_THROW(ssm->finalize());
  213. ASSERT_EQ(ssm->getState(), mrmc::sparse::StaticSparseMatrix<int>::MatrixStatus::ReadReady);
  214. int target = -1;
  215. for (auto &coeff: tripletList) {
  216. ASSERT_TRUE(ssm->getValue(coeff.row(), coeff.col(), &target));
  217. ASSERT_EQ(target, coeff.value());
  218. }
  219. }
  220. TEST(StaticSparseMatrixTest, ConversionToSparseEigen_RowMajor_SparseMatrixTest) {
  221. int values[100];
  222. mrmc::sparse::StaticSparseMatrix<int> *ssm = new mrmc::sparse::StaticSparseMatrix<int>(10);
  223. for (uint_fast32_t i = 0; i < 100; ++i) {
  224. values[i] = i;
  225. }
  226. ASSERT_NO_THROW(ssm->initialize(100 - 10));
  227. ASSERT_EQ(ssm->getState(), mrmc::sparse::StaticSparseMatrix<int>::MatrixStatus::Initialized);
  228. for (uint_fast32_t row = 0; row < 10; ++row) {
  229. for (uint_fast32_t col = 0; col < 10; ++col) {
  230. ASSERT_NO_THROW(ssm->addNextValue(row, col, values[row * 10 + col]));
  231. }
  232. }
  233. ASSERT_EQ(ssm->getState(), mrmc::sparse::StaticSparseMatrix<int>::MatrixStatus::Initialized);
  234. ASSERT_NO_THROW(ssm->finalize());
  235. ASSERT_EQ(ssm->getState(), mrmc::sparse::StaticSparseMatrix<int>::MatrixStatus::ReadReady);
  236. Eigen::SparseMatrix<int, Eigen::RowMajor, int_fast32_t>* esm = ssm->toEigenSparseMatrix();
  237. for (uint_fast32_t row = 0; row < 10; ++row) {
  238. for (uint_fast32_t col = 0; col < 10; ++col) {
  239. ASSERT_EQ(values[row * 10 + col], esm->coeff(row, col));
  240. }
  241. }
  242. }