netatalk  4.6.0
Free and Open Source Apple Filing Protocol (AFP) Server
Loading...
Searching...
No Matches
test.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2010 Frank Lahm <franklahm@gmail.com>
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13*/
14
15#ifndef TEST_H
16#define TEST_H
17
18#ifdef HAVE_CONFIG_H
19#include "config.h"
20#endif /* HAVE_CONFIG_H */
21
22#include <errno.h>
23#include <stdio.h>
24#include <stdlib.h>
25#include <string.h>
26
27#include <atalk/cnid.h>
28#include <atalk/directory.h>
29#include <atalk/globals.h>
30#include <atalk/logger.h>
31#include <atalk/queue.h>
32#include <atalk/util.h>
33#include <atalk/volume.h>
34
35#include "afp_config.h"
36#include "dircache.h"
37#include "directory.h"
38#include "hash.h"
39#include "subtests.h"
40#include "volume.h"
41
42extern int test_output_tap;
43extern int test_case_num;
44extern FILE *test_report_stream;
45
46static inline FILE *test_stream(void)
47{
48 return test_report_stream ? test_report_stream : stdout;
49}
50
51static inline void alignok(int len)
52{
53 int i = 1;
54
55 if (len < 80) {
56 i = 80 - len;
57 }
58
59 while (i--) {
60 fprintf(test_stream(), " ");
61 }
62}
63
64static inline void test_plan(int count)
65{
66 if (test_output_tap) {
67 fprintf(test_stream(), "1..%d\n", count);
68 fflush(test_stream());
69 }
70}
71
72static inline void test_section(const char *title, const char *underline)
73{
74 if (!test_output_tap) {
75 fprintf(test_stream(), "%s\n%s\n", title, underline);
76 }
77}
78
79static inline void test_begin(const char *name)
80{
81 if (!test_output_tap) {
82 int name_len;
83 fprintf(test_stream(), "Testing: ");
84 name_len = fprintf(test_stream(), "%s", name);
85 fprintf(test_stream(), " ... ");
86
87 if (name_len >= 0) {
88 alignok(name_len);
89 }
90 }
91}
92
93static inline void test_ok(const char *name)
94{
95 if (test_output_tap) {
96 fprintf(test_stream(), "ok %d - %s\n", ++test_case_num, name);
97 fflush(test_stream());
98 } else {
99 fprintf(test_stream(), "[ok]\n");
100 }
101}
102
103static inline void test_fail(const char *name, const char *file, int line)
104{
105 if (test_output_tap) {
106 fprintf(test_stream(), "not ok %d - %s\n", ++test_case_num, name);
107 fprintf(test_stream(), "# failed at %s:%d\n", file, line);
108 fflush(test_stream());
109 } else {
110 fprintf(test_stream(), "[error]\n");
111 }
112}
113
114static inline void test_fail_int(const char *name, int got, int expected,
115 const char *file, int line)
116{
117 test_fail(name, file, line);
118
119 if (test_output_tap) {
120 fprintf(test_stream(), "# got: %d\n", got);
121 fprintf(test_stream(), "# expected: %d\n", expected);
122 fflush(test_stream());
123 }
124}
125
126static inline void test_abort(void)
127{
128 if (test_output_tap) {
129 fprintf(test_stream(), "Bail out! stopping after failed assertion\n");
130 fflush(test_stream());
131 }
132}
133
137#define TEST_SKIP 77
138
139static inline void test_skip(const char *name)
140{
141 if (test_output_tap) {
142 fprintf(test_stream(), "ok %d - %s # SKIP\n", ++test_case_num, name);
143 fflush(test_stream());
144 } else {
145 fprintf(test_stream(), "[skip]\n");
146 }
147}
148
149/* Every TEST* macro takes a trailing human-readable summary `desc` that becomes
150 * the test NAME in the output: the text after "ok N - " in TAP mode (what
151 * `meson test` shows), and after "Testing: " in plain mode. `desc` is REQUIRED
152 * -- it is what makes the run self-describing ("ok 25 - openfork() error path
153 * closes the leaked fork" instead of an opaque stringified expression). On
154 * failure, file:line still pinpoints the offending call. */
155
156#define TEST(a, desc) \
157 do { \
158 test_begin(desc); \
159 a; \
160 test_ok(desc); \
161 } while (0)
162
163#define TEST_int(a, b, desc) \
164 do { \
165 test_begin(desc); \
166 if ((reti = (a)) != b) { \
167 test_fail_int(desc, reti, b, __FILE__, \
168 __LINE__); \
169 test_abort(); \
170 exit(1); \
171 } else { \
172 test_ok(desc); \
173 } \
174 } while (0)
175
176#define TEST_expr(a, b, desc) \
177 do { \
178 test_begin(desc); \
179 a; \
180 if (b) { \
181 test_ok(desc); \
182 } else { \
183 test_fail(desc, __FILE__, __LINE__); \
184 test_abort(); \
185 exit(1); \
186 } \
187 } while (0)
188
191#define TEST_int_or_skip(a, b, desc) \
192 do { \
193 test_begin(desc); \
194 reti = (a); \
195 if (reti == TEST_SKIP) { \
196 test_skip(desc); \
197 } else if (reti != b) { \
198 test_fail_int(desc, reti, b, __FILE__, \
199 __LINE__); \
200 test_abort(); \
201 exit(1); \
202 } else { \
203 test_ok(desc); \
204 } \
205 } while (0)
206#endif /* TEST_H */
Netatalk utility functions.
int test_case_num
Definition test.c:78
int test_output_tap
Definition test.c:77
FILE * test_report_stream
Definition test.c:79
static void test_plan(int count)
Definition test.h:64
static void test_section(const char *title, const char *underline)
Definition test.h:72
static void test_ok(const char *name)
Definition test.h:93
static void test_skip(const char *name)
Definition test.h:139
static void test_abort(void)
Definition test.h:126
static void test_begin(const char *name)
Definition test.h:79
static void alignok(int len)
Definition test.h:51
static void test_fail_int(const char *name, int got, int expected, const char *file, int line)
Definition test.h:114
static void test_fail(const char *name, const char *file, int line)
Definition test.h:103
static FILE * test_stream(void)
Definition test.h:46