libfort/tests/bb_tests/test_memory_errors.c

140 lines
3.3 KiB
C
Raw Normal View History

#include "tests.h"
2018-05-06 13:11:03 +02:00
#include "fort.h"
static int aloc_num = 0;
static int aloc_lim = 9999;
void *test_malloc(size_t size)
{
if (aloc_num < aloc_lim) {
void *result = malloc(size);
if (result)
aloc_num++;
return result;
} else {
return NULL;
}
}
void test_free(void *ptr)
{
if (ptr != 0) {
aloc_num--;
free(ptr);
}
}
2018-12-01 09:43:29 +01:00
static int create_table_and_show(void)
{
2018-05-05 21:34:45 +02:00
ft_table_t *table = NULL;
2019-01-02 07:52:31 +01:00
ft_table_t *table_copy = NULL;
int result = 0;
table = ft_create_table();
if (table == NULL) {
result = 1;
goto exit;
}
/*
2018-11-03 21:50:30 +01:00
if (set_test_props_for_table(table) != FT_SUCCESS)
return 2;
*/
2018-11-03 21:50:30 +01:00
if (ft_set_cell_prop(table, 0, FT_ANY_COLUMN, FT_CPROP_ROW_TYPE, FT_ROW_HEADER) != FT_SUCCESS) {
result = 3;
goto exit;
}
2019-01-02 08:08:36 +01:00
if (ft_set_cell_prop(table, FT_ANY_ROW, 0, FT_CPROP_LEFT_PADDING, 2) != FT_SUCCESS) {
result = 4;
goto exit;
}
2019-01-02 08:08:36 +01:00
if (ft_set_cell_prop(table, 2, 1, FT_CPROP_TOP_PADDING, 2) != FT_SUCCESS) {
result = 5;
goto exit;
}
2019-01-02 08:08:36 +01:00
if (ft_set_cell_prop(table, 2, 1, FT_CPROP_BOTTOM_PADDING, 0) != FT_SUCCESS) {
result = 6;
goto exit;
}
if (ft_write_ln(table, "3", "c", "234", "3.140000") != FT_SUCCESS) {
result = 7;
goto exit;
}
if (ft_write_ln(table, "3", "c", "234", "3.140000") != FT_SUCCESS) {
result = 8;
goto exit;
}
2018-12-01 09:43:29 +01:00
ft_add_separator(table);
2018-04-24 19:41:14 +02:00
if (ft_write_ln(table, "3", "c", "234", "3.140000") != FT_SUCCESS) {
2019-01-02 08:08:36 +01:00
result = 9;
goto exit;
}
2018-12-22 10:30:26 +01:00
if (ft_printf_ln(table, "%d|%c|%d|%f", 3, 'c', 234, 3.140000) != 4) {
2019-01-02 08:08:36 +01:00
result = 10;
2018-12-22 10:30:26 +01:00
goto exit;
}
2019-01-02 07:52:31 +01:00
table_copy = ft_copy_table(table);
if (table_copy == NULL) {
2019-01-02 08:08:36 +01:00
result = 11;
goto exit;
}
2019-01-02 07:52:31 +01:00
const char *table_str = ft_to_string(table_copy);
if (table_str == NULL) {
2019-01-02 08:08:36 +01:00
result = 12;
2019-01-02 07:52:31 +01:00
goto exit;
}
const char *table_str_etalon =
2019-01-02 08:08:36 +01:00
"+----+---+-----+----------+\n"
"| | | | |\n"
"| 3 | c | 234 | 3.140000 |\n"
"| | | | |\n"
"+----+---+-----+----------+\n"
"| | | | |\n"
"| 3 | c | 234 | 3.140000 |\n"
"| | | | |\n"
"+====+===+=====+==========+\n"
"| | | | |\n"
"| 3 | | 234 | 3.140000 |\n"
"| | c | | |\n"
"+----+---+-----+----------+\n"
"| | | | |\n"
"| 3 | c | 234 | 3.140000 |\n"
"| | | | |\n"
"+----+---+-----+----------+\n";
/*assert_str_equal(table_str, table_str_etalon);*/
if (strcmp(table_str, table_str_etalon) != 0) {
2019-01-02 08:08:36 +01:00
result = 13;
goto exit;
}
exit:
ft_destroy_table(table);
2019-01-02 07:52:31 +01:00
ft_destroy_table(table_copy);
return result;
}
void test_memory_errors(void)
{
ft_set_memory_funcs(&test_malloc, &test_free);
const int ITER_MAX = 150;
int i;
for (i = 0; i < ITER_MAX; ++i) {
aloc_lim = i;
2018-12-01 09:43:29 +01:00
int result = create_table_and_show();
if (result == 0)
break;
if (aloc_num != 0) {
assert_true(0);
break;
}
}
assert_true(i != ITER_MAX);
ft_set_memory_funcs(NULL, NULL);
}