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