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.

325 lines
11 KiB

  1. #include "gtest/gtest.h"
  2. #include "src/storage/SquareSparseMatrix.h"
  3. #include "src/exceptions/invalid_argument.h"
  4. TEST(SquareSparseMatrixTest, ZeroRowsTest) {
  5. mrmc::storage::SquareSparseMatrix<int> *ssm = new mrmc::storage::SquareSparseMatrix<int>(0);
  6. ASSERT_EQ(ssm->getState(), mrmc::storage::SquareSparseMatrix<int>::MatrixStatus::UnInitialized);
  7. ASSERT_THROW(ssm->initialize(50), mrmc::exceptions::invalid_argument);
  8. ASSERT_EQ(ssm->getState(), mrmc::storage::SquareSparseMatrix<int>::MatrixStatus::Error);
  9. delete ssm;
  10. }
  11. TEST(SquareSparseMatrixTest, TooManyEntriesTest) {
  12. mrmc::storage::SquareSparseMatrix<int> *ssm = new mrmc::storage::SquareSparseMatrix<int>(2);
  13. ASSERT_EQ(ssm->getState(), mrmc::storage::SquareSparseMatrix<int>::MatrixStatus::UnInitialized);
  14. ASSERT_THROW(ssm->initialize(10), mrmc::exceptions::invalid_argument);
  15. ASSERT_EQ(ssm->getState(), mrmc::storage::SquareSparseMatrix<int>::MatrixStatus::Error);
  16. delete ssm;
  17. }
  18. TEST(SquareSparseMatrixTest, addNextValueTest) {
  19. mrmc::storage::SquareSparseMatrix<int> *ssm = new mrmc::storage::SquareSparseMatrix<int>(5);
  20. ASSERT_EQ(ssm->getState(), mrmc::storage::SquareSparseMatrix<int>::MatrixStatus::UnInitialized);
  21. ASSERT_NO_THROW(ssm->initialize(1));
  22. ASSERT_EQ(ssm->getState(), mrmc::storage::SquareSparseMatrix<int>::MatrixStatus::Initialized);
  23. ASSERT_THROW(ssm->addNextValue(-1, 1, 1), mrmc::exceptions::out_of_range);
  24. ASSERT_EQ(ssm->getState(), mrmc::storage::SquareSparseMatrix<int>::MatrixStatus::Error);
  25. ASSERT_THROW(ssm->addNextValue(1, -1, 1), mrmc::exceptions::out_of_range);
  26. ASSERT_EQ(ssm->getState(), mrmc::storage::SquareSparseMatrix<int>::MatrixStatus::Error);
  27. ASSERT_THROW(ssm->addNextValue(6, 1, 1), mrmc::exceptions::out_of_range);
  28. ASSERT_EQ(ssm->getState(), mrmc::storage::SquareSparseMatrix<int>::MatrixStatus::Error);
  29. ASSERT_THROW(ssm->addNextValue(1, 6, 1), mrmc::exceptions::out_of_range);
  30. ASSERT_EQ(ssm->getState(), mrmc::storage::SquareSparseMatrix<int>::MatrixStatus::Error);
  31. delete ssm;
  32. }
  33. TEST(SquareSparseMatrixTest, finalizeTest) {
  34. mrmc::storage::SquareSparseMatrix<int> *ssm = new mrmc::storage::SquareSparseMatrix<int>(5);
  35. ASSERT_EQ(ssm->getState(), mrmc::storage::SquareSparseMatrix<int>::MatrixStatus::UnInitialized);
  36. ASSERT_NO_THROW(ssm->initialize(5));
  37. ASSERT_EQ(ssm->getState(), mrmc::storage::SquareSparseMatrix<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::storage::SquareSparseMatrix<int>::MatrixStatus::Initialized);
  43. ASSERT_THROW(ssm->finalize(), mrmc::exceptions::invalid_state);
  44. ASSERT_EQ(ssm->getState(), mrmc::storage::SquareSparseMatrix<int>::MatrixStatus::Error);
  45. delete ssm;
  46. }
  47. TEST(SquareSparseMatrixTest, Test) {
  48. // 25 rows, 50 non zero entries
  49. mrmc::storage::SquareSparseMatrix<int> *ssm = new mrmc::storage::SquareSparseMatrix<int>(25);
  50. ASSERT_EQ(ssm->getState(), mrmc::storage::SquareSparseMatrix<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::storage::SquareSparseMatrix<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::storage::SquareSparseMatrix<int>::MatrixStatus::Initialized);
  82. ASSERT_NO_THROW(ssm->finalize());
  83. ASSERT_EQ(ssm->getState(), mrmc::storage::SquareSparseMatrix<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::storage::SquareSparseMatrix<int>::MatrixStatus::ReadReady);
  102. delete ssm;
  103. }
  104. TEST(SquareSparseMatrixTest, ConversionFromDenseEigen_ColMajor_SparseMatrixTest) {
  105. // 10 rows, 100 non zero entries
  106. mrmc::storage::SquareSparseMatrix<int> *ssm = new mrmc::storage::SquareSparseMatrix<int>(10);
  107. ASSERT_EQ(ssm->getState(), mrmc::storage::SquareSparseMatrix<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::storage::SquareSparseMatrix<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. delete ssm;
  127. }
  128. TEST(SquareSparseMatrixTest, ConversionFromDenseEigen_RowMajor_SparseMatrixTest) {
  129. // 10 rows, 100 non zero entries
  130. mrmc::storage::SquareSparseMatrix<int> *ssm = new mrmc::storage::SquareSparseMatrix<int>(10);
  131. ASSERT_EQ(ssm->getState(), mrmc::storage::SquareSparseMatrix<int>::MatrixStatus::UnInitialized);
  132. Eigen::SparseMatrix<int, Eigen::RowMajor> esm(10, 10);
  133. for (int row = 0; row < 10; ++row) {
  134. for (int col = 0; col < 10; ++col) {
  135. esm.insert(row, col) = row * 10 + col;
  136. }
  137. }
  138. // make compressed, important for initialize()
  139. esm.makeCompressed();
  140. ASSERT_NO_THROW(ssm->initialize(esm));
  141. ASSERT_NO_THROW(ssm->finalize());
  142. ASSERT_EQ(ssm->getState(), mrmc::storage::SquareSparseMatrix<int>::MatrixStatus::ReadReady);
  143. int target = -1;
  144. for (int row = 0; row < 10; ++row) {
  145. for (int col = 0; col < 10; ++col) {
  146. ASSERT_TRUE(ssm->getValue(row, col, &target));
  147. ASSERT_EQ(target, row * 10 + col);
  148. }
  149. }
  150. delete ssm;
  151. }
  152. TEST(SquareSparseMatrixTest, ConversionFromSparseEigen_ColMajor_SparseMatrixTest) {
  153. // 10 rows, 15 non zero entries
  154. mrmc::storage::SquareSparseMatrix<int> *ssm = new mrmc::storage::SquareSparseMatrix<int>(10);
  155. ASSERT_EQ(ssm->getState(), mrmc::storage::SquareSparseMatrix<int>::MatrixStatus::UnInitialized);
  156. Eigen::SparseMatrix<int> esm(10, 10);
  157. typedef Eigen::Triplet<int> IntTriplet;
  158. std::vector<IntTriplet> tripletList;
  159. tripletList.reserve(15);
  160. tripletList.push_back(IntTriplet(1, 0, 0));
  161. tripletList.push_back(IntTriplet(1, 1, 1));
  162. tripletList.push_back(IntTriplet(1, 2, 2));
  163. tripletList.push_back(IntTriplet(1, 3, 3));
  164. tripletList.push_back(IntTriplet(1, 4, 4));
  165. tripletList.push_back(IntTriplet(1, 5, 5));
  166. tripletList.push_back(IntTriplet(1, 6, 6));
  167. tripletList.push_back(IntTriplet(1, 7, 7));
  168. tripletList.push_back(IntTriplet(1, 8, 8));
  169. tripletList.push_back(IntTriplet(1, 9, 9));
  170. tripletList.push_back(IntTriplet(4, 3, 10));
  171. tripletList.push_back(IntTriplet(4, 6, 11));
  172. tripletList.push_back(IntTriplet(4, 9, 12));
  173. tripletList.push_back(IntTriplet(6, 0, 13));
  174. tripletList.push_back(IntTriplet(8, 9, 14));
  175. esm.setFromTriplets(tripletList.begin(), tripletList.end());
  176. // make compressed, important for initialize()
  177. esm.makeCompressed();
  178. ASSERT_NO_THROW(ssm->initialize(esm));
  179. ASSERT_NO_THROW(ssm->finalize());
  180. ASSERT_EQ(ssm->getState(), mrmc::storage::SquareSparseMatrix<int>::MatrixStatus::ReadReady);
  181. int target = -1;
  182. for (auto &coeff: tripletList) {
  183. ASSERT_TRUE(ssm->getValue(coeff.row(), coeff.col(), &target));
  184. ASSERT_EQ(target, coeff.value());
  185. }
  186. delete ssm;
  187. }
  188. TEST(SquareSparseMatrixTest, ConversionFromSparseEigen_RowMajor_SparseMatrixTest) {
  189. // 10 rows, 15 non zero entries
  190. mrmc::storage::SquareSparseMatrix<int> *ssm = new mrmc::storage::SquareSparseMatrix<int>(10);
  191. ASSERT_EQ(ssm->getState(), mrmc::storage::SquareSparseMatrix<int>::MatrixStatus::UnInitialized);
  192. Eigen::SparseMatrix<int, Eigen::RowMajor> esm(10, 10);
  193. typedef Eigen::Triplet<int> IntTriplet;
  194. std::vector<IntTriplet> tripletList;
  195. tripletList.reserve(15);
  196. tripletList.push_back(IntTriplet(1, 0, 0));
  197. tripletList.push_back(IntTriplet(1, 1, 1));
  198. tripletList.push_back(IntTriplet(1, 2, 2));
  199. tripletList.push_back(IntTriplet(1, 3, 3));
  200. tripletList.push_back(IntTriplet(1, 4, 4));
  201. tripletList.push_back(IntTriplet(1, 5, 5));
  202. tripletList.push_back(IntTriplet(1, 6, 6));
  203. tripletList.push_back(IntTriplet(1, 7, 7));
  204. tripletList.push_back(IntTriplet(1, 8, 8));
  205. tripletList.push_back(IntTriplet(1, 9, 9));
  206. tripletList.push_back(IntTriplet(4, 3, 10));
  207. tripletList.push_back(IntTriplet(4, 6, 11));
  208. tripletList.push_back(IntTriplet(4, 9, 12));
  209. tripletList.push_back(IntTriplet(6, 0, 13));
  210. tripletList.push_back(IntTriplet(8, 9, 14));
  211. esm.setFromTriplets(tripletList.begin(), tripletList.end());
  212. // make compressed, important for initialize()
  213. esm.makeCompressed();
  214. ASSERT_NO_THROW(ssm->initialize(esm));
  215. ASSERT_NO_THROW(ssm->finalize());
  216. ASSERT_EQ(ssm->getState(), mrmc::storage::SquareSparseMatrix<int>::MatrixStatus::ReadReady);
  217. int target = -1;
  218. for (auto &coeff: tripletList) {
  219. ASSERT_TRUE(ssm->getValue(coeff.row(), coeff.col(), &target));
  220. ASSERT_EQ(target, coeff.value());
  221. }
  222. delete ssm;
  223. }
  224. TEST(SquareSparseMatrixTest, ConversionToSparseEigen_RowMajor_SparseMatrixTest) {
  225. int values[100];
  226. mrmc::storage::SquareSparseMatrix<int> *ssm = new mrmc::storage::SquareSparseMatrix<int>(10);
  227. for (uint_fast32_t i = 0; i < 100; ++i) {
  228. values[i] = i;
  229. }
  230. ASSERT_NO_THROW(ssm->initialize(100 - 10));
  231. ASSERT_EQ(ssm->getState(), mrmc::storage::SquareSparseMatrix<int>::MatrixStatus::Initialized);
  232. for (uint_fast32_t row = 0; row < 10; ++row) {
  233. for (uint_fast32_t col = 0; col < 10; ++col) {
  234. ASSERT_NO_THROW(ssm->addNextValue(row, col, values[row * 10 + col]));
  235. }
  236. }
  237. ASSERT_EQ(ssm->getState(), mrmc::storage::SquareSparseMatrix<int>::MatrixStatus::Initialized);
  238. ASSERT_NO_THROW(ssm->finalize());
  239. ASSERT_EQ(ssm->getState(), mrmc::storage::SquareSparseMatrix<int>::MatrixStatus::ReadReady);
  240. Eigen::SparseMatrix<int, Eigen::RowMajor, int_fast32_t>* esm = ssm->toEigenSparseMatrix();
  241. for (uint_fast32_t row = 0; row < 10; ++row) {
  242. for (uint_fast32_t col = 0; col < 10; ++col) {
  243. ASSERT_EQ(values[row * 10 + col], esm->coeff(row, col));
  244. }
  245. }
  246. delete esm;
  247. delete ssm;
  248. }