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.

326 lines
11 KiB

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