1/*
2 * Copyright (c) 2013 Andreas Sandberg
3 * 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 * Authors: Andreas Sandberg
18 */
19
20#include "test_helpers.h"
21
22#include <assert.h>
23#include <stdarg.h>
24#include <stdlib.h>
25
26unsigned test_current = 0;
27unsigned test_count = 0;
28unsigned test_fail_count = 0;
29
30void
31test_init(unsigned no_tests)
32{
33    assert(test_count == 0 && test_current == 0);
34
35    test_count = no_tests;
36    test_current = 1;
37    test_fail_count = 0;
38
39    printf("1..%u\n", no_tests);
40}
41
42void
43test_exit()
44{
45    if (test_fail_count)
46        exit(EXIT_FAILURE);
47    else
48        exit(EXIT_SUCCESS);
49}
50
51void
52test_bail(const char *fmt, ...)
53{
54    va_list ap;
55    va_start(ap, fmt);
56
57    printf("Bail out! ");
58    vprintf(fmt, ap);
59    printf("\n");
60
61    va_end(ap);
62
63    exit(EXIT_FAILURE);
64}
65
66void
67test_diag(const char *fmt, ...)
68{
69    va_list ap;
70    va_start(ap, fmt);
71
72    printf("# ");
73    vprintf(fmt, ap);
74    printf("\n");
75
76    va_end(ap);
77}
78
79static void
80test_vstatus(const char *status, const char *test,
81             const char *directive,
82             const char *fmt_why, va_list ap)
83{
84    printf("%s %i", status, test_current);
85
86    if (test && test[0] != '\0')
87        printf(" - %s", test);
88
89    if (directive && directive[0] != '\0') {
90        printf(" # %s ", directive);
91        if (fmt_why && fmt_why[0] != '\0')
92            vprintf(fmt_why, ap);
93    }
94    printf("\n");
95
96    ++test_current;
97}
98
99static void __attribute__((format (printf, 4, 5)))
100test_status(const char *status, const char *test,
101            const char *directive,
102            const char *fmt_why, ...)
103{
104    va_list ap;
105    va_start(ap, fmt_why);
106
107    test_vstatus(status, test, directive, fmt_why, ap);
108
109    va_end(ap);
110}
111
112void
113test_ok(const char *test)
114{
115    test_status("ok", test, NULL, NULL);
116}
117
118void
119test_fail(const char *test)
120{
121    test_status("not ok", test, NULL, NULL);
122    ++test_fail_count;
123}
124
125void
126test_skip(const char *test, const char *fmt_why, ...)
127{
128    va_list ap;
129    va_start(ap, fmt_why);
130
131    test_vstatus("ok", test, "SKIP", fmt_why, ap);
132
133    va_end(ap);
134}
135
136void
137test_todo(const char *test, const char *fmt_why, ...)
138{
139    va_list ap;
140    va_start(ap, fmt_why);
141
142    test_vstatus("not ok", test, "TODO", fmt_why, ap);
143
144    va_end(ap);
145
146    ++test_fail_count;
147}
148