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.

79 lines
2.4 KiB

  1. #include "gtest/gtest.h"
  2. #include "Eigen/Sparse"
  3. #include "src/exceptions/InvalidArgumentException.h"
  4. #include "boost/integer/integer_mask.hpp"
  5. TEST(EigenSparseMatrixTest, BasicReadWriteTest) {
  6. // 25 rows, 50 non zero entries
  7. Eigen::SparseMatrix<int, 1> esm(25, 25);
  8. int values[50] = {
  9. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
  10. 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
  11. 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
  12. 30, 31, 32, 33, 34, 35, 36, 37, 38, 39,
  13. 40, 41, 42, 43, 44, 45, 46, 47, 48, 49
  14. };
  15. int position_row[50] = {
  16. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  17. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  18. 2, 2, 2, 2, /* first row empty, one full row ��� 25 minus the diagonal entry */
  19. 4, 4, /* one empty row, then first and last column */
  20. 13, 13, 13, 13, /* a few empty rows, middle columns */
  21. 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
  22. 24, 24, 24, 24, 24, 24, 24, 24, 24, 24 /* second to last row */
  23. };
  24. int position_col[50] = {
  25. 1, 3, 4, 5, 6, 7, 8, 9, 10,
  26. 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
  27. 21, 22, 23, 24, 25, /* first row empty, one full row a 25 */
  28. 1, 25, /* one empty row, then first and last column */
  29. 16, 17, 18, 19, /* a few empty rows, middle columns */
  30. 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
  31. 14, 15, 16, 17, 18, 19, 20, 21, 22, 23 /* second to last row */
  32. };
  33. typedef Eigen::Triplet<double> ETd;
  34. std::vector<ETd> tripletList;
  35. tripletList.reserve(50);
  36. for (int i = 0; i < 50; ++i) {
  37. ASSERT_NO_THROW(tripletList.push_back(ETd(position_row[i] - 1, position_col[i] - 1, values[i])));
  38. }
  39. ASSERT_NO_THROW(esm.setFromTriplets(tripletList.begin(), tripletList.end()));
  40. for (int i = 0; i < 50; ++i) {
  41. Eigen::SparseMatrix<int, 1>::InnerIterator it(esm, position_row[i] - 1);
  42. ASSERT_EQ(values[i], esm.coeff(position_row[i] - 1, position_col[i] - 1));
  43. }
  44. // test for a few of the empty rows
  45. for (int row = 15; row < 24; ++row) {
  46. for (int col = 1; col <= 25; ++col) {
  47. ASSERT_EQ(0, esm.coeff(row - 1, col - 1));
  48. }
  49. }
  50. }
  51. TEST(EigenSparseMatrixTest, BoundaryTest) {
  52. Eigen::SparseMatrix<int, 1> esm(10, 10);
  53. esm.reserve(100);
  54. int values[100];
  55. for (uint_fast32_t i = 0; i < 100; ++i) {
  56. values[i] = i;
  57. }
  58. for (uint_fast32_t row = 0; row < 10; ++row) {
  59. for (uint_fast32_t col = 0; col < 10; ++col) {
  60. ASSERT_NO_THROW(esm.insert(row, col) = values[row * 10 + col]);
  61. }
  62. }
  63. for (uint_fast32_t row = 0; row < 10; ++row) {
  64. for (uint_fast32_t col = 0; col < 10; ++col) {
  65. ASSERT_EQ(values[row * 10 + col], esm.coeff(row, col));
  66. }
  67. }
  68. }