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.

317 lines
11 KiB

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