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/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. ASSERT_NO_THROW(ssm->initialize(50));
  78. ASSERT_EQ(ssm->getState(), storm::storage::SparseMatrix<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(), storm::storage::SparseMatrix<int>::MatrixStatus::Initialized);
  83. ASSERT_NO_THROW(ssm->finalize());
  84. ASSERT_EQ(ssm->getState(), storm::storage::SparseMatrix<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. ASSERT_FALSE(ssm->getValue(row, col, &target));
  95. ASSERT_EQ(0, target);
  96. }
  97. }
  98. ASSERT_EQ(ssm->getState(), storm::storage::SparseMatrix<int>::MatrixStatus::ReadReady);
  99. delete ssm;
  100. }
  101. TEST(SparseMatrixTest, ConversionFromDenseEigen_ColMajor_SparseMatrixTest) {
  102. // 10 rows, 100 non zero entries
  103. storm::storage::SparseMatrix<int> *ssm = new storm::storage::SparseMatrix<int>(10);
  104. ASSERT_EQ(ssm->getState(), storm::storage::SparseMatrix<int>::MatrixStatus::UnInitialized);
  105. Eigen::SparseMatrix<int> esm(10, 10);
  106. for (int row = 0; row < 10; ++row) {
  107. for (int col = 0; col < 10; ++col) {
  108. esm.insert(row, col) = row * 10 + col;
  109. }
  110. }
  111. // make compressed, important for initialize()
  112. esm.makeCompressed();
  113. ASSERT_NO_THROW(ssm->initialize(esm));
  114. ASSERT_NO_THROW(ssm->finalize());
  115. ASSERT_EQ(ssm->getState(), storm::storage::SparseMatrix<int>::MatrixStatus::ReadReady);
  116. int target = -1;
  117. for (int row = 0; row < 10; ++row) {
  118. for (int col = 0; col < 10; ++col) {
  119. ASSERT_TRUE(ssm->getValue(row, col, &target));
  120. ASSERT_EQ(target, row * 10 + col);
  121. }
  122. }
  123. delete ssm;
  124. }
  125. TEST(SparseMatrixTest, ConversionFromDenseEigen_RowMajor_SparseMatrixTest) {
  126. // 10 rows, 100 non zero entries
  127. storm::storage::SparseMatrix<int> *ssm = new storm::storage::SparseMatrix<int>(10);
  128. ASSERT_EQ(ssm->getState(), storm::storage::SparseMatrix<int>::MatrixStatus::UnInitialized);
  129. Eigen::SparseMatrix<int, Eigen::RowMajor> esm(10, 10);
  130. for (int row = 0; row < 10; ++row) {
  131. for (int col = 0; col < 10; ++col) {
  132. esm.insert(row, col) = row * 10 + col;
  133. }
  134. }
  135. // make compressed, important for initialize()
  136. esm.makeCompressed();
  137. ASSERT_NO_THROW(ssm->initialize(esm));
  138. ASSERT_NO_THROW(ssm->finalize());
  139. ASSERT_EQ(ssm->getState(), storm::storage::SparseMatrix<int>::MatrixStatus::ReadReady);
  140. int target = -1;
  141. for (int row = 0; row < 10; ++row) {
  142. for (int col = 0; col < 10; ++col) {
  143. ASSERT_TRUE(ssm->getValue(row, col, &target));
  144. ASSERT_EQ(target, row * 10 + col);
  145. }
  146. }
  147. delete ssm;
  148. }
  149. TEST(SparseMatrixTest, ConversionFromSparseEigen_ColMajor_SparseMatrixTest) {
  150. // 10 rows, 15 non zero entries
  151. storm::storage::SparseMatrix<int> *ssm = new storm::storage::SparseMatrix<int>(10);
  152. ASSERT_EQ(ssm->getState(), storm::storage::SparseMatrix<int>::MatrixStatus::UnInitialized);
  153. Eigen::SparseMatrix<int> esm(10, 10);
  154. typedef Eigen::Triplet<int> IntTriplet;
  155. std::vector<IntTriplet> tripletList;
  156. tripletList.reserve(15);
  157. tripletList.push_back(IntTriplet(1, 0, 0));
  158. tripletList.push_back(IntTriplet(1, 1, 1));
  159. tripletList.push_back(IntTriplet(1, 2, 2));
  160. tripletList.push_back(IntTriplet(1, 3, 3));
  161. tripletList.push_back(IntTriplet(1, 4, 4));
  162. tripletList.push_back(IntTriplet(1, 5, 5));
  163. tripletList.push_back(IntTriplet(1, 6, 6));
  164. tripletList.push_back(IntTriplet(1, 7, 7));
  165. tripletList.push_back(IntTriplet(1, 8, 8));
  166. tripletList.push_back(IntTriplet(1, 9, 9));
  167. tripletList.push_back(IntTriplet(4, 3, 10));
  168. tripletList.push_back(IntTriplet(4, 6, 11));
  169. tripletList.push_back(IntTriplet(4, 9, 12));
  170. tripletList.push_back(IntTriplet(6, 0, 13));
  171. tripletList.push_back(IntTriplet(8, 9, 14));
  172. esm.setFromTriplets(tripletList.begin(), tripletList.end());
  173. // make compressed, important for initialize()
  174. esm.makeCompressed();
  175. ASSERT_NO_THROW(ssm->initialize(esm));
  176. ASSERT_NO_THROW(ssm->finalize());
  177. ASSERT_EQ(ssm->getState(), storm::storage::SparseMatrix<int>::MatrixStatus::ReadReady);
  178. int target = -1;
  179. for (auto &coeff: tripletList) {
  180. ASSERT_TRUE(ssm->getValue(coeff.row(), coeff.col(), &target));
  181. ASSERT_EQ(target, coeff.value());
  182. }
  183. delete ssm;
  184. }
  185. TEST(SparseMatrixTest, ConversionFromSparseEigen_RowMajor_SparseMatrixTest) {
  186. // 10 rows, 15 non zero entries
  187. storm::storage::SparseMatrix<int> *ssm = new storm::storage::SparseMatrix<int>(10, 10);
  188. ASSERT_EQ(ssm->getState(), storm::storage::SparseMatrix<int>::MatrixStatus::UnInitialized);
  189. Eigen::SparseMatrix<int, Eigen::RowMajor> esm(10, 10);
  190. typedef Eigen::Triplet<int> IntTriplet;
  191. std::vector<IntTriplet> tripletList;
  192. tripletList.reserve(15);
  193. tripletList.push_back(IntTriplet(1, 0, 15));
  194. tripletList.push_back(IntTriplet(1, 1, 1));
  195. tripletList.push_back(IntTriplet(1, 2, 2));
  196. tripletList.push_back(IntTriplet(1, 3, 3));
  197. tripletList.push_back(IntTriplet(1, 4, 4));
  198. tripletList.push_back(IntTriplet(1, 5, 5));
  199. tripletList.push_back(IntTriplet(1, 6, 6));
  200. tripletList.push_back(IntTriplet(1, 7, 7));
  201. tripletList.push_back(IntTriplet(1, 8, 8));
  202. tripletList.push_back(IntTriplet(1, 9, 9));
  203. tripletList.push_back(IntTriplet(4, 3, 10));
  204. tripletList.push_back(IntTriplet(4, 6, 11));
  205. tripletList.push_back(IntTriplet(4, 9, 12));
  206. tripletList.push_back(IntTriplet(6, 0, 13));
  207. tripletList.push_back(IntTriplet(8, 9, 14));
  208. esm.setFromTriplets(tripletList.begin(), tripletList.end());
  209. // make compressed, important for initialize()
  210. esm.makeCompressed();
  211. ASSERT_NO_THROW(ssm->initialize(esm));
  212. ASSERT_NO_THROW(ssm->finalize());
  213. ASSERT_EQ(ssm->getState(), storm::storage::SparseMatrix<int>::MatrixStatus::ReadReady);
  214. const std::vector<uint_fast64_t> rowP = ssm->getRowIndicationsPointer();
  215. const std::vector<uint_fast64_t> colP = ssm->getColumnIndicationsPointer();
  216. const std::vector<int> valP = ssm->getStoragePointer();
  217. int target = -1;
  218. for (auto &coeff: tripletList) {
  219. bool retVal = ssm->getValue(coeff.row(), coeff.col(), &target);
  220. ASSERT_TRUE(retVal);
  221. ASSERT_EQ(target, coeff.value());
  222. }
  223. delete ssm;
  224. }
  225. TEST(SparseMatrixTest, ConversionToSparseEigen_RowMajor_SparseMatrixTest) {
  226. int values[100];
  227. storm::storage::SparseMatrix<int> *ssm = new storm::storage::SparseMatrix<int>(10);
  228. for (uint_fast32_t i = 0; i < 100; ++i) {
  229. values[i] = i;
  230. }
  231. ASSERT_NO_THROW(ssm->initialize(100));
  232. ASSERT_EQ(ssm->getState(), storm::storage::SparseMatrix<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(), storm::storage::SparseMatrix<int>::MatrixStatus::Initialized);
  239. ASSERT_NO_THROW(ssm->finalize());
  240. ASSERT_EQ(ssm->getState(), storm::storage::SparseMatrix<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. }