mirror of
https://github.com/Pithikos/C-Thread-Pool
synced 2024-11-25 06:49:37 +03:00
65 lines
1.8 KiB
Bash
Executable File
65 lines
1.8 KiB
Bash
Executable File
#! /bin/bash
|
|
|
|
#
|
|
# This file has several tests to check for memory leaks.
|
|
# valgrind is used so make sure you have it installed
|
|
#
|
|
|
|
. funcs
|
|
|
|
|
|
# ---------------------------- Tests -----------------------------------
|
|
|
|
|
|
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)
|
|
heap_usage=$(echo "$output" | grep "total heap usage")
|
|
allocs=$(extract_num "[0-9]* allocs" "$heap_usage")
|
|
frees=$(extract_num "[0-9]* frees" "$heap_usage")
|
|
if (( "$allocs" == 0 )); then
|
|
err "Allocated 0 times. Something is wrong.." "$output"
|
|
fi
|
|
if (( "$allocs" == "$frees" )); then
|
|
return
|
|
fi
|
|
err "Allocated $allocs times but freed only $frees" "$output"
|
|
}
|
|
|
|
|
|
# This is the same with test_many_thread_allocs but multiplied
|
|
function test_thread_free_multi { #threads #times
|
|
echo "Testing multiple threads creation and destruction in pool(threads=$1 times=$2)"
|
|
gcc src/no_work.c ../thpool.c -pthread -o test
|
|
for ((i = 1; i <= $2; i++)); do
|
|
python -c "import sys; sys.stdout.write('$i/$2\r')"
|
|
output=$(valgrind --leak-check=full --track-origins=yes ./test "$1" 2>&1 > /dev/null)
|
|
heap_usage=$(echo "$output" | grep "total heap usage")
|
|
allocs=$(extract_num "[0-9]* allocs" "$heap_usage")
|
|
frees=$(extract_num "[0-9]* frees" "$heap_usage")
|
|
if (( "$allocs" == 0 )); then
|
|
err "Allocated 0 times. Something is wrong.." "$output"
|
|
fi
|
|
if (( "$allocs" != "$frees" )); then
|
|
err "Allocated $allocs times but freed only $frees" "$output"
|
|
fi
|
|
#echo "Allocs: $allocs Frees: $frees"
|
|
done
|
|
echo
|
|
}
|
|
|
|
|
|
|
|
# Run tests
|
|
test_thread_free 1
|
|
test_thread_free 2
|
|
test_thread_free 4
|
|
test_thread_free 8
|
|
test_thread_free 1
|
|
test_thread_free 20
|
|
test_thread_free_multi 4 20
|
|
test_thread_free_multi 3 1000
|
|
|
|
echo "No memory leaks"
|