]> git.proxmox.com Git - libgit2.git/blob - tests/core/pool.c
Merge branch 'development'
[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 cl_git_pass(git_pool_init(&p, 1, 4000));
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 /* 1+2+4+8+16+32+64+128+256+512+1024 -> original block */
21 /* 2048 -> 1 block */
22 /* 4096 -> 1 block */
23 /* 8192 -> 1 block */
24
25 cl_assert(git_pool__open_pages(&p) + git_pool__full_pages(&p) == 4);
26
27 git_pool_clear(&p);
28 }
29
30 void test_core_pool__1(void)
31 {
32 int i;
33 git_pool p;
34
35 cl_git_pass(git_pool_init(&p, 1, 4000));
36
37 for (i = 2010; i > 0; i--)
38 cl_assert(git_pool_malloc(&p, i) != NULL);
39
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);
43
44 git_pool_clear(&p);
45
46 cl_git_pass(git_pool_init(&p, 1, 4100));
47
48 for (i = 2010; i > 0; i--)
49 cl_assert(git_pool_malloc(&p, i) != NULL);
50
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);
54
55 git_pool_clear(&p);
56 }
57
58 static char to_hex[] = "0123456789abcdef";
59
60 void test_core_pool__2(void)
61 {
62 git_pool p;
63 char oid_hex[GIT_OID_HEXSZ];
64 git_oid *oid;
65 int i, j;
66
67 memset(oid_hex, '0', sizeof(oid_hex));
68
69 cl_git_pass(git_pool_init(&p, sizeof(git_oid), 100));
70
71 for (i = 1000; i < 10000; i++) {
72 oid = git_pool_malloc(&p, 1);
73 cl_assert(oid != NULL);
74
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));
78 }
79
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);
83
84 git_pool_clear(&p);
85 }
86
87 void test_core_pool__free_list(void)
88 {
89 int i;
90 git_pool p;
91 void *ptr, *ptrs[50];
92
93 cl_git_pass(git_pool_init(&p, 100, 100));
94
95 for (i = 0; i < 10; ++i) {
96 ptr = git_pool_malloc(&p, 1);
97 cl_assert(ptr != NULL);
98 }
99 cl_assert_equal_i(10, (int)p.items);
100
101 for (i = 0; i < 50; ++i) {
102 ptrs[i] = git_pool_malloc(&p, 1);
103 cl_assert(ptrs[i] != NULL);
104 }
105 cl_assert_equal_i(60, (int)p.items);
106
107 git_pool_free(&p, ptr);
108 cl_assert_equal_i(60, (int)p.items);
109
110 git_pool_free_array(&p, 50, ptrs);
111 cl_assert_equal_i(60, (int)p.items);
112
113 for (i = 0; i < 50; ++i) {
114 ptrs[i] = git_pool_malloc(&p, 1);
115 cl_assert(ptrs[i] != NULL);
116 }
117 cl_assert_equal_i(60, (int)p.items);
118
119 for (i = 0; i < 111; ++i) {
120 ptr = git_pool_malloc(&p, 1);
121 cl_assert(ptr != NULL);
122 }
123 cl_assert_equal_i(170, (int)p.items);
124
125 git_pool_free_array(&p, 50, ptrs);
126 cl_assert_equal_i(170, (int)p.items);
127
128 for (i = 0; i < 50; ++i) {
129 ptrs[i] = git_pool_malloc(&p, 1);
130 cl_assert(ptrs[i] != NULL);
131 }
132 cl_assert_equal_i(170, (int)p.items);
133
134 git_pool_clear(&p);
135 }
136
137 void test_core_pool__strndup_limit(void)
138 {
139 git_pool p;
140
141 cl_git_pass(git_pool_init(&p, 1, 100));
142 cl_assert(git_pool_strndup(&p, "foo", -1) == NULL);
143 git_pool_clear(&p);
144 }
145