[C] Rename ft_delete_range to ft_erase_range

This commit is contained in:
seleznevae 2020-01-19 22:06:25 +03:00
parent 4e922a7eca
commit 1ed13718d5
13 changed files with 200 additions and 38 deletions

View File

@ -2,7 +2,7 @@
### API ### 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. - Add functions to check if table is empty to the API.
- `ft_ln` returns status of operation. - `ft_ln` returns status of operation.
- Add new table property `adding_strategy` (2 strategies available - replace(default) and insert). - Add new table property `adding_strategy` (2 strategies available - replace(default) and insert).

View File

@ -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) // Delete range [left; right] of cells (both ends included)
FT_INTERNAL 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 FT_INTERNAL
f_row_t *create_row_from_string(const char *str); f_row_t *create_row_from_string(const char *str);
@ -2813,7 +2813,7 @@ size_t ft_row_count(const ft_table_t *table)
return vector_size(table->rows); return vector_size(table->rows);
} }
int ft_delete_range(ft_table_t *table, int ft_erase_range(ft_table_t *table,
size_t top_left_row, size_t top_left_col, size_t top_left_row, size_t top_left_col,
size_t bottom_right_row, size_t bottom_right_col) size_t bottom_right_row, size_t bottom_right_col)
{ {
@ -2839,7 +2839,7 @@ int ft_delete_range(ft_table_t *table,
size_t i = top_left_row; size_t i = top_left_row;
while (i < rows_n && i <= bottom_right_row) { while (i < rows_n && i <= bottom_right_row) {
row = VECTOR_AT(table->rows, i, f_row_t *); 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)) if (FT_IS_ERROR(status))
return status; return status;
++i; ++i;
@ -5329,7 +5329,7 @@ f_row_t *split_row(f_row_t *row, size_t pos)
} }
FT_INTERNAL 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); assert(row);
size_t cols_n = vector_size(row->cells); size_t cols_n = vector_size(row->cells);

View File

@ -341,7 +341,7 @@ int ft_is_empty(const ft_table_t *table);
size_t ft_row_count(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 * Range of cells is determined by 2 points (top-left and bottom-right) (both
* ends are included). * ends are included).
@ -360,7 +360,7 @@ size_t ft_row_count(const ft_table_t *table);
* - 0 - Operation was successfully implemented * - 0 - Operation was successfully implemented
* - (<0): In case of error * - (<0): In case of error
*/ */
int ft_delete_range(ft_table_t *table, int ft_erase_range(ft_table_t *table,
size_t top_left_row, size_t top_left_col, size_t top_left_row, size_t top_left_col,
size_t bottom_right_row, size_t bottom_right_col); size_t bottom_right_row, size_t bottom_right_col);

View File

@ -990,6 +990,15 @@ public:
{ {
return table_cell(ps_row_idx_, coll_idx, *ps_table_); 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 class table_column: public property_owner_t
{ {
using property_owner_t::ps_coll_idx_;
using property_owner_t::ps_table_;
public: public:
table_column(std::size_t col_idx, table &tbl) table_column(std::size_t col_idx, table &tbl)
: property_owner_t(FT_ANY_ROW, col_idx, &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 class default_properties: public property_owner_t
{ {
public: public:

View File

@ -341,7 +341,7 @@ int ft_is_empty(const ft_table_t *table);
size_t ft_row_count(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 * Range of cells is determined by 2 points (top-left and bottom-right) (both
* ends are included). * ends are included).
@ -360,7 +360,7 @@ size_t ft_row_count(const ft_table_t *table);
* - 0 - Operation was successfully implemented * - 0 - Operation was successfully implemented
* - (<0): In case of error * - (<0): In case of error
*/ */
int ft_delete_range(ft_table_t *table, int ft_erase_range(ft_table_t *table,
size_t top_left_row, size_t top_left_col, size_t top_left_row, size_t top_left_col,
size_t bottom_right_row, size_t bottom_right_col); size_t bottom_right_row, size_t bottom_right_col);

View File

@ -990,6 +990,15 @@ public:
{ {
return table_cell(ps_row_idx_, coll_idx, *ps_table_); 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 class table_column: public property_owner_t
{ {
using property_owner_t::ps_coll_idx_;
using property_owner_t::ps_table_;
public: public:
table_column(std::size_t col_idx, table &tbl) table_column(std::size_t col_idx, table &tbl)
: property_owner_t(FT_ANY_ROW, col_idx, &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 class default_properties: public property_owner_t
{ {
public: public:

View File

@ -233,7 +233,7 @@ size_t ft_row_count(const ft_table_t *table)
return vector_size(table->rows); return vector_size(table->rows);
} }
int ft_delete_range(ft_table_t *table, int ft_erase_range(ft_table_t *table,
size_t top_left_row, size_t top_left_col, size_t top_left_row, size_t top_left_col,
size_t bottom_right_row, size_t bottom_right_col) size_t bottom_right_row, size_t bottom_right_col)
{ {
@ -259,7 +259,7 @@ int ft_delete_range(ft_table_t *table,
size_t i = top_left_row; size_t i = top_left_row;
while (i < rows_n && i <= bottom_right_row) { while (i < rows_n && i <= bottom_right_row) {
row = VECTOR_AT(table->rows, i, f_row_t *); 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)) if (FT_IS_ERROR(status))
return status; return status;
++i; ++i;

View File

@ -99,7 +99,7 @@ f_row_t *split_row(f_row_t *row, size_t pos)
} }
FT_INTERNAL 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); assert(row);
size_t cols_n = vector_size(row->cells); size_t cols_n = vector_size(row->cells);

View File

@ -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) // Delete range [left; right] of cells (both ends included)
FT_INTERNAL 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 FT_INTERNAL
f_row_t *create_row_from_string(const char *str); f_row_t *create_row_from_string(const char *str);

View File

@ -1823,23 +1823,23 @@ static struct ft_table *create_test_table()
return table; return table;
} }
void test_table_cell_deletion(void) void test_table_erase(void)
{ {
WHEN("Test invalid arguments") { WHEN("Test invalid arguments") {
ft_table_t *table = create_test_table(); ft_table_t *table = create_test_table();
// invalid rows // 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 // 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); ft_destroy_table(table);
} }
WHEN("Delete one cell") { WHEN("Erase one cell") {
ft_table_t *table = create_test_table(); 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); const char *table_str = ft_to_string(table);
assert_true(table_str != NULL); assert_true(table_str != NULL);
@ -1853,10 +1853,10 @@ void test_table_cell_deletion(void)
ft_destroy_table(table); ft_destroy_table(table);
} }
WHEN("Delete one last cell") { WHEN("Erase one last cell") {
ft_table_t *table = create_test_table(); ft_table_t *table = create_test_table();
ft_write_ln(table, "30"); 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); const char *table_str = ft_to_string(table);
assert_true(table_str != NULL); assert_true(table_str != NULL);
@ -1870,9 +1870,9 @@ void test_table_cell_deletion(void)
ft_destroy_table(table); ft_destroy_table(table);
} }
WHEN("Delete row") { WHEN("Erase row") {
ft_table_t *table = create_test_table(); 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); const char *table_str = ft_to_string(table);
assert_true(table_str != NULL); assert_true(table_str != NULL);
@ -1885,9 +1885,9 @@ void test_table_cell_deletion(void)
ft_destroy_table(table); ft_destroy_table(table);
} }
WHEN("Delete last row") { WHEN("Erase last row") {
ft_table_t *table = create_test_table(); 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); const char *table_str = ft_to_string(table);
assert_true(table_str != NULL); assert_true(table_str != NULL);
@ -1900,9 +1900,9 @@ void test_table_cell_deletion(void)
ft_destroy_table(table); ft_destroy_table(table);
} }
WHEN("Delete column") { WHEN("Erase column") {
ft_table_t *table = create_test_table(); 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); const char *table_str = ft_to_string(table);
assert_true(table_str != NULL); assert_true(table_str != NULL);
@ -1916,9 +1916,9 @@ void test_table_cell_deletion(void)
ft_destroy_table(table); ft_destroy_table(table);
} }
WHEN("Delete last column") { WHEN("Erase last column") {
ft_table_t *table = create_test_table(); 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); const char *table_str = ft_to_string(table);
assert_true(table_str != NULL); assert_true(table_str != NULL);

View File

@ -388,3 +388,77 @@ void test_cpp_table_changing_cell(void)
assert_string_equal(table_str, table_str_etalon); 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);
// }
}

View File

@ -12,7 +12,7 @@ void test_table_geometry(void);
void test_table_basic(void); void test_table_basic(void);
void test_table_copy(void); void test_table_copy(void);
void test_table_changing_cell(void); void test_table_changing_cell(void);
void test_table_cell_deletion(void); void test_table_erase(void);
#ifdef FT_HAVE_WCHAR #ifdef FT_HAVE_WCHAR
void test_wcs_table_boundaries(void); void test_wcs_table_boundaries(void);
#endif #endif
@ -53,7 +53,7 @@ struct test_case bb_test_suite [] = {
{"test_table_write", test_table_write}, {"test_table_write", test_table_write},
{"test_table_insert_strategy", test_table_insert_strategy}, {"test_table_insert_strategy", test_table_insert_strategy},
{"test_table_changing_cell", test_table_changing_cell}, {"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_border_style", test_table_border_style},
{"test_table_builtin_border_styles", test_table_builtin_border_styles}, {"test_table_builtin_border_styles", test_table_builtin_border_styles},
{"test_table_cell_properties", test_table_cell_properties}, {"test_table_cell_properties", test_table_cell_properties},

View File

@ -6,6 +6,7 @@
void test_cpp_table_basic(void); void test_cpp_table_basic(void);
void test_cpp_table_write(void); void test_cpp_table_write(void);
void test_cpp_table_insert(void); void test_cpp_table_insert(void);
void test_cpp_table_erase(void);
void test_cpp_table_changing_cell(void); void test_cpp_table_changing_cell(void);
void test_cpp_table_tbl_properties(void); void test_cpp_table_tbl_properties(void);
void test_cpp_table_cell_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_basic", test_cpp_table_basic},
{"test_cpp_table_write", test_cpp_table_write}, {"test_cpp_table_write", test_cpp_table_write},
{"test_cpp_table_insert", test_cpp_table_insert}, {"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_changing_cell", test_cpp_table_changing_cell},
{"test_cpp_table_tbl_properties", test_cpp_table_tbl_properties}, {"test_cpp_table_tbl_properties", test_cpp_table_tbl_properties},
{"test_cpp_table_cell_properties", test_cpp_table_cell_properties}, {"test_cpp_table_cell_properties", test_cpp_table_cell_properties},