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.

111 lines
3.7 KiB

  1. #include "gtest/gtest.h"
  2. #include "gmm/gmm_matrix.h"
  3. #include "src/adapters/GmmxxAdapter.h"
  4. #include "src/exceptions/InvalidArgumentException.h"
  5. #include "boost/integer/integer_mask.hpp"
  6. #define STORM_GMMADAPTERTEST_SIMPLEDENSESQUARECOPY_SIZE 5
  7. #define STORM_GMMADAPTERTEST_SIMPLESPARSESQUARECOPY_SIZE 5
  8. double getValue(gmm::csr_matrix<double> const& gmmSparseMatrix, uint_fast64_t row, uint_fast64_t col) {
  9. uint_fast64_t rowStart = gmmSparseMatrix.jc.at(row);
  10. uint_fast64_t rowEnd = gmmSparseMatrix.jc.at(row + 1);
  11. while (rowStart < rowEnd) {
  12. if (gmmSparseMatrix.ir.at(rowStart) == col) {
  13. return gmmSparseMatrix.pr.at(rowStart);
  14. }
  15. if (gmmSparseMatrix.ir.at(rowStart) > col) {
  16. break;
  17. }
  18. ++rowStart;
  19. }
  20. return 0.0;
  21. }
  22. TEST(GmmAdapterTest, SimpleDenseSquareCopy) {
  23. // 5 rows
  24. storm::storage::SparseMatrix<double> sm(STORM_GMMADAPTERTEST_SIMPLEDENSESQUARECOPY_SIZE);
  25. double values[STORM_GMMADAPTERTEST_SIMPLEDENSESQUARECOPY_SIZE * STORM_GMMADAPTERTEST_SIMPLEDENSESQUARECOPY_SIZE];
  26. sm.initialize(STORM_GMMADAPTERTEST_SIMPLEDENSESQUARECOPY_SIZE * STORM_GMMADAPTERTEST_SIMPLEDENSESQUARECOPY_SIZE);
  27. int row = 0;
  28. int col = 0;
  29. for (int i = 0; i < STORM_GMMADAPTERTEST_SIMPLEDENSESQUARECOPY_SIZE * STORM_GMMADAPTERTEST_SIMPLEDENSESQUARECOPY_SIZE; ++i) {
  30. values[i] = static_cast<double>(i + 1);
  31. sm.addNextValue(row, col, values[i]);
  32. ++col;
  33. if (col == STORM_GMMADAPTERTEST_SIMPLEDENSESQUARECOPY_SIZE) {
  34. ++row;
  35. col = 0;
  36. }
  37. }
  38. sm.finalize();
  39. auto gsm = storm::adapters::GmmxxAdapter::toGmmxxSparseMatrix(sm);
  40. ASSERT_EQ(gsm->nrows(), STORM_GMMADAPTERTEST_SIMPLEDENSESQUARECOPY_SIZE);
  41. ASSERT_EQ(gsm->ncols(), STORM_GMMADAPTERTEST_SIMPLEDENSESQUARECOPY_SIZE);
  42. ASSERT_EQ(gmm::nnz(*gsm), STORM_GMMADAPTERTEST_SIMPLEDENSESQUARECOPY_SIZE * STORM_GMMADAPTERTEST_SIMPLEDENSESQUARECOPY_SIZE);
  43. row = 0;
  44. col = 0;
  45. for (int i = 0; i < STORM_GMMADAPTERTEST_SIMPLEDENSESQUARECOPY_SIZE * STORM_GMMADAPTERTEST_SIMPLEDENSESQUARECOPY_SIZE; ++i) {
  46. ASSERT_EQ(values[i], getValue(*gsm, row, col));
  47. ++col;
  48. if (col == STORM_GMMADAPTERTEST_SIMPLEDENSESQUARECOPY_SIZE) {
  49. ++row;
  50. col = 0;
  51. }
  52. }
  53. }
  54. TEST(GmmAdapterTest, SimpleSparseSquareCopy) {
  55. // 5 rows
  56. storm::storage::SparseMatrix<double> sm(STORM_GMMADAPTERTEST_SIMPLESPARSESQUARECOPY_SIZE);
  57. double values[STORM_GMMADAPTERTEST_SIMPLESPARSESQUARECOPY_SIZE * STORM_GMMADAPTERTEST_SIMPLESPARSESQUARECOPY_SIZE];
  58. sm.initialize((STORM_GMMADAPTERTEST_SIMPLESPARSESQUARECOPY_SIZE * STORM_GMMADAPTERTEST_SIMPLESPARSESQUARECOPY_SIZE + 1) / 2);
  59. int row = 0;
  60. int col = 0;
  61. bool everySecondElement = true;
  62. for (int i = 0; i < STORM_GMMADAPTERTEST_SIMPLESPARSESQUARECOPY_SIZE * STORM_GMMADAPTERTEST_SIMPLESPARSESQUARECOPY_SIZE; ++i) {
  63. values[i] = static_cast<double>(i + 1);
  64. if (everySecondElement) {
  65. sm.addNextValue(row, col, values[i]);
  66. }
  67. everySecondElement = !everySecondElement;
  68. ++col;
  69. if (col == STORM_GMMADAPTERTEST_SIMPLESPARSESQUARECOPY_SIZE) {
  70. ++row;
  71. col = 0;
  72. }
  73. }
  74. sm.finalize();
  75. auto gsm = storm::adapters::GmmxxAdapter::toGmmxxSparseMatrix(sm);
  76. ASSERT_EQ(gsm->nrows(), STORM_GMMADAPTERTEST_SIMPLESPARSESQUARECOPY_SIZE);
  77. ASSERT_EQ(gsm->ncols(), STORM_GMMADAPTERTEST_SIMPLESPARSESQUARECOPY_SIZE);
  78. ASSERT_EQ(gmm::nnz(*gsm), (STORM_GMMADAPTERTEST_SIMPLESPARSESQUARECOPY_SIZE * STORM_GMMADAPTERTEST_SIMPLESPARSESQUARECOPY_SIZE + 1) / 2);
  79. row = 0;
  80. col = 0;
  81. everySecondElement = true;
  82. for (int i = 0; i < STORM_GMMADAPTERTEST_SIMPLESPARSESQUARECOPY_SIZE * STORM_GMMADAPTERTEST_SIMPLESPARSESQUARECOPY_SIZE; ++i) {
  83. if (everySecondElement) {
  84. ASSERT_EQ(values[i], getValue(*gsm, row, col));
  85. }
  86. everySecondElement = !everySecondElement;
  87. ++col;
  88. if (col == STORM_GMMADAPTERTEST_SIMPLESPARSESQUARECOPY_SIZE) {
  89. ++row;
  90. col = 0;
  91. }
  92. }
  93. }