diff --git a/ChangeLog.md b/ChangeLog.md index fbbfd9e..7450683 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -2,7 +2,7 @@ ### API -- Add function `ft_delete_range` to delete range of the cells. +- Add function `ft_erase_range` to erase range of the cells. - Add functions to check if table is empty to the API. - `ft_ln` returns status of operation. - Add new table property `adding_strategy` (2 strategies available - replace(default) and insert). diff --git a/lib/fort.c b/lib/fort.c index 6e34a69..c145d67 100644 --- a/lib/fort.c +++ b/lib/fort.c @@ -2152,7 +2152,7 @@ f_row_t *split_row(f_row_t *row, size_t pos); // Delete range [left; right] of cells (both ends included) FT_INTERNAL -int ft_row_delete_range(f_row_t *row, size_t left, size_t right); +int ft_row_erase_range(f_row_t *row, size_t left, size_t right); FT_INTERNAL f_row_t *create_row_from_string(const char *str); @@ -2813,9 +2813,9 @@ size_t ft_row_count(const ft_table_t *table) return vector_size(table->rows); } -int ft_delete_range(ft_table_t *table, - size_t top_left_row, size_t top_left_col, - size_t bottom_right_row, size_t bottom_right_col) +int ft_erase_range(ft_table_t *table, + size_t top_left_row, size_t top_left_col, + size_t bottom_right_row, size_t bottom_right_col) { assert(table && table->rows); int status = FT_SUCCESS; @@ -2839,7 +2839,7 @@ int ft_delete_range(ft_table_t *table, size_t i = top_left_row; while (i < rows_n && i <= bottom_right_row) { row = VECTOR_AT(table->rows, i, f_row_t *); - status = ft_row_delete_range(row, top_left_col, bottom_right_col); + status = ft_row_erase_range(row, top_left_col, bottom_right_col); if (FT_IS_ERROR(status)) return status; ++i; @@ -5329,7 +5329,7 @@ f_row_t *split_row(f_row_t *row, size_t pos) } FT_INTERNAL -int ft_row_delete_range(f_row_t *row, size_t left, size_t right) +int ft_row_erase_range(f_row_t *row, size_t left, size_t right) { assert(row); size_t cols_n = vector_size(row->cells); diff --git a/lib/fort.h b/lib/fort.h index 06b8695..4d39e51 100644 --- a/lib/fort.h +++ b/lib/fort.h @@ -341,7 +341,7 @@ int ft_is_empty(const ft_table_t *table); size_t ft_row_count(const ft_table_t *table); /** - * Delete range of cells. + * Erase range of cells. * * Range of cells is determined by 2 points (top-left and bottom-right) (both * ends are included). @@ -360,9 +360,9 @@ size_t ft_row_count(const ft_table_t *table); * - 0 - Operation was successfully implemented * - (<0): In case of error */ -int ft_delete_range(ft_table_t *table, - size_t top_left_row, size_t top_left_col, - size_t bottom_right_row, size_t bottom_right_col); +int ft_erase_range(ft_table_t *table, + size_t top_left_row, size_t top_left_col, + size_t bottom_right_row, size_t bottom_right_col); #if defined(FT_CLANG_COMPILER) || defined(FT_GCC_COMPILER) diff --git a/lib/fort.hpp b/lib/fort.hpp index 2591814..4cc02bf 100644 --- a/lib/fort.hpp +++ b/lib/fort.hpp @@ -990,6 +990,15 @@ public: { return table_cell(ps_row_idx_, coll_idx, *ps_table_); } + + void erase() + { + if (FT_IS_ERROR(ft_erase_range(ps_table_->table_, + property_owner_t::ps_row_idx_, 0, + property_owner_t::ps_row_idx_, (UINT_MAX - 2)))) { + throw std::runtime_error("Failed to erase row"); + } + } }; /** @@ -997,11 +1006,45 @@ public: */ class table_column: public property_owner_t { + using property_owner_t::ps_coll_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) {} + + void erase() + { + if (FT_IS_ERROR(ft_erase_range(ps_table_->table_, + 0, ps_coll_idx_, + (UINT_MAX - 2), ps_coll_idx_))) { + throw std::runtime_error("Failed to erase column"); + } + } }; + /** + * Table column. + */ + class cell_range: public property_owner_t + { + using property_owner_t::ps_coll_idx_; + 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) {} + + void erase() + { + if (FT_IS_ERROR(ft_erase_range(ps_table_->table_, + 0, ps_coll_idx_, + (UINT_MAX - 2), ps_coll_idx_))) { + throw std::runtime_error("Failed to erase column"); + } + } + }; + + class default_properties: public property_owner_t { public: diff --git a/src/fort.h b/src/fort.h index 06b8695..4d39e51 100644 --- a/src/fort.h +++ b/src/fort.h @@ -341,7 +341,7 @@ int ft_is_empty(const ft_table_t *table); size_t ft_row_count(const ft_table_t *table); /** - * Delete range of cells. + * Erase range of cells. * * Range of cells is determined by 2 points (top-left and bottom-right) (both * ends are included). @@ -360,9 +360,9 @@ size_t ft_row_count(const ft_table_t *table); * - 0 - Operation was successfully implemented * - (<0): In case of error */ -int ft_delete_range(ft_table_t *table, - size_t top_left_row, size_t top_left_col, - size_t bottom_right_row, size_t bottom_right_col); +int ft_erase_range(ft_table_t *table, + size_t top_left_row, size_t top_left_col, + size_t bottom_right_row, size_t bottom_right_col); #if defined(FT_CLANG_COMPILER) || defined(FT_GCC_COMPILER) diff --git a/src/fort.hpp b/src/fort.hpp index 2591814..4cc02bf 100644 --- a/src/fort.hpp +++ b/src/fort.hpp @@ -990,6 +990,15 @@ public: { return table_cell(ps_row_idx_, coll_idx, *ps_table_); } + + void erase() + { + if (FT_IS_ERROR(ft_erase_range(ps_table_->table_, + property_owner_t::ps_row_idx_, 0, + property_owner_t::ps_row_idx_, (UINT_MAX - 2)))) { + throw std::runtime_error("Failed to erase row"); + } + } }; /** @@ -997,11 +1006,45 @@ public: */ class table_column: public property_owner_t { + using property_owner_t::ps_coll_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) {} + + void erase() + { + if (FT_IS_ERROR(ft_erase_range(ps_table_->table_, + 0, ps_coll_idx_, + (UINT_MAX - 2), ps_coll_idx_))) { + throw std::runtime_error("Failed to erase column"); + } + } }; + /** + * Table column. + */ + class cell_range: public property_owner_t + { + using property_owner_t::ps_coll_idx_; + 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) {} + + void erase() + { + if (FT_IS_ERROR(ft_erase_range(ps_table_->table_, + 0, ps_coll_idx_, + (UINT_MAX - 2), ps_coll_idx_))) { + throw std::runtime_error("Failed to erase column"); + } + } + }; + + class default_properties: public property_owner_t { public: diff --git a/src/fort_impl.c b/src/fort_impl.c index 8590c8f..d06447c 100644 --- a/src/fort_impl.c +++ b/src/fort_impl.c @@ -233,9 +233,9 @@ size_t ft_row_count(const ft_table_t *table) return vector_size(table->rows); } -int ft_delete_range(ft_table_t *table, - size_t top_left_row, size_t top_left_col, - size_t bottom_right_row, size_t bottom_right_col) +int ft_erase_range(ft_table_t *table, + size_t top_left_row, size_t top_left_col, + size_t bottom_right_row, size_t bottom_right_col) { assert(table && table->rows); int status = FT_SUCCESS; @@ -259,7 +259,7 @@ int ft_delete_range(ft_table_t *table, size_t i = top_left_row; while (i < rows_n && i <= bottom_right_row) { row = VECTOR_AT(table->rows, i, f_row_t *); - status = ft_row_delete_range(row, top_left_col, bottom_right_col); + status = ft_row_erase_range(row, top_left_col, bottom_right_col); if (FT_IS_ERROR(status)) return status; ++i; diff --git a/src/row.c b/src/row.c index da67664..64ca37b 100644 --- a/src/row.c +++ b/src/row.c @@ -99,7 +99,7 @@ f_row_t *split_row(f_row_t *row, size_t pos) } FT_INTERNAL -int ft_row_delete_range(f_row_t *row, size_t left, size_t right) +int ft_row_erase_range(f_row_t *row, size_t left, size_t right) { assert(row); size_t cols_n = vector_size(row->cells); diff --git a/src/row.h b/src/row.h index ae68a15..2402fff 100644 --- a/src/row.h +++ b/src/row.h @@ -23,7 +23,7 @@ f_row_t *split_row(f_row_t *row, size_t pos); // Delete range [left; right] of cells (both ends included) FT_INTERNAL -int ft_row_delete_range(f_row_t *row, size_t left, size_t right); +int ft_row_erase_range(f_row_t *row, size_t left, size_t right); FT_INTERNAL f_row_t *create_row_from_string(const char *str); diff --git a/tests/bb_tests/test_table_basic.c b/tests/bb_tests/test_table_basic.c index e68c562..3424ccf 100644 --- a/tests/bb_tests/test_table_basic.c +++ b/tests/bb_tests/test_table_basic.c @@ -1823,23 +1823,23 @@ static struct ft_table *create_test_table() return table; } -void test_table_cell_deletion(void) +void test_table_erase(void) { WHEN("Test invalid arguments") { ft_table_t *table = create_test_table(); // invalid rows - assert_true(ft_delete_range(table, 1, 1, 0, 2) == FT_EINVAL); + assert_true(ft_erase_range(table, 1, 1, 0, 2) == FT_EINVAL); // invalid colums - assert_true(ft_delete_range(table, 1, 1, 2, 0) == FT_EINVAL); + assert_true(ft_erase_range(table, 1, 1, 2, 0) == FT_EINVAL); ft_destroy_table(table); } - WHEN("Delete one cell") { + WHEN("Erase one cell") { ft_table_t *table = create_test_table(); - assert_true(FT_IS_SUCCESS(ft_delete_range(table, 1, 1, 1, 1))); + assert_true(FT_IS_SUCCESS(ft_erase_range(table, 1, 1, 1, 1))); const char *table_str = ft_to_string(table); assert_true(table_str != NULL); @@ -1853,10 +1853,10 @@ void test_table_cell_deletion(void) ft_destroy_table(table); } - WHEN("Delete one last cell") { + WHEN("Erase one last cell") { ft_table_t *table = create_test_table(); ft_write_ln(table, "30"); - assert_true(FT_IS_SUCCESS(ft_delete_range(table, 3, 0, 3, 0))); + assert_true(FT_IS_SUCCESS(ft_erase_range(table, 3, 0, 3, 0))); const char *table_str = ft_to_string(table); assert_true(table_str != NULL); @@ -1870,9 +1870,9 @@ void test_table_cell_deletion(void) ft_destroy_table(table); } - WHEN("Delete row") { + WHEN("Erase row") { ft_table_t *table = create_test_table(); - assert_true(FT_IS_SUCCESS(ft_delete_range(table, 1, 0, 1, 999))); + assert_true(FT_IS_SUCCESS(ft_erase_range(table, 1, 0, 1, 999))); const char *table_str = ft_to_string(table); assert_true(table_str != NULL); @@ -1885,9 +1885,9 @@ void test_table_cell_deletion(void) ft_destroy_table(table); } - WHEN("Delete last row") { + WHEN("Erase last row") { ft_table_t *table = create_test_table(); - assert_true(FT_IS_SUCCESS(ft_delete_range(table, 2, 0, 2, 999))); + assert_true(FT_IS_SUCCESS(ft_erase_range(table, 2, 0, 2, 999))); const char *table_str = ft_to_string(table); assert_true(table_str != NULL); @@ -1900,9 +1900,9 @@ void test_table_cell_deletion(void) ft_destroy_table(table); } - WHEN("Delete column") { + WHEN("Erase column") { ft_table_t *table = create_test_table(); - assert_true(FT_IS_SUCCESS(ft_delete_range(table, 0, 1, 999, 1))); + assert_true(FT_IS_SUCCESS(ft_erase_range(table, 0, 1, 999, 1))); const char *table_str = ft_to_string(table); assert_true(table_str != NULL); @@ -1916,9 +1916,9 @@ void test_table_cell_deletion(void) ft_destroy_table(table); } - WHEN("Delete last column") { + WHEN("Erase last column") { ft_table_t *table = create_test_table(); - assert_true(FT_IS_SUCCESS(ft_delete_range(table, 0, 2, 999, 2))); + 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); diff --git a/tests/bb_tests_cpp/test_table_basic.cpp b/tests/bb_tests_cpp/test_table_basic.cpp index baf180e..f3091df 100644 --- a/tests/bb_tests_cpp/test_table_basic.cpp +++ b/tests/bb_tests_cpp/test_table_basic.cpp @@ -388,3 +388,77 @@ void test_cpp_table_changing_cell(void) assert_string_equal(table_str, table_str_etalon); } } + +static fort::char_table create_test_table() +{ + fort::char_table table; + table.write_ln("00", "01", "02"); + table.write_ln("10", "11", "12"); + table.write_ln("20", "21", "22"); + + return table; +} + +void test_cpp_table_erase(void) +{ + WHEN("Erase row") { + fort::char_table table = create_test_table(); + table[1].erase(); + + std::string table_str = table.to_string(); + std::string table_str_etalon = + "+----+----+----+\n" + "| 00 | 01 | 02 |\n" + "| 20 | 21 | 22 |\n" + "+----+----+----+\n"; + 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))); + +// 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); +// } + +// WHEN("Erase column") { +// ft_table_t *table = create_test_table(); +// assert_true(FT_IS_SUCCESS(ft_erase_range(table, 0, 1, 999, 1))); + +// 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); +// } + +// 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); +// } + +} diff --git a/tests/main_test.c b/tests/main_test.c index fa93034..6ea2a1a 100644 --- a/tests/main_test.c +++ b/tests/main_test.c @@ -12,7 +12,7 @@ void test_table_geometry(void); void test_table_basic(void); void test_table_copy(void); void test_table_changing_cell(void); -void test_table_cell_deletion(void); +void test_table_erase(void); #ifdef FT_HAVE_WCHAR void test_wcs_table_boundaries(void); #endif @@ -53,7 +53,7 @@ struct test_case bb_test_suite [] = { {"test_table_write", test_table_write}, {"test_table_insert_strategy", test_table_insert_strategy}, {"test_table_changing_cell", test_table_changing_cell}, - {"test_table_cell_deletion", test_table_cell_deletion}, + {"test_table_erase", test_table_erase}, {"test_table_border_style", test_table_border_style}, {"test_table_builtin_border_styles", test_table_builtin_border_styles}, {"test_table_cell_properties", test_table_cell_properties}, diff --git a/tests/main_test_cpp.cpp b/tests/main_test_cpp.cpp index 7c92e83..d9c8d0e 100644 --- a/tests/main_test_cpp.cpp +++ b/tests/main_test_cpp.cpp @@ -6,6 +6,7 @@ void test_cpp_table_basic(void); void test_cpp_table_write(void); void test_cpp_table_insert(void); +void test_cpp_table_erase(void); void test_cpp_table_changing_cell(void); void test_cpp_table_tbl_properties(void); void test_cpp_table_cell_properties(void); @@ -17,6 +18,7 @@ struct test_case bb_test_suite [] = { {"test_cpp_table_basic", test_cpp_table_basic}, {"test_cpp_table_write", test_cpp_table_write}, {"test_cpp_table_insert", test_cpp_table_insert}, + {"test_cpp_table_erase", test_cpp_table_erase}, {"test_cpp_table_changing_cell", test_cpp_table_changing_cell}, {"test_cpp_table_tbl_properties", test_cpp_table_tbl_properties}, {"test_cpp_table_cell_properties", test_cpp_table_cell_properties},