C-Thread-Pool/tests/memleaks

62 lines
1.7 KiB
Plaintext
Raw Normal View History

2015-01-03 17:18:07 +03:00
#! /bin/bash
#
# This file has several tests to check for memory leaks.
# valgrind is used so make sure you have it installed
#
2015-01-09 22:30:29 +03:00
. funcs
2015-01-06 22:10:30 +03:00
# ---------------------------- Tests -----------------------------------
2015-01-09 22:30:29 +03:00
function test_thread_free { #threads
echo "Testing creation and destruction of threads(=$1)"
gcc src/no_work.c ../thpool.c -pthread -o test
output=$(valgrind --leak-check=full --track-origins=yes ./test "$1" 2>&1 > /dev/null)
2015-01-03 17:18:07 +03:00
heap_usage=$(echo "$output" | grep "total heap usage")
allocs=$(extract_num "[0-9]* allocs" "$heap_usage")
frees=$(extract_num "[0-9]* frees" "$heap_usage")
2015-01-09 22:30:29 +03:00
if (( "$allocs" == 0 )); then
err "Allocated 0 times. Something is wrong.." "$output"
2015-01-03 17:18:07 +03:00
fi
2015-01-09 22:30:29 +03:00
if (( "$allocs" == "$frees" )); then
2015-01-06 22:10:30 +03:00
return
fi
err "Allocated $allocs times but freed only $frees" "$output"
}
# This is the same with test_many_thread_allocs but multiplied
2015-01-09 22:30:29 +03:00
function test_thread_free_multi { #threads #times
echo "Testing multiple threads creation and destruction in pool (this can take a while)"
gcc src/no_work.c ../thpool.c -pthread -o test
for ((i = 1; i <= $1; i++)); do
output=$(valgrind --leak-check=full --track-origins=yes ./test "$1" 2>&1 > /dev/null)
2015-01-06 22:10:30 +03:00
heap_usage=$(echo "$output" | grep "total heap usage")
allocs=$(extract_num "[0-9]* allocs" "$heap_usage")
frees=$(extract_num "[0-9]* frees" "$heap_usage")
2015-01-09 22:30:29 +03:00
if (( "$allocs" == 0 )); then
err "Allocated 0 times. Something is wrong.." "$output"
fi
if (( "$allocs" != "$frees" )); then
2015-01-09 20:35:04 +03:00
err "Allocated $allocs times but freed only $frees" "$output"
2015-01-06 22:10:30 +03:00
fi
2015-01-09 22:30:29 +03:00
echo "Allocs: $allocs Frees: $frees"
2015-01-06 22:10:30 +03:00
done
}
2015-01-03 17:18:07 +03:00
# Run tests
2015-01-09 22:30:29 +03:00
test_thread_free 1
test_thread_free 2
test_thread_free 4
test_thread_free 8
test_thread_free 1
test_thread_free 100
test_thread_free_multi 4 20
2015-01-03 17:18:07 +03:00
2015-01-06 22:10:30 +03:00
echo "No memory leaks"