diff --git a/src/row.h b/src/row.h index d6af84a..59a76d3 100644 --- a/src/row.h +++ b/src/row.h @@ -48,8 +48,6 @@ int wprint_row_separator(wchar_t *buffer, size_t buffer_sz, -fort_row_t* create_row_from_string(const char *str); -fort_row_t* create_row_from_fmt_string(const char* FT_RESTRICT fmt, va_list *va_args); int snprintf_row(const fort_row_t *row, char *buffer, size_t buf_sz, size_t *col_width_arr, size_t col_width_arr_sz, size_t row_height, const context_t *context); diff --git a/tests/test.c b/tests/test.c index e18b8d9..7a3146c 100644 --- a/tests/test.c +++ b/tests/test.c @@ -7,6 +7,7 @@ struct test_case test_suit [] = { {"test_table_sizes", test_table_sizes}, {"test_table_geometry", test_table_geometry}, {"test_table_basic", test_table_basic}, + {"test_table_write", test_table_write}, {"test_table_border_style", test_table_border_style}, {"test_table_options", test_table_options}, }; diff --git a/tests/test_table_basic.c b/tests/test_table_basic.c index 21a0c28..bce0ae1 100644 --- a/tests/test_table_basic.c +++ b/tests/test_table_basic.c @@ -1,4 +1,5 @@ #include "tests.h" +#include "wchar.h" void test_table_basic(void) { @@ -10,13 +11,9 @@ void test_table_basic(void) assert_true( set_test_options_for_table(table) == FT_SUCCESS); ft_set_option(table, 0, FT_ANY_COLUMN, FT_OPT_ROW_TYPE, Header); - int n = ft_printf_ln(table, "%d|%c|%s|%f", 3, 'c', "234", 3.14); - - assert_true( n == 4 ); - n = ft_printf_ln(table, "%d|%c|%s|%f", 3, 'c', "234", 3.14); - assert_true( n == 4 ); - n = ft_printf_ln(table, "%d|%c|%s|%f", 3, 'c', "234", 3.14); - assert_true( n == 4 ); + assert_true( FT_NWRITE_LN(table, "3", "c", "234", "3.140000") == FT_SUCCESS); + assert_true( FT_NWRITE_LN(table, "3", "c", "234", "3.140000") == FT_SUCCESS); + assert_true( FT_NWRITE_LN(table, "3", "c", "234", "3.140000") == FT_SUCCESS); const char *table_str = ft_to_string(table); assert_true( table_str != NULL ); @@ -35,9 +32,38 @@ void test_table_basic(void) "| | | | |\n" "+---+---+-----+----------+\n"; // fprintf(stderr, "content:\n%s", table_str); - assert_true( strcmp(table_str, table_str_etalon) == 0); + ft_destroy_table(table); + } + WHEN("All columns are equal and not empty (wide strings)") { + table = ft_create_table(); + assert_true( table != NULL ); + assert_true( set_test_options_for_table(table) == FT_SUCCESS); + + ft_set_option(table, 0, FT_ANY_COLUMN, FT_OPT_ROW_TYPE, Header); + assert_true( FT_NWWRITE_LN(table, L"3", L"c", L"234", L"3.140000") == FT_SUCCESS); + assert_true( FT_NWWRITE_LN(table, L"3", L"c", L"234", L"3.140000") == FT_SUCCESS); + assert_true( FT_NWWRITE_LN(table, L"3", L"c", L"234", L"3.140000") == FT_SUCCESS); + + const wchar_t *table_str = ft_to_wstring(table); + assert_true( table_str != NULL ); + const wchar_t *table_str_etalon = + L"+---+---+-----+----------+\n" + L"| | | | |\n" + L"| 3 | c | 234 | 3.140000 |\n" + L"| | | | |\n" + L"+---+---+-----+----------+\n" + L"| | | | |\n" + L"| 3 | c | 234 | 3.140000 |\n" + L"| | | | |\n" + L"+---+---+-----+----------+\n" + L"| | | | |\n" + L"| 3 | c | 234 | 3.140000 |\n" + L"| | | | |\n" + L"+---+---+-----+----------+\n"; +// fprintf(stderr, "content:\n%s", table_str); + assert_true( wcscmp(table_str, table_str_etalon) == 0); ft_destroy_table(table); } @@ -49,8 +75,200 @@ void test_table_basic(void) assert_true( set_test_options_for_table(table) == FT_SUCCESS); ft_set_option(table, 0, FT_ANY_COLUMN, FT_OPT_ROW_TYPE, Header); - int n = ft_printf_ln(table, "%d|%c|%s|%f", 3, 'c', "234", 3.14); + assert_true( FT_NWRITE_LN(table, "3", "c", "234", "3.140000") == FT_SUCCESS); + assert_true( FT_NWRITE_LN(table, "c", "234", "3.140000", "3") == FT_SUCCESS); + assert_true( FT_NWRITE_LN(table, "234", "3.140000", "3", "c") == FT_SUCCESS); + const char *table_str = ft_to_string(table); + assert_true( table_str != NULL ); + const char *table_str_etalon = + "+-----+----------+----------+----------+\n" + "| | | | |\n" + "| 3 | c | 234 | 3.140000 |\n" + "| | | | |\n" + "+-----+----------+----------+----------+\n" + "| | | | |\n" + "| c | 234 | 3.140000 | 3 |\n" + "| | | | |\n" + "+-----+----------+----------+----------+\n" + "| | | | |\n" + "| 234 | 3.140000 | 3 | c |\n" + "| | | | |\n" + "+-----+----------+----------+----------+\n"; +// fprintf(stderr, "content:\n%s", table_str); + assert_true( strcmp(table_str, table_str_etalon) == 0); + ft_destroy_table(table); + } + + WHEN("All columns are not equal and not empty (wide strings)") { + table = ft_create_table(); + assert_true( table != NULL ); + assert_true( set_test_options_for_table(table) == FT_SUCCESS); + + ft_set_option(table, 0, FT_ANY_COLUMN, FT_OPT_ROW_TYPE, Header); + assert_true( FT_NWWRITE_LN(table, L"3", L"c", L"234", L"3.140000") == FT_SUCCESS); + assert_true( FT_NWWRITE_LN(table, L"c", L"234", L"3.140000", L"3") == FT_SUCCESS); + assert_true( FT_NWWRITE_LN(table, L"234", L"3.140000", L"3", L"c") == FT_SUCCESS); + + const wchar_t *table_str = ft_to_wstring(table); + assert_true( table_str != NULL ); + const wchar_t *table_str_etalon = + L"+-----+----------+----------+----------+\n" + L"| | | | |\n" + L"| 3 | c | 234 | 3.140000 |\n" + L"| | | | |\n" + L"+-----+----------+----------+----------+\n" + L"| | | | |\n" + L"| c | 234 | 3.140000 | 3 |\n" + L"| | | | |\n" + L"+-----+----------+----------+----------+\n" + L"| | | | |\n" + L"| 234 | 3.140000 | 3 | c |\n" + L"| | | | |\n" + L"+-----+----------+----------+----------+\n"; +// fprintf(stderr, "content:\n%s", table_str); + assert_true( wcscmp(table_str, table_str_etalon) == 0); + ft_destroy_table(table); + } + + WHEN("All columns are not equal and some cells are empty") { + table = ft_create_table(); + assert_true( table != NULL ); + assert_true( set_test_options_for_table(table) == FT_SUCCESS); + + ft_set_option(table, 0, FT_ANY_COLUMN, FT_OPT_ROW_TYPE, Header); + assert_true( FT_NWRITE_LN(table, "", "", "234", "3.140000") == FT_SUCCESS); + assert_true( FT_NWRITE_LN(table, "c", "234", "3.140000", "") == FT_SUCCESS); + assert_true( FT_NWRITE_LN(table, "234", "3.140000", "", "") == FT_SUCCESS); + + const char *table_str = ft_to_string(table); + assert_true( table_str != NULL ); + const char *table_str_etalon = + "+-----+----------+----------+----------+\n" + "| | | | |\n" + "| | | 234 | 3.140000 |\n" + "| | | | |\n" + "+-----+----------+----------+----------+\n" + "| | | | |\n" + "| c | 234 | 3.140000 | |\n" + "| | | | |\n" + "+-----+----------+----------+----------+\n" + "| | | | |\n" + "| 234 | 3.140000 | | |\n" + "| | | | |\n" + "+-----+----------+----------+----------+\n"; +// fprintf(stderr, "content:\n%s", table_str); + assert_true( strcmp(table_str, table_str_etalon) == 0); + ft_destroy_table(table); + } + + WHEN("All columns are not equal and some cells are empty (wide strings)") { + table = ft_create_table(); + assert_true( table != NULL ); + assert_true( set_test_options_for_table(table) == FT_SUCCESS); + + ft_set_option(table, 0, FT_ANY_COLUMN, FT_OPT_ROW_TYPE, Header); + assert_true( FT_NWWRITE_LN(table, L"", L"", L"234", L"3.140000") == FT_SUCCESS); + assert_true( FT_NWWRITE_LN(table, L"c", L"234", L"3.140000", L"") == FT_SUCCESS); + assert_true( FT_NWWRITE_LN(table, L"234", L"3.140000", L"", L"") == FT_SUCCESS); + + const wchar_t *table_str = ft_to_wstring(table); + assert_true( table_str != NULL ); + const wchar_t *table_str_etalon = + L"+-----+----------+----------+----------+\n" + L"| | | | |\n" + L"| | | 234 | 3.140000 |\n" + L"| | | | |\n" + L"+-----+----------+----------+----------+\n" + L"| | | | |\n" + L"| c | 234 | 3.140000 | |\n" + L"| | | | |\n" + L"+-----+----------+----------+----------+\n" + L"| | | | |\n" + L"| 234 | 3.140000 | | |\n" + L"| | | | |\n" + L"+-----+----------+----------+----------+\n"; +// fprintf(stderr, "content:\n%s", table_str); + assert_true( wcscmp(table_str, table_str_etalon) == 0); + ft_destroy_table(table); + } + + WHEN("All cells are empty") { + table = ft_create_table(); + assert_true( table != NULL ); + assert_true( set_test_options_for_table(table) == FT_SUCCESS); + + ft_set_option(table, 0, FT_ANY_COLUMN, FT_OPT_ROW_TYPE, Header); + assert_true( FT_NWRITE_LN(table, "", "", "", "") == FT_SUCCESS); + assert_true( FT_NWRITE_LN(table, "", "", "", "") == FT_SUCCESS); + assert_true( FT_NWRITE_LN(table, "", "", "", "") == FT_SUCCESS); + + const char *table_str = ft_to_string(table); + assert_true( table_str != NULL ); + const char *table_str_etalon = + "+--+--+--+--+\n" + "| | | | |\n" + "| | | | |\n" + "| | | | |\n" + "+--+--+--+--+\n" + "| | | | |\n" + "| | | | |\n" + "| | | | |\n" + "+--+--+--+--+\n" + "| | | | |\n" + "| | | | |\n" + "| | | | |\n" + "+--+--+--+--+\n"; +// fprintf(stderr, "content:\n%s", table_str); + assert_true( strcmp(table_str, table_str_etalon) == 0); + ft_destroy_table(table); + } + + WHEN("All cells are empty (wide strings)") { + table = ft_create_table(); + assert_true( table != NULL ); + assert_true( set_test_options_for_table(table) == FT_SUCCESS); + + ft_set_option(table, 0, FT_ANY_COLUMN, FT_OPT_ROW_TYPE, Header); + assert_true( FT_NWWRITE_LN(table, L"", L"", L"", L"") == FT_SUCCESS); + assert_true( FT_NWWRITE_LN(table, L"", L"", L"", L"") == FT_SUCCESS); + assert_true( FT_NWWRITE_LN(table, L"", L"", L"", L"") == FT_SUCCESS); + + const wchar_t *table_str = ft_to_wstring(table); + assert_true( table_str != NULL ); + const wchar_t *table_str_etalon = + L"+--+--+--+--+\n" + L"| | | | |\n" + L"| | | | |\n" + L"| | | | |\n" + L"+--+--+--+--+\n" + L"| | | | |\n" + L"| | | | |\n" + L"| | | | |\n" + L"+--+--+--+--+\n" + L"| | | | |\n" + L"| | | | |\n" + L"| | | | |\n" + L"+--+--+--+--+\n"; +// fprintf(stderr, "content:\n%s", table_str); + assert_true( wcscmp(table_str, table_str_etalon) == 0); + ft_destroy_table(table); + } +} + + +void test_table_write(void) +{ + FTABLE *table = NULL; + + + SCENARIO("Test printf functions") { + table = ft_create_table(); + assert_true( table != NULL ); + assert_true( set_test_options_for_table(table) == FT_SUCCESS); + + ft_set_option(table, 0, FT_ANY_COLUMN, FT_OPT_ROW_TYPE, Header); + int n = ft_printf_ln(table, "%d|%c|%s|%f", 3, 'c', "234", 3.14); assert_true( n == 4 ); n = ft_printf_ln(table, "%c|%s|%f|%d", 'c', "234", 3.14, 3); assert_true( n == 4 ); @@ -74,82 +292,6 @@ void test_table_basic(void) "| | | | |\n" "+-----+----------+----------+----------+\n"; -// fprintf(stderr, "content:\n%s", table_str); - assert_true( strcmp(table_str, table_str_etalon) == 0); - - ft_destroy_table(table); - } - - WHEN("All columns are not equal and some cells are empty") { - table = ft_create_table(); - assert_true( table != NULL ); -// ft_set_table_options(table, &test_table_opts); - assert_true( set_test_options_for_table(table) == FT_SUCCESS); - - ft_set_option(table, 0, FT_ANY_COLUMN, FT_OPT_ROW_TYPE, Header); - int n = ft_printf_ln(table, "||%s|%f", "234", 3.14); - - assert_true( n == 4 ); - n = ft_printf_ln(table, "%c|%s|%f", 'c', "234", 3.14); - assert_true( n == 3 ); - n = ft_printf_ln(table, "%s|%f||", "234", 3.14); - assert_true( n == 4 ); - - const char *table_str = ft_to_string(table); - assert_true( table_str != NULL ); - const char *table_str_etalon = - "+-----+----------+----------+----------+\n" - "| | | | |\n" - "| | | 234 | 3.140000 |\n" - "| | | | |\n" - "+-----+----------+----------+----------+\n" - "| | | | |\n" - "| c | 234 | 3.140000 | |\n" - "| | | | |\n" - "+-----+----------+----------+----------+\n" - "| | | | |\n" - "| 234 | 3.140000 | | |\n" - "| | | | |\n" - "+-----+----------+----------+----------+\n"; - -// fprintf(stderr, "content:\n%s", table_str); - assert_true( strcmp(table_str, table_str_etalon) == 0); - - ft_destroy_table(table); - } - - WHEN("All cells are empty") { - table = ft_create_table(); - assert_true( table != NULL ); - assert_true( set_test_options_for_table(table) == FT_SUCCESS); - - ft_set_option(table, 0, FT_ANY_COLUMN, FT_OPT_ROW_TYPE, Header); - int n = ft_printf_ln(table, "|||"); - - assert_true( n == 4 ); - n = ft_printf_ln(table, "|||"); - assert_true( n == 4 ); - n = ft_printf_ln(table, "|||"); - assert_true( n == 4 ); - - const char *table_str = ft_to_string(table); - assert_true( table_str != NULL ); - const char *table_str_etalon = - "+--+--+--+--+\n" - "| | | | |\n" - "| | | | |\n" - "| | | | |\n" - "+--+--+--+--+\n" - "| | | | |\n" - "| | | | |\n" - "| | | | |\n" - "+--+--+--+--+\n" - "| | | | |\n" - "| | | | |\n" - "| | | | |\n" - "+--+--+--+--+\n"; - - // fprintf(stderr, "content:\n%s", table_str); assert_true( strcmp(table_str, table_str_etalon) == 0); diff --git a/tests/tests.h b/tests/tests.h index 4a39bca..097f7ea 100644 --- a/tests/tests.h +++ b/tests/tests.h @@ -9,6 +9,7 @@ #define WHEN(...) #define THEN(...) +#define SCENARIO(...) /* Test cases */ void test_vector_basic(void); @@ -16,6 +17,7 @@ void test_string_buffer(void); void test_table_sizes(void); void test_table_geometry(void); void test_table_basic(void); +void test_table_write(void); void test_table_border_style(void); void test_table_options(void);