]> git.saurik.com Git - apple/mdnsresponder.git/blob - unittests/unittest.h
a219aa8660342bb9bfa0d05b6d41e6780d98d277
[apple/mdnsresponder.git] / unittests / unittest.h
1 /* -*- Mode: C; tab-width: 4 -*-
2 *
3 * Copyright (c) 2015 Apple Inc. All rights reserved.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <unistd.h>
21 #include <assert.h>
22 #include <signal.h>
23
24 #ifndef _UNITTEST_H_
25 #define _UNITTEST_H_
26
27 #ifdef __cplusplus
28 extern "C" {
29 #endif
30
31 typedef struct __test_item_
32 {
33 struct __test_item_* next;
34 const char* file;
35 unsigned int line;
36 const char* func;
37 const char* s;
38 int iter_count;
39 } __test_item;
40
41
42 #define UNITTEST_HEADER(X) int X() { int __success = 1; __test_item* __i = NULL;
43
44 #define UNITTEST_GROUP(X) { printf("== %s ==\n", #X); __success = X() && __success; }
45 #define UNITTEST_TEST(X) { printf("%s: ", #X); fflush(NULL); __success = X() && __success; }
46
47 int _unittest_assert_i(const int condition, const int i, const char * const conditionStr,
48 const char * const filename, const unsigned int linenum,
49 const char * const functionname, __test_item ** __i, int * const __success);
50 #define UNITTEST_ASSERTI(X,I) (_unittest_assert_i((X)!=0, (I), #X, __FILE__, __LINE__, __func__, &__i, &__success))
51 #define UNITTEST_ASSERT(X) UNITTEST_ASSERTI(X, -1)
52 #define UNITTEST_ASSERTI_RETURN(X,I) { if (!UNITTEST_ASSERTI(X,I)) goto __unittest_footer__; }
53 #define UNITTEST_ASSERT_RETURN(X) UNITTEST_ASSERTI_RETURN(X, -1)
54
55 void _unittest_print_list(__test_item* __i);
56 #define UNITTEST_FOOTER goto __unittest_footer__; __unittest_footer__: printf("\n"); fflush(NULL); _unittest_print_list(__i); return __success; }
57
58 #define UNITTEST_MAIN int main (int argc, char** argv) \
59 { \
60 (void)(argv); \
61 signal(SIGPIPE, SIG_IGN); \
62 FILE* fp; \
63 unlink("unittest_success"); \
64 if (!run_tests()) return -1; \
65 fp = fopen("unittest_success", "w"); \
66 if (!fp) return -2; \
67 fclose(fp); \
68 printf("unit test SUCCESS\n"); \
69 if (argc != 1) \
70 { \
71 char c; \
72 printf("run leaks now\n"); \
73 read(STDIN_FILENO, &c, 1); \
74 } \
75 return 0; \
76 }
77
78 #define UNITTEST_FAIL_ASSERT { assert(((void*)__func__) == 0); }
79
80 #ifdef __cplusplus
81 }
82 #endif
83
84 #endif // ndef _UNITTEST_H_