]> git.proxmox.com Git - libgit2.git/blob - tests/core/pool.c
New upstream version 1.4.3+dfsg.1
[libgit2.git] / tests / core / pool.c
1 #include "clar_libgit2.h"
2 #include "pool.h"
3 #include "git2/oid.h"
4
5 void test_core_pool__0(void)
6 {
7 int i;
8 git_pool p;
9 void *ptr;
10
11 git_pool_init(&p, 1);
12
13 for (i = 1; i < 10000; i *= 2) {
14 ptr = git_pool_malloc(&p, i);
15 cl_assert(ptr != NULL);
16 cl_assert(git_pool__ptr_in_pool(&p, ptr));
17 cl_assert(!git_pool__ptr_in_pool(&p, &i));
18 }
19
20 git_pool_clear(&p);
21 }
22
23 void test_core_pool__1(void)
24 {
25 int i;
26 git_pool p;
27
28 git_pool_init(&p, 1);
29 p.page_size = 4000;
30
31 for (i = 2010; i > 0; i--)
32 cl_assert(git_pool_malloc(&p, i) != NULL);
33
34 #ifndef GIT_DEBUG_POOL
35 /* with fixed page size, allocation must end up with these values */
36 cl_assert_equal_i(591, git_pool__open_pages(&p));
37 #endif
38 git_pool_clear(&p);
39
40 git_pool_init(&p, 1);
41 p.page_size = 4120;
42
43 for (i = 2010; i > 0; i--)
44 cl_assert(git_pool_malloc(&p, i) != NULL);
45
46 #ifndef GIT_DEBUG_POOL
47 /* with fixed page size, allocation must end up with these values */
48 cl_assert_equal_i(sizeof(void *) == 8 ? 575 : 573, git_pool__open_pages(&p));
49 #endif
50 git_pool_clear(&p);
51 }
52
53 static char to_hex[] = "0123456789abcdef";
54
55 void test_core_pool__2(void)
56 {
57 git_pool p;
58 char oid_hex[GIT_OID_HEXSZ];
59 git_oid *oid;
60 int i, j;
61
62 memset(oid_hex, '0', sizeof(oid_hex));
63
64 git_pool_init(&p, sizeof(git_oid));
65 p.page_size = 4000;
66
67 for (i = 1000; i < 10000; i++) {
68 oid = git_pool_malloc(&p, 1);
69 cl_assert(oid != NULL);
70
71 for (j = 0; j < 8; j++)
72 oid_hex[j] = to_hex[(i >> (4 * j)) & 0x0f];
73 cl_git_pass(git_oid_fromstr(oid, oid_hex));
74 }
75
76 #ifndef GIT_DEBUG_POOL
77 /* with fixed page size, allocation must end up with these values */
78 cl_assert_equal_i(sizeof(void *) == 8 ? 55 : 45, git_pool__open_pages(&p));
79 #endif
80 git_pool_clear(&p);
81 }
82
83 void test_core_pool__strndup_limit(void)
84 {
85 git_pool p;
86
87 git_pool_init(&p, 1);
88 /* ensure 64 bit doesn't overflow */
89 cl_assert(git_pool_strndup(&p, "foo", (size_t)-1) == NULL);
90 git_pool_clear(&p);
91 }
92