summaryrefslogtreecommitdiffstats
path: root/mDNSResponder/unittests/unittest.c
blob: 1ad4b0303c889d643181ddca1934468b7ccd5e0b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/* -*- Mode: C; tab-width: 4 -*-
 *
 * Copyright (c) 2015 Apple Inc. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#include <stdlib.h>
#include "unittest.h"

int _unittest_assert_i(const int condition, const int i, const char * const conditionStr,
                       const char * const filename, const unsigned int linenum,
                       const char * const functionname, __test_item ** __i, int * const __success)
{
    if (!condition)
    {
        __test_item* tba = malloc(sizeof(__test_item));
        tba->next = *__i;
        tba->file = filename;
        tba->line = linenum;
        tba->func = functionname;
        tba->s = conditionStr;
        tba->iter_count = i;
        *__i = tba;
        *__success = 0;
        printf("F");
    }
    else
    {
        printf(".");
    }

    fflush(NULL);
    return condition;
}

void _unittest_print_list(__test_item* __i)
{
    __test_item* __tmp = NULL;
    while (__i)
    {
        __test_item* __o = __i->next;
        __i->next = __tmp;
        __tmp = __i;
        __i = __o;
    }
    __i = __tmp;

    while(__i)
    {
        printf("%s: In function `%s':\n%s:%d: error: failed UNITTEST_ASSERT", __i->file, __i->func, __i->file, __i->line);
        if (__i->iter_count != -1) printf(" at iteration %d", __i->iter_count);
        printf(": %s\n", __i->s);
        __test_item* tbd = __i;
        __i = __i->next;
        free(tbd);
    }
}

// test by building like:
//   gcc -g -Wall -Werror -DTEST_UNITTEST_SCAFFOLD unittest.c
// #define TEST_UNITTEST_SCAFFOLD 1
#if TEST_UNITTEST_SCAFFOLD

// modify this test as necessary to test the scaffold
UNITTEST_HEADER(test1)
    int i = 0;
    int j = 1;
    int k = 2;

    UNITTEST_ASSERT(i==j);
    UNITTEST_ASSERTI(j==i, k);
    UNITTEST_ASSERT(i==i);
    UNITTEST_ASSERTI(j==j, k);
    UNITTEST_ASSERT_RETURN(j==j);
    UNITTEST_ASSERTI_RETURN(j==j, k);
UNITTEST_FOOTER

UNITTEST_HEADER(test2)
    UNITTEST_ASSERT(1);
    UNITTEST_ASSERT(0);
    UNITTEST_ASSERT(1);
UNITTEST_FOOTER

UNITTEST_HEADER(unittest_tests)
UNITTEST_TEST(test1)
UNITTEST_TEST(test2)
UNITTEST_FOOTER

UNITTEST_HEADER(run_tests)
UNITTEST_GROUP(unittest_tests)
UNITTEST_FOOTER

UNITTEST_MAIN

#endif // TEST_UNITTEST_SCAFFOLD