aboutsummaryrefslogtreecommitdiff
path: root/tests/mem/alloc_tests.c
blob: 17973c7d8cfa2db2bc8f38c74c42c9c80991b827 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include "alloc_tests.h"

#include <limits.h>
#include <moslib/mem/alloc.h>
#include <stdlib.h>

Test malloc_exit() {
    safe_malloc(ULLONG_MAX);
}

Test test_safe_malloc() {
    void *p = safe_malloc(1024);
    test_assert(p != NULL, "Failed to malloc 1KiB");

    suppress_output(&stderr);
    test_assert_exit(malloc_exit, EXIT_FAILURE);
    unsuppress_outputs();
}

Test realloc_exit() {
    void *p = NULL;
    safe_realloc(p, ULLONG_MAX);
}

Test test_safe_realloc() {
    void *p = NULL;
    p = safe_realloc(p, 1024);
    test_assert(p != NULL, "Failed to realloc NULL to 1KiB");

    p = safe_realloc(p, 4096);
    test_assert(p != NULL, "Failed to realloc 1KiB to 4KiB");

    suppress_output(&stderr);
    test_assert_exit(realloc_exit, EXIT_FAILURE);
    unsuppress_outputs();
}

void mem_alloc_tests(Tester *tester) {
    TestGroup *group = add_test_group(tester, "moslib/mem/alloc.h");
    add_test(group, test_safe_malloc);
    add_test(group, test_safe_realloc);
}