-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_extreme.c
60 lines (55 loc) · 1.5 KB
/
test_extreme.c
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include "malloc.h"
#include "internal.h"
#include "test_malloc.h"
#include <limits.h>
#include <errno.h>
static void test_extreme_malloc_single(size_t n) {
errno = 0;
void* mem = malloc(n);
if (mem == NULL) {
EXPECT_EQ_I(errno, ENOMEM);
} else {
EXPECT_EQ_I(errno, 0);
}
free(mem);
}
void test_extreme_malloc(void) {
test_extreme_malloc_single(0);
test_extreme_malloc_single(UINT_MAX);
test_extreme_malloc_single(SIZE_MAX / 2);
test_extreme_malloc_single(SIZE_MAX - 1000);
test_extreme_malloc_single(SIZE_MAX - 100);
test_extreme_malloc_single(SIZE_MAX - 10);
test_extreme_malloc_single(SIZE_MAX - 1);
test_extreme_malloc_single(SIZE_MAX);
}
static void test_extreme_realloc_single(size_t n, size_t m) {
errno = 0;
void* mem = realloc(NULL, n);
if (mem == NULL) {
EXPECT_EQ_I(errno, ENOMEM);
} else {
EXPECT_EQ_I(errno, 0);
}
errno = 0;
void* mem2 = realloc(mem, m);
if (mem2 == NULL) {
EXPECT_EQ_I(errno, ENOMEM);
} else {
EXPECT_EQ_I(errno, 0);
}
if (mem != mem2 && mem2 == NULL) {
free(mem);
}
free(mem2);
}
void test_extreme_realloc(void) {
size_t sizes[] = {1, UINT_MAX, SIZE_MAX / 2, SIZE_MAX - 1000, SIZE_MAX - 100, SIZE_MAX - 10, SIZE_MAX - 1, SIZE_MAX};
// size_t sizes[] = {1, UINT_MAX, SIZE_MAX / 2};
for (size_t i = 0; i < sizeof(sizes) / sizeof(sizes[0]); ++i) {
for (size_t j = 0; j < sizeof(sizes) / sizeof(sizes[0]); ++j) {
yoyo_dprintf(STDOUT_FILENO, "<< %zu B -> %zu B >>\n", sizes[i], sizes[j]);
test_extreme_realloc_single(sizes[i], sizes[j]);
}
}
}