ex29 extra credit
This commit is contained in:
48
lcthw-remnants-2/ex29/tests/ex29_tests.c
Normal file
48
lcthw-remnants-2/ex29/tests/ex29_tests.c
Normal file
@@ -0,0 +1,48 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "dbg.h"
|
||||
#include <dlfcn.h>
|
||||
|
||||
typedef int (*lib_function)(const char *data, int count);
|
||||
|
||||
int test_ex29(char *lib_file, char *func_to_run, char *data)
|
||||
{
|
||||
int rc = 0;
|
||||
|
||||
void *lib = dlopen(lib_file, RTLD_NOW);
|
||||
check(lib != NULL, "Failed to open the library %s: %s", lib_file, dlerror());
|
||||
|
||||
lib_function func = dlsym(lib, func_to_run);
|
||||
check(func != NULL, "Did not find %s function in the library %s: %s", func_to_run, lib_file, dlerror());
|
||||
|
||||
rc = func(data, (int)strlen(data));
|
||||
check(rc == 0, "Function %s return %d for data: %s", func_to_run, rc, data);
|
||||
|
||||
rc = dlclose(lib);
|
||||
check(rc == 0, "Failed to close %s", lib_file);
|
||||
|
||||
return 0;
|
||||
|
||||
error:
|
||||
return 1;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
char *dllib = getenv("EX29_DLLIB");
|
||||
if(dllib == NULL) {
|
||||
dllib = "build/libex29.so";
|
||||
}
|
||||
|
||||
check(test_ex29(dllib, "print_a_message", "hello there") == 0, "print failed");
|
||||
check(test_ex29(dllib, "uppercase", "hello there") == 0, "uppercase failed");
|
||||
check(test_ex29(dllib, "lowercase", "HELLO tHeRe") == 0, "lowercase failed");
|
||||
check(test_ex29(dllib, "fail_on_purpose", "i fail") == 1, "fail failed to fail");
|
||||
check(test_ex29(dllib, "fail_on_purpose", "") == 1, "fail failed to fail");
|
||||
check(test_ex29(dllib, "adfasfasdf", "asdfadff") == 1, "adfasfasdf failed to fail");
|
||||
check(test_ex29("libex.so", "adfasfasdf", "asdfadff") == 1, "libex failed to fail");
|
||||
|
||||
return 0;
|
||||
error:
|
||||
return 1;
|
||||
}
|
19
lcthw-remnants-2/ex29/tests/runtests.sh
Normal file
19
lcthw-remnants-2/ex29/tests/runtests.sh
Normal file
@@ -0,0 +1,19 @@
|
||||
echo "Running unit tests:"
|
||||
|
||||
for i in tests/*_tests
|
||||
do
|
||||
if test -f $i
|
||||
then
|
||||
if $VALGRIND ./$i 2>> tests/tests.log
|
||||
then
|
||||
echo $i PASS
|
||||
else
|
||||
echo "ERROR in test $i: here's tests/tests.log"
|
||||
echo "------"
|
||||
tail tests/tests.log
|
||||
exit 1
|
||||
fi
|
||||
fi
|
||||
done
|
||||
|
||||
echo ""
|
Reference in New Issue
Block a user