Stub malloc toggle (#69)

This commit is contained in:
Bailey Thompson
2020-08-07 16:57:41 -04:00
committed by GitHub
parent f56b347098
commit 48b0751b6c
19 changed files with 165 additions and 2 deletions

View File

@@ -27,4 +27,4 @@ header:
python3 compile_headers.py $(version)
valgrind:
cmake -DCMAKE_BUILD_TYPE=Debug -G "CodeBlocks - Unix Makefiles" . ; cmake --build . --target Containers -- -j 2 ; valgrind --leak-check=yes ./Containers
cmake -DCMAKE_BUILD_TYPE=Debug -G "CodeBlocks - Unix Makefiles" . ; cmake --build . --target Containers -- -j 2 ; valgrind --leak-check=yes ./Containers

View File

@@ -78,6 +78,7 @@ static void test_not_empty_array(void)
assert(!me);
}
#if STUB_MALLOC
static void test_init_out_of_memory(void)
{
fail_malloc = 1;
@@ -85,11 +86,14 @@ static void test_init_out_of_memory(void)
fail_calloc = 1;
assert(!array_init(10, sizeof(int)));
}
#endif
void test_array(void)
{
test_invalid_init();
test_empty_array();
test_not_empty_array();
#if STUB_MALLOC
test_init_out_of_memory();
#endif
}

View File

@@ -144,6 +144,7 @@ static void test_trim(void)
deque_destroy(me);
}
#if STUB_MALLOC
static void test_init_out_of_memory(void)
{
fail_malloc = 1;
@@ -155,7 +156,9 @@ static void test_init_out_of_memory(void)
delay_fail_malloc = 2;
assert(!deque_init(sizeof(int)));
}
#endif
#if STUB_MALLOC
static void test_trim_out_of_memory(void)
{
deque me = deque_init(sizeof(int));
@@ -174,7 +177,9 @@ static void test_trim_out_of_memory(void)
assert(deque_size(me) == 32);
assert(!deque_destroy(me));
}
#endif
#if STUB_MALLOC
static void test_push_front_out_of_memory(void)
{
deque me = deque_init(sizeof(int));
@@ -199,7 +204,9 @@ static void test_push_front_out_of_memory(void)
}
assert(!deque_destroy(me));
}
#endif
#if STUB_MALLOC
static void test_push_back_out_of_memory(void)
{
deque me = deque_init(sizeof(int));
@@ -224,7 +231,9 @@ static void test_push_back_out_of_memory(void)
}
assert(!deque_destroy(me));
}
#endif
#if STUB_MALLOC
static void test_clear_out_of_memory(void)
{
deque me = deque_init(sizeof(int));
@@ -252,6 +261,7 @@ static void test_clear_out_of_memory(void)
assert(deque_size(me) == 32);
assert(!deque_destroy(me));
}
#endif
void test_single_full_block(void)
{
@@ -318,11 +328,13 @@ void test_deque(void)
test_invalid_init();
test_basic();
test_trim();
#if STUB_MALLOC
test_init_out_of_memory();
test_trim_out_of_memory();
test_push_front_out_of_memory();
test_push_back_out_of_memory();
test_clear_out_of_memory();
#endif
test_single_full_block();
assert(test_puzzle(2, 5) == 4);
assert(test_puzzle(2, 10) == 5);

View File

@@ -169,12 +169,15 @@ static void test_add_back(void)
assert(!forward_list_destroy(me));
}
#if STUB_MALLOC
static void test_init_out_of_memory(void)
{
fail_malloc = 1;
assert(!forward_list_init(sizeof(int)));
}
#endif
#if STUB_MALLOC
static void test_add_first_out_of_memory(void)
{
int i;
@@ -203,7 +206,9 @@ static void test_add_first_out_of_memory(void)
}
assert(!forward_list_destroy(me));
}
#endif
#if STUB_MALLOC
static void test_add_at_out_of_memory(void)
{
int get;
@@ -249,7 +254,9 @@ static void test_add_at_out_of_memory(void)
assert(get == 982);
assert(!forward_list_destroy(me));
}
#endif
#if STUB_MALLOC
static void test_add_last_out_of_memory(void)
{
int i;
@@ -278,14 +285,17 @@ static void test_add_last_out_of_memory(void)
}
assert(!forward_list_destroy(me));
}
#endif
void test_forward_list(void)
{
test_invalid_init();
test_basic();
test_add_back();
#if STUB_MALLOC
test_init_out_of_memory();
test_add_first_out_of_memory();
test_add_at_out_of_memory();
test_add_last_out_of_memory();
#endif
}

View File

@@ -150,12 +150,15 @@ static void test_basic(void)
assert(!list_destroy(me));
}
#if STUB_MALLOC
static void test_init_out_of_memory(void)
{
fail_malloc = 1;
assert(!list_init(sizeof(int)));
}
#endif
#if STUB_MALLOC
static void test_add_first_out_of_memory(void)
{
int i;
@@ -184,7 +187,9 @@ static void test_add_first_out_of_memory(void)
}
assert(!list_destroy(me));
}
#endif
#if STUB_MALLOC
static void test_add_at_out_of_memory(void)
{
int get;
@@ -230,7 +235,9 @@ static void test_add_at_out_of_memory(void)
assert(get == 982);
assert(!list_destroy(me));
}
#endif
#if STUB_MALLOC
static void test_add_last_out_of_memory(void)
{
int i;
@@ -259,6 +266,7 @@ static void test_add_last_out_of_memory(void)
}
assert(!list_destroy(me));
}
#endif
void test_remove_all(void)
{
@@ -327,10 +335,12 @@ void test_list(void)
{
test_invalid_init();
test_basic();
#if STUB_MALLOC
test_init_out_of_memory();
test_add_first_out_of_memory();
test_add_at_out_of_memory();
test_add_last_out_of_memory();
#endif
test_remove_all();
assert(test_puzzle(2, 5) == 4);
assert(test_puzzle(2, 10) == 5);

View File

@@ -492,12 +492,15 @@ static void test_override_value(void)
assert(!map_destroy(me));
}
#if STUB_MALLOC
static void test_init_out_of_memory(void)
{
fail_malloc = 1;
assert(!map_init(sizeof(int), sizeof(int), compare_int));
}
#endif
#if STUB_MALLOC
static void test_put_root_out_of_memory(map me)
{
int key = 2;
@@ -510,7 +513,9 @@ static void test_put_root_out_of_memory(map me)
delay_fail_malloc = 2;
assert(map_put(me, &key, &key) == -ENOMEM);
}
#endif
#if STUB_MALLOC
static void test_put_on_left_out_of_memory(map me)
{
int key = 1;
@@ -523,7 +528,9 @@ static void test_put_on_left_out_of_memory(map me)
delay_fail_malloc = 2;
assert(map_put(me, &key, &key) == -ENOMEM);
}
#endif
#if STUB_MALLOC
static void test_put_on_right_out_of_memory(map me)
{
int key = 3;
@@ -536,7 +543,9 @@ static void test_put_on_right_out_of_memory(map me)
delay_fail_malloc = 2;
assert(map_put(me, &key, &key) == -ENOMEM);
}
#endif
#if STUB_MALLOC
static void test_put_out_of_memory(void)
{
int key = 2;
@@ -548,6 +557,7 @@ static void test_put_out_of_memory(void)
test_put_on_right_out_of_memory(me);
assert(!map_destroy(me));
}
#endif
void test_map(void)
{
@@ -560,6 +570,8 @@ void test_map(void)
test_stress_remove();
test_unique_deletion_patterns();
test_override_value();
#if STUB_MALLOC
test_init_out_of_memory();
test_put_out_of_memory();
#endif
}

View File

@@ -544,12 +544,15 @@ static void test_multiple_operations(void)
assert(!multimap_destroy(me));
}
#if STUB_MALLOC
static void test_init_out_of_memory(void)
{
fail_malloc = 1;
assert(!multimap_init(sizeof(int), sizeof(int), compare_int, compare_int));
}
#endif
#if STUB_MALLOC
static void test_put_root_out_of_memory(multimap me)
{
int key = 2;
@@ -565,7 +568,9 @@ static void test_put_root_out_of_memory(multimap me)
delay_fail_malloc = 3;
assert(multimap_put(me, &key, &key) == -ENOMEM);
}
#endif
#if STUB_MALLOC
static void test_put_on_left_out_of_memory(multimap me)
{
int key = 1;
@@ -581,7 +586,9 @@ static void test_put_on_left_out_of_memory(multimap me)
delay_fail_malloc = 3;
assert(multimap_put(me, &key, &key) == -ENOMEM);
}
#endif
#if STUB_MALLOC
static void test_put_on_right_out_of_memory(multimap me)
{
int key = 3;
@@ -597,7 +604,9 @@ static void test_put_on_right_out_of_memory(multimap me)
delay_fail_malloc = 3;
assert(multimap_put(me, &key, &key) == -ENOMEM);
}
#endif
#if STUB_MALLOC
static void test_put_out_of_memory(void)
{
int key = 2;
@@ -610,6 +619,7 @@ static void test_put_out_of_memory(void)
test_put_on_right_out_of_memory(me);
assert(!multimap_destroy(me));
}
#endif
void test_multimap(void)
{
@@ -623,6 +633,8 @@ void test_multimap(void)
test_unique_deletion_patterns();
test_override_value();
test_multiple_operations();
#if STUB_MALLOC
test_init_out_of_memory();
test_put_out_of_memory();
#endif
}

View File

@@ -483,12 +483,15 @@ static void test_multiple_operations(void)
assert(!multiset_destroy(me));
}
#if STUB_MALLOC
static void test_init_out_of_memory(void)
{
fail_malloc = 1;
assert(!multiset_init(sizeof(int), compare_int));
}
#endif
#if STUB_MALLOC
static void test_put_root_out_of_memory(multiset me)
{
int key = 2;
@@ -498,7 +501,9 @@ static void test_put_root_out_of_memory(multiset me)
delay_fail_malloc = 1;
assert(multiset_put(me, &key) == -ENOMEM);
}
#endif
#if STUB_MALLOC
static void test_put_on_left_out_of_memory(multiset me)
{
int key = 1;
@@ -508,7 +513,9 @@ static void test_put_on_left_out_of_memory(multiset me)
delay_fail_malloc = 1;
assert(multiset_put(me, &key) == -ENOMEM);
}
#endif
#if STUB_MALLOC
static void test_put_on_right_out_of_memory(multiset me)
{
int key = 3;
@@ -518,7 +525,9 @@ static void test_put_on_right_out_of_memory(multiset me)
delay_fail_malloc = 1;
assert(multiset_put(me, &key) == -ENOMEM);
}
#endif
#if STUB_MALLOC
static void test_put_out_of_memory(void)
{
int key = 2;
@@ -530,6 +539,7 @@ static void test_put_out_of_memory(void)
test_put_on_right_out_of_memory(me);
assert(!multiset_destroy(me));
}
#endif
void test_multiset(void)
{
@@ -542,6 +552,8 @@ void test_multiset(void)
test_stress_remove();
test_unique_deletion_patterns();
test_multiple_operations();
#if STUB_MALLOC
test_init_out_of_memory();
test_put_out_of_memory();
#endif
}

View File

@@ -134,6 +134,7 @@ static void test_basic(void)
assert(!priority_queue_destroy(me));
}
#if STUB_MALLOC
static void test_init_out_of_memory(void)
{
fail_malloc = 1;
@@ -145,7 +146,9 @@ static void test_init_out_of_memory(void)
delay_fail_malloc = 2;
assert(!priority_queue_init(sizeof(int), compare_int));
}
#endif
#if STUB_MALLOC
static void test_push_out_of_memory(void)
{
int i;
@@ -177,11 +180,14 @@ static void test_push_out_of_memory(void)
}
assert(!priority_queue_destroy(me));
}
#endif
void test_priority_queue(void)
{
test_invalid_init();
test_basic();
#if STUB_MALLOC
test_init_out_of_memory();
test_push_out_of_memory();
#endif
}

View File

@@ -99,6 +99,7 @@ static void test_automated_trim(void)
assert(!queue_destroy(me));
}
#if STUB_MALLOC
static void test_init_out_of_memory(void)
{
fail_malloc = 1;
@@ -113,6 +114,7 @@ static void test_init_out_of_memory(void)
delay_fail_malloc = 3;
assert(!queue_init(sizeof(int)));
}
#endif
struct pair {
int cur_node;
@@ -161,7 +163,9 @@ void test_queue(void)
test_basic();
test_large_alloc();
test_automated_trim();
#if STUB_MALLOC
test_init_out_of_memory();
#endif
assert(test_puzzle(2, 5) == 4);
assert(test_puzzle(2, 10) == 5);
}

View File

@@ -461,12 +461,15 @@ static void test_unique_deletion_patterns(void)
assert(!set_destroy(me));
}
#if STUB_MALLOC
static void test_init_out_of_memory(void)
{
fail_malloc = 1;
assert(!set_init(sizeof(int), compare_int));
}
#endif
#if STUB_MALLOC
static void test_put_root_out_of_memory(set me)
{
int key = 2;
@@ -476,7 +479,9 @@ static void test_put_root_out_of_memory(set me)
delay_fail_malloc = 1;
assert(set_put(me, &key) == -ENOMEM);
}
#endif
#if STUB_MALLOC
static void test_put_on_left_out_of_memory(set me)
{
int key = 1;
@@ -486,7 +491,9 @@ static void test_put_on_left_out_of_memory(set me)
delay_fail_malloc = 1;
assert(set_put(me, &key) == -ENOMEM);
}
#endif
#if STUB_MALLOC
static void test_put_on_right_out_of_memory(set me)
{
int key = 3;
@@ -496,7 +503,9 @@ static void test_put_on_right_out_of_memory(set me)
delay_fail_malloc = 1;
assert(set_put(me, &key) == -ENOMEM);
}
#endif
#if STUB_MALLOC
static void test_put_out_of_memory(void)
{
int key = 2;
@@ -508,6 +517,7 @@ static void test_put_out_of_memory(void)
test_put_on_right_out_of_memory(me);
assert(!set_destroy(me));
}
#endif
void test_set(void)
{
@@ -519,6 +529,8 @@ void test_set(void)
test_stress_add();
test_stress_remove();
test_unique_deletion_patterns();
#if STUB_MALLOC
test_init_out_of_memory();
test_put_out_of_memory();
#endif
}

View File

@@ -67,6 +67,7 @@ static void test_automated_trim(void)
assert(!stack_destroy(me));
}
#if STUB_MALLOC
static void test_init_out_of_memory(void)
{
fail_malloc = 1;
@@ -81,11 +82,14 @@ static void test_init_out_of_memory(void)
delay_fail_malloc = 3;
assert(!stack_init(sizeof(int)));
}
#endif
void test_stack(void)
{
test_invalid_init();
test_basic();
test_automated_trim();
#if STUB_MALLOC
test_init_out_of_memory();
#endif
}

View File

@@ -1,7 +1,8 @@
#include "test.h"
#define _GNU_SOURCE
#if STUB_MALLOC
#define _GNU_SOURCE
#include <dlfcn.h>
#ifndef RTLD_NEXT
@@ -65,6 +66,8 @@ void *realloc(void *ptr, size_t new_size)
return real_realloc(ptr, new_size);
}
#endif /* STUB_MALLOC */
int main(void)
{
test_array();

View File

@@ -5,6 +5,10 @@
#include <errno.h>
#include <assert.h>
#define STUB_MALLOC 1
#if STUB_MALLOC
extern int fail_malloc;
extern int fail_calloc;
extern int fail_realloc;
@@ -13,6 +17,8 @@ extern int delay_fail_malloc;
extern int delay_fail_calloc;
extern int delay_fail_realloc;
#endif /* STUB_MALLOC */
void test_array(void);
void test_vector(void);
void test_deque(void);

View File

@@ -202,6 +202,7 @@ static void test_bad_hash(void)
assert(!unordered_map_destroy(me));
}
#if STUB_MALLOC
static void test_init_out_of_memory(void)
{
fail_malloc = 1;
@@ -211,7 +212,9 @@ static void test_init_out_of_memory(void)
assert(!unordered_map_init(sizeof(int), sizeof(int), bad_hash_int,
compare_int));
}
#endif
#if STUB_MALLOC
static void test_rehash_out_of_memory(void)
{
int key = 5;
@@ -228,7 +231,9 @@ static void test_rehash_out_of_memory(void)
assert(unordered_map_contains(me, &key));
assert(!unordered_map_destroy(me));
}
#endif
#if STUB_MALLOC
static void test_put_out_of_memory(void)
{
int key = 5;
@@ -256,7 +261,9 @@ static void test_put_out_of_memory(void)
assert(unordered_map_put(me, &key, &value) == -ENOMEM);
assert(!unordered_map_destroy(me));
}
#endif
#if STUB_MALLOC
static void test_resize_out_of_memory(void)
{
int i;
@@ -275,7 +282,9 @@ static void test_resize_out_of_memory(void)
}
assert(!unordered_map_destroy(me));
}
#endif
#if STUB_MALLOC
static void test_clear_out_of_memory(void)
{
int key = 5;
@@ -292,15 +301,18 @@ static void test_clear_out_of_memory(void)
assert(unordered_map_contains(me, &key));
assert(!unordered_map_destroy(me));
}
#endif
void test_unordered_map(void)
{
test_invalid_init();
test_basic();
test_bad_hash();
#if STUB_MALLOC
test_init_out_of_memory();
test_rehash_out_of_memory();
test_put_out_of_memory();
test_resize_out_of_memory();
test_clear_out_of_memory();
#endif
}

View File

@@ -303,6 +303,7 @@ static void test_bad_hash_collision(void)
assert(!unordered_multimap_destroy(me));
}
#if STUB_MALLOC
static void test_init_out_of_memory(void)
{
fail_malloc = 1;
@@ -316,7 +317,9 @@ static void test_init_out_of_memory(void)
assert(!unordered_multimap_init(sizeof(int), sizeof(int), hash_int,
compare_int, compare_int));
}
#endif
#if STUB_MALLOC
static void test_rehash_out_of_memory(void)
{
int key = 5;
@@ -334,7 +337,9 @@ static void test_rehash_out_of_memory(void)
assert(unordered_multimap_contains(me, &key));
assert(!unordered_multimap_destroy(me));
}
#endif
#if STUB_MALLOC
static void test_put_out_of_memory(void)
{
int key = 5;
@@ -363,7 +368,9 @@ static void test_put_out_of_memory(void)
assert(unordered_multimap_put(me, &key, &value) == -ENOMEM);
assert(!unordered_multimap_destroy(me));
}
#endif
#if STUB_MALLOC
static void test_resize_out_of_memory(void)
{
int i;
@@ -383,7 +390,9 @@ static void test_resize_out_of_memory(void)
}
assert(!unordered_multimap_destroy(me));
}
#endif
#if STUB_MALLOC
static void test_clear_out_of_memory(void)
{
int key = 5;
@@ -401,6 +410,7 @@ static void test_clear_out_of_memory(void)
assert(unordered_multimap_contains(me, &key));
assert(!unordered_multimap_destroy(me));
}
#endif
void test_unordered_multimap(void)
{
@@ -409,9 +419,11 @@ void test_unordered_multimap(void)
test_bad_hash();
test_collision();
test_bad_hash_collision();
#if STUB_MALLOC
test_init_out_of_memory();
test_rehash_out_of_memory();
test_put_out_of_memory();
test_resize_out_of_memory();
test_clear_out_of_memory();
#endif
}

View File

@@ -241,6 +241,7 @@ static void test_collision(void)
assert(!unordered_multiset_destroy(me));
}
#if STUB_MALLOC
static void test_init_out_of_memory(void)
{
fail_malloc = 1;
@@ -248,7 +249,9 @@ static void test_init_out_of_memory(void)
fail_calloc = 1;
assert(!unordered_multiset_init(sizeof(int), hash_int, compare_int));
}
#endif
#if STUB_MALLOC
static void test_rehash_out_of_memory(void)
{
int key = 5;
@@ -264,7 +267,9 @@ static void test_rehash_out_of_memory(void)
assert(unordered_multiset_contains(me, &key));
assert(!unordered_multiset_destroy(me));
}
#endif
#if STUB_MALLOC
static void test_put_out_of_memory(void)
{
int key = 5;
@@ -285,7 +290,9 @@ static void test_put_out_of_memory(void)
assert(unordered_multiset_put(me, &key) == -ENOMEM);
assert(!unordered_multiset_destroy(me));
}
#endif
#if STUB_MALLOC
static void test_resize_out_of_memory(void)
{
int i;
@@ -304,7 +311,9 @@ static void test_resize_out_of_memory(void)
}
assert(!unordered_multiset_destroy(me));
}
#endif
#if STUB_MALLOC
static void test_clear_out_of_memory(void)
{
int key = 5;
@@ -320,6 +329,7 @@ static void test_clear_out_of_memory(void)
assert(unordered_multiset_contains(me, &key));
assert(!unordered_multiset_destroy(me));
}
#endif
void test_unordered_multiset(void)
{
@@ -327,9 +337,11 @@ void test_unordered_multiset(void)
test_basic();
test_bad_hash();
test_collision();
#if STUB_MALLOC
test_init_out_of_memory();
test_rehash_out_of_memory();
test_put_out_of_memory();
test_resize_out_of_memory();
test_clear_out_of_memory();
#endif
}

View File

@@ -186,6 +186,7 @@ static void test_bad_hash(void)
assert(!unordered_set_destroy(me));
}
#if STUB_MALLOC
static void test_init_out_of_memory(void)
{
fail_malloc = 1;
@@ -193,7 +194,9 @@ static void test_init_out_of_memory(void)
fail_calloc = 1;
assert(!unordered_set_init(sizeof(int), hash_int, compare_int));
}
#endif
#if STUB_MALLOC
static void test_rehash_out_of_memory(void)
{
int key = 5;
@@ -208,7 +211,9 @@ static void test_rehash_out_of_memory(void)
assert(unordered_set_contains(me, &key));
assert(!unordered_set_destroy(me));
}
#endif
#if STUB_MALLOC
static void test_put_out_of_memory(void)
{
int key = 5;
@@ -229,7 +234,9 @@ static void test_put_out_of_memory(void)
assert(unordered_set_put(me, &key) == -ENOMEM);
assert(!unordered_set_destroy(me));
}
#endif
#if STUB_MALLOC
static void test_resize_out_of_memory(void)
{
int i;
@@ -247,7 +254,9 @@ static void test_resize_out_of_memory(void)
}
assert(!unordered_set_destroy(me));
}
#endif
#if STUB_MALLOC
static void test_clear_out_of_memory(void)
{
int key = 5;
@@ -262,15 +271,18 @@ static void test_clear_out_of_memory(void)
assert(unordered_set_contains(me, &key));
assert(!unordered_set_destroy(me));
}
#endif
void test_unordered_set(void)
{
test_invalid_init();
test_basic();
test_bad_hash();
#if STUB_MALLOC
test_init_out_of_memory();
test_rehash_out_of_memory();
test_put_out_of_memory();
test_resize_out_of_memory();
test_clear_out_of_memory();
#endif
}

View File

@@ -218,6 +218,7 @@ static void test_dynamic(void)
assert(!vector_destroy(str_vector));
}
#if STUB_MALLOC
static void test_init_out_of_memory(void)
{
fail_malloc = 1;
@@ -226,7 +227,9 @@ static void test_init_out_of_memory(void)
delay_fail_malloc = 1;
assert(!vector_init(sizeof(int)));
}
#endif
#if STUB_MALLOC
static void test_set_space_out_of_memory(void)
{
vector me = vector_init(sizeof(int));
@@ -245,7 +248,9 @@ static void test_set_space_out_of_memory(void)
}
assert(!vector_destroy(me));
}
#endif
#if STUB_MALLOC
static void test_add_out_of_memory(void)
{
vector me = vector_init(sizeof(int));
@@ -265,6 +270,7 @@ static void test_add_out_of_memory(void)
}
assert(!vector_destroy(me));
}
#endif
void test_vector(void)
{
@@ -272,7 +278,9 @@ void test_vector(void)
test_basic();
test_vector_of_vectors();
test_dynamic();
#if STUB_MALLOC
test_init_out_of_memory();
test_set_space_out_of_memory();
test_add_out_of_memory();
#endif
}