Mercurial > molko
comparison tests/test-alloc.c @ 228:2734223d3daf
core: add a alloc_pool module
A miniamlist expandable array that gros each time new data is required, ideal
where data must be allocated dynamically without knowing in advance the number
of items.
author | David Demelier <markand@malikania.fr> |
---|---|
date | Thu, 19 Nov 2020 14:11:11 +0100 |
parents | |
children | 9b758eb84556 |
comparison
equal
deleted
inserted
replaced
227:befa2e855d3b | 228:2734223d3daf |
---|---|
1 /* | |
2 * test-alloc.c -- test allocators | |
3 * | |
4 * Copyright (c) 2020 David Demelier <markand@malikania.fr> | |
5 * | |
6 * Permission to use, copy, modify, and/or distribute this software for any | |
7 * purpose with or without fee is hereby granted, provided that the above | |
8 * copyright notice and this permission notice appear in all copies. | |
9 * | |
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | |
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | |
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | |
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | |
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | |
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | |
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | |
17 */ | |
18 | |
19 #define GREATEST_USE_ABBREVS 0 | |
20 #include <greatest.h> | |
21 | |
22 #include <core/alloc.h> | |
23 | |
24 struct point { | |
25 int x; | |
26 int y; | |
27 }; | |
28 | |
29 GREATEST_TEST | |
30 test_basics_simple(void) | |
31 { | |
32 struct alloc_pool pool; | |
33 struct point *p; | |
34 | |
35 GREATEST_ASSERT(alloc_pool_init(&pool, sizeof (*p), NULL)); | |
36 GREATEST_ASSERT_EQ(sizeof (*p), pool.elemsize); | |
37 GREATEST_ASSERT_EQ(0, pool.size); | |
38 GREATEST_ASSERT_EQ(ALLOC_POOL_INIT_DEFAULT, pool.capacity); | |
39 | |
40 /* Create until we reach the capacity. */ | |
41 for (size_t i = 0; i < pool.capacity; ++i) { | |
42 p = alloc_pool_new(&pool); | |
43 p->x = (int)i + 1; | |
44 p->y = (int)i + 1; | |
45 } | |
46 | |
47 GREATEST_ASSERT_EQ(pool.size, pool.capacity); | |
48 | |
49 /* Verify values are correct. */ | |
50 for (size_t i = 0; i < pool.size; ++i) { | |
51 p = ((struct point *)pool.data) + i; | |
52 | |
53 GREATEST_ASSERT_EQ((int)i + 1, p->x); | |
54 GREATEST_ASSERT_EQ((int)i + 1, p->y); | |
55 } | |
56 | |
57 /* Now it should reallocate. */ | |
58 p = alloc_pool_new(&pool); | |
59 p->x = 9999; | |
60 p->y = 9999; | |
61 | |
62 GREATEST_ASSERT(pool.capacity > pool.size); | |
63 | |
64 alloc_pool_finish(&pool); | |
65 | |
66 GREATEST_ASSERT_EQ(NULL, pool.data); | |
67 GREATEST_ASSERT_EQ(0, pool.elemsize); | |
68 GREATEST_ASSERT_EQ(0, pool.size); | |
69 GREATEST_ASSERT_EQ(0, pool.capacity); | |
70 | |
71 GREATEST_PASS(); | |
72 } | |
73 | |
74 GREATEST_SUITE(suite_basics) | |
75 { | |
76 GREATEST_RUN_TEST(test_basics_simple); | |
77 } | |
78 | |
79 GREATEST_MAIN_DEFS(); | |
80 | |
81 int | |
82 main(int argc, char **argv) | |
83 { | |
84 GREATEST_MAIN_BEGIN(); | |
85 GREATEST_RUN_SUITE(suite_basics); | |
86 GREATEST_MAIN_END(); | |
87 | |
88 return 0; | |
89 } |