ex30 bits
This commit is contained in:
parent
659c7626af
commit
02ab4888dd
32
lcthw-remnants-2/c-skeleton/tests/minunit.h
Normal file
32
lcthw-remnants-2/c-skeleton/tests/minunit.h
Normal file
@ -0,0 +1,32 @@
|
||||
#undef DNDEBUG
|
||||
#ifndef _minunit_h
|
||||
#define _minunit_h
|
||||
|
||||
#include <stdio.h>
|
||||
#include <dbg.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#define mu_suite_start() char *message = NULL
|
||||
|
||||
#define mu_assert(test, message) if (!(test)) { log_err(message); return message; }
|
||||
#define mu_run_test(test) debug("\n-----%s", " " #test); \
|
||||
message = test(); tests_run++; if (message) return message;
|
||||
|
||||
#define RUN_TESTS(name) int main(int argc, char *argv[]) {\
|
||||
argc = 1; \
|
||||
debug("----- RUNNING: %s", argv[0]);\
|
||||
printf("----\nRUNNING: %s\n", argv[0]);\
|
||||
char *result = name();\
|
||||
if (result != 0) {\
|
||||
printf("FAILED: %s\n", result);\
|
||||
}\
|
||||
else {\
|
||||
printf("ALL TESTS PASSED\n");\
|
||||
}\
|
||||
printf("Tests run: %d\n", tests_run);\
|
||||
exit(result != 0);\
|
||||
}
|
||||
|
||||
int tests_run;
|
||||
|
||||
#endif
|
@ -1,48 +1,65 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "dbg.h"
|
||||
#include "minunit.h"
|
||||
#include <dlfcn.h>
|
||||
|
||||
typedef int (*lib_function)(const char *data, int count);
|
||||
char *lib_file = "build/libex29.so";
|
||||
void *lib = NULL;
|
||||
|
||||
int test_ex29(char *lib_file, char *func_to_run, char *data)
|
||||
int check_function(const char *func_to_run, const char *data, int expected)
|
||||
{
|
||||
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);
|
||||
int rc = func(data, (int)strlen(data));
|
||||
check(rc == expected, "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;
|
||||
error:
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main()
|
||||
char *test_dlopen()
|
||||
{
|
||||
char *dllib = getenv("EX29_DLLIB");
|
||||
if(dllib == NULL) {
|
||||
dllib = "build/libex29.so";
|
||||
lib = dlopen(lib_file, RTLD_NOW);
|
||||
mu_assert(lib != NULL, "Failed to open the library to test.");
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
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");
|
||||
char *test_functions()
|
||||
{
|
||||
mu_assert(check_function("print_a_message", "Hello", 0), "print_a_message failed.");
|
||||
mu_assert(check_function("uppercase", "Hello", 0), "uppercase failed.");
|
||||
mu_assert(check_function("lowercase", "Hello", 0), "lowercase failed.");
|
||||
|
||||
return 0;
|
||||
error:
|
||||
return 1;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
char *test_failures()
|
||||
{
|
||||
mu_assert(check_function("fail_on_purpose", "Hello", 1), "fail_on_purpose should fail.");
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
char *test_dlclose()
|
||||
{
|
||||
int rc = dlclose(lib);
|
||||
mu_assert(rc == 0, "Failed to close lib.");
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
char *all_tests()
|
||||
{
|
||||
mu_suite_start();
|
||||
|
||||
mu_run_test(test_dlopen);
|
||||
mu_run_test(test_functions);
|
||||
mu_run_test(test_failures);
|
||||
mu_run_test(test_dlclose);
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
RUN_TESTS(all_tests);
|
||||
|
32
lcthw-remnants-2/ex29/tests/minunit.h
Normal file
32
lcthw-remnants-2/ex29/tests/minunit.h
Normal file
@ -0,0 +1,32 @@
|
||||
#undef DNDEBUG
|
||||
#ifndef _minunit_h
|
||||
#define _minunit_h
|
||||
|
||||
#include <stdio.h>
|
||||
#include <dbg.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#define mu_suite_start() char *message = NULL
|
||||
|
||||
#define mu_assert(test, message) if (!(test)) { log_err(message); return message; }
|
||||
#define mu_run_test(test) debug("\n-----%s", " " #test); \
|
||||
message = test(); tests_run++; if (message) return message;
|
||||
|
||||
#define RUN_TESTS(name) int main(int argc, char *argv[]) {\
|
||||
argc = 1; \
|
||||
debug("----- RUNNING: %s", argv[0]);\
|
||||
printf("----\nRUNNING: %s\n", argv[0]);\
|
||||
char *result = name();\
|
||||
if (result != 0) {\
|
||||
printf("FAILED: %s\n", result);\
|
||||
}\
|
||||
else {\
|
||||
printf("ALL TESTS PASSED\n");\
|
||||
}\
|
||||
printf("Tests run: %d\n", tests_run);\
|
||||
exit(result != 0);\
|
||||
}
|
||||
|
||||
int tests_run;
|
||||
|
||||
#endif
|
Loading…
Reference in New Issue
Block a user