GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: test/test_alloc.c Lines: 101 101 100.0 %
Date: 2020-12-10 21:44:00 Branches: 90 258 34.9 %

Line Branch Exec Source
1
#include <signal.h>
2
#include <stddef.h>
3
#include <criterion/criterion.h>
4
5
#include "alloc.h"
6
7
13
static void setup(void) {
8
#ifdef USE_RPMALLOC
9
13
    rpmalloc_initialize();
10
#endif
11
13
}
12
13
13
static void teardown(void) {
14
#ifdef USE_RPMALLOC
15
13
    rpmalloc_finalize();
16
#endif
17
13
}
18
19
20
TestSuite(WSS_malloc, .init = setup, .fini = teardown);
21
22
4
Test(WSS_malloc, size_zero) {
23
2
    char *test = (char *)WSS_malloc(0);
24
25


2
    cr_assert(test == NULL);
26
2
}
27
28
4
Test(WSS_malloc, all_bytes_are_zero_bytes) {
29
2
    int i, n = 10;
30
2
    char *test = (char *)WSS_malloc(n*sizeof(char));
31
32
22
    for (i = 0; i < n; i++) {
33


20
        cr_assert(test[i] == '\0');
34
    }
35
36
2
    WSS_free((void **) &test);
37


2
    cr_assert(test == NULL);
38
2
}
39
40
TestSuite(WSS_copy, .init = setup, .fini = teardown);
41
42
4
Test(WSS_copy, size_zero) {
43
2
    char *test = NULL;
44
2
    char *dup = WSS_copy(test, 0);
45
46


2
    cr_assert(NULL == dup);
47
2
}
48
49
4
Test(WSS_copy, all_bytes_are_zero_bytes) {
50
2
    char *test = "TESTING 123";
51
2
    char *dup = WSS_copy(test, strlen(test)+1);
52
53


2
    cr_assert(strncmp(test, dup, strlen(test)) == 0);
54
2
    WSS_free((void **) &dup);
55
2
}
56
57
TestSuite(WSS_calloc, .init = setup, .fini = teardown);
58
59
4
Test(WSS_calloc, memb_zero) {
60
2
    char *test = (char *)WSS_calloc(0, sizeof(int));
61
62


2
    cr_assert(test == NULL);
63
2
}
64
65
4
Test(WSS_calloc, size_zero) {
66
2
    char *test = (char *)WSS_calloc(10, 0);
67
68


2
    cr_assert(test == NULL);
69
2
}
70
71
4
Test(WSS_calloc, can_use_as_integer_array) {
72
2
    int i, n = 10;
73
2
    int *test = (int *)WSS_calloc(10, sizeof(int));
74
75
22
    for (i = 0; i < n; i++) {
76


20
        cr_assert(test[i] == 0);
77
20
        test[i] = i;
78
    }
79
80
22
    for (i = 0; i < n; i++) {
81


20
        cr_assert(test[i] == i);
82
    }
83
84
2
    WSS_free((void **) &test);
85
86


2
    cr_assert(test == NULL);
87
2
}
88
89
TestSuite(WSS_realloc, .init = setup, .fini = teardown);
90
91
4
Test(WSS_realloc, size_zero) {
92
2
    char *test = NULL;
93
2
    test = (char *)WSS_realloc((void **)&test, 0, 0);
94
95


2
    cr_assert(test == NULL);
96
2
}
97
98
4
Test(WSS_realloc, realloc_as_malloc) {
99
2
    int i, n = 10;
100
2
    char *test = (char *) WSS_realloc(NULL, 0, n*sizeof(char));
101
102
22
    for (i = 0; i < n; i++) {
103


20
        cr_assert(test[i] == '\0');
104
    }
105
106
2
    WSS_free((void **) &test);
107
108


2
    cr_assert(test == NULL);
109
2
}
110
111
TestSuite(WSS_realloc_normal, .init = setup, .fini = teardown);
112
113
4
Test(WSS_realloc_normal, realloc_as_malloc) {
114
2
    int i, n = 10;
115
2
    char *test = (char *) WSS_realloc_normal(NULL, n*sizeof(char));
116
117
24
    for (i = 0; i < n; i++) {
118


20
        cr_assert(test[i] == '\0');
119
    }
120
121
2
    WSS_free_normal(test);
122
2
}
123
124
4
Test(WSS_realloc, free_ptr_on_size_zero) {
125
2
    int i, n = 10;
126
2
    char *test = (char *)WSS_malloc(n);
127
2
    char *res;
128
129
22
    for (i = 0; i < n; i++) {
130


20
        cr_assert(test[i] == '\0');
131
    }
132
133
2
    res = WSS_realloc((void **)&test, n, 0);
134
135


2
    cr_assert(res == NULL);
136


2
    cr_assert(test == NULL);
137
2
}
138
139
4
Test(WSS_realloc, all_bytes_are_zero_bytes) {
140
2
    int i, n = 10;
141
2
    char *test = (char *)WSS_malloc(n);
142
143
22
    for (i = 0; i < n; i++) {
144


20
        cr_assert(test[i] == '\0');
145
    }
146
147
2
    test = WSS_realloc((void **)&test, n, n+5);
148
149
32
    for (i = 0; i < n+5; i++) {
150


30
        cr_assert(test[i] == '\0');
151
    }
152
153
2
    WSS_free((void **) &test);
154
155


2
    cr_assert(test == NULL);
156
2
}
157
158
4
Test(WSS_realloc, shrinking) {
159
2
    int i, n = 10;
160
2
    char *test = (char *)WSS_malloc(n);
161
2
    char *res;
162
163
22
    for (i = 0; i < n; i++) {
164


20
        cr_assert(test[i] == '\0');
165
    }
166
167
2
    res = WSS_realloc((void **)&test, n, n-(n/2));
168
169


2
    cr_assert(test != NULL);
170


2
    cr_assert(res != NULL);
171
172
2
    WSS_free((void **) &res);
173
174


2
    cr_assert(res == NULL);
175
2
}