C-Thread-Pool/tests/threadpool

86 lines
1.4 KiB
Plaintext
Raw Normal View History

2015-01-03 20:11:12 +03:00
#! /bin/bash
#
# This file has several tests to check for memory leaks.
# valgrind is used so make sure you have it installed
#
# --------------------------- Generic ----------------------------------
function needle { #needle #haystack
python -c "import re; print(re.search(r'$1', '$2').group(0))"
}
function extract_num { #needle with number #haystack
string=$(needle "$1" "$2")
needle "[0-9]*" "$string"
}
function err { #string #log
echo "------------------- ERROR ------------------------"
echo "$1"
echo "$2" >> memleaks.log
exit 1
}
# ---------------------------- Tests -----------------------------------
function test_mass_addition {
read -d '' code <<"EOF"
#include <stdio.h>
#include <unistd.h>
#include <time.h>
#include "../thpool.h"
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
int sum=0;
void add_one() {
pthread_mutex_lock(&mutex);
sum ++;
pthread_mutex_unlock(&mutex);
}
int main(){
thpool_t* threadpool;
threadpool = thpool_init(10);
int n;
for (n=0; n<1000; n++){
thpool_add_work(threadpool, (void*)add_one, NULL);
}
thpool_wait(threadpool);
printf("%d", sum);
return 0;
}
EOF
echo "Adding up to 1000 with 10 threads"
echo "$code" > _test_.c
gcc _test_.c ../thpool.c -pthread -o test
output=$(./test 2>&1 /dev/null)
if [ "$output" == "1000" ]; then
return
fi
err "Expected 1000 but got $output" "$output"
}
# Run tests
test_mass_addition
echo "No errors"