]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
b66de1f9 | 2 | |
41364475 | 3 | #include <malloc.h> |
f30788ee YW |
4 | #include <stdint.h> |
5 | ||
b66de1f9 RC |
6 | #include "alloc-util.h" |
7 | #include "macro.h" | |
0a970718 | 8 | #include "memory-util.h" |
b66de1f9 RC |
9 | |
10 | static void test_alloca(void) { | |
11 | static const uint8_t zero[997] = { }; | |
12 | char *t; | |
13 | ||
14 | t = alloca_align(17, 512); | |
15 | assert_se(!((uintptr_t)t & 0xff)); | |
16 | memzero(t, 17); | |
17 | ||
18 | t = alloca0_align(997, 1024); | |
19 | assert_se(!((uintptr_t)t & 0x1ff)); | |
20 | assert_se(!memcmp(t, zero, 997)); | |
21 | } | |
22 | ||
cc99274d ZJS |
23 | static void test_GREEDY_REALLOC(void) { |
24 | _cleanup_free_ int *a = NULL, *b = NULL; | |
41364475 | 25 | size_t n_allocated = 0, i, j; |
cc99274d | 26 | |
41364475 | 27 | /* Give valgrind a chance to verify our realloc() operations */ |
cc99274d | 28 | |
41364475 | 29 | for (i = 0; i < 20480; i++) { |
cc99274d | 30 | assert_se(GREEDY_REALLOC(a, n_allocated, i + 1)); |
41364475 LP |
31 | assert_se(n_allocated >= i + 1); |
32 | assert_se(malloc_usable_size(a) >= (i + 1) * sizeof(int)); | |
33 | a[i] = (int) i; | |
cc99274d | 34 | assert_se(GREEDY_REALLOC(a, n_allocated, i / 2)); |
41364475 LP |
35 | assert_se(n_allocated >= i / 2); |
36 | assert_se(malloc_usable_size(a) >= (i / 2) * sizeof(int)); | |
cc99274d ZJS |
37 | } |
38 | ||
41364475 LP |
39 | for (j = 0; j < i / 2; j++) |
40 | assert_se(a[j] == (int) j); | |
41 | ||
42 | for (i = 30, n_allocated = 0; i < 20480; i += 7) { | |
cc99274d | 43 | assert_se(GREEDY_REALLOC(b, n_allocated, i + 1)); |
41364475 LP |
44 | assert_se(n_allocated >= i + 1); |
45 | assert_se(malloc_usable_size(b) >= (i + 1) * sizeof(int)); | |
46 | b[i] = (int) i; | |
cc99274d | 47 | assert_se(GREEDY_REALLOC(b, n_allocated, i / 2)); |
41364475 LP |
48 | assert_se(n_allocated >= i / 2); |
49 | assert_se(malloc_usable_size(b) >= (i / 2) * sizeof(int)); | |
cc99274d | 50 | } |
41364475 LP |
51 | |
52 | for (j = 30; j < i / 2; j += 7) | |
53 | assert_se(b[j] == (int) j); | |
cc99274d ZJS |
54 | } |
55 | ||
f30788ee | 56 | static void test_memdup_multiply_and_greedy_realloc(void) { |
b66de1f9 | 57 | int org[] = {1, 2, 3}; |
f30788ee YW |
58 | _cleanup_free_ int *dup; |
59 | int *p; | |
60 | size_t i, allocated = 3; | |
b66de1f9 | 61 | |
f30788ee | 62 | dup = (int*) memdup_suffix0_multiply(org, sizeof(int), 3); |
b66de1f9 RC |
63 | assert_se(dup); |
64 | assert_se(dup[0] == 1); | |
65 | assert_se(dup[1] == 2); | |
66 | assert_se(dup[2] == 3); | |
f30788ee | 67 | assert_se(*(uint8_t*) (dup + 3) == (uint8_t) 0); |
b66de1f9 | 68 | free(dup); |
f30788ee YW |
69 | |
70 | dup = (int*) memdup_multiply(org, sizeof(int), 3); | |
71 | assert_se(dup); | |
72 | assert_se(dup[0] == 1); | |
73 | assert_se(dup[1] == 2); | |
74 | assert_se(dup[2] == 3); | |
75 | ||
76 | p = dup; | |
77 | assert_se(greedy_realloc0((void**) &dup, &allocated, 2, sizeof(int)) == p); | |
78 | ||
79 | p = (int *) greedy_realloc0((void**) &dup, &allocated, 10, sizeof(int)); | |
80 | assert_se(p == dup); | |
81 | assert_se(allocated >= 10); | |
82 | assert_se(p[0] == 1); | |
83 | assert_se(p[1] == 2); | |
84 | assert_se(p[2] == 3); | |
85 | for (i = 3; i < allocated; i++) | |
86 | assert_se(p[i] == 0); | |
b66de1f9 RC |
87 | } |
88 | ||
37e744e8 | 89 | static void test_bool_assign(void) { |
108ccae9 | 90 | bool b, c, *cp = &c, d, e, f, g, h; |
37e744e8 ZJS |
91 | |
92 | b = 123; | |
93 | *cp = -11; | |
94 | d = 0xF & 0xFF; | |
95 | e = b & d; | |
96 | f = 0x0; | |
108ccae9 ZJS |
97 | g = cp; /* cast from pointer */ |
98 | h = NULL; /* cast from pointer */ | |
37e744e8 ZJS |
99 | |
100 | assert(b); | |
101 | assert(c); | |
102 | assert(d); | |
103 | assert(e); | |
104 | assert(!f); | |
108ccae9 ZJS |
105 | assert(g); |
106 | assert(!h); | |
37e744e8 ZJS |
107 | } |
108 | ||
b66de1f9 RC |
109 | int main(int argc, char *argv[]) { |
110 | test_alloca(); | |
cc99274d | 111 | test_GREEDY_REALLOC(); |
f30788ee | 112 | test_memdup_multiply_and_greedy_realloc(); |
37e744e8 | 113 | test_bool_assign(); |
b66de1f9 RC |
114 | |
115 | return 0; | |
116 | } |