From c4be1ca649743f4d53f4c525a764148f3f0eaef2 Mon Sep 17 00:00:00 2001 From: Sean Morrison Date: Sun, 29 Nov 2020 12:38:31 -0500 Subject: [PATCH 1/2] Implement ft_col_count() along with corresponding unit tests This pairs with the existing ft_row_count() using the same column counting method used elsewhere in the code (i.e., iterate over rows and find the biggest). Added unit tests for ft_row_count(), ft_col_count(), ft_is_empty(), and use a myriad of insertion methods to increase coverage. --- src/fort.h | 10 ++++ src/fort.hpp | 12 +++++ src/fort_impl.c | 13 +++++ tests/main_test.c | 2 + tests/wb_tests/test_table_geometry.c | 79 ++++++++++++++++++++++++++++ 5 files changed, 116 insertions(+) diff --git a/src/fort.h b/src/fort.h index 50b78e5..f4a7d05 100644 --- a/src/fort.h +++ b/src/fort.h @@ -370,6 +370,16 @@ int ft_is_empty(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. * diff --git a/src/fort.hpp b/src/fort.hpp index 7e05e07..75ea470 100644 --- a/src/fort.hpp +++ b/src/fort.hpp @@ -1172,6 +1172,18 @@ public: 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. * diff --git a/src/fort_impl.c b/src/fort_impl.c index 79ca7ce..065e891 100644 --- a/src/fort_impl.c +++ b/src/fort_impl.c @@ -233,6 +233,19 @@ size_t ft_row_count(const ft_table_t *table) 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, size_t top_left_row, size_t top_left_col, size_t bottom_right_row, size_t bottom_right_col) diff --git a/tests/main_test.c b/tests/main_test.c index 263ba88..5442d78 100644 --- a/tests/main_test.c +++ b/tests/main_test.c @@ -8,6 +8,7 @@ void test_vector_basic(void); void test_vector_stress(void); void test_string_buffer(void); void test_table_sizes(void); +void test_table_counts(void); void test_table_geometry(void); void test_table_basic(void); void test_table_copy(void); @@ -36,6 +37,7 @@ struct test_case wb_test_suite [] = { {"test_vector_stress", test_vector_stress}, {"test_string_buffer", test_string_buffer}, {"test_table_sizes", test_table_sizes}, + {"test_table_counts", test_table_counts}, {"test_table_geometry", test_table_geometry}, {"test_table_copy", test_table_copy}, }; diff --git a/tests/wb_tests/test_table_geometry.c b/tests/wb_tests/test_table_geometry.c index 3a76ccf..1f97344 100644 --- a/tests/wb_tests/test_table_geometry.c +++ b/tests/wb_tests/test_table_geometry.c @@ -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) { ft_table_t *table = ft_create_table(); From 80cbf3de448b0e0b787517f9692dfe8348072999 Mon Sep 17 00:00:00 2001 From: seleznevae Date: Mon, 30 Nov 2020 20:09:09 +0300 Subject: [PATCH 2/2] [U] Update ChangeLog and docs --- CMakeLists.txt | 2 +- ChangeLog.md | 6 ++++++ docs/index.md | 2 ++ lib/fort.c | 13 +++++++++++++ lib/fort.h | 16 +++++++++++++--- lib/fort.hpp | 12 ++++++++++++ src/fort.h | 6 +++--- 7 files changed, 50 insertions(+), 7 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 7463aaa..7573b13 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,6 @@ cmake_minimum_required(VERSION 3.0) -project(libfort VERSION 0.4.2) +project(libfort VERSION 0.5.0) string(REGEX REPLACE "([0-9]+)\\.([0-9]+)\\.([0-9]+)" "\\1.\\2" libfort_SOVERSION diff --git a/ChangeLog.md b/ChangeLog.md index f8beb74..d0a6131 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -1,3 +1,9 @@ +## v0.5.0 + +### API + +- Add function `ft_col_count()` to get number of columns in the table. + ## v0.4.2 ### Internal diff --git a/docs/index.md b/docs/index.md index bdf7942..04096d2 100644 --- a/docs/index.md +++ b/docs/index.md @@ -44,6 +44,7 @@ These pages contain the API documentation of **libfort** - simple library to cre - @link ft_set_default_printf_field_separator ft_set_default_printf_field_separator @endlink -- Set field separator for ft_printf, ft_printf_ln - @link ft_is_empty ft_is_empty @endlink -- check if table is empty - @link ft_row_count ft_row_count @endlink -- get number of rows in the table + - @link ft_col_count ft_col_count @endlink -- get number of columns in the table - @link ft_strerror ft_strerror @endlink -- get string describing the error code - Data structures and types @@ -82,6 +83,7 @@ These pages contain the API documentation of **libfort** - simple library to cre - @link fort::table::column column @endlink -- get column - @link fort::table::is_empty is_empty @endlink -- check if table is empty - @link fort::table::row_count row_count @endlink -- get number of rows in the table + - @link fort::table::col_count col_count @endlink -- get number of columns in the table - @link fort::property_owner fort::property_owner @endlink -- base class for all objects (table, row, column, cell) for which user can specify properties - Modify appearance diff --git a/lib/fort.c b/lib/fort.c index 6c5e3d6..bffe8c1 100644 --- a/lib/fort.c +++ b/lib/fort.c @@ -2829,6 +2829,19 @@ size_t ft_row_count(const ft_table_t *table) 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, size_t top_left_row, size_t top_left_col, size_t bottom_right_row, size_t bottom_right_col) diff --git a/lib/fort.h b/lib/fort.h index 50b78e5..11c35a2 100644 --- a/lib/fort.h +++ b/lib/fort.h @@ -45,9 +45,9 @@ SOFTWARE. *****************************************************************************/ #define LIBFORT_MAJOR_VERSION 0 -#define LIBFORT_MINOR_VERSION 4 -#define LIBFORT_REVISION 2 -#define LIBFORT_VERSION_STR "0.4.2" +#define LIBFORT_MINOR_VERSION 5 +#define LIBFORT_REVISION 0 +#define LIBFORT_VERSION_STR "0.5.0" /***************************************************************************** @@ -370,6 +370,16 @@ int ft_is_empty(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. * diff --git a/lib/fort.hpp b/lib/fort.hpp index 7e05e07..75ea470 100644 --- a/lib/fort.hpp +++ b/lib/fort.hpp @@ -1172,6 +1172,18 @@ public: 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. * diff --git a/src/fort.h b/src/fort.h index f4a7d05..11c35a2 100644 --- a/src/fort.h +++ b/src/fort.h @@ -45,9 +45,9 @@ SOFTWARE. *****************************************************************************/ #define LIBFORT_MAJOR_VERSION 0 -#define LIBFORT_MINOR_VERSION 4 -#define LIBFORT_REVISION 2 -#define LIBFORT_VERSION_STR "0.4.2" +#define LIBFORT_MINOR_VERSION 5 +#define LIBFORT_REVISION 0 +#define LIBFORT_VERSION_STR "0.5.0" /*****************************************************************************