]> git.saurik.com Git - apple/mdnsresponder.git/blob - unittests/unittest.c
mDNSResponder-765.1.2.tar.gz
[apple/mdnsresponder.git] / unittests / unittest.c
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 #include <stdlib.h>
18 #include "unittest.h"
19
20 int _unittest_assert_i(const int condition, const int i, const char * const conditionStr,
21 const char * const filename, const unsigned int linenum,
22 const char * const functionname, __test_item ** __i, int * const __success)
23 {
24 if (!condition)
25 {
26 __test_item* tba = malloc(sizeof(__test_item));
27 tba->next = *__i;
28 tba->file = filename;
29 tba->line = linenum;
30 tba->func = functionname;
31 tba->s = conditionStr;
32 tba->iter_count = i;
33 *__i = tba;
34 *__success = 0;
35 printf("F");
36 }
37 else
38 {
39 printf(".");
40 }
41
42 fflush(NULL);
43 return condition;
44 }
45
46 void _unittest_print_list(__test_item* __i)
47 {
48 __test_item* __tmp = NULL;
49 while (__i)
50 {
51 __test_item* __o = __i->next;
52 __i->next = __tmp;
53 __tmp = __i;
54 __i = __o;
55 }
56 __i = __tmp;
57
58 while(__i)
59 {
60 printf("%s: In function `%s':\n%s:%d: error: failed UNITTEST_ASSERT", __i->file, __i->func, __i->file, __i->line);
61 if (__i->iter_count != -1) printf(" at iteration %d", __i->iter_count);
62 printf(": %s\n", __i->s);
63 __test_item* tbd = __i;
64 __i = __i->next;
65 free(tbd);
66 }
67 }
68
69 // test by building like:
70 // gcc -g -Wall -Werror -DTEST_UNITTEST_SCAFFOLD unittest.c
71 // #define TEST_UNITTEST_SCAFFOLD 1
72 #if TEST_UNITTEST_SCAFFOLD
73
74 // modify this test as necessary to test the scaffold
75 UNITTEST_HEADER(test1)
76 int i = 0;
77 int j = 1;
78 int k = 2;
79
80 UNITTEST_ASSERT(i==j);
81 UNITTEST_ASSERTI(j==i, k);
82 UNITTEST_ASSERT(i==i);
83 UNITTEST_ASSERTI(j==j, k);
84 UNITTEST_ASSERT_RETURN(j==j);
85 UNITTEST_ASSERTI_RETURN(j==j, k);
86 UNITTEST_FOOTER
87
88 UNITTEST_HEADER(test2)
89 UNITTEST_ASSERT(1);
90 UNITTEST_ASSERT(0);
91 UNITTEST_ASSERT(1);
92 UNITTEST_FOOTER
93
94 UNITTEST_HEADER(unittest_tests)
95 UNITTEST_TEST(test1)
96 UNITTEST_TEST(test2)
97 UNITTEST_FOOTER
98
99 UNITTEST_HEADER(run_tests)
100 UNITTEST_GROUP(unittest_tests)
101 UNITTEST_FOOTER
102
103 UNITTEST_MAIN
104
105 #endif // TEST_UNITTEST_SCAFFOLD