Statistics
| Branch: | Revision:

iof-bird / bird-2.0.1 / test / birdtest.h @ 6b3f1a54

History | View | Annotate | Download (5.27 KB)

1
/*
2
 *        BIRD -- Unit Test Framework (BIRD Test)
3
 *
4
 *        Can be freely distributed and used under the terms of the GNU GPL.
5
 */
6

    
7
#ifndef _BIRDTEST_H_
8
#define _BIRDTEST_H_
9

    
10
#include <stdio.h>
11
#include <stdlib.h>
12
#include <stdint.h>
13
#include <string.h>
14
#include <errno.h>
15
#include <sys/types.h>
16

    
17
#include "nest/bird.h"
18

    
19

    
20
extern int bt_result;
21
extern int bt_suite_result;
22
extern char bt_out_fmt_buf[1024];
23

    
24
extern uint bt_verbose;
25
#define BT_VERBOSE_NO                        0
26
#define BT_VERBOSE_SUITE                1
27
#define BT_VERBOSE_SUITE_CASE                2
28
#define BT_VERBOSE_ABSOLUTELY_ALL        3
29

    
30
extern const char *bt_filename;
31
extern const char *bt_test_id;
32

    
33
void bt_init(int argc, char *argv[]);
34
int  bt_exit_value(void);
35
int bt_test_suite_base(int (*test_fn)(const void *), const char *test_id, const void *test_fn_argument, int forked, int timeout, const char *dsc, ...);
36
long int bt_random(void);
37

    
38
void bt_log_suite_result(int result, const char *fmt, ...);
39
void bt_log_suite_case_result(int result, const char *fmt, ...);
40

    
41
#define BT_TIMEOUT                         5        /* Default timeout in seconds */
42
#define BT_FORKING                         1        /* Forking is enabled in default */
43

    
44
#define BT_RANDOM_SEED                         982451653
45

    
46
#define BT_BUFFER_SIZE                         10000
47

    
48
#define BT_PROMPT_GREEN                 "\e[1;32m"
49
#define BT_PROMPT_RED                         "\e[1;31m"
50
#define BT_PROMPT_NORMAL                "\e[0m"
51
#define BT_PROMPT_OK                        " [" BT_PROMPT_GREEN " OK " BT_PROMPT_NORMAL "] "
52
#define BT_PROMPT_OK_NO_COLOR                " ["                 " OK "                  "] "
53
#define BT_PROMPT_FAIL                        " [" BT_PROMPT_RED   "FAIL" BT_PROMPT_NORMAL "] "
54
#define BT_PROMPT_FAIL_NO_COLOR                " ["                 "FAIL"                  "] "
55
#define BT_PROMPT_OK_FAIL_STRLEN        8        /* strlen ' [FAIL] ' */
56

    
57
#define bt_test_suite(fn, dsc, ...) \
58
  bt_test_suite_extra(fn, BT_FORKING, BT_TIMEOUT, dsc, ##__VA_ARGS__)
59

    
60
#define bt_test_suite_extra(fn, f, t, dsc, ...) \
61
  bt_test_suite_base((int (*)(const void *))fn, #fn, NULL, f, t, dsc, ##__VA_ARGS__)
62

    
63
#define bt_test_suite_arg(fn, arg, dsc, ...) \
64
  bt_test_suite_arg_extra(fn, arg, BT_FORKING, BT_TIMEOUT, dsc, ##__VA_ARGS__)
65

    
66
#define bt_test_suite_arg_extra(fn, arg, f, t, dsc, ...) \
67
  bt_test_suite_base(fn, #fn, arg, f, t, dsc, ##__VA_ARGS__)
68

    
69
#define bt_abort() \
70
  bt_abort_msg("Aborted at %s:%d", __FILE__, __LINE__)
71

    
72
#define bt_abort_msg(format, ...)                                         \
73
  do                                                                         \
74
  {                                                                         \
75
    bt_log(format, ##__VA_ARGS__);                                         \
76
    abort();                                                                 \
77
  } while (0)
78

    
79
#define bt_log(format, ...)                                                 \
80
  do                                                                         \
81
  {                                                                         \
82
    if (bt_test_id)                                                         \
83
      printf("%s: %s: " format "\n", bt_filename, bt_test_id, ##__VA_ARGS__); \
84
    else                                                                 \
85
      printf("%s: " format "\n", bt_filename, ##__VA_ARGS__);                \
86
  } while(0)
87

    
88
#define bt_debug(format, ...)                                                 \
89
  do                                                                         \
90
  {                                                                         \
91
    if (bt_verbose >= BT_VERBOSE_ABSOLUTELY_ALL)                        \
92
      printf(format, ##__VA_ARGS__);                                         \
93
  } while (0)
94

    
95
#define bt_assert(test) \
96
  bt_assert_msg(test, "Assertion (%s) at %s:%d", #test, __FILE__, __LINE__)
97

    
98
#define bt_assert_msg(test, format, ...)                                \
99
  do                                                                        \
100
  {                                                                        \
101
    int bt_suit_case_result = 1;                                \
102
    if ((test) == 0)                                                         \
103
    {                                                                        \
104
      bt_result = 0;                                                \
105
      bt_suite_result = 0;                                        \
106
      bt_suit_case_result = 0;                                        \
107
    }                                                                        \
108
    bt_log_suite_case_result(bt_suit_case_result, format, ##__VA_ARGS__); \
109
  } while (0)
110

    
111
#define bt_syscall(test, format, ...)                                         \
112
  do                                                                         \
113
  {                                                                         \
114
    if (test)                                                                 \
115
    {                                                                        \
116
      bt_log(format ": %s", ##__VA_ARGS__, strerror(errno));                 \
117
      exit(3);                                                                \
118
    }                                                                        \
119
  } while (0)
120

    
121
#define bt_sprintf_concat(s, format, ...) \
122
    snprintf(s + strlen(s), sizeof(s) - strlen(s), format, ##__VA_ARGS__)
123

    
124
struct bt_pair {
125
  const void *in;
126
  const void *out;
127
};
128

    
129
/* Data structure used by bt_assert_batch() function */
130
struct bt_batch {
131
  /* in_fmt / out_fmt - formating data
132
   * @buf: buffer for write stringified @data
133
   * @size: empty size in @buf
134
   * @data: data for stringify
135
   *
136
   * There are some build-in functions, see bt_fmt_* functions */
137
  void (*in_fmt)(char *buf, size_t size, const void *data);
138
  void (*out_fmt)(char *buf, size_t size, const void *data);
139

    
140
  /* Temporary output buffer */
141
  void *out_buf;
142

    
143
  /* test_fn - testing function
144
   * @out: output data from tested function
145
   * @in: data for input
146
   * @expected_out: expected data from tested function
147
   *
148
   * Input arguments should not be stringified using in_fmt() or out_fmt()
149
   * function already. This function should return only 0 or 1 */
150
  int (*test_fn)(void *out, const void *in, const void *expected_out);
151

    
152
  /* Name of testing function @test_fn */
153
  const char *test_fn_name;
154

    
155
  /* Number of items in data */
156
  int ndata;
157

    
158
  /* Array of input and expected output pairs */
159
  struct bt_pair *data;
160
};
161

    
162
void bt_fmt_str(char *buf, size_t size, const void *data);
163
void bt_fmt_unsigned(char *buf, size_t size, const void *data);
164
void bt_fmt_ipa(char *buf, size_t size, const void *data);
165
int bt_assert_batch__(struct bt_batch *opts);
166
int bt_is_char(byte c);
167

    
168
#define bt_assert_batch(data__, fn__, in_fmt__, out_fmt__)                \
169
  bt_assert_batch__(& (struct bt_batch) {                                \
170
    .data = data__,                                                        \
171
    .ndata = ARRAY_SIZE(data__),                                        \
172
    .test_fn = fn__,                                                        \
173
    .test_fn_name = #fn__,                                                \
174
    .in_fmt = in_fmt__,                                                        \
175
    .out_fmt = out_fmt__,                                                \
176
    .out_buf = bt_out_fmt_buf,        /* Global memory for this usage */        \
177
  })
178

    
179
#endif /* _BIRDTEST_H_ */