diff --git a/lib/fort.hpp b/lib/fort.hpp index 4cc02bf..cf2dc7c 100644 --- a/lib/fort.hpp +++ b/lib/fort.hpp @@ -1023,7 +1023,11 @@ public: }; /** - * Table column. + * Range of cells. + * + * @note: at the moment function of propery owener will work only on the + * top left cell. + * @todo: Implement their work on the whole range. */ class cell_range: public property_owner_t { @@ -1031,17 +1035,26 @@ public: using property_owner_t::ps_row_idx_; using property_owner_t::ps_table_; public: - table_column(std::size_t col_idx, table &tbl) - : property_owner_t(FT_ANY_ROW, col_idx, &tbl) {} + cell_range(size_t top_left_row, size_t top_left_col, + size_t bottom_right_row, size_t bottom_right_col, + table &tbl) + : property_owner_t(top_left_row, top_left_col, &tbl), + bottom_right_row_(bottom_right_row), + bottom_right_col_(bottom_right_col) + {} void erase() { if (FT_IS_ERROR(ft_erase_range(ps_table_->table_, - 0, ps_coll_idx_, - (UINT_MAX - 2), ps_coll_idx_))) { + ps_row_idx_, ps_coll_idx_, + bottom_right_row_, bottom_right_col_))) { throw std::runtime_error("Failed to erase column"); } } + + private: + std::size_t bottom_right_row_; + std::size_t bottom_right_col_; }; @@ -1164,6 +1177,23 @@ public: return table_column(col_idx, *this); } + /** + * Get range of cells. + * + * @param col_idx + * Column index. + * @return + * table_column object. + */ + class cell_range + range(std::size_t top_left_row, std::size_t top_left_col, + std::size_t bottom_right_row, std::size_t bottom_right_col) + { + return cell_range(top_left_row, top_left_col, + bottom_right_row, bottom_right_col, + *this); + } + static class default_properties default_props() { diff --git a/src/fort.hpp b/src/fort.hpp index 4cc02bf..cf2dc7c 100644 --- a/src/fort.hpp +++ b/src/fort.hpp @@ -1023,7 +1023,11 @@ public: }; /** - * Table column. + * Range of cells. + * + * @note: at the moment function of propery owener will work only on the + * top left cell. + * @todo: Implement their work on the whole range. */ class cell_range: public property_owner_t { @@ -1031,17 +1035,26 @@ public: using property_owner_t::ps_row_idx_; using property_owner_t::ps_table_; public: - table_column(std::size_t col_idx, table &tbl) - : property_owner_t(FT_ANY_ROW, col_idx, &tbl) {} + cell_range(size_t top_left_row, size_t top_left_col, + size_t bottom_right_row, size_t bottom_right_col, + table &tbl) + : property_owner_t(top_left_row, top_left_col, &tbl), + bottom_right_row_(bottom_right_row), + bottom_right_col_(bottom_right_col) + {} void erase() { if (FT_IS_ERROR(ft_erase_range(ps_table_->table_, - 0, ps_coll_idx_, - (UINT_MAX - 2), ps_coll_idx_))) { + ps_row_idx_, ps_coll_idx_, + bottom_right_row_, bottom_right_col_))) { throw std::runtime_error("Failed to erase column"); } } + + private: + std::size_t bottom_right_row_; + std::size_t bottom_right_col_; }; @@ -1164,6 +1177,23 @@ public: return table_column(col_idx, *this); } + /** + * Get range of cells. + * + * @param col_idx + * Column index. + * @return + * table_column object. + */ + class cell_range + range(std::size_t top_left_row, std::size_t top_left_col, + std::size_t bottom_right_row, std::size_t bottom_right_col) + { + return cell_range(top_left_row, top_left_col, + bottom_right_row, bottom_right_col, + *this); + } + static class default_properties default_props() { diff --git a/tests/bb_tests/test_table_basic.c b/tests/bb_tests/test_table_basic.c index 3424ccf..c8424ce 100644 --- a/tests/bb_tests/test_table_basic.c +++ b/tests/bb_tests/test_table_basic.c @@ -1825,7 +1825,7 @@ static struct ft_table *create_test_table() void test_table_erase(void) { - WHEN("Test invalid arguments") { + WHEN("Invalid arguments") { ft_table_t *table = create_test_table(); // invalid rows diff --git a/tests/bb_tests_cpp/test_table_basic.cpp b/tests/bb_tests_cpp/test_table_basic.cpp index f3091df..6f839a5 100644 --- a/tests/bb_tests_cpp/test_table_basic.cpp +++ b/tests/bb_tests_cpp/test_table_basic.cpp @@ -401,6 +401,31 @@ static fort::char_table create_test_table() void test_cpp_table_erase(void) { + WHEN("Invalid arguments") { + std::string err_msg; + try { + fort::char_table table = create_test_table(); + table.range(1, 1, 0, 0).erase(); + } catch (std::exception &e) { + err_msg = e.what(); + } + assert_string_equal(err_msg, std::string("Failed to erase column")); + } + + WHEN("Erase one cell") { + fort::char_table table = create_test_table(); + table.range(1, 1, 1, 1).erase(); + + std::string table_str = table.to_string(); + std::string table_str_etalon = + "+----+----+----+\n" + "| 00 | 01 | 02 |\n" + "| 10 | 12 | |\n" + "| 20 | 21 | 22 |\n" + "+----+----+----+\n"; + assert_string_equal(table_str, table_str_etalon); + } + WHEN("Erase row") { fort::char_table table = create_test_table(); table[1].erase(); @@ -414,51 +439,44 @@ void test_cpp_table_erase(void) assert_string_equal(table_str, table_str_etalon); } -// WHEN("Erase last row") { -// ft_table_t *table = create_test_table(); -// assert_true(FT_IS_SUCCESS(ft_erase_range(table, 2, 0, 2, 999))); + WHEN("Erase last row") { + fort::char_table table = create_test_table(); + table[2].erase(); -// const char *table_str = ft_to_string(table); -// assert_true(table_str != NULL); -// const char *table_str_etalon = -// "+----+----+----+\n" -// "| 00 | 01 | 02 |\n" -// "| 10 | 11 | 12 |\n" -// "+----+----+----+\n"; -// assert_str_equal(table_str, table_str_etalon); -// ft_destroy_table(table); -// } + std::string table_str = table.to_string(); + std::string table_str_etalon = + "+----+----+----+\n" + "| 00 | 01 | 02 |\n" + "| 10 | 11 | 12 |\n" + "+----+----+----+\n"; + assert_string_equal(table_str, table_str_etalon); + } -// WHEN("Erase column") { -// ft_table_t *table = create_test_table(); -// assert_true(FT_IS_SUCCESS(ft_erase_range(table, 0, 1, 999, 1))); + WHEN("Erase column") { + fort::char_table table = create_test_table(); + table.column(1).erase(); -// const char *table_str = ft_to_string(table); -// assert_true(table_str != NULL); -// const char *table_str_etalon = -// "+----+----+\n" -// "| 00 | 02 |\n" -// "| 10 | 12 |\n" -// "| 20 | 22 |\n" -// "+----+----+\n"; -// assert_str_equal(table_str, table_str_etalon); -// ft_destroy_table(table); -// } + std::string table_str = table.to_string(); + std::string table_str_etalon = + "+----+----+\n" + "| 00 | 02 |\n" + "| 10 | 12 |\n" + "| 20 | 22 |\n" + "+----+----+\n"; + assert_string_equal(table_str, table_str_etalon); + } -// WHEN("Erase last column") { -// ft_table_t *table = create_test_table(); -// assert_true(FT_IS_SUCCESS(ft_erase_range(table, 0, 2, 999, 2))); - -// const char *table_str = ft_to_string(table); -// assert_true(table_str != NULL); -// const char *table_str_etalon = -// "+----+----+\n" -// "| 00 | 01 |\n" -// "| 10 | 11 |\n" -// "| 20 | 21 |\n" -// "+----+----+\n"; -// assert_str_equal(table_str, table_str_etalon); -// ft_destroy_table(table); -// } + WHEN("Erase last column") { + fort::char_table table = create_test_table(); + table.column(2).erase(); + std::string table_str = table.to_string(); + std::string table_str_etalon = + "+----+----+\n" + "| 00 | 01 |\n" + "| 10 | 11 |\n" + "| 20 | 21 |\n" + "+----+----+\n"; + assert_string_equal(table_str, table_str_etalon); + } }