]>
git.proxmox.com Git - libgit2.git/blob - tests/core/pool.c
1 #include "clar_libgit2.h"
5 void test_core_pool__0(void)
11 cl_git_pass(git_pool_init(&p
, 1, 4000));
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
));
20 /* 1+2+4+8+16+32+64+128+256+512+1024 -> original block */
25 cl_assert(git_pool__open_pages(&p
) + git_pool__full_pages(&p
) == 4);
30 void test_core_pool__1(void)
35 cl_git_pass(git_pool_init(&p
, 1, 4000));
37 for (i
= 2010; i
> 0; i
--)
38 cl_assert(git_pool_malloc(&p
, i
) != NULL
);
40 /* with fixed page size, allocation must end up with these values */
41 cl_assert(git_pool__open_pages(&p
) == 1);
42 cl_assert(git_pool__full_pages(&p
) == 505);
46 cl_git_pass(git_pool_init(&p
, 1, 4100));
48 for (i
= 2010; i
> 0; i
--)
49 cl_assert(git_pool_malloc(&p
, i
) != NULL
);
51 /* with fixed page size, allocation must end up with these values */
52 cl_assert(git_pool__open_pages(&p
) == 1);
53 cl_assert(git_pool__full_pages(&p
) == 492);
58 static char to_hex
[] = "0123456789abcdef";
60 void test_core_pool__2(void)
63 char oid_hex
[GIT_OID_HEXSZ
];
67 memset(oid_hex
, '0', sizeof(oid_hex
));
69 cl_git_pass(git_pool_init(&p
, sizeof(git_oid
), 100));
71 for (i
= 1000; i
< 10000; i
++) {
72 oid
= git_pool_malloc(&p
, 1);
73 cl_assert(oid
!= NULL
);
75 for (j
= 0; j
< 8; j
++)
76 oid_hex
[j
] = to_hex
[(i
>> (4 * j
)) & 0x0f];
77 cl_git_pass(git_oid_fromstr(oid
, oid_hex
));
80 /* with fixed page size, allocation must end up with these values */
81 cl_assert(git_pool__open_pages(&p
) == 0);
82 cl_assert(git_pool__full_pages(&p
) == 90);
87 void test_core_pool__free_list(void)
93 cl_git_pass(git_pool_init(&p
, 100, 100));
95 for (i
= 0; i
< 10; ++i
) {
96 ptr
= git_pool_malloc(&p
, 1);
97 cl_assert(ptr
!= NULL
);
99 cl_assert_equal_i(10, (int)p
.items
);
101 for (i
= 0; i
< 50; ++i
) {
102 ptrs
[i
] = git_pool_malloc(&p
, 1);
103 cl_assert(ptrs
[i
] != NULL
);
105 cl_assert_equal_i(60, (int)p
.items
);
107 git_pool_free(&p
, ptr
);
108 cl_assert_equal_i(60, (int)p
.items
);
110 git_pool_free_array(&p
, 50, ptrs
);
111 cl_assert_equal_i(60, (int)p
.items
);
113 for (i
= 0; i
< 50; ++i
) {
114 ptrs
[i
] = git_pool_malloc(&p
, 1);
115 cl_assert(ptrs
[i
] != NULL
);
117 cl_assert_equal_i(60, (int)p
.items
);
119 for (i
= 0; i
< 111; ++i
) {
120 ptr
= git_pool_malloc(&p
, 1);
121 cl_assert(ptr
!= NULL
);
123 cl_assert_equal_i(170, (int)p
.items
);
125 git_pool_free_array(&p
, 50, ptrs
);
126 cl_assert_equal_i(170, (int)p
.items
);
128 for (i
= 0; i
< 50; ++i
) {
129 ptrs
[i
] = git_pool_malloc(&p
, 1);
130 cl_assert(ptrs
[i
] != NULL
);
132 cl_assert_equal_i(170, (int)p
.items
);
137 void test_core_pool__strndup_limit(void)
141 cl_git_pass(git_pool_init(&p
, 1, 100));
142 cl_assert(git_pool_strndup(&p
, "foo", -1) == NULL
);