1
0
Fork 0
libfort/tests/test_table.c

400 lines
13 KiB
C
Raw Normal View History

2018-01-02 20:48:38 +01:00
#define FORT_EXTERN static
2018-01-01 09:26:34 +01:00
#include "tests.h"
#include "fort.h"
#include <string.h>
2018-01-01 19:45:01 +01:00
#include <stdlib.h>
#include <stdio.h>
2018-01-02 10:03:58 +01:00
#include "fort.c"
2018-01-02 20:48:38 +01:00
void test_table_sizes(void **state)
2018-01-02 10:03:58 +01:00
{
(void)state;
FTABLE *table = ft_create_table();
2018-01-02 20:48:38 +01:00
size_t rows = 0;
size_t cols = 0;
int status = F_SUCCESS;
WHEN("Table is empty") {
status = get_table_sizes(table, &rows, &cols);
assert_true( IS_SUCCESS(status) );
assert_true( rows == 0 );
assert_true( cols == 0 );
}
WHEN("Insert one cell") {
int n = ft_hdr_printf(table, "%c", 'c');
assert_true( n == 1 );
status = get_table_sizes(table, &rows, &cols);
assert_true( IS_SUCCESS(status) );
assert_true( rows == 1 );
assert_true( cols == 1 );
}
WHEN("Insert two cells in the next row") {
int n = ft_row_printf(table, 1, "%c|%c", 'c', 'd');
assert_true( n == 2 );
status = get_table_sizes(table, &rows, &cols);
assert_true( IS_SUCCESS(status) );
assert_true( rows == 2 );
assert_true( cols == 2 );
}
WHEN("Insert five cells in the next row") {
int n = ft_row_printf(table, 2, "%d|%d|%d|%d|%d", 1, 2, 3, 4, 5);
assert_true( n == 5 );
status = get_table_sizes(table, &rows, &cols);
assert_true( IS_SUCCESS(status) );
assert_true( rows == 3 );
assert_true( cols == 5 );
}
ft_destroy_table(table);
}
void test_table_geometry(void **state)
{
(void)state;
FTABLE *table = ft_create_table();
2018-01-02 10:03:58 +01:00
size_t height = 0;
size_t width = 0;
2018-01-02 20:48:38 +01:00
int status = F_SUCCESS;
2018-01-02 10:03:58 +01:00
2018-01-02 20:48:38 +01:00
WHEN("Table is empty") {
status = table_geometry(table, &height, &width);
assert_true( IS_SUCCESS(status) );
assert_true( height == 2 );
assert_true( width == 3 );
}
WHEN("Table has one cell") {
int n = ft_hdr_printf(table, "%c", 'c');
assert_true( n == 1 );
status = table_geometry(table, &height, &width);
assert_true( IS_SUCCESS(status) );
assert_true( height == 5 );
assert_true( width == 6 );
}
WHEN("Inserting 3 cells in the next row") {
int n = ft_row_printf(table, 1, "%c|%s|%c", 'c', "as", 'e');
assert_true( n == 3 );
status = table_geometry(table, &height, &width);
assert_true( IS_SUCCESS(status) );
assert_true( height == 9 );
assert_true( width == 15 );
}
2018-01-02 10:03:58 +01:00
ft_destroy_table(table);
}
2018-01-01 09:26:34 +01:00
void test_table_basic(void **state)
{
(void)state;
FTABLE *table = ft_create_table();
2018-01-02 20:48:38 +01:00
WHEN("All columns are equal and not empty") {
int n = ft_hdr_printf(table, "%d|%c|%s|%f", 3, 'c', "234", 3.14);
assert_true( n == 4 );
n = ft_row_printf(table, 1, "%d|%c|%s|%f", 3, 'c', "234", 3.14);
assert_true( n == 4 );
n = ft_row_printf(table, 2, "%d|%c|%s|%f", 3, 'c', "234", 3.14);
assert_true( n == 4 );
2018-01-01 19:45:01 +01:00
2018-01-02 20:48:38 +01:00
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"
"| 3 | c | 234 | 3.140000 |\n"
"| | | | |\n"
"==========================\n"
"| | | | |\n"
"| 3 | c | 234 | 3.140000 |\n"
"| | | | |\n"
"==========================\n";
// fprintf(stderr, "content:\n%s", table_str);
2018-01-02 10:03:58 +01:00
2018-01-02 20:48:38 +01:00
assert_true( strcmp(table_str, table_str_etalon) == 0);
2018-01-01 19:45:01 +01:00
2018-01-02 20:48:38 +01:00
free(table_str);
ft_destroy_table(table);
}
2018-01-01 09:26:34 +01:00
2018-01-02 20:48:38 +01:00
WHEN("All columns are not equal and not empty") {
table = ft_create_table();
int n = ft_hdr_printf(table, "%d|%c|%s|%f", 3, 'c', "234", 3.14);
assert_true( n == 4 );
n = ft_row_printf(table, 1, "%c|%s|%f|%d", 'c', "234", 3.14, 3);
assert_true( n == 4 );
n = ft_row_printf(table, 2, "%s|%f|%d|%c", "234", 3.14, 3, 'c');
assert_true( n == 4 );
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);
free(table_str);
ft_destroy_table(table);
}
WHEN("All columns are not equal and some cells are empty") {
table = ft_create_table();
int n = ft_hdr_printf(table, "||%s|%f", "234", 3.14);
assert_true( n == 4 );
n = ft_row_printf(table, 1, "%c|%s|%f", 'c', "234", 3.14);
assert_true( n == 3 );
2018-01-03 08:10:54 +01:00
n = ft_row_printf(table, 2, "%s|%f||", "234", 3.14);
2018-01-02 20:48:38 +01:00
assert_true( n == 4 );
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"
2018-01-03 08:10:54 +01:00
"| 234 | 3.140000 | | |\n"
2018-01-02 20:48:38 +01:00
"| | | | |\n"
"========================================\n";
2018-01-03 08:10:54 +01:00
// fprintf(stderr, "content:\n%s", table_str);
assert_true( strcmp(table_str, table_str_etalon) == 0);
free(table_str);
ft_destroy_table(table);
}
WHEN("All cells are empty") {
table = ft_create_table();
int n = ft_hdr_printf(table, "|||");
assert_true( n == 4 );
n = ft_row_printf(table, 1, "|||");
assert_true( n == 4 );
n = ft_row_printf(table, 2, "|||");
assert_true( n == 4 );
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";
2018-01-02 20:48:38 +01:00
// fprintf(stderr, "content:\n%s", table_str);
assert_true( strcmp(table_str, table_str_etalon) == 0);
free(table_str);
ft_destroy_table(table);
}
2018-01-03 08:40:54 +01:00
}
FTABLE *create_test_int_table()
{
FTABLE *table = NULL;
table = ft_create_table();
assert_true (table != NULL);
int n = ft_hdr_printf(table, "%d|%d|%d|%d", 3, 4, 55, 67);
assert_true( n == 4 );
n = ft_row_printf(table, 1, "%d|%d|%d|%d", 3, 4, 55, 67);
assert_true( n == 4 );
n = ft_row_printf(table, 2, "%d|%d|%d|%d", 3, 4, 55, 67);
assert_true( n == 4 );
return table;
}
void test_table_options(void **state)
{
(void)state;
FTABLE *table = NULL;
fort_table_options_t def_options;
ft_get_default_options(&def_options);
WHEN("All paddings = 1") {
fort_table_options_t table_options;
memcpy(&table_options, &def_options, sizeof(fort_table_options_t));
table_options.cell_padding_bottom = 1;
table_options.cell_padding_top = 1;
table_options.cell_padding_left = 1;
table_options.cell_padding_right = 1;
ft_set_default_options(&table_options);
table = create_test_int_table();
char *table_str = ft_to_string(table);
assert_true( table_str != NULL );
const char *table_str_etalon =
"===================\n"
"| | | | |\n"
"| 3 | 4 | 55 | 67 |\n"
"| | | | |\n"
"===================\n"
"| | | | |\n"
"| 3 | 4 | 55 | 67 |\n"
"| | | | |\n"
"===================\n"
"| | | | |\n"
"| 3 | 4 | 55 | 67 |\n"
"| | | | |\n"
"===================\n";
// fprintf(stderr, "content:\n%s", table_str);
assert_true( strcmp(table_str, table_str_etalon) == 0);
free(table_str);
ft_destroy_table(table);
}
WHEN("Top and bottom padding = 0") {
fort_table_options_t table_options;
memcpy(&table_options, &def_options, sizeof(fort_table_options_t));
table_options.cell_padding_bottom = 0;
table_options.cell_padding_top = 0;
table_options.cell_padding_left = 1;
table_options.cell_padding_right = 1;
ft_set_default_options(&table_options);
table = create_test_int_table();
char *table_str = ft_to_string(table);
assert_true( table_str != NULL );
const char *table_str_etalon =
"===================\n"
"| 3 | 4 | 55 | 67 |\n"
"===================\n"
"| 3 | 4 | 55 | 67 |\n"
"===================\n"
"| 3 | 4 | 55 | 67 |\n"
"===================\n";
// fprintf(stderr, "content:\n%s", table_str);
assert_true( strcmp(table_str, table_str_etalon) == 0);
free(table_str);
ft_destroy_table(table);
}
WHEN("Left and right padding = 0") {
fort_table_options_t table_options;
memcpy(&table_options, &def_options, sizeof(fort_table_options_t));
table_options.cell_padding_bottom = 1;
table_options.cell_padding_top = 1;
table_options.cell_padding_left = 0;
table_options.cell_padding_right = 0;
ft_set_default_options(&table_options);
table = create_test_int_table();
char *table_str = ft_to_string(table);
assert_true( table_str != NULL );
const char *table_str_etalon =
"===========\n"
"| | | | |\n"
"|3|4|55|67|\n"
"| | | | |\n"
"===========\n"
"| | | | |\n"
"|3|4|55|67|\n"
"| | | | |\n"
"===========\n"
"| | | | |\n"
"|3|4|55|67|\n"
"| | | | |\n"
"===========\n";
// fprintf(stderr, "content:\n%s", table_str);
assert_true( strcmp(table_str, table_str_etalon) == 0);
free(table_str);
ft_destroy_table(table);
}
WHEN("All paddings = 0") {
fort_table_options_t table_options;
memcpy(&table_options, &def_options, sizeof(fort_table_options_t));
table_options.cell_padding_bottom = 0;
table_options.cell_padding_top = 0;
table_options.cell_padding_left = 0;
table_options.cell_padding_right = 0;
ft_set_default_options(&table_options);
table = create_test_int_table();
char *table_str = ft_to_string(table);
assert_true( table_str != NULL );
const char *table_str_etalon =
"===========\n"
"|3|4|55|67|\n"
"===========\n"
"|3|4|55|67|\n"
"===========\n"
"|3|4|55|67|\n"
"===========\n";
// fprintf(stderr, "content:\n%s", table_str);
assert_true( strcmp(table_str, table_str_etalon) == 0);
free(table_str);
ft_destroy_table(table);
}
2018-01-02 20:48:38 +01:00
2018-01-01 09:26:34 +01:00
}