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.

104 lines
3.8 KiB

  1. #include "gtest/gtest.h"
  2. #include "gmm/gmm_matrix.h"
  3. #include "src/adapters/StormAdapter.h"
  4. #include "src/exceptions/InvalidArgumentException.h"
  5. #include "boost/integer/integer_mask.hpp"
  6. #define STORM_STORMADAPTERTEST_SIMPLEDENSESQUARECOPY_SIZE 5
  7. #define STORM_STORMADAPTERTEST_SIMPLESPARSESQUARECOPY_SIZE 5
  8. TEST(StormAdapterTest, SimpleDenseSquareCopy) {
  9. // 5 rows
  10. gmm::csr_matrix<double> gmmSparseMatrix;
  11. /*
  12. * As CSR_Matrix is read-only we have to prepare the data in this row_matrix and then do a copy.
  13. */
  14. gmm::row_matrix<gmm::wsvector<double>> gmmPreMatrix(STORM_STORMADAPTERTEST_SIMPLEDENSESQUARECOPY_SIZE, STORM_STORMADAPTERTEST_SIMPLEDENSESQUARECOPY_SIZE);
  15. double values[STORM_STORMADAPTERTEST_SIMPLEDENSESQUARECOPY_SIZE * STORM_STORMADAPTERTEST_SIMPLEDENSESQUARECOPY_SIZE];
  16. int row = 0;
  17. int col = 0;
  18. for (int i = 0; i < STORM_STORMADAPTERTEST_SIMPLEDENSESQUARECOPY_SIZE * STORM_STORMADAPTERTEST_SIMPLEDENSESQUARECOPY_SIZE; ++i) {
  19. values[i] = static_cast<double>(i + 1);
  20. gmmPreMatrix(row, col) = values[i];
  21. ++col;
  22. if (col == STORM_STORMADAPTERTEST_SIMPLEDENSESQUARECOPY_SIZE) {
  23. ++row;
  24. col = 0;
  25. }
  26. }
  27. gmm::copy(gmmPreMatrix, gmmSparseMatrix);
  28. auto stormSparseMatrix = storm::adapters::StormAdapter::toStormSparseMatrix(gmmSparseMatrix);
  29. ASSERT_EQ(stormSparseMatrix->getRowCount(), STORM_STORMADAPTERTEST_SIMPLEDENSESQUARECOPY_SIZE);
  30. ASSERT_EQ(stormSparseMatrix->getColumnCount(), STORM_STORMADAPTERTEST_SIMPLEDENSESQUARECOPY_SIZE);
  31. ASSERT_EQ(stormSparseMatrix->getNonZeroEntryCount(), STORM_STORMADAPTERTEST_SIMPLEDENSESQUARECOPY_SIZE * STORM_STORMADAPTERTEST_SIMPLEDENSESQUARECOPY_SIZE);
  32. row = 0;
  33. col = 0;
  34. for (int i = 0; i < STORM_STORMADAPTERTEST_SIMPLEDENSESQUARECOPY_SIZE * STORM_STORMADAPTERTEST_SIMPLEDENSESQUARECOPY_SIZE; ++i) {
  35. ASSERT_EQ(values[i], stormSparseMatrix->getValue(row, col));
  36. ++col;
  37. if (col == STORM_STORMADAPTERTEST_SIMPLEDENSESQUARECOPY_SIZE) {
  38. ++row;
  39. col = 0;
  40. }
  41. }
  42. }
  43. TEST(StormAdapterTest, SimpleSparseSquareCopy) {
  44. // 5 rows
  45. gmm::csr_matrix<double> gmmSparseMatrix;
  46. /*
  47. * As CSR_Matrix is read-only we have to prepare the data in this row_matrix and then do a copy.
  48. */
  49. gmm::row_matrix<gmm::wsvector<double>> gmmPreMatrix(STORM_STORMADAPTERTEST_SIMPLESPARSESQUARECOPY_SIZE, STORM_STORMADAPTERTEST_SIMPLESPARSESQUARECOPY_SIZE);
  50. double values[STORM_STORMADAPTERTEST_SIMPLESPARSESQUARECOPY_SIZE * STORM_STORMADAPTERTEST_SIMPLESPARSESQUARECOPY_SIZE];
  51. int row = 0;
  52. int col = 0;
  53. bool everySecondElement = true;
  54. for (int i = 0; i < STORM_STORMADAPTERTEST_SIMPLESPARSESQUARECOPY_SIZE * STORM_STORMADAPTERTEST_SIMPLESPARSESQUARECOPY_SIZE; ++i) {
  55. values[i] = static_cast<double>(i + 1);
  56. if (everySecondElement) {
  57. gmmPreMatrix(row, col) = values[i];
  58. }
  59. everySecondElement = !everySecondElement;
  60. ++col;
  61. if (col == STORM_STORMADAPTERTEST_SIMPLESPARSESQUARECOPY_SIZE) {
  62. ++row;
  63. col = 0;
  64. }
  65. }
  66. gmm::copy(gmmPreMatrix, gmmSparseMatrix);
  67. auto stormSparseMatrix = storm::adapters::StormAdapter::toStormSparseMatrix(gmmSparseMatrix);
  68. ASSERT_EQ(stormSparseMatrix->getRowCount(), STORM_STORMADAPTERTEST_SIMPLESPARSESQUARECOPY_SIZE);
  69. ASSERT_EQ(stormSparseMatrix->getColumnCount(), STORM_STORMADAPTERTEST_SIMPLESPARSESQUARECOPY_SIZE);
  70. ASSERT_EQ(stormSparseMatrix->getNonZeroEntryCount(), (STORM_STORMADAPTERTEST_SIMPLESPARSESQUARECOPY_SIZE * STORM_STORMADAPTERTEST_SIMPLESPARSESQUARECOPY_SIZE + 1) / 2);
  71. row = 0;
  72. col = 0;
  73. everySecondElement = true;
  74. for (int i = 0; i < STORM_STORMADAPTERTEST_SIMPLESPARSESQUARECOPY_SIZE * STORM_STORMADAPTERTEST_SIMPLESPARSESQUARECOPY_SIZE; ++i) {
  75. if (everySecondElement) {
  76. ASSERT_EQ(values[i], stormSparseMatrix->getValue(row, col));
  77. }
  78. everySecondElement = !everySecondElement;
  79. ++col;
  80. if (col == STORM_STORMADAPTERTEST_SIMPLESPARSESQUARECOPY_SIZE) {
  81. ++row;
  82. col = 0;
  83. }
  84. }
  85. }