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.

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