@@ -24,20 +24,31 @@ int main(void) {
2424
2525static void test_data_pool_new (void ) {
2626 {
27- MMDB_data_pool_s * const pool = data_pool_new (0 );
27+ MMDB_data_pool_s * const pool = data_pool_new (0 , 512 );
2828 ok (!pool , "size 0 is not valid" );
2929 }
3030
3131 {
32- MMDB_data_pool_s * const pool = data_pool_new (SIZE_MAX - 10 );
32+ MMDB_data_pool_s * const pool =
33+ data_pool_new (SIZE_MAX - 10 , SIZE_MAX );
3334 ok (!pool , "very large size is not valid" );
3435 }
3536
3637 {
37- MMDB_data_pool_s * const pool = data_pool_new (512 );
38+ MMDB_data_pool_s * const pool = data_pool_new (512 , 1024 );
3839 ok (pool != NULL , "size 512 is valid" );
3940 cmp_ok (pool -> size , "==" , 512 , "size is 512" );
4041 cmp_ok (pool -> used , "==" , 0 , "used size is 0" );
42+ cmp_ok (pool -> capacity , "==" , 512 , "capacity is 512" );
43+ cmp_ok (pool -> max_size , "==" , 1024 , "maximum size is 1024" );
44+ data_pool_destroy (pool );
45+ }
46+
47+ {
48+ MMDB_data_pool_s * const pool = data_pool_new (512 , 10 );
49+ ok (pool != NULL , "maximum smaller than initial size is valid" );
50+ cmp_ok (pool -> size , "==" , 10 , "initial size is clamped to maximum" );
51+ cmp_ok (pool -> capacity , "==" , 10 , "capacity is clamped to maximum" );
4152 data_pool_destroy (pool );
4253 }
4354}
@@ -48,15 +59,15 @@ static void test_data_pool_destroy(void) {
4859 }
4960
5061 {
51- MMDB_data_pool_s * const pool = data_pool_new (512 );
62+ MMDB_data_pool_s * const pool = data_pool_new (512 , 512 );
5263 ok (pool != NULL , "created pool" );
5364 data_pool_destroy (pool );
5465 }
5566}
5667
5768static void test_data_pool_alloc (void ) {
5869 {
59- MMDB_data_pool_s * const pool = data_pool_new (1 );
70+ MMDB_data_pool_s * const pool = data_pool_new (1 , 3 );
6071 ok (pool != NULL , "created pool" );
6172 cmp_ok (pool -> used , "==" , 0 , "used size starts at 0" );
6273
@@ -75,6 +86,12 @@ static void test_data_pool_alloc(void) {
7586 cmp_ok (pool -> size , "==" , 2 , "size is 2 (new block)" );
7687 cmp_ok (pool -> used , "==" , 1 , "used size is 1 in current block" );
7788
89+ MMDB_entry_data_list_s * const entry3 = data_pool_alloc (pool );
90+ ok (entry3 != NULL , "got the final allowed entry" );
91+ ok (data_pool_alloc (pool ) == NULL ,
92+ "allocation past maximum capacity is rejected" );
93+ cmp_ok (pool -> capacity , "==" , 3 , "capacity does not exceed maximum" );
94+
7895 ok (entry1 -> entry_data .offset == 123 ,
7996 "accessing the original entry's memory is ok" );
8097
@@ -83,7 +100,8 @@ static void test_data_pool_alloc(void) {
83100
84101 {
85102 size_t const initial_size = 10 ;
86- MMDB_data_pool_s * const pool = data_pool_new (initial_size );
103+ MMDB_data_pool_s * const pool =
104+ data_pool_new (initial_size , initial_size * 3 );
87105 ok (pool != NULL , "created pool" );
88106
89107 MMDB_entry_data_list_s * entry1 = NULL ;
@@ -124,12 +142,32 @@ static void test_data_pool_alloc(void) {
124142
125143 data_pool_destroy (pool );
126144 }
145+
146+ {
147+ size_t const maximum_size = 65536 ;
148+ MMDB_data_pool_s * const pool = data_pool_new (64 , maximum_size );
149+ ok (pool != NULL , "created a decoder-sized pool" );
150+ for (size_t i = 0 ; i < maximum_size ; i ++ ) {
151+ assert (data_pool_alloc (pool ) != NULL );
152+ }
153+ cmp_ok (pool -> capacity ,
154+ "==" ,
155+ maximum_size ,
156+ "final block is clamped to the remaining capacity" );
157+ cmp_ok (pool -> sizes [pool -> index ],
158+ "==" ,
159+ 64 ,
160+ "the clamped final block reserves only 64 entries" );
161+ ok (data_pool_alloc (pool ) == NULL ,
162+ "decoder-sized pool refuses a 65,537th entry" );
163+ data_pool_destroy (pool );
164+ }
127165}
128166
129167static void test_data_pool_to_list (void ) {
130168 {
131169 size_t const initial_size = 16 ;
132- MMDB_data_pool_s * const pool = data_pool_new (initial_size );
170+ MMDB_data_pool_s * const pool = data_pool_new (initial_size , initial_size );
133171 ok (pool != NULL , "created pool" );
134172
135173 MMDB_entry_data_list_s * const entry1 = data_pool_alloc (pool );
@@ -162,7 +200,7 @@ static void test_data_pool_to_list(void) {
162200
163201 {
164202 size_t const initial_size = 1 ;
165- MMDB_data_pool_s * const pool = data_pool_new (initial_size );
203+ MMDB_data_pool_s * const pool = data_pool_new (initial_size , initial_size );
166204 ok (pool != NULL , "created pool" );
167205
168206 MMDB_entry_data_list_s * const entry1 = data_pool_alloc (pool );
@@ -180,7 +218,7 @@ static void test_data_pool_to_list(void) {
180218
181219 {
182220 size_t const initial_size = 2 ;
183- MMDB_data_pool_s * const pool = data_pool_new (initial_size );
221+ MMDB_data_pool_s * const pool = data_pool_new (initial_size , initial_size );
184222 ok (pool != NULL , "created pool" );
185223
186224 MMDB_entry_data_list_s * const entry1 = data_pool_alloc (pool );
@@ -271,7 +309,9 @@ static void test_data_pool_to_list(void) {
271309// this frequently.
272310static bool create_and_check_list (size_t const initial_size ,
273311 size_t const element_count ) {
274- MMDB_data_pool_s * const pool = data_pool_new (initial_size );
312+ size_t const max_size =
313+ element_count > initial_size ? element_count : initial_size ;
314+ MMDB_data_pool_s * const pool = data_pool_new (initial_size , max_size );
275315 assert (pool != NULL );
276316
277317 assert (pool -> used == 0 );
0 commit comments