Sync usage with man page.
[netbsd-mini2440.git] / external / bsd / atf / dist / atf-c / tcr.c
blobfe10df2290704844d0b34d5bdb21f205fcda9f47
1 /*
2 * Automated Testing Framework (atf)
4 * Copyright (c) 2008, 2009 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
17 * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
18 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #include <stdbool.h>
32 #include "atf-c/error.h"
33 #include "atf-c/io.h"
34 #include "atf-c/sanity.h"
35 #include "atf-c/tcr.h"
37 /* ---------------------------------------------------------------------
38 * Auxiliary types and functions.
39 * --------------------------------------------------------------------- */
41 static
42 bool
43 state_allows_reason(atf_tcr_state_t state)
45 return state == atf_tcr_failed_state || state == atf_tcr_skipped_state;
48 static
49 atf_error_t
50 format_reason(atf_dynstr_t *reason, const char *fmt, va_list ap)
52 atf_error_t err;
53 atf_dynstr_t tmp;
54 va_list ap2;
56 va_copy(ap2, ap);
57 err = atf_dynstr_init_ap(&tmp, fmt, ap2);
58 va_end(ap2);
59 if (atf_is_error(err))
60 goto out;
62 /* There is no reason for calling rfind instead of find other than
63 * find is not implemented. */
64 if (atf_dynstr_rfind_ch(&tmp, '\n') == atf_dynstr_npos) {
65 err = atf_dynstr_copy(reason, &tmp);
66 } else {
67 const char *iter;
69 err = atf_dynstr_init_fmt(reason, "BOGUS REASON (THE ORIGINAL "
70 "HAD NEWLINES): ");
71 if (atf_is_error(err))
72 goto out_tmp;
74 for (iter = atf_dynstr_cstring(&tmp); *iter != '\0'; iter++) {
75 if (*iter == '\n')
76 err = atf_dynstr_append_fmt(reason, "<<NEWLINE>>");
77 else
78 err = atf_dynstr_append_fmt(reason, "%c", *iter);
80 if (atf_is_error(err)) {
81 atf_dynstr_fini(reason);
82 break;
87 out_tmp:
88 atf_dynstr_fini(&tmp);
89 out:
90 return err;
93 /* ---------------------------------------------------------------------
94 * The "atf_tcr" type.
95 * --------------------------------------------------------------------- */
98 * Constants.
100 const atf_tcr_state_t atf_tcr_passed_state = 0;
101 const atf_tcr_state_t atf_tcr_failed_state = 1;
102 const atf_tcr_state_t atf_tcr_skipped_state = 2;
105 * Constructors/destructors.
108 atf_error_t
109 atf_tcr_init(atf_tcr_t *tcr, atf_tcr_state_t state)
111 atf_error_t err;
113 PRE(!state_allows_reason(state));
115 atf_object_init(&tcr->m_object);
117 tcr->m_state = state;
119 err = atf_dynstr_init(&tcr->m_reason);
120 if (atf_is_error(err))
121 goto err_object;
123 INV(!atf_is_error(err));
124 return err;
126 err_object:
127 atf_object_fini(&tcr->m_object);
129 return err;
132 atf_error_t
133 atf_tcr_init_reason_ap(atf_tcr_t *tcr, atf_tcr_state_t state,
134 const char *fmt, va_list ap)
136 va_list ap2;
137 atf_error_t err;
139 PRE(state_allows_reason(state));
141 atf_object_init(&tcr->m_object);
143 tcr->m_state = state;
145 va_copy(ap2, ap);
146 err = format_reason(&tcr->m_reason, fmt, ap2);
147 va_end(ap2);
148 if (atf_is_error(err))
149 goto err_object;
151 INV(!atf_is_error(err));
152 return err;
154 err_object:
155 atf_object_fini(&tcr->m_object);
157 return err;
160 atf_error_t
161 atf_tcr_init_reason_fmt(atf_tcr_t *tcr, atf_tcr_state_t state,
162 const char *fmt, ...)
164 va_list ap;
165 atf_error_t err;
167 va_start(ap, fmt);
168 err = atf_tcr_init_reason_ap(tcr, state, fmt, ap);
169 va_end(ap);
171 return err;
174 void
175 atf_tcr_fini(atf_tcr_t *tcr)
177 atf_dynstr_fini(&tcr->m_reason);
179 atf_object_fini(&tcr->m_object);
183 * Getters.
186 atf_tcr_state_t
187 atf_tcr_get_state(const atf_tcr_t *tcr)
189 return tcr->m_state;
192 const atf_dynstr_t *
193 atf_tcr_get_reason(const atf_tcr_t *tcr)
195 PRE(state_allows_reason(tcr->m_state));
196 return &tcr->m_reason;
199 bool
200 atf_tcr_has_reason(const atf_tcr_t *tcr)
202 return state_allows_reason(tcr->m_state);
206 * Operators.
209 bool
210 atf_equal_tcr_tcr(const atf_tcr_t *tcr1, const atf_tcr_t *tcr2)
212 const bool ar1 = state_allows_reason(tcr1->m_state);
213 const bool ar2 = state_allows_reason(tcr2->m_state);
215 bool equal;
217 if (ar1 && ar2) {
218 equal = tcr1->m_state == tcr2->m_state &&
219 atf_equal_dynstr_dynstr(&tcr1->m_reason, &tcr2->m_reason);
220 } else if (ar1 && !ar2) {
221 equal = false;
222 } else if (!ar1 && ar2) {
223 equal = false;
224 } else {
225 INV(!ar1 && !ar2);
226 equal = tcr1->m_state == tcr2->m_state;
229 return equal;
233 * Serialization.
236 atf_error_t
237 atf_tcr_serialize(const atf_tcr_t *tcr, const int fd)
239 atf_error_t err;
240 const atf_tcr_state_t s = atf_tcr_get_state(tcr);
241 const char *str;
243 if (s == atf_tcr_passed_state)
244 str = "passed";
245 else if (s == atf_tcr_failed_state)
246 str = "failed";
247 else if (s == atf_tcr_skipped_state)
248 str = "skipped";
249 else {
250 UNREACHABLE;
251 str = NULL;
254 err = atf_io_write_fmt(fd, "%s\n", str);
255 if (atf_is_error(err))
256 goto out;
258 if (atf_tcr_has_reason(tcr)) {
259 const atf_dynstr_t *r = atf_tcr_get_reason(tcr);
260 err = atf_io_write_fmt(fd, "%s\n", atf_dynstr_cstring(r));
261 if (atf_is_error(err))
262 goto out;
265 INV(!atf_is_error(err));
267 out:
268 return err;
271 atf_error_t
272 atf_tcr_deserialize(atf_tcr_t *tcr, const int fd)
274 atf_error_t err;
275 atf_dynstr_t state, reason;
276 bool eof;
278 err = atf_dynstr_init(&state);
279 if (atf_is_error(err))
280 goto out;
282 err = atf_dynstr_init(&reason);
283 if (atf_is_error(err))
284 goto out_state;
286 err = atf_io_readline(fd, &state, &eof);
287 if (atf_is_error(err))
288 goto out_reason;
289 INV(!eof);
291 if (atf_equal_dynstr_cstring(&state, "passed"))
292 err = atf_tcr_init(tcr, atf_tcr_passed_state);
293 else {
294 err = atf_io_readline(fd, &reason, &eof);
295 if (atf_is_error(err))
296 goto out_reason;
297 INV(!eof);
299 if (atf_equal_dynstr_cstring(&state, "failed"))
300 err = atf_tcr_init_reason_fmt(tcr, atf_tcr_failed_state, "%s",
301 atf_dynstr_cstring(&reason));
302 else if (atf_equal_dynstr_cstring(&state, "skipped"))
303 err = atf_tcr_init_reason_fmt(tcr, atf_tcr_skipped_state, "%s",
304 atf_dynstr_cstring(&reason));
305 else
306 UNREACHABLE;
309 out_reason:
310 atf_dynstr_fini(&reason);
311 out_state:
312 atf_dynstr_fini(&state);
313 out:
314 return err;