ex30 bits

This commit is contained in:
Dan Buch 2016-04-16 16:41:20 -04:00
parent 659c7626af
commit 02ab4888dd
No known key found for this signature in database
GPG Key ID: FAEF12936DD3E3EC
3 changed files with 114 additions and 33 deletions

View 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

View File

@ -1,48 +1,65 @@
#include <stdio.h> #include "minunit.h"
#include <stdlib.h>
#include "dbg.h"
#include <dlfcn.h> #include <dlfcn.h>
typedef int (*lib_function)(const char *data, int count); 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); 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()); 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)); int rc = func(data, (int)strlen(data));
check(rc == 0, "Function %s return %d for data: %s", func_to_run, rc, 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; return 1;
error:
return 0;
} }
int main() char *test_dlopen()
{ {
char *dllib = getenv("EX29_DLLIB"); lib = dlopen(lib_file, RTLD_NOW);
if(dllib == NULL) { mu_assert(lib != NULL, "Failed to open the library to test.");
dllib = "build/libex29.so";
return NULL;
} }
check(test_ex29(dllib, "print_a_message", "hello there") == 0, "print failed"); char *test_functions()
check(test_ex29(dllib, "uppercase", "hello there") == 0, "uppercase failed"); {
check(test_ex29(dllib, "lowercase", "HELLO tHeRe") == 0, "lowercase failed"); mu_assert(check_function("print_a_message", "Hello", 0), "print_a_message failed.");
check(test_ex29(dllib, "fail_on_purpose", "i fail") == 1, "fail failed to fail"); mu_assert(check_function("uppercase", "Hello", 0), "uppercase failed.");
check(test_ex29(dllib, "fail_on_purpose", "") == 1, "fail failed to fail"); mu_assert(check_function("lowercase", "Hello", 0), "lowercase failed.");
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; return NULL;
error:
return 1;
} }
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);

View 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