OSDN Git Service

font manager: libavl.
[putex/putex.git] / src / texsourc / libavl / libavl / unitTests / minunit.h
1 // ======================================================================================
2 // File         : minunit.h
3 // Author       : Adrien Oliva 
4 // Last Change  : 01/02/2013 | 10:46:45 AM | Wednesday,January
5 // Description  : Minimal unit test framework
6 // ======================================================================================
7 #ifndef __MINUNIT_H__
8 #define __MINUNIT_H__
9
10 // ========================================================
11 //  Test framework configuration
12 // ========================================================
13
14 #ifdef WITH_RANDOM
15 #ifdef __CPLUSPLUS
16 #include <ctime>
17 #include <random>
18 #endif
19 #endif
20
21 // ========================================================
22 //  Test framework:
23 //      Each unit test function must be on form
24 //          char *unit_test();
25 //      On success, function must return NULL and on error,
26 //      function must return a string describing the error.
27 //
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.
32 //
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.
41 //
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
45 //      test run.
46 // ========================================================
47
48 #ifdef __CPLUSPLUS
49 #define mu_assert(message, test) do {                       \
50     if (!(test))                                            \
51         return const_cast<char *>(message);                 \
52 } while (0)
53 #else
54 #define mu_assert(message, test) do {                       \
55     if (!(test))                                            \
56         return (char *) (message);                          \
57 } while (0)
58 #endif
59
60 #ifdef __CPLUSPLUS
61 #define mu_try try
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());           \
68 }
69 #endif
70
71 #define mu_run_test(test) do {                              \
72     char *message = test();                                 \
73     tests_run++;                                            \
74     if (message)                                            \
75         return message;                                     \
76 } while (0)
77
78 #define mu_run_set(test_set) do {                           \
79     char *message = test_set();                             \
80     if (message)                                            \
81         return message;                                     \
82 } while (0)
83
84 #define mu_run_all(all_tests) do {                          \
85     char *result = all_tests();                             \
86     printf("%d test(s) run.\n", tests_run);                 \
87     if (result != 0) {                                      \
88         fprintf(stdout, "%s\n", result);                    \
89         fprintf(stderr, "\e[01;31m");                       \
90         fflush(stderr);                                     \
91         fprintf(stdout, "TEST FAILED");                     \
92         fflush(stdout);                                     \
93         fprintf(stderr, "\e[0m");                           \
94         fflush(stderr);                                     \
95         fprintf(stdout, "\n");                              \
96         return -1;                                          \
97     } else {                                                \
98         fprintf(stderr, "\e[01;32m");                       \
99         fflush(stderr);                                     \
100         fprintf(stdout, "ALL TESTS PASSED");                \
101         fflush(stdout);                                     \
102         fprintf(stderr, "\e[0m");                           \
103         fflush(stderr);                                     \
104         fprintf(stdout, "\n");                              \
105         return 0;                                           \
106     }                                                       \
107 } while (0)
108
109 extern int tests_run;
110
111 // ========================================================
112 // Usefull function to generate random stuff
113 // ========================================================
114
115 #ifdef __CPLUSPLUS
116 static void inline get_random_string(std::string &str, std::string &pool, int size) {
117     unsigned index = 0;
118 #ifdef WITH_RANDOM
119     std::random_device rd;
120     unsigned bytes = 0;
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)
125                 index--;
126             if (((pool[index] & 0xfe) >> 1) == 0x7e)
127                 bytes = 6;
128             else if (((pool[index] & 0xfc) >> 2) == 0x3e)
129                 bytes = 5;
130             else if (((pool[index] & 0xf8) >> 3) == 0x1e)
131                 bytes = 4;
132             else if (((pool[index] & 0xf0) >> 4) == 0x0e)
133                 bytes = 3;
134             else if (((pool[index] & 0xe0) >> 5) == 0x06)
135                 bytes = 2;
136             for (unsigned int j = 0; j < bytes; j++)
137                 str += pool[index + j];
138         } else {
139             str += pool[index];
140         }
141     }
142 #else
143     for (int i = 0; i < size; i++) {
144         do {
145             str += pool[index % pool.size()];
146             index++;
147         } while (pool[index % pool.size()] & 0x80);
148     }
149
150 #endif
151 }
152 #endif
153
154 #endif