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.

100 lines
3.3 KiB

  1. #include "gtest/gtest.h"
  2. #include "Eigen/Sparse"
  3. #include "src/adapters/EigenAdapter.h"
  4. #include "src/exceptions/InvalidArgumentException.h"
  5. #include "boost/integer/integer_mask.hpp"
  6. #define STORM_EIGENADAPTERTEST_SIMPLEDENSESQUARECOPY_SIZE 5
  7. #define STORM_EIGENADAPTERTEST_SIMPLESPARSESQUARECOPY_SIZE 5
  8. TEST(EigenAdapterTest, SimpleDenseSquareCopy) {
  9. // 5 rows
  10. storm::storage::SparseMatrix<double> sm(STORM_EIGENADAPTERTEST_SIMPLEDENSESQUARECOPY_SIZE);
  11. double values[STORM_EIGENADAPTERTEST_SIMPLEDENSESQUARECOPY_SIZE * STORM_EIGENADAPTERTEST_SIMPLEDENSESQUARECOPY_SIZE];
  12. sm.initialize(STORM_EIGENADAPTERTEST_SIMPLEDENSESQUARECOPY_SIZE * STORM_EIGENADAPTERTEST_SIMPLEDENSESQUARECOPY_SIZE);
  13. int row = 0;
  14. int col = 0;
  15. for (int i = 0; i < STORM_EIGENADAPTERTEST_SIMPLEDENSESQUARECOPY_SIZE * STORM_EIGENADAPTERTEST_SIMPLEDENSESQUARECOPY_SIZE; ++i) {
  16. values[i] = static_cast<double>(i + 1);
  17. sm.addNextValue(row, col, values[i]);
  18. ++col;
  19. if (col == STORM_EIGENADAPTERTEST_SIMPLEDENSESQUARECOPY_SIZE) {
  20. ++row;
  21. col = 0;
  22. }
  23. }
  24. sm.finalize();
  25. auto esm = storm::adapters::EigenAdapter::toEigenSparseMatrix(sm);
  26. ASSERT_EQ(esm->rows(), STORM_EIGENADAPTERTEST_SIMPLEDENSESQUARECOPY_SIZE);
  27. ASSERT_EQ(esm->cols(), STORM_EIGENADAPTERTEST_SIMPLEDENSESQUARECOPY_SIZE);
  28. ASSERT_EQ(esm->nonZeros(), STORM_EIGENADAPTERTEST_SIMPLEDENSESQUARECOPY_SIZE * STORM_EIGENADAPTERTEST_SIMPLEDENSESQUARECOPY_SIZE);
  29. row = 0;
  30. col = 0;
  31. for (int i = 0; i < STORM_EIGENADAPTERTEST_SIMPLEDENSESQUARECOPY_SIZE * STORM_EIGENADAPTERTEST_SIMPLEDENSESQUARECOPY_SIZE; ++i) {
  32. ASSERT_EQ(values[i], esm->coeff(row, col));
  33. ++col;
  34. if (col == STORM_EIGENADAPTERTEST_SIMPLEDENSESQUARECOPY_SIZE) {
  35. ++row;
  36. col = 0;
  37. }
  38. }
  39. delete esm;
  40. }
  41. TEST(EigenAdapterTest, SimpleSparseSquareCopy) {
  42. // 5 rows
  43. storm::storage::SparseMatrix<double> sm(STORM_EIGENADAPTERTEST_SIMPLESPARSESQUARECOPY_SIZE);
  44. double values[STORM_EIGENADAPTERTEST_SIMPLESPARSESQUARECOPY_SIZE * STORM_EIGENADAPTERTEST_SIMPLESPARSESQUARECOPY_SIZE];
  45. sm.initialize((STORM_EIGENADAPTERTEST_SIMPLESPARSESQUARECOPY_SIZE * STORM_EIGENADAPTERTEST_SIMPLESPARSESQUARECOPY_SIZE + 1) / 2);
  46. int row = 0;
  47. int col = 0;
  48. bool everySecondElement = true;
  49. for (int i = 0; i < STORM_EIGENADAPTERTEST_SIMPLESPARSESQUARECOPY_SIZE * STORM_EIGENADAPTERTEST_SIMPLESPARSESQUARECOPY_SIZE; ++i) {
  50. values[i] = static_cast<double>(i + 1);
  51. if (everySecondElement) {
  52. sm.addNextValue(row, col, values[i]);
  53. }
  54. everySecondElement = !everySecondElement;
  55. ++col;
  56. if (col == STORM_EIGENADAPTERTEST_SIMPLESPARSESQUARECOPY_SIZE) {
  57. ++row;
  58. col = 0;
  59. }
  60. }
  61. sm.finalize();
  62. auto esm = storm::adapters::EigenAdapter::toEigenSparseMatrix(sm);
  63. ASSERT_EQ(esm->rows(), STORM_EIGENADAPTERTEST_SIMPLESPARSESQUARECOPY_SIZE);
  64. ASSERT_EQ(esm->cols(), STORM_EIGENADAPTERTEST_SIMPLESPARSESQUARECOPY_SIZE);
  65. ASSERT_EQ(esm->nonZeros(), (STORM_EIGENADAPTERTEST_SIMPLESPARSESQUARECOPY_SIZE * STORM_EIGENADAPTERTEST_SIMPLESPARSESQUARECOPY_SIZE + 1) / 2);
  66. row = 0;
  67. col = 0;
  68. everySecondElement = true;
  69. for (int i = 0; i < STORM_EIGENADAPTERTEST_SIMPLESPARSESQUARECOPY_SIZE * STORM_EIGENADAPTERTEST_SIMPLESPARSESQUARECOPY_SIZE; ++i) {
  70. if (everySecondElement) {
  71. ASSERT_EQ(values[i], esm->coeff(row, col));
  72. }
  73. everySecondElement = !everySecondElement;
  74. ++col;
  75. if (col == STORM_EIGENADAPTERTEST_SIMPLESPARSESQUARECOPY_SIZE) {
  76. ++row;
  77. col = 0;
  78. }
  79. }
  80. delete esm;
  81. }