1 /*
2    Bacula(R) - The Network Backup Solution
3 
4    Copyright (C) 2000-2020 Kern Sibbald
5 
6    The original author of Bacula is Kern Sibbald, with contributions
7    from many others, a complete list can be found in the file AUTHORS.
8 
9    You may use this file and others of this release according to the
10    license defined in the LICENSE file, which includes the Affero General
11    Public License, v3.0 ("AGPLv3") and some additional permissions and
12    terms pursuant to its AGPLv3 Section 7.
13 
14    This notice must be preserved when any source code is
15    conveyed and/or propagated.
16 
17    Bacula(R) is a registered trademark of Kern Sibbald.
18 */
19 /*
20  * Support routines for Unit Tests.
21  */
22 
23 #ifndef _UNITTESTS_H_
24 #define _UNITTESTS_H_
25 
26 // Test success if value x is not zero
27 #define ok(x, label) _ok(__FILE__, __LINE__, #x, (x), label)
28 // Test success if value x is zero
29 #define nok(x, label) _nok(__FILE__, __LINE__, #x, (x), label)
30 
31 #define is(x, y, label) _is(__FILE__, __LINE__, #x, (x), (y), label)
32 #define isnt(x, y, label) _isnt(__FILE__, __LINE__, #x, (x), (y), label)
33 
34 /* TODO: log() ported from BEE it should be updated. */
35 #ifdef RTEST_LOG_THREADID
36 #define log(format, ...)  do { \
37    pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL); \
38    printf("%p: " format "\n", (void *)pthread_self(),  ##__VA_ARGS__ ); \
39    pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);                 \
40  } while (0)
41 #else
42 #define log(format, ...)  do { \
43    printf("\n------------------------------------------\n" format "\n------------------------------------------\n", ##__VA_ARGS__ ); \
44  } while (0)
45 #endif
46 
47 enum {
48    TEST_VERBOSE = 1,
49    TEST_QUIET   = 2,
50    TEST_END     = 4,
51    TEST_PRINT_LOCAL = 8
52 };
53 void configure_test(uint64_t options);
54 bool _ok(const char *file, int l, const char *op, int value, const char *label);
55 bool _nok(const char *file, int l, const char *op, int value, const char *label);
56 bool _is(const char *file, int l, const char *op, const char *str, const char *str2, const char *label);
57 bool _isnt(const char *file, int l, const char *op, const char *str, const char *str2, const char *label);
58 bool _is(const char *file, int l, const char *op, int64_t nb, int64_t nb2, const char *label);
59 bool _isnt(const char *file, int l, const char *op, int64_t nb, int64_t nb2, const char *label);
60 int report();
61 void terminate(int sig);
62 void prolog(const char *name, bool lmgr=false, bool motd=true);
63 void epilog();
64 int unittest_get_nb_tests();
65 int unittest_get_nb_errors();
66 
67 /* The class based approach for C++ geeks */
68 class Unittests
69 {
70 public:
71    Unittests(const char *name, bool lmgr=false, bool motd=true);
~Unittests()72    ~Unittests() { epilog(); };
configure(uint64_t v)73    void configure(uint64_t v) { configure_test(v); };
74 };
75 
76 #endif /* _UNITTESTS_H_ */
77