nextcloud-desktop/tests/std_tests/check_std_c_alloc.c

150 lines
2.8 KiB
C
Raw Normal View History

2008-02-27 20:56:47 +03:00
#include "support.h"
2008-07-28 13:49:56 +04:00
#include <stdlib.h>
2008-02-27 20:56:47 +03:00
#include <string.h>
#include "std/c_alloc.h"
struct test_s {
int answer;
};
2008-07-28 13:49:56 +04:00
#ifdef CSYNC_MEM_NULL_TESTS
static void setup(void) {
setenv("CSYNC_NOMEMORY", "1", 1);
}
static void teardown(void) {
unsetenv("CSYNC_NOMEMORY");
}
#endif /* CSYNC_MEM_NULL_TESTS */
2008-02-27 20:56:47 +03:00
START_TEST (check_c_malloc)
{
struct test_s *p = NULL;
p = c_malloc(sizeof(struct test_s));
fail_unless(p != NULL, NULL);
fail_unless(p->answer == 0, NULL);
p->answer = 42;
fail_unless(p->answer == 42, NULL);
free(p);
}
END_TEST
START_TEST (check_c_malloc_zero)
{
void *p;
p = c_malloc((size_t) 0);
fail_unless(p == NULL, NULL);
}
END_TEST
START_TEST (check_c_strdup)
{
2008-07-28 13:49:56 +04:00
const char *str = "test";
char *tdup = NULL;
2008-02-27 20:56:47 +03:00
2008-04-18 21:16:54 +04:00
tdup = c_strdup(str);
fail_unless(strcmp(tdup, str) == 0, NULL);
2008-07-28 13:49:56 +04:00
2008-04-18 21:16:54 +04:00
free(tdup);
2008-02-27 20:56:47 +03:00
}
END_TEST
2008-07-28 13:49:56 +04:00
START_TEST (check_c_strndup)
{
const char *str = "test";
char *tdup = NULL;
tdup = c_strndup(str, 3);
fail_unless(strncmp(tdup, "tes", 3) == 0, NULL);
free(tdup);
}
END_TEST
#ifdef CSYNC_MEM_NULL_TESTS
START_TEST (check_c_malloc_nomem)
{
struct test_s *p = NULL;
p = c_malloc(sizeof(struct test_s));
fail_unless(p == NULL, NULL);
}
END_TEST
START_TEST (check_c_strdup_nomem)
{
const char *str = "test";
char *tdup = NULL;
tdup = c_strdup(str);
fail_unless(tdup == NULL, NULL);
}
END_TEST
START_TEST (check_c_strndup_nomem)
{
const char *str = "test";
char *tdup = NULL;
tdup = c_strndup(str, 3);
fail_unless(tdup == NULL, NULL);
}
END_TEST
#endif /* CSYNC_MEM_NULL_TESTS */
2008-02-27 20:56:47 +03:00
static Suite *make_c_malloc_suite(void) {
2008-03-04 11:01:58 +03:00
Suite *s = suite_create("std:alloc:malloc");
2008-02-27 20:56:47 +03:00
create_case(s, "check_c_malloc", check_c_malloc);
create_case(s, "check_c_malloc_zero", check_c_malloc_zero);
2008-07-28 13:49:56 +04:00
#ifdef CSYNC_MEM_NULL_TESTS
create_case_fixture(s, "check_c_malloc_nomem", check_c_malloc_nomem, setup, teardown);
#endif
2008-02-27 20:56:47 +03:00
return s;
}
static Suite *make_c_strdup_suite(void) {
2008-03-04 11:01:58 +03:00
Suite *s = suite_create("std:alloc:strdup");
2008-02-27 20:56:47 +03:00
create_case(s, "check_c_strdup", check_c_strdup);
2008-07-28 13:49:56 +04:00
create_case(s, "check_c_strndup", check_c_strndup);
#ifdef CSYNC_MEM_NULL_TESTS
create_case_fixture(s, "check_c_strdup_nomem", check_c_strdup_nomem, setup, teardown);
create_case_fixture(s, "check_c_strndup_nomem", check_c_strndup_nomem, setup, teardown);
#endif
2008-02-27 20:56:47 +03:00
return s;
}
2008-05-07 13:09:56 +04:00
int main(int argc, char **argv) {
Suite *s = NULL;
Suite *s2 = NULL;
SRunner *sr = NULL;
struct argument_s arguments;
2008-02-27 20:56:47 +03:00
int nf;
2008-05-07 13:09:56 +04:00
ZERO_STRUCT(arguments);
cmdline_parse(argc, argv, &arguments);
s = make_c_malloc_suite();
s2 = make_c_strdup_suite();
2008-02-27 20:56:47 +03:00
sr = srunner_create(s);
2008-05-07 13:09:56 +04:00
if (arguments.nofork) {
srunner_set_fork_status(sr, CK_NOFORK);
}
2008-02-27 20:56:47 +03:00
srunner_add_suite (sr, s2);
srunner_run_all(sr, CK_VERBOSE);
nf = srunner_ntests_failed(sr);
srunner_free(sr);
return (nf == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
}