void test_trie_insert(void) { Trie *trie; unsigned int entries; size_t allocated; trie = generate_trie(); /* Test insert of NULL value has no effect */ entries = trie_num_entries(trie); assert(trie_insert(trie, "hello world", NULL) == 0); assert(trie_num_entries(trie) == entries); /* Test out of memory scenario */ allocated = alloc_test_get_allocated(); alloc_test_set_limit(0); assert(trie_insert(trie, "a", "test value") == 0); assert(trie_num_entries(trie) == entries); /* Test rollback */ alloc_test_set_limit(5); assert(trie_insert(trie, "hello world", "test value") == 0); assert(alloc_test_get_allocated() == allocated); assert(trie_num_entries(trie) == entries); trie_free(trie); }
static void test_insert_out_of_memory (void) { BinomialHeap* heap; int i; /* There are various memory allocations performed during the insert; * probe at different limit levels to catch them all. */ for (i = 0; i < 6; ++i) { heap = generate_heap(); /* Insert should fail */ alloc_test_set_limit (i); test_array[TEST_VALUE] = TEST_VALUE; assert (binomial_heap_insert (heap, &test_array[TEST_VALUE]) == 0); alloc_test_set_limit (-1); /* Check that the heap is unharmed */ verify_heap (heap); binomial_heap_free (heap); } }
void test_hash_table_new_free(void) { HashTable *hash_table; hash_table = hash_table_new(int_hash, int_equal); assert(hash_table != NULL); /* Add some values */ hash_table_insert(hash_table, &value1, &value1); hash_table_insert(hash_table, &value2, &value2); hash_table_insert(hash_table, &value3, &value3); hash_table_insert(hash_table, &value4, &value4); /* Free the hash table */ hash_table_free(hash_table); /* Test out of memory scenario */ alloc_test_set_limit(0); hash_table = hash_table_new(int_hash, int_equal); assert(hash_table == NULL); assert(alloc_test_get_allocated() == 0); alloc_test_set_limit(1); hash_table = hash_table_new(int_hash, int_equal); assert(hash_table == NULL); assert(alloc_test_get_allocated() == 0); }
void test_set_intersection (void) { int numbers1[] = {1, 2, 3, 4, 5, 6, 7}; int numbers2[] = {5, 6, 7, 8, 9, 10, 11}; int result[] = {5, 6, 7}; int i; Set* set1; Set* set2; Set* result_set; size_t allocated; /* Create the first set */ set1 = set_new (int_hash, int_equal); for (i = 0; i < 7; ++i) { set_insert (set1, &numbers1[i]); } /* Create the second set */ set2 = set_new (int_hash, int_equal); for (i = 0; i < 7; ++i) { set_insert (set2, &numbers2[i]); } /* Perform the intersection */ result_set = set_intersection (set1, set2); assert (set_num_entries (result_set) == 3); for (i = 0; i < 3; ++i) { assert (set_query (result_set, &result[i]) != 0); } /* Test out of memory scenario */ alloc_test_set_limit (0); assert (set_intersection (set1, set2) == NULL); /* Can allocate set, can't copy all values */ alloc_test_set_limit (2 + 2); allocated = alloc_test_get_allocated(); assert (set_intersection (set1, set2) == NULL); assert (alloc_test_get_allocated() == allocated); set_free (set1); set_free (set2); set_free (result_set); }
void test_set_to_array (void) { Set* set; int values[100]; int** array; int i; /* Create a set containing pointers to all entries in the "values" * array. */ set = set_new (pointer_hash, pointer_equal); for (i = 0; i < 100; ++i) { values[i] = 1; set_insert (set, &values[i]); } array = (int**) set_to_array (set); /* Check the array */ for (i = 0; i < 100; ++i) { assert (*array[i] == 1); *array[i] = 0; } /* Test out of memory scenario */ alloc_test_set_limit (0); assert (set_to_array (set) == NULL); free (array); set_free (set); }
void test_queue_new_free(void) { int i; Queue *queue; /* Create and destroy a queue */ queue = queue_new(); queue_free(queue); /* Add lots of values and then destroy */ queue = queue_new(); for (i=0; i<1000; ++i) { queue_push_head(queue, &variable1); } queue_free(queue); /* Test allocation when there is no free memory */ alloc_test_set_limit(0); queue = queue_new(); assert(queue == NULL); }
void test_list_to_array(void) { ListEntry *list; void **array; list = generate_list(); array = list_to_array(list); assert(array[0] == &variable1); assert(array[1] == &variable2); assert(array[2] == &variable3); assert(array[3] == &variable4); free(array); /* Test out of memory scenario */ alloc_test_set_limit(0); array = list_to_array(list); assert(array == NULL); list_free(list); }
void test_list_prepend(void) { ListEntry *list = NULL; assert(list_prepend(&list, &variable1) != NULL); check_list_integrity(list); assert(list_prepend(&list, &variable2) != NULL); check_list_integrity(list); assert(list_prepend(&list, &variable3) != NULL); check_list_integrity(list); assert(list_prepend(&list, &variable4) != NULL); check_list_integrity(list); assert(list_nth_data(list, 0) == &variable4); assert(list_nth_data(list, 1) == &variable3); assert(list_nth_data(list, 2) == &variable2); assert(list_nth_data(list, 3) == &variable1); /* Test out of memory scenario */ alloc_test_set_limit(0); assert(list_length(list) == 4); assert(list_prepend(&list, &variable1) == NULL); assert(list_length(list) == 4); check_list_integrity(list); list_free(list); }
void test_hash_table_out_of_memory(void) { HashTable *hash_table; int values[66]; unsigned int i; hash_table = hash_table_new(int_hash, int_equal); /* Test normal failure */ alloc_test_set_limit(0); values[0] = 0; assert(hash_table_insert(hash_table, &values[0], &values[0]) == 0); assert(hash_table_num_entries(hash_table) == 0); alloc_test_set_limit(-1); /* Test failure when increasing table size. * The initial table size is 193 entries. The table increases in * size when 1/3 full, so the 66th entry should cause the insert * to fail. */ for (i=0; i<65; ++i) { values[i] = (int) i; assert(hash_table_insert(hash_table, &values[i], &values[i]) != 0); assert(hash_table_num_entries(hash_table) == i + 1); } assert(hash_table_num_entries(hash_table) == 65); /* Test the 66th insert */ alloc_test_set_limit(0); values[65] = 65; assert(hash_table_insert(hash_table, &values[65], &values[65]) == 0); assert(hash_table_num_entries(hash_table) == 65); hash_table_free(hash_table); }
void test_binary_heap_new_free(void) { BinaryHeap *heap; int i; for (i=0; i<NUM_TEST_VALUES; ++i) { heap = binary_heap_new(BINARY_HEAP_TYPE_MIN, int_compare); binary_heap_free(heap); } /* Test low memory scenario */ alloc_test_set_limit(0); heap = binary_heap_new(BINARY_HEAP_TYPE_MIN, int_compare); assert(heap == NULL); alloc_test_set_limit(1); heap = binary_heap_new(BINARY_HEAP_TYPE_MIN, int_compare); assert(heap == NULL); }
void test_pop_out_of_memory (void) { BinomialHeap* heap; int i; /* There are various memory allocations performed as part of the merge * done during the pop. Probe at different limit levels to catch them * all. */ for (i = 0; i < 6; ++i) { heap = generate_heap(); /* Pop should fail */ alloc_test_set_limit (i); assert (binomial_heap_pop (heap) == NULL); alloc_test_set_limit (-1); /* Check the heap is unharmed */ binomial_heap_free (heap); } }
static void run_test(UnitTestFunction test) { /* Turn off any allocation limits that may have been set * by a previous test. */ alloc_test_set_limit(-1); /* Run the test */ test(); /* Check that all memory was correctly freed back during * the test. */ assert(alloc_test_get_allocated() == 0); }
void test_binomial_heap_new_free (void) { BinomialHeap* heap; int i; for (i = 0; i < NUM_TEST_VALUES; ++i) { heap = binomial_heap_new (BINOMIAL_HEAP_TYPE_MIN, int_compare); binomial_heap_free (heap); } /* Test for out of memory */ alloc_test_set_limit (0); assert (binomial_heap_new (BINOMIAL_HEAP_TYPE_MIN, int_compare) == NULL); }
void test_set_new_free (void) { Set* set; int i; int* value; set = set_new (int_hash, int_equal); set_register_free_function (set, free); assert (set != NULL); /* Fill the set with many values before freeing */ for (i = 0; i < 10000; ++i) { value = (int*) malloc (sizeof (int)); *value = i; set_insert (set, value); } /* Free the set */ set_free (set); /* Test out of memory scenario */ alloc_test_set_limit (0); set = set_new (int_hash, int_equal); assert (set == NULL); alloc_test_set_limit (1); set = set_new (int_hash, int_equal); assert (set == NULL); assert (alloc_test_get_allocated() == 0); }
void test_trie_insert_out_of_memory(void) { Trie *trie; trie = generate_binary_trie(); alloc_test_set_limit(3); assert(trie_insert_binary(trie, bin_key4, sizeof(bin_key4), "test value") == 0); assert(trie_lookup_binary(trie, bin_key4, sizeof(bin_key4)) == NULL); assert(trie_num_entries(trie) == 2); trie_free(trie); }
void test_queue_push_head(void) { Queue *queue; int i; queue = queue_new(); /* Add some values */ for (i=0; i<1000; ++i) { queue_push_head(queue, &variable1); queue_push_head(queue, &variable2); queue_push_head(queue, &variable3); queue_push_head(queue, &variable4); } assert(!queue_is_empty(queue)); /* Check values come out of the tail properly */ assert(queue_pop_tail(queue) == &variable1); assert(queue_pop_tail(queue) == &variable2); assert(queue_pop_tail(queue) == &variable3); assert(queue_pop_tail(queue) == &variable4); /* Check values come back out of the head properly */ assert(queue_pop_head(queue) == &variable4); assert(queue_pop_head(queue) == &variable3); assert(queue_pop_head(queue) == &variable2); assert(queue_pop_head(queue) == &variable1); queue_free(queue); /* Test behavior when running out of memory. */ queue = queue_new(); alloc_test_set_limit(0); assert(!queue_push_head(queue, &variable1)); queue_free(queue); }
void test_binomial_heap_insert (void) { BinomialHeap* heap; int i; heap = binomial_heap_new (BINOMIAL_HEAP_TYPE_MIN, int_compare); for (i = 0; i < NUM_TEST_VALUES; ++i) { test_array[i] = i; assert (binomial_heap_insert (heap, &test_array[i]) != 0); } assert (binomial_heap_num_entries (heap) == NUM_TEST_VALUES); /* Test for out of memory */ alloc_test_set_limit (0); assert (binomial_heap_insert (heap, &i) == 0); binomial_heap_free (heap); }
void test_rb_tree_new (void) { RBTree* tree; tree = rb_tree_new ( (RBTreeCompareFunc) int_compare); assert (tree != NULL); assert (rb_tree_root_node (tree) == NULL); assert (rb_tree_num_entries (tree) == 0); rb_tree_free (tree); /* Test out of memory scenario */ alloc_test_set_limit (0); tree = rb_tree_new ( (RBTreeCompareFunc) int_compare); assert (tree == NULL); }
void test_rb_tree_to_array (void) { RBTree* tree; int entries[] = { 89, 23, 42, 4, 16, 15, 8, 99, 50, 30 }; int sorted[] = { 4, 8, 15, 16, 23, 30, 42, 50, 89, 99 }; int num_entries = sizeof (entries) / sizeof (int); int i; int** array; /* Add all entries to the tree */ tree = rb_tree_new ( (RBTreeCompareFunc) int_compare); for (i = 0; i < num_entries; ++i) { rb_tree_insert (tree, &entries[i], NULL); } assert (rb_tree_num_entries (tree) == num_entries); /* Convert to an array and check the contents */ array = (int**) rb_tree_to_array (tree); for (i = 0; i < num_entries; ++i) { assert (*array[i] == sorted[i]); } free (array); /* Test out of memory scenario */ alloc_test_set_limit (0); array = (int**) rb_tree_to_array (tree); assert (array == NULL); validate_tree (tree); rb_tree_free (tree); }
void test_trie_new_free(void) { Trie *trie; /* Allocate and free an empty trie */ trie = trie_new(); assert(trie != NULL); trie_free(trie); /* Add some values before freeing */ trie = trie_new(); assert(trie_insert(trie, "hello", "there") != 0); assert(trie_insert(trie, "hell", "testing") != 0); assert(trie_insert(trie, "testing", "testing") != 0); assert(trie_insert(trie, "", "asfasf") != 0); trie_free(trie); /* Add a value, remove it and then free */ trie = trie_new(); assert(trie_insert(trie, "hello", "there") != 0); assert(trie_remove(trie, "hello") != 0); trie_free(trie); /* Test out of memory scenario */ alloc_test_set_limit(0); trie = trie_new(); assert(trie == NULL); }
void test_out_of_memory(void) { BinaryHeap *heap; int *value; int values[] = { 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 }; int i; /* Allocate a heap and fill to the default limit */ heap = binary_heap_new(BINARY_HEAP_TYPE_MIN, int_compare); alloc_test_set_limit(0); for (i=0; i<16; ++i) { assert(binary_heap_insert(heap, &values[i]) != 0); } assert(binary_heap_num_entries(heap) == 16); /* Check that we cannot add new values */ for (i=0; i<16; ++i) { assert(binary_heap_insert(heap, &values[i]) == 0); assert(binary_heap_num_entries(heap) == 16); } /* Check that we can read the values back out again and they * are in the right order. */ for (i=0; i<16; ++i) { value = binary_heap_pop(heap); assert(*value == i); } assert(binary_heap_num_entries(heap) == 0); binary_heap_free(heap); }
void test_out_of_memory (void) { RBTree* tree; RBTreeNode* node; int i; /* Create a tree */ tree = create_tree(); /* Set a limit to stop any more entries from being added. */ alloc_test_set_limit (0); /* Try to add some more nodes and verify that this fails. */ for (i = 10000; i < 20000; ++i) { node = rb_tree_insert (tree, &i, &i); assert (node == NULL); validate_tree (tree); } rb_tree_free (tree); }