]> git.ozlabs.org Git - ccan/blob - ccan/alloc/test/run-testsize.c
Remove old run-tests, clean up #includes to all be <ccan/...
[ccan] / ccan / alloc / test / run-testsize.c
1 #include <ccan/alloc/alloc.h>
2 #include <ccan/tap/tap.h>
3 #include <ccan/alloc/alloc.c>
4 #include <stdlib.h>
5 #include <stdbool.h>
6 #include <err.h>
7
8 #define POOL_ORD 16
9 #define POOL_SIZE (1 << POOL_ORD)
10
11 static void invert_bytes(unsigned char *p, unsigned long size)
12 {
13         unsigned int i;
14
15         for (i = 0; i < size; i++)
16                 p[i] ^= 0xFF;
17 }
18
19 static bool sizes_ok(void *mem, unsigned long poolsize, void *p[], unsigned num)
20 {
21         unsigned int i;
22
23         for (i = 0; i < num; i++)
24                 if (alloc_size(mem, poolsize, p[i]) < i)
25                         return false;
26         return true;
27 }
28
29 int main(int argc, char *argv[])
30 {
31         void *mem;
32         unsigned int i, num;
33         void *p[POOL_SIZE];
34
35         plan_tests(5);
36
37         /* FIXME: Needs to be page aligned for now. */
38         if (posix_memalign(&mem, 1 << POOL_ORD, POOL_SIZE) != 0)
39                 errx(1, "Failed allocating aligned memory"); 
40
41         alloc_init(mem, POOL_SIZE);
42
43         /* Check that alloc_size() gives reasonable answers. */
44         for (i = 0; i < POOL_SIZE; i++) {
45                 p[i] = alloc_get(mem, POOL_SIZE, i, 1);
46                 if (!p[i])
47                         break;
48                 invert_bytes(p[i], alloc_size(mem, POOL_SIZE, p[i]));
49         }
50         ok1(i < POOL_SIZE);
51         num = i;
52         ok1(alloc_check(mem, POOL_SIZE));
53         ok1(sizes_ok(mem, POOL_SIZE, p, num));
54
55         /* Free every second one. */
56         for (i = 0; i < num; i+=2) {
57                 alloc_free(mem, POOL_SIZE, p[i]);
58                 /* Compact. */
59                 if (i + 1 < num) {
60                         p[i/2] = p[i + 1];
61                         invert_bytes(p[i/2], alloc_size(mem,POOL_SIZE,p[i/2]));
62                 }
63         }
64         num /= 2;
65         ok1(alloc_check(mem, POOL_SIZE));
66         ok1(sizes_ok(mem, POOL_SIZE, p, num));
67
68         return exit_status();
69 }