Skip to content

Commit e8882b5

Browse files
committed
Add out of memory checks for set, hash table.
1 parent e46dc21 commit e8882b5

File tree

2 files changed

+117
-0
lines changed

2 files changed

+117
-0
lines changed

test/test-hash-table.c

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,18 @@ void test_hash_table_new_free(void)
8989
/* Free the hash table */
9090

9191
hash_table_free(hash_table);
92+
93+
/* Test out of memory scenario */
94+
95+
alloc_test_set_limit(0);
96+
hash_table = hash_table_new(int_hash, int_equal);
97+
assert(hash_table == NULL);
98+
assert(alloc_test_get_allocated() == 0);
99+
100+
alloc_test_set_limit(8 * sizeof(void *));
101+
hash_table = hash_table_new(int_hash, int_equal);
102+
assert(hash_table == NULL);
103+
assert(alloc_test_get_allocated() == 0);
92104
}
93105

94106
/* Test insert and lookup functions */
@@ -367,13 +379,60 @@ void test_hash_table_free_functions(void)
367379
assert(allocated_values == 0);
368380
}
369381

382+
/* Test for out of memory scenario */
383+
384+
void test_hash_table_out_of_memory(void)
385+
{
386+
HashTable *hash_table;
387+
int values[66];
388+
int i;
389+
390+
hash_table = hash_table_new(int_hash, int_equal);
391+
392+
/* Test normal failure */
393+
394+
alloc_test_set_limit(0);
395+
values[0] = 0;
396+
assert(hash_table_insert(hash_table, &values[0], &values[0]) == 0);
397+
assert(hash_table_num_entries(hash_table) == 0);
398+
399+
alloc_test_set_limit(-1);
400+
401+
/* Test failure when increasing table size.
402+
* The initial table size is 193 entries. The table increases in
403+
* size when 1/3 full, so the 66th entry should cause the insert
404+
* to fail. */
405+
406+
for (i=0; i<65; ++i) {
407+
values[i] = i;
408+
409+
assert(hash_table_insert(hash_table,
410+
&values[i], &values[i]) != 0);
411+
assert(hash_table_num_entries(hash_table) == i + 1);
412+
}
413+
414+
assert(hash_table_num_entries(hash_table) == 65);
415+
416+
/* Test the 66th insert */
417+
418+
alloc_test_set_limit(0);
419+
420+
values[65] = 65;
421+
422+
assert(hash_table_insert(hash_table, &values[65], &values[65]) == 0);
423+
assert(hash_table_num_entries(hash_table) == 65);
424+
425+
hash_table_free(hash_table);
426+
}
427+
370428
static UnitTestFunction tests[] = {
371429
test_hash_table_new_free,
372430
test_hash_table_insert_lookup,
373431
test_hash_table_remove,
374432
test_hash_table_iterating,
375433
test_hash_table_iterating_remove,
376434
test_hash_table_free_functions,
435+
test_hash_table_out_of_memory,
377436
NULL
378437
};
379438

test/test-set.c

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,17 @@ void test_set_new_free(void)
8787
/* Free the set */
8888

8989
set_free(set);
90+
91+
/* Test out of memory scenario */
92+
93+
alloc_test_set_limit(0);
94+
set = set_new(int_hash, int_equal);
95+
assert(set == NULL);
96+
97+
alloc_test_set_limit(7 * sizeof(void *));
98+
set = set_new(int_hash, int_equal);
99+
assert(set == NULL);
100+
assert(alloc_test_get_allocated() == 0);
90101
}
91102

92103
void test_set_insert(void)
@@ -446,6 +457,52 @@ void test_set_free_function(void)
446457
assert(allocated_values == 0);
447458
}
448459

460+
/* Test for out of memory scenario */
461+
462+
void test_set_out_of_memory(void)
463+
{
464+
Set *set;
465+
int values[66];
466+
int i;
467+
468+
set = set_new(int_hash, int_equal);
469+
470+
/* Test normal failure */
471+
472+
alloc_test_set_limit(0);
473+
values[0] = 0;
474+
assert(set_insert(set, &values[0]) == 0);
475+
assert(set_num_entries(set) == 0);
476+
477+
alloc_test_set_limit(-1);
478+
479+
/* Test failure when increasing table size.
480+
* The initial table size is 193 entries. The table increases in
481+
* size when 1/3 full, so the 66th entry should cause the insert
482+
* to fail. */
483+
484+
for (i=0; i<65; ++i) {
485+
values[i] = i;
486+
487+
assert(set_insert(set, &values[i]) != 0);
488+
assert(set_num_entries(set) == i + 1);
489+
}
490+
491+
assert(set_num_entries(set) == 65);
492+
493+
/* Test the 66th insert */
494+
495+
alloc_test_set_limit(0);
496+
497+
values[65] = 65;
498+
499+
assert(set_insert(set, &values[65]) == 0);
500+
assert(set_num_entries(set) == 65);
501+
502+
set_free(set);
503+
}
504+
505+
449506
static UnitTestFunction tests[] = {
450507
test_set_new_free,
451508
test_set_insert,
@@ -457,6 +514,7 @@ static UnitTestFunction tests[] = {
457514
test_set_iterating_remove,
458515
test_set_to_array,
459516
test_set_free_function,
517+
test_set_out_of_memory,
460518
NULL
461519
};
462520

0 commit comments

Comments
 (0)