Merge pull request #57 from brlcad/develop

[A] Implemented ft_col_count() along with corresponding unit tests
This commit is contained in:
Seleznev Anton 2020-11-30 20:00:01 +03:00 committed by GitHub
commit ab689ca5c7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 116 additions and 0 deletions

View File

@ -370,6 +370,16 @@ 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);
/**
* Get number of columns in the table.
*
* @param table
* Pointer to formatted table.
* @return
* Number of columns in the table.
*/
size_t ft_col_count(const ft_table_t *table);
/** /**
* Erase range of cells. * Erase range of cells.
* *

View File

@ -1172,6 +1172,18 @@ public:
return ft_row_count(table_); return ft_row_count(table_);
} }
/**
* Get number of columns in the table.
*
* @return
* Number of columns in the table.
*/
std::size_t
col_count() const noexcept
{
return ft_col_count(table_);
}
/** /**
* Get current cell. * Get current cell.
* *

View File

@ -233,6 +233,19 @@ size_t ft_row_count(const ft_table_t *table)
return vector_size(table->rows); return vector_size(table->rows);
} }
size_t ft_col_count(const ft_table_t *table)
{
assert(table && table->rows);
size_t cols_n = 0;
size_t rows_n = vector_size(table->rows);
for (size_t i = 0; i < rows_n; ++i) {
f_row_t *row = VECTOR_AT(table->rows, i, f_row_t *);
size_t ncols = columns_in_row(row);
cols_n = MAX(cols_n, ncols);
}
return cols_n;
}
int ft_erase_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

@ -8,6 +8,7 @@ void test_vector_basic(void);
void test_vector_stress(void); void test_vector_stress(void);
void test_string_buffer(void); void test_string_buffer(void);
void test_table_sizes(void); void test_table_sizes(void);
void test_table_counts(void);
void test_table_geometry(void); 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);
@ -36,6 +37,7 @@ struct test_case wb_test_suite [] = {
{"test_vector_stress", test_vector_stress}, {"test_vector_stress", test_vector_stress},
{"test_string_buffer", test_string_buffer}, {"test_string_buffer", test_string_buffer},
{"test_table_sizes", test_table_sizes}, {"test_table_sizes", test_table_sizes},
{"test_table_counts", test_table_counts},
{"test_table_geometry", test_table_geometry}, {"test_table_geometry", test_table_geometry},
{"test_table_copy", test_table_copy}, {"test_table_copy", test_table_copy},
}; };

View File

@ -51,6 +51,85 @@ void test_table_sizes(void)
} }
void test_table_counts(void)
{
ft_table_t *table = ft_create_table();
assert_true(table != NULL);
assert_true(set_test_props_for_table(table) == FT_SUCCESS);
size_t rows = 0;
size_t cols = 0;
int empty = 0;
WHEN("Table is empty") {
empty = ft_is_empty(table);
rows = ft_row_count(table);
cols = ft_col_count(table);
assert_true(empty);
assert_true(rows == 0);
assert_true(cols == 0);
}
WHEN("Insert one cell") {
int n = ft_printf(table, "%s", "1");
assert_true(n == 1);
empty = ft_is_empty(table);
rows = ft_row_count(table);
cols = ft_col_count(table);
assert_true(!empty);
assert_true(rows == 1);
assert_true(cols == 1);
}
WHEN("Insert two cells in the same row") {
int n = ft_printf_ln(table, "%s|%s", "2", "3");
assert_true(n == 2);
empty = ft_is_empty(table);
rows = ft_row_count(table);
cols = ft_col_count(table);
assert_true(!empty);
assert_true(rows == 1);
assert_true(cols == 3);
}
WHEN("Insert one cell in the next row") {
int status = ft_write(table, "hello");
assert_true(FT_IS_SUCCESS(status));
empty = ft_is_empty(table);
rows = ft_row_count(table);
cols = ft_col_count(table);
assert_true(!empty);
assert_true(rows == 2);
assert_true(cols == 3);
}
WHEN("Delete first row") {
int status = ft_erase_range(table, 0, 0, 0, DEFAULT_VECTOR_CAPACITY);
assert_true(FT_IS_SUCCESS(status));
empty = ft_is_empty(table);
rows = ft_row_count(table);
cols = ft_col_count(table);
assert_true(!empty);
assert_true(rows == 1);
assert_true(cols == 1);
}
WHEN("Delete second/last row") {
int status = ft_erase_range(table, 0, 0, 0, DEFAULT_VECTOR_CAPACITY);
assert_true(FT_IS_SUCCESS(status));
empty = ft_is_empty(table);
rows = ft_row_count(table);
cols = ft_col_count(table);
assert_true(empty);
assert_true(rows == 0);
assert_true(cols == 0);
}
ft_destroy_table(table);
}
void test_table_geometry(void) void test_table_geometry(void)
{ {
ft_table_t *table = ft_create_table(); ft_table_t *table = ft_create_table();