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.

96 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. }
  40. TEST(EigenAdapterTest, SimpleSparseSquareCopy) {
  41. // 5 rows
  42. storm::storage::SparseMatrix<double> sm(STORM_EIGENADAPTERTEST_SIMPLESPARSESQUARECOPY_SIZE);
  43. double values[STORM_EIGENADAPTERTEST_SIMPLESPARSESQUARECOPY_SIZE * STORM_EIGENADAPTERTEST_SIMPLESPARSESQUARECOPY_SIZE];
  44. sm.initialize((STORM_EIGENADAPTERTEST_SIMPLESPARSESQUARECOPY_SIZE * STORM_EIGENADAPTERTEST_SIMPLESPARSESQUARECOPY_SIZE + 1) / 2);
  45. int row = 0;
  46. int col = 0;
  47. bool everySecondElement = true;
  48. for (int i = 0; i < STORM_EIGENADAPTERTEST_SIMPLESPARSESQUARECOPY_SIZE * STORM_EIGENADAPTERTEST_SIMPLESPARSESQUARECOPY_SIZE; ++i) {
  49. values[i] = static_cast<double>(i + 1);
  50. if (everySecondElement) {
  51. sm.addNextValue(row, col, values[i]);
  52. }
  53. everySecondElement = !everySecondElement;
  54. ++col;
  55. if (col == STORM_EIGENADAPTERTEST_SIMPLESPARSESQUARECOPY_SIZE) {
  56. ++row;
  57. col = 0;
  58. }
  59. }
  60. sm.finalize();
  61. auto esm = storm::adapters::EigenAdapter::toEigenSparseMatrix(sm);
  62. ASSERT_EQ(esm->rows(), STORM_EIGENADAPTERTEST_SIMPLESPARSESQUARECOPY_SIZE);
  63. ASSERT_EQ(esm->cols(), STORM_EIGENADAPTERTEST_SIMPLESPARSESQUARECOPY_SIZE);
  64. ASSERT_EQ(esm->nonZeros(), (STORM_EIGENADAPTERTEST_SIMPLESPARSESQUARECOPY_SIZE * STORM_EIGENADAPTERTEST_SIMPLESPARSESQUARECOPY_SIZE + 1) / 2);
  65. row = 0;
  66. col = 0;
  67. everySecondElement = true;
  68. for (int i = 0; i < STORM_EIGENADAPTERTEST_SIMPLESPARSESQUARECOPY_SIZE * STORM_EIGENADAPTERTEST_SIMPLESPARSESQUARECOPY_SIZE; ++i) {
  69. if (everySecondElement) {
  70. ASSERT_EQ(values[i], esm->coeff(row, col));
  71. }
  72. everySecondElement = !everySecondElement;
  73. ++col;
  74. if (col == STORM_EIGENADAPTERTEST_SIMPLESPARSESQUARECOPY_SIZE) {
  75. ++row;
  76. col = 0;
  77. }
  78. }
  79. }