]>
git.saurik.com Git - apple/mdnsresponder.git/blob - unittests/unittest.h
a219aa8660342bb9bfa0d05b6d41e6780d98d277
1 /* -*- Mode: C; tab-width: 4 -*-
3 * Copyright (c) 2015 Apple Inc. All rights reserved.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
31 typedef struct __test_item_
33 struct __test_item_
* next
;
42 #define UNITTEST_HEADER(X) int X() { int __success = 1; __test_item* __i = NULL;
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; }
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)
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; }
58 #define UNITTEST_MAIN int main (int argc, char** argv) \
61 signal(SIGPIPE, SIG_IGN); \
63 unlink("unittest_success"); \
64 if (!run_tests()) return -1; \
65 fp = fopen("unittest_success", "w"); \
68 printf("unit test SUCCESS\n"); \
72 printf("run leaks now\n"); \
73 read(STDIN_FILENO, &c, 1); \
78 #define UNITTEST_FAIL_ASSERT { assert(((void*)__func__) == 0); }
84 #endif // ndef _UNITTEST_H_