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.

108 lines
3.3 KiB

  1. #include "gtest/gtest.h"
  2. #include "src/sparse/static_sparse_matrix.h"
  3. #include "src/exceptions/invalid_argument.h"
  4. TEST(StaticSparseMatrixTest, ZeroRowsTest) {
  5. mrmc::sparse::StaticSparseMatrix<int> *ssm = new mrmc::sparse::StaticSparseMatrix<int>(0, 50);
  6. ASSERT_THROW(ssm->initialize(), mrmc::exceptions::invalid_argument);
  7. delete ssm;
  8. }
  9. TEST(StaticSparseMatrixTest, TooManyEntriesTest) {
  10. mrmc::sparse::StaticSparseMatrix<int> *ssm = new mrmc::sparse::StaticSparseMatrix<int>(2, 10);
  11. ASSERT_THROW(ssm->initialize(), mrmc::exceptions::invalid_argument);
  12. delete ssm;
  13. }
  14. TEST(StaticSparseMatrixTest, addNextValueTest) {
  15. mrmc::sparse::StaticSparseMatrix<int> *ssm = new mrmc::sparse::StaticSparseMatrix<int>(5, 1);
  16. ASSERT_NO_THROW(ssm->initialize());
  17. ASSERT_THROW(ssm->addNextValue(0, 1, 1), mrmc::exceptions::out_of_range);
  18. ASSERT_THROW(ssm->addNextValue(1, 0, 1), mrmc::exceptions::out_of_range);
  19. ASSERT_THROW(ssm->addNextValue(6, 1, 1), mrmc::exceptions::out_of_range);
  20. ASSERT_THROW(ssm->addNextValue(1, 6, 1), mrmc::exceptions::out_of_range);
  21. delete ssm;
  22. }
  23. TEST(StaticSparseMatrixTest, finalizeTest) {
  24. mrmc::sparse::StaticSparseMatrix<int> *ssm = new mrmc::sparse::StaticSparseMatrix<int>(5, 5);
  25. ASSERT_NO_THROW(ssm->initialize());
  26. ASSERT_NO_THROW(ssm->addNextValue(1, 2, 1));
  27. ASSERT_NO_THROW(ssm->addNextValue(1, 3, 1));
  28. ASSERT_NO_THROW(ssm->addNextValue(1, 4, 1));
  29. ASSERT_NO_THROW(ssm->addNextValue(1, 5, 1));
  30. ASSERT_THROW(ssm->finalize(), mrmc::exceptions::invalid_state);
  31. delete ssm;
  32. }
  33. TEST(StaticSparseMatrixTest, Test) {
  34. // 25 rows, 50 non zero entries
  35. mrmc::sparse::StaticSparseMatrix<int> *ssm = new mrmc::sparse::StaticSparseMatrix<int>(25, 50);
  36. int values[50] = {
  37. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
  38. 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
  39. 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
  40. 30, 31, 32, 33, 34, 35, 36, 37, 38, 39,
  41. 40, 41, 42, 43, 44, 45, 46, 47, 48, 49
  42. };
  43. int position_row[50] = {
  44. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  45. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  46. 2, 2, 2, 2, /* first row empty, one full row � 25 minus the diagonal entry */
  47. 4, 4, /* one empty row, then first and last column */
  48. 13, 13, 13, 13, /* a few empty rows, middle columns */
  49. 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
  50. 24, 24, 24, 24, 24, 24, 24, 24, 24, 24 /* second to last row */
  51. };
  52. int position_col[50] = {
  53. 1, 3, 4, 5, 6, 7, 8, 9, 10,
  54. 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
  55. 21, 22, 23, 24, 25, /* first row empty, one full row a 25 */
  56. 1, 25, /* one empty row, then first and last column */
  57. 16, 17, 18, 19, /* a few empty rows, middle columns */
  58. 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
  59. 14, 15, 16, 17, 18, 19, 20, 21, 22, 23 /* second to last row */
  60. };
  61. ASSERT_NO_THROW(ssm->initialize());
  62. for (int i = 0; i < 50; ++i) {
  63. ASSERT_NO_THROW(ssm->addNextValue(position_row[i], position_col[i], values[i]));
  64. }
  65. ASSERT_NO_THROW(ssm->finalize());
  66. int target;
  67. for (int i = 0; i < 50; ++i) {
  68. ASSERT_TRUE(ssm->getValue(position_row[i], position_col[i], &target));
  69. ASSERT_EQ(values[i], target);
  70. }
  71. // test for a few of the empty rows
  72. for (int row = 15; row < 24; ++row) {
  73. for (int col = 1; col <= 25; ++col) {
  74. target = 1;
  75. if (row != col) {
  76. ASSERT_FALSE(ssm->getValue(row, col, &target));
  77. } else {
  78. ASSERT_TRUE(ssm->getValue(row, col, &target));
  79. }
  80. ASSERT_EQ(0, target);
  81. }
  82. }
  83. delete ssm;
  84. }