diff --git a/lib/fort.c b/lib/fort.c index 3944f40..1bfea72 100644 --- a/lib/fort.c +++ b/lib/fort.c @@ -2071,11 +2071,12 @@ int ft_table_wwrite_ln(ft_table_t *table, size_t rows, size_t cols, const wchar_ const char *ft_to_string(const ft_table_t *table) { typedef char char_type; - const char_type *empty_string = ""; +// const char_type *empty_string = ""; const enum str_buf_type buf_type = CharBuf; const char *space_char = " "; const char *new_line_char = "\n"; #define cur_F_STRDUP F_STRDUP +#define EMPTY_STRING "" int (*snprintf_row_)(const fort_row_t *, char *, size_t, size_t *, size_t, size_t, const context_t *) = snprintf_row; int (*print_row_separator_)(char *, size_t, const size_t *, size_t, @@ -2118,7 +2119,7 @@ const char *ft_to_string(const ft_table_t *table) return NULL; if (rows == 0) - return cur_F_STRDUP(empty_string); + return EMPTY_STRING; int written = 0; int tmp = 0; @@ -2166,6 +2167,7 @@ clear: // F_FREE(buffer); return NULL; #undef cur_F_STRDUP +#undef EMPTY_STRING } @@ -2174,11 +2176,12 @@ clear: const wchar_t *ft_to_wstring(const ft_table_t *table) { typedef wchar_t char_type; - const char_type *empty_string = L""; +// const char_type *empty_string = L""; const enum str_buf_type buf_type = WCharBuf; const char *space_char = " "; const char *new_line_char = "\n"; #define cur_F_STRDUP F_WCSDUP +#define EMPTY_STRING L"" int (*snprintf_row_)(const fort_row_t *, wchar_t *, size_t, size_t *, size_t, size_t, const context_t *) = wsnprintf_row; int (*print_row_separator_)(wchar_t *, size_t, const size_t *, size_t, @@ -2221,7 +2224,7 @@ const wchar_t *ft_to_wstring(const ft_table_t *table) status = table_rows_and_cols_geometry(table, &col_width_arr, &cols, &row_height_arr, &rows); if (rows == 0) - return cur_F_STRDUP(empty_string); + return EMPTY_STRING; if (FT_IS_ERROR(status)) return NULL; @@ -2271,6 +2274,7 @@ clear: // F_FREE(buffer); return NULL; #undef cur_F_STRDUP +#undef EMPTY_STRING } #endif diff --git a/src/fort_impl.c b/src/fort_impl.c index ddebd43..b80c137 100644 --- a/src/fort_impl.c +++ b/src/fort_impl.c @@ -535,11 +535,12 @@ int ft_table_wwrite_ln(ft_table_t *table, size_t rows, size_t cols, const wchar_ const char *ft_to_string(const ft_table_t *table) { typedef char char_type; - const char_type *empty_string = ""; +// const char_type *empty_string = ""; const enum str_buf_type buf_type = CharBuf; const char *space_char = " "; const char *new_line_char = "\n"; #define cur_F_STRDUP F_STRDUP +#define EMPTY_STRING "" int (*snprintf_row_)(const fort_row_t *, char *, size_t, size_t *, size_t, size_t, const context_t *) = snprintf_row; int (*print_row_separator_)(char *, size_t, const size_t *, size_t, @@ -582,7 +583,7 @@ const char *ft_to_string(const ft_table_t *table) return NULL; if (rows == 0) - return cur_F_STRDUP(empty_string); + return EMPTY_STRING; int written = 0; int tmp = 0; @@ -630,6 +631,7 @@ clear: // F_FREE(buffer); return NULL; #undef cur_F_STRDUP +#undef EMPTY_STRING } @@ -638,11 +640,12 @@ clear: const wchar_t *ft_to_wstring(const ft_table_t *table) { typedef wchar_t char_type; - const char_type *empty_string = L""; +// const char_type *empty_string = L""; const enum str_buf_type buf_type = WCharBuf; const char *space_char = " "; const char *new_line_char = "\n"; #define cur_F_STRDUP F_WCSDUP +#define EMPTY_STRING L"" int (*snprintf_row_)(const fort_row_t *, wchar_t *, size_t, size_t *, size_t, size_t, const context_t *) = wsnprintf_row; int (*print_row_separator_)(wchar_t *, size_t, const size_t *, size_t, @@ -685,7 +688,7 @@ const wchar_t *ft_to_wstring(const ft_table_t *table) status = table_rows_and_cols_geometry(table, &col_width_arr, &cols, &row_height_arr, &rows); if (rows == 0) - return cur_F_STRDUP(empty_string); + return EMPTY_STRING; if (FT_IS_ERROR(status)) return NULL; @@ -735,6 +738,7 @@ clear: // F_FREE(buffer); return NULL; #undef cur_F_STRDUP +#undef EMPTY_STRING } #endif diff --git a/tests/bb_tests/test_table_border_style.c b/tests/bb_tests/test_table_border_style.c index 5fcd9d9..6e526bb 100644 --- a/tests/bb_tests/test_table_border_style.c +++ b/tests/bb_tests/test_table_border_style.c @@ -129,3 +129,285 @@ void test_table_border_style(void) ft_destroy_table(table); } } + + +static ft_table_t *create_basic_table(void) +{ + ft_table_t *table = ft_create_table(); + ft_set_cell_option(table, FT_ANY_ROW, 0, FT_COPT_TEXT_ALIGN, FT_ALIGNED_CENTER); + ft_set_cell_option(table, FT_ANY_ROW, 1, FT_COPT_TEXT_ALIGN, FT_ALIGNED_LEFT); + + ft_set_cell_option(table, 0, FT_ANY_COLUMN, FT_COPT_ROW_TYPE, FT_ROW_HEADER); + ft_write_ln(table, "Rank", "Title", "Year", "Rating"); + + ft_write_ln(table, "1", "The Shawshank Redemption", "1994", "9.5"); + ft_write_ln(table, "2", "12 Angry Men", "1957", "8.8"); + ft_write_ln(table, "3", "It's a Wonderful Life", "1946", "8.6"); + ft_add_separator(table); + ft_write_ln(table, "4", "2001: A Space Odyssey", "1968", "8.5"); + ft_write_ln(table, "5", "Blade Runner", "1982", "8.1"); + return table; +} + +void test_table_builtin_border_styles(void) +{ + ft_table_t *table = NULL; + const char *table_str = NULL; + const char *table_str_etalon = NULL; + + + ft_set_default_border_style(FT_BASIC_STYLE); + table = create_basic_table(); + table_str = ft_to_string(table); + assert_true(table_str != NULL); + + table_str_etalon = + "+------+--------------------------+------+--------+\n" + "| Rank | Title | Year | Rating |\n" + "+------+--------------------------+------+--------+\n" + "| 1 | The Shawshank Redemption | 1994 | 9.5 |\n" + "| 2 | 12 Angry Men | 1957 | 8.8 |\n" + "| 3 | It's a Wonderful Life | 1946 | 8.6 |\n" + "+------+--------------------------+------+--------+\n" + "| 4 | 2001: A Space Odyssey | 1968 | 8.5 |\n" + "| 5 | Blade Runner | 1982 | 8.1 |\n" + "+------+--------------------------+------+--------+\n"; + assert_str_equal(table_str, table_str_etalon); + ft_destroy_table(table); + + /* *************************************************************** */ + + ft_set_default_border_style(FT_SIMPLE_STYLE); + table = create_basic_table(); + table_str = ft_to_string(table); + assert_true(table_str != NULL); + + table_str_etalon = + " \n" + " Rank Title Year Rating \n" + " ------ -------------------------- ------ -------- \n" + " 1 The Shawshank Redemption 1994 9.5 \n" + " 2 12 Angry Men 1957 8.8 \n" + " 3 It's a Wonderful Life 1946 8.6 \n" + " ------ -------------------------- ------ -------- \n" + " 4 2001: A Space Odyssey 1968 8.5 \n" + " 5 Blade Runner 1982 8.1 \n" + " \n"; + assert_str_equal(table_str, table_str_etalon); + ft_destroy_table(table); + + /* *************************************************************** */ + + ft_set_default_border_style(FT_PLAIN_STYLE); + table = create_basic_table(); + table_str = ft_to_string(table); + assert_true(table_str != NULL); + + table_str_etalon = + " ------------------------------------------------- \n" + " Rank Title Year Rating \n" + " ------------------------------------------------- \n" + " 1 The Shawshank Redemption 1994 9.5 \n" + " 2 12 Angry Men 1957 8.8 \n" + " 3 It's a Wonderful Life 1946 8.6 \n" + " ------------------------------------------------- \n" + " 4 2001: A Space Odyssey 1968 8.5 \n" + " 5 Blade Runner 1982 8.1 \n" + " \n"; + assert_str_equal(table_str, table_str_etalon); + ft_destroy_table(table); + + /* *************************************************************** */ + + ft_set_default_border_style(FT_DOT_STYLE); + table = create_basic_table(); + table_str = ft_to_string(table); + assert_true(table_str != NULL); + + table_str_etalon = + "...................................................\n" + ": Rank : Title : Year : Rating :\n" + ":......:..........................:......:........:\n" + ": 1 : The Shawshank Redemption : 1994 : 9.5 :\n" + ": 2 : 12 Angry Men : 1957 : 8.8 :\n" + ": 3 : It's a Wonderful Life : 1946 : 8.6 :\n" + ":......:..........................:......:........:\n" + ": 4 : 2001: A Space Odyssey : 1968 : 8.5 :\n" + ": 5 : Blade Runner : 1982 : 8.1 :\n" + ":......:..........................:......:........:\n"; + assert_str_equal(table_str, table_str_etalon); + ft_destroy_table(table); + + /* *************************************************************** */ + + ft_set_default_border_style(FT_EMPTY_STYLE); + table = create_basic_table(); + table_str = ft_to_string(table); + assert_true(table_str != NULL); + + table_str_etalon = + " \n" + " Rank Title Year Rating \n" + " 1 The Shawshank Redemption 1994 9.5 \n" + " 2 12 Angry Men 1957 8.8 \n" + " 3 It's a Wonderful Life 1946 8.6 \n" + " \n" + " 4 2001: A Space Odyssey 1968 8.5 \n" + " 5 Blade Runner 1982 8.1 \n" + " \n"; + assert_str_equal(table_str, table_str_etalon); + ft_destroy_table(table); + + /* *************************************************************** */ + + ft_set_default_border_style(FT_SOLID_STYLE); + table = create_basic_table(); + table_str = ft_to_string(table); + assert_true(table_str != NULL); + + table_str_etalon = + "┌──────┬──────────────────────────┬──────┬────────┐\n" + "│ Rank │ Title │ Year │ Rating │\n" + "├──────┼──────────────────────────┼──────┼────────┤\n" + "│ 1 │ The Shawshank Redemption │ 1994 │ 9.5 │\n" + "│ 2 │ 12 Angry Men │ 1957 │ 8.8 │\n" + "│ 3 │ It's a Wonderful Life │ 1946 │ 8.6 │\n" + "├──────┼──────────────────────────┼──────┼────────┤\n" + "│ 4 │ 2001: A Space Odyssey │ 1968 │ 8.5 │\n" + "│ 5 │ Blade Runner │ 1982 │ 8.1 │\n" + "└──────┴──────────────────────────┴──────┴────────╯\n"; + assert_str_equal(table_str, table_str_etalon); + ft_destroy_table(table); + + /* *************************************************************** */ + + ft_set_default_border_style(FT_SOLID_ROUND_STYLE); + table = create_basic_table(); + table_str = ft_to_string(table); + assert_true(table_str != NULL); + + table_str_etalon = + "╭──────┬──────────────────────────┬──────┬────────╮\n" + "│ Rank │ Title │ Year │ Rating │\n" + "├──────┼──────────────────────────┼──────┼────────┤\n" + "│ 1 │ The Shawshank Redemption │ 1994 │ 9.5 │\n" + "│ 2 │ 12 Angry Men │ 1957 │ 8.8 │\n" + "│ 3 │ It's a Wonderful Life │ 1946 │ 8.6 │\n" + "├──────┼──────────────────────────┼──────┼────────┤\n" + "│ 4 │ 2001: A Space Odyssey │ 1968 │ 8.5 │\n" + "│ 5 │ Blade Runner │ 1982 │ 8.1 │\n" + "╰──────┴──────────────────────────┴──────┴────────╯\n"; + assert_str_equal(table_str, table_str_etalon); + ft_destroy_table(table); + + /* *************************************************************** */ + + ft_set_default_border_style(FT_DOUBLE_STYLE); + table = create_basic_table(); + table_str = ft_to_string(table); + assert_true(table_str != NULL); + + table_str_etalon = + "╔══════╦══════════════════════════╦══════╦════════╗\n" + "║ Rank ║ Title ║ Year ║ Rating ║\n" + "╠══════╬══════════════════════════╬══════╬════════╣\n" + "║ 1 ║ The Shawshank Redemption ║ 1994 ║ 9.5 ║\n" + "║ 2 ║ 12 Angry Men ║ 1957 ║ 8.8 ║\n" + "║ 3 ║ It's a Wonderful Life ║ 1946 ║ 8.6 ║\n" + "╠══════╬══════════════════════════╬══════╬════════╣\n" + "║ 4 ║ 2001: A Space Odyssey ║ 1968 ║ 8.5 ║\n" + "║ 5 ║ Blade Runner ║ 1982 ║ 8.1 ║\n" + "╚══════╩══════════════════════════╩══════╩════════╝\n"; + assert_str_equal(table_str, table_str_etalon); + ft_destroy_table(table); + + /* *************************************************************** */ + + ft_set_default_border_style(FT_DOUBLE2_STYLE); + table = create_basic_table(); + table_str = ft_to_string(table); + assert_true(table_str != NULL); + + table_str_etalon = + "╔══════╤══════════════════════════╤══════╤════════╗\n" + "║ Rank │ Title │ Year │ Rating ║\n" + "╠══════╪══════════════════════════╪══════╪════════╣\n" + "║ 1 │ The Shawshank Redemption │ 1994 │ 9.5 ║\n" + "╟──────┼──────────────────────────┼──────┼────────╢\n" + "║ 2 │ 12 Angry Men │ 1957 │ 8.8 ║\n" + "╟──────┼──────────────────────────┼──────┼────────╢\n" + "║ 3 │ It's a Wonderful Life │ 1946 │ 8.6 ║\n" + "╠══════╪══════════════════════════╪══════╪════════╣\n" + "║ 4 │ 2001: A Space Odyssey │ 1968 │ 8.5 ║\n" + "╟──────┼──────────────────────────┼──────┼────────╢\n" + "║ 5 │ Blade Runner │ 1982 │ 8.1 ║\n" + "╚══════╧══════════════════════════╧══════╧════════╝\n"; + assert_str_equal(table_str, table_str_etalon); + ft_destroy_table(table); + + /* *************************************************************** */ + + ft_set_default_border_style(FT_BOLD_STYLE); + table = create_basic_table(); + table_str = ft_to_string(table); + assert_true(table_str != NULL); + + table_str_etalon = + "┏━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━┳━━━━━━━━┓\n" + "┃ Rank ┃ Title ┃ Year ┃ Rating ┃\n" + "┣━━━━━━╋━━━━━━━━━━━━━━━━━━━━━━━━━━╋━━━━━━╋━━━━━━━━┫\n" + "┃ 1 ┃ The Shawshank Redemption ┃ 1994 ┃ 9.5 ┃\n" + "┃ 2 ┃ 12 Angry Men ┃ 1957 ┃ 8.8 ┃\n" + "┃ 3 ┃ It's a Wonderful Life ┃ 1946 ┃ 8.6 ┃\n" + "┣━━━━━━╋━━━━━━━━━━━━━━━━━━━━━━━━━━╋━━━━━━╋━━━━━━━━┫\n" + "┃ 4 ┃ 2001: A Space Odyssey ┃ 1968 ┃ 8.5 ┃\n" + "┃ 5 ┃ Blade Runner ┃ 1982 ┃ 8.1 ┃\n" + "┗━━━━━━┻━━━━━━━━━━━━━━━━━━━━━━━━━━┻━━━━━━┻━━━━━━━━┛\n"; + assert_str_equal(table_str, table_str_etalon); + ft_destroy_table(table); + + /* *************************************************************** */ + + ft_set_default_border_style(FT_BOLD2_STYLE); + table = create_basic_table(); + table_str = ft_to_string(table); + assert_true(table_str != NULL); + + table_str_etalon = + "┏━━━━━━┯━━━━━━━━━━━━━━━━━━━━━━━━━━┯━━━━━━┯━━━━━━━━┓\n" + "┃ Rank │ Title │ Year │ Rating ┃\n" + "┣━━━━━━┿━━━━━━━━━━━━━━━━━━━━━━━━━━┿━━━━━━┿━━━━━━━━┫\n" + "┃ 1 │ The Shawshank Redemption │ 1994 │ 9.5 ┃\n" + "┠──────┼──────────────────────────┼──────┼────────┨\n" + "┃ 2 │ 12 Angry Men │ 1957 │ 8.8 ┃\n" + "┠──────┼──────────────────────────┼──────┼────────┨\n" + "┃ 3 │ It's a Wonderful Life │ 1946 │ 8.6 ┃\n" + "┣━━━━━━┿━━━━━━━━━━━━━━━━━━━━━━━━━━┿━━━━━━┿━━━━━━━━┫\n" + "┃ 4 │ 2001: A Space Odyssey │ 1968 │ 8.5 ┃\n" + "┠──────┼──────────────────────────┼──────┼────────┨\n" + "┃ 5 │ Blade Runner │ 1982 │ 8.1 ┃\n" + "┗━━━━━━┷━━━━━━━━━━━━━━━━━━━━━━━━━━┷━━━━━━┷━━━━━━━━┛\n"; + assert_str_equal(table_str, table_str_etalon); + ft_destroy_table(table); + + /* *************************************************************** */ + + ft_set_default_border_style(FT_FRAME_STYLE); + table = create_basic_table(); + table_str = ft_to_string(table); + assert_true(table_str != NULL); + + table_str_etalon = + "▛▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▜\n" + "▌ Rank ┃ Title ┃ Year ┃ Rating ▐\n" + "▌━━━━━━╋━━━━━━━━━━━━━━━━━━━━━━━━━━╋━━━━━━╋━━━━━━━━▐\n" + "▌ 1 ┃ The Shawshank Redemption ┃ 1994 ┃ 9.5 ▐\n" + "▌ 2 ┃ 12 Angry Men ┃ 1957 ┃ 8.8 ▐\n" + "▌ 3 ┃ It's a Wonderful Life ┃ 1946 ┃ 8.6 ▐\n" + "▌━━━━━━╋━━━━━━━━━━━━━━━━━━━━━━━━━━╋━━━━━━╋━━━━━━━━▐\n" + "▌ 4 ┃ 2001: A Space Odyssey ┃ 1968 ┃ 8.5 ▐\n" + "▌ 5 ┃ Blade Runner ┃ 1982 ┃ 8.1 ▐\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 45f7365..0686e13 100644 --- a/tests/main_test.c +++ b/tests/main_test.c @@ -34,6 +34,7 @@ struct test_case bb_test_suit [] = { #endif {"test_table_write", test_table_write}, {"test_table_border_style", test_table_border_style}, + {"test_table_builtin_border_styles", test_table_builtin_border_styles}, {"test_table_cell_options", test_table_cell_options}, {"test_table_tbl_options", test_table_tbl_options}, {"test_memory_errors", test_memory_errors}, diff --git a/tests/tests.h b/tests/tests.h index 3db3519..2f990a7 100644 --- a/tests/tests.h +++ b/tests/tests.h @@ -30,6 +30,7 @@ void test_wcs_table_boundaries(void); #endif void test_table_write(void); void test_table_border_style(void); +void test_table_builtin_border_styles(void); void test_table_cell_options(void); void test_table_tbl_options(void); void test_memory_errors(void);