test_helper.c revision 9888:68d6b600d51f
1/*
2 * Copyright (c) 2013, Andreas Sandberg
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above
12 *    copyright notice, this list of conditions and the following
13 *    disclaimer in the documentation and/or other materials provided
14 *    with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
19 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
20 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
25 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
27 * OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30#include "test_helper.h"
31
32#include <assert.h>
33#include <stdarg.h>
34#include <stdlib.h>
35
36unsigned test_current = 0;
37unsigned test_count = 0;
38unsigned test_fail_count = 0;
39
40void
41test_init(unsigned no_tests)
42{
43    assert(test_count == 0 && test_current == 0);
44
45    test_count = no_tests;
46    test_current = 1;
47    test_fail_count = 0;
48
49    printf("1..%u\n", no_tests);
50}
51
52void
53test_exit()
54{
55    if (test_fail_count)
56        exit(EXIT_FAILURE);
57    else
58        exit(EXIT_SUCCESS);
59}
60
61void
62test_bail(const char *fmt, ...)
63{
64    va_list ap;
65    va_start(ap, fmt);
66
67    printf("Bail out! ");
68    vprintf(fmt, ap);
69    printf("\n");
70
71    va_end(ap);
72
73    exit(EXIT_FAILURE);
74}
75
76void
77test_diag(const char *fmt, ...)
78{
79    va_list ap;
80    va_start(ap, fmt);
81
82    printf("# ");
83    vprintf(fmt, ap);
84    printf("\n");
85
86    va_end(ap);
87}
88
89static void
90test_vstatus(const char *status, const char *test,
91             const char *directive,
92             const char *fmt_why, va_list ap)
93{
94    printf("%s %i", status, test_current);
95
96    if (test && test[0] != '\0')
97        printf(" - %s", test);
98
99    if (directive && directive[0] != '\0') {
100        printf(" # %s ", directive);
101        if (fmt_why && fmt_why[0] != '\0')
102            vprintf(fmt_why, ap);
103    }
104    printf("\n");
105
106    ++test_current;
107}
108
109static void __attribute__((format (printf, 4, 5)))
110test_status(const char *status, const char *test,
111            const char *directive,
112            const char *fmt_why, ...)
113{
114    va_list ap;
115    va_start(ap, fmt_why);
116
117    test_vstatus(status, test, directive, fmt_why, ap);
118
119    va_end(ap);
120}
121
122void
123test_ok(const char *test)
124{
125    test_status("ok", test, NULL, NULL);
126}
127
128void
129test_fail(const char *test)
130{
131    test_status("not ok", test, NULL, NULL);
132    ++test_fail_count;
133}
134
135void
136test_skip(const char *test, const char *fmt_why, ...)
137{
138    va_list ap;
139    va_start(ap, fmt_why);
140
141    test_vstatus("ok", test, "SKIP", fmt_why, ap);
142
143    va_end(ap);
144}
145
146void
147test_todo(const char *test, const char *fmt_why, ...)
148{
149    va_list ap;
150    va_start(ap, fmt_why);
151
152    test_vstatus("not ok", test, "TODO", fmt_why, ap);
153
154    va_end(ap);
155
156    ++test_fail_count;
157}
158