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