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.

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