1 // ======================================================================================
3 // Author : Adrien Oliva
4 // Last Change : 01/02/2013 | 10:46:45 AM | Wednesday,January
5 // Description : Minimal unit test framework
6 // ======================================================================================
10 // ========================================================
11 // Test framework configuration
12 // ========================================================
21 // ========================================================
23 // Each unit test function must be on form
25 // On success, function must return NULL and on error,
26 // function must return a string describing the error.
28 // To help writing test, you can use the "mu_assert"
29 // macro to perform a vital test. If test fails, macro
30 // return the given error message. If test is ok,
31 // program continue without doing anything.
33 // Once a test function is written, you must call it
34 // with macro "mu_run_test" which takes in argument
35 // the test function. You can compile all tests in a
36 // single function "char *all_test()" which calls all
37 // your "mu_run_test". With such a function, you can
38 // simply call in your main, the macro "mu_run_all":
39 // it runs all your tests, print the number of passed
40 // tests, and the error message if a test fails.
42 // Finally, the last macro provide by minunit is
43 // "mu_run_set". This macro is similar to
44 // "mu_run_test" but does not increase the number of
46 // ========================================================
49 #define mu_assert(message, test) do { \
51 return const_cast<char *>(message); \
54 #define mu_assert(message, test) do { \
56 return (char *) (message); \
62 #define mu_catch(message) catch (...) { \
63 std::string error_msg = std::string("Exception in "); \
64 error_msg += std::string(__func__); \
65 error_msg += std::string(": "); \
66 error_msg += message; \
67 return const_cast<char *>(error_msg.c_str()); \
71 #define mu_run_test(test) do { \
72 char *message = test(); \
78 #define mu_run_set(test_set) do { \
79 char *message = test_set(); \
84 #define mu_run_all(all_tests) do { \
85 char *result = all_tests(); \
86 printf("%d test(s) run.\n", tests_run); \
88 fprintf(stdout, "%s\n", result); \
89 fprintf(stderr, "\e[01;31m"); \
91 fprintf(stdout, "TEST FAILED"); \
93 fprintf(stderr, "\e[0m"); \
95 fprintf(stdout, "\n"); \
98 fprintf(stderr, "\e[01;32m"); \
100 fprintf(stdout, "ALL TESTS PASSED"); \
102 fprintf(stderr, "\e[0m"); \
104 fprintf(stdout, "\n"); \
109 extern int tests_run;
111 // ========================================================
112 // Usefull function to generate random stuff
113 // ========================================================
116 static void inline get_random_string(std::string &str, std::string &pool, int size) {
119 std::random_device rd;
121 for (int i = 0; i < size; i++) {
122 index = rd() % pool.size();
123 if (pool[index] & 0x80) {
124 while (((pool[index] & 0xc0) >> 6) != 0x03)
126 if (((pool[index] & 0xfe) >> 1) == 0x7e)
128 else if (((pool[index] & 0xfc) >> 2) == 0x3e)
130 else if (((pool[index] & 0xf8) >> 3) == 0x1e)
132 else if (((pool[index] & 0xf0) >> 4) == 0x0e)
134 else if (((pool[index] & 0xe0) >> 5) == 0x06)
136 for (unsigned int j = 0; j < bytes; j++)
137 str += pool[index + j];
143 for (int i = 0; i < size; i++) {
145 str += pool[index % pool.size()];
147 } while (pool[index % pool.size()] & 0x80);