2 * Copyright (c) 2006 Michael Bushkov <bushman@freebsd.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/queue.h>
31 #define DECLARE_TEST_DATA(ent) \
32 struct ent##_entry { \
34 STAILQ_ENTRY(ent##_entry) entries; \
37 struct ent##_test_data { \
38 void (*clone_func)(struct ent *, struct ent const *); \
39 void (*free_func)(struct ent *); \
41 STAILQ_HEAD(ent_head, ent##_entry) snapshot_data; \
44 void __##ent##_test_data_init(struct ent##_test_data *, \
45 void (*)(struct ent *, struct ent const *), \
46 void (*freef)(struct ent *)); \
47 void __##ent##_test_data_destroy(struct ent##_test_data *); \
49 void __##ent##_test_data_append(struct ent##_test_data *, struct ent *data);\
50 int __##ent##_test_data_foreach(struct ent##_test_data *, \
51 int (*)(struct ent *, void *), void *); \
52 int __##ent##_test_data_compare(struct ent##_test_data *, \
53 struct ent##_test_data *, int (*)(struct ent *, struct ent *, \
55 struct ent *__##ent##_test_data_find(struct ent##_test_data *, struct ent *,\
56 int (*)(struct ent *, struct ent *, void *), void *); \
57 void __##ent##_test_data_clear(struct ent##_test_data *);
59 #define TEST_DATA_INIT(ent, td, clonef, freef)\
60 __##ent##_test_data_init(td, clonef, freef)
61 #define TEST_DATA_DESTROY(ent, td) __##ent##_test_data_destroy(td)
62 #define TEST_DATA_APPEND(ent, td, d) __##ent##_test_data_append(td, d)
63 #define TEST_DATA_FOREACH(ent, td, f, mdata)\
64 __##ent##_test_data_foreach(td, f, mdata)
65 #define TEST_DATA_COMPARE(ent, td1, td2, fcmp, mdata)\
66 __##ent##_test_data_compare(td1, td2, fcmp, mdata);
67 #define TEST_DATA_FIND(ent, td, d, fcmp, mdata)\
68 __##ent##_test_data_find(td, d, fcmp, mdata)
69 #define TEST_DATA_CLEAR(ent, td) __##ent##_test_data_clear(td)
71 #define IMPLEMENT_TEST_DATA(ent) \
73 __##ent##_test_data_init(struct ent##_test_data *td, \
74 void (*clonef)(struct ent *, struct ent const *), \
75 void (*freef)(struct ent *)) \
78 assert(clonef != NULL); \
79 assert(freef != NULL); \
81 memset(td, 0, sizeof(*td)); \
82 td->clone_func = clonef; \
83 td->free_func = freef; \
84 STAILQ_INIT(&td->snapshot_data); \
88 __##ent##_test_data_destroy(struct ent##_test_data *td) \
90 __##ent##_test_data_clear(td); \
94 __##ent##_test_data_append(struct ent##_test_data *td, struct ent *app_data)\
96 struct ent##_entry *e; \
99 assert(app_data != NULL); \
101 e = (struct ent##_entry *)malloc(sizeof(struct ent##_entry)); \
103 memset(e, 0, sizeof(struct ent##_entry)); \
105 td->clone_func(&e->data, app_data); \
106 STAILQ_INSERT_TAIL(&td->snapshot_data, e, entries); \
110 __##ent##_test_data_foreach(struct ent##_test_data *td, \
111 int (*forf)(struct ent *, void *), void *mdata) \
113 struct ent##_entry *e; \
116 assert(td != NULL); \
117 assert(forf != NULL); \
120 STAILQ_FOREACH(e, &td->snapshot_data, entries) { \
121 rv = forf(&e->data, mdata); \
130 __##ent##_test_data_compare(struct ent##_test_data *td1, struct ent##_test_data *td2,\
131 int (*cmp_func)(struct ent *, struct ent *, void *), void *mdata)\
133 struct ent##_entry *e1, *e2; \
136 assert(td1 != NULL); \
137 assert(td2 != NULL); \
138 assert(cmp_func != NULL); \
140 e1 = STAILQ_FIRST(&td1->snapshot_data); \
141 e2 = STAILQ_FIRST(&td2->snapshot_data); \
145 if ((e1 == NULL) || (e2 == NULL)) { \
152 rv = cmp_func(&e1->data, &e2->data, mdata); \
153 e1 = STAILQ_NEXT(e1, entries); \
154 e2 = STAILQ_NEXT(e2, entries); \
161 __##ent##_test_data_find(struct ent##_test_data *td, struct ent *data, \
162 int (*cmp)(struct ent *, struct ent *, void *), void *mdata) \
164 struct ent##_entry *e; \
165 struct ent *result; \
167 assert(td != NULL); \
168 assert(cmp != NULL); \
171 STAILQ_FOREACH(e, &td->snapshot_data, entries) { \
172 if (cmp(&e->data, data, mdata) == 0) { \
183 __##ent##_test_data_clear(struct ent##_test_data *td) \
185 struct ent##_entry *e; \
186 assert(td != NULL); \
188 while (!STAILQ_EMPTY(&td->snapshot_data)) { \
189 e = STAILQ_FIRST(&td->snapshot_data); \
190 STAILQ_REMOVE_HEAD(&td->snapshot_data, entries); \
192 td->free_func(&e->data); \
198 #define DECLARE_TEST_FILE_SNAPSHOT(ent) \
199 struct ent##_snp_param { \
201 void (*sdump_func)(struct ent *, char *, size_t); \
204 int __##ent##_snapshot_write_func(struct ent *, void *); \
205 int __##ent##_snapshot_write(char const *, struct ent##_test_data *, \
206 void (*)(struct ent *, char *, size_t)); \
207 int __##ent##_snapshot_read(char const *, struct ent##_test_data *, \
208 int (*)(struct ent *, char *));
210 #define TEST_SNAPSHOT_FILE_WRITE(ent, fname, td, f) \
211 __##ent##_snapshot_write(fname, td, f)
212 #define TEST_SNAPSHOT_FILE_READ(ent, fname, td, f) \
213 __##ent##_snapshot_read(fname, td, f)
215 #define IMPLEMENT_TEST_FILE_SNAPSHOT(ent) \
217 __##ent##_snapshot_write_func(struct ent *data, void *mdata) \
220 struct ent##_snp_param *param; \
222 assert(data != NULL); \
224 param = (struct ent##_snp_param *)mdata; \
225 param->sdump_func(data, buffer, sizeof(buffer)); \
226 fputs(buffer, param->fp); \
227 fputc('\n', param->fp); \
233 __##ent##_snapshot_write(char const *fname, struct ent##_test_data *td, \
234 void (*sdump_func)(struct ent *, char *, size_t)) \
236 struct ent##_snp_param param; \
238 assert(fname != NULL); \
239 assert(td != NULL); \
241 param.fp = fopen(fname, "w"); \
242 if (param.fp == NULL) \
245 param.sdump_func = sdump_func; \
246 __##ent##_test_data_foreach(td, __##ent##_snapshot_write_func, ¶m);\
253 __##ent##_snapshot_read(char const *fname, struct ent##_test_data *td, \
254 int (*read_func)(struct ent *, char *)) \
263 assert(fname != NULL); \
264 assert(td != NULL); \
266 fi = fopen(fname, "r"); \
271 memset(buffer, 0, sizeof(buffer)); \
272 while (!feof(fi)) { \
273 s = fgets(buffer, sizeof(buffer), fi); \
274 if (s != NULL && s[0] != '#') { \
278 if (buffer[len - 1] == '\n') \
279 buffer[len -1] = '\0'; \
281 rv = read_func(&data, s); \
283 __##ent##_test_data_append(td, &data); \
284 td->free_func(&data); \
295 #define DECLARE_1PASS_TEST(ent) \
296 int __##ent##_1pass_test(struct ent##_test_data *, \
297 int (*)(struct ent *, void *), \
300 #define DO_1PASS_TEST(ent, td, f, mdata) \
301 __##ent##_1pass_test(td, f, mdata)
303 #define IMPLEMENT_1PASS_TEST(ent) \
305 __##ent##_1pass_test(struct ent##_test_data *td, \
306 int (*tf)(struct ent *, void *), \
310 rv = __##ent##_test_data_foreach(td, tf, mdata); \
315 #define DECLARE_2PASS_TEST(ent) \
316 int __##ent##_2pass_test(struct ent##_test_data *, \
317 struct ent##_test_data *, \
318 int (*)(struct ent *, struct ent *, void *), void *);
320 #define DO_2PASS_TEST(ent, td1, td2, f, mdata) \
321 __##ent##_2pass_test(td1, td2, f, mdata)
323 #define IMPLEMENT_2PASS_TEST(ent) \
325 __##ent##_2pass_test(struct ent##_test_data *td1, \
326 struct ent##_test_data *td2, \
327 int (*cmp_func)(struct ent *, struct ent *, void *), \
332 rv = __##ent##_test_data_compare(td1, td2, cmp_func, cmp_mdata); \