tools/llvm: Do not build with symbols
[minix3.git] / external / bsd / atf / dist / atf-c / error_test.c
blob2c2a30789b2c92e022a03ae951f639ca90f4a9fa
1 /*
2 * Automated Testing Framework (atf)
4 * Copyright (c) 2008 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 <errno.h>
31 #include <stdint.h>
32 #include <stdio.h>
33 #include <string.h>
35 #include <atf-c.h>
37 #include "atf-c/defs.h"
38 #include "atf-c/error.h"
40 #include "detail/test_helpers.h"
42 /* ---------------------------------------------------------------------
43 * Auxiliary functions.
44 * --------------------------------------------------------------------- */
46 static
47 void
48 test_format(const atf_error_t err ATF_DEFS_ATTRIBUTE_UNUSED,
49 char *buf, size_t buflen)
51 snprintf(buf, buflen, "Test formatting function");
54 /* ---------------------------------------------------------------------
55 * Tests for the "atf_error" type.
56 * --------------------------------------------------------------------- */
58 ATF_TC(error_new);
59 ATF_TC_HEAD(error_new, tc)
61 atf_tc_set_md_var(tc, "descr", "Checks the construction of an error "
62 "object");
64 ATF_TC_BODY(error_new, tc)
66 atf_error_t err;
67 int data;
69 err = atf_error_new("test_error", NULL, 0, NULL);
70 ATF_REQUIRE(atf_error_is(err, "test_error"));
71 ATF_REQUIRE(!atf_error_is(err, "unknown_error"));
72 ATF_REQUIRE(atf_error_data(err) == NULL);
73 atf_error_free(err);
75 data = 5;
76 err = atf_error_new("test_data_error", &data, sizeof(data), NULL);
77 ATF_REQUIRE(atf_error_is(err, "test_data_error"));
78 ATF_REQUIRE(!atf_error_is(err, "unknown_error"));
79 ATF_REQUIRE(atf_error_data(err) != NULL);
80 ATF_REQUIRE_EQ(*((const int *)atf_error_data(err)), 5);
81 atf_error_free(err);
84 ATF_TC(error_new_wo_memory);
85 ATF_TC_HEAD(error_new_wo_memory, tc)
87 atf_tc_set_md_var(tc, "descr", "Checks that an unavailable memory error "
88 "raised when constructing an error object "
89 "is properly converted to the no_memory "
90 "static error type");
92 ATF_TC_BODY(error_new_wo_memory, tc)
94 atf_error_t err;
95 void *invalid;
97 invalid = (void *)1;
99 err = atf_error_new("test_error", invalid, SIZE_MAX, NULL);
100 ATF_REQUIRE(atf_error_is(err, "no_memory"));
101 ATF_REQUIRE(atf_error_data(err) == NULL);
102 atf_error_free(err);
105 ATF_TC(no_error);
106 ATF_TC_HEAD(no_error, tc)
108 atf_tc_set_md_var(tc, "descr", "Checks that constructing a non-error "
109 "object works");
111 ATF_TC_BODY(no_error, tc)
113 atf_error_t err;
115 err = atf_no_error();
116 ATF_REQUIRE(!atf_is_error(err));
119 ATF_TC(is_error);
120 ATF_TC_HEAD(is_error, tc)
122 atf_tc_set_md_var(tc, "descr", "Checks the is_error method to determine "
123 "if an error object holds success or an error");
125 ATF_TC_BODY(is_error, tc)
127 atf_error_t err;
129 err = atf_no_error();
130 ATF_REQUIRE(!atf_is_error(err));
132 err = atf_error_new("test_error", NULL, 0, NULL);
133 ATF_REQUIRE(atf_is_error(err));
134 atf_error_free(err);
137 ATF_TC(format);
138 ATF_TC_HEAD(format, tc)
140 atf_tc_set_md_var(tc, "descr", "Checks the default formatting function "
141 "and the ability to change it");
143 ATF_TC_BODY(format, tc)
145 atf_error_t err;
146 char buf[1024];
148 printf("Testing default formatting function\n");
149 err = atf_error_new("test_error", NULL, 0, NULL);
150 atf_error_format(err, buf, sizeof(buf));
151 printf("Error string is: %s\n", buf);
152 ATF_REQUIRE(strcmp(buf, "Error 'test_error'") == 0);
153 atf_error_free(err);
155 printf("Testing custom formatting function\n");
156 err = atf_error_new("test_error", NULL, 0, test_format);
157 atf_error_format(err, buf, sizeof(buf));
158 printf("Error string is: %s\n", buf);
159 ATF_REQUIRE(strcmp(buf, "Test formatting function") == 0);
160 atf_error_free(err);
163 /* ---------------------------------------------------------------------
164 * Tests for the "libc" error.
165 * --------------------------------------------------------------------- */
167 ATF_TC(libc_new);
168 ATF_TC_HEAD(libc_new, tc)
170 atf_tc_set_md_var(tc, "descr", "Checks the construction of libc errors");
172 ATF_TC_BODY(libc_new, tc)
174 atf_error_t err;
176 err = atf_libc_error(ENOMEM, "Test message 1");
177 ATF_REQUIRE(atf_error_is(err, "libc"));
178 ATF_REQUIRE_EQ(atf_libc_error_code(err), ENOMEM);
179 ATF_REQUIRE(strcmp(atf_libc_error_msg(err), "Test message 1") == 0);
180 atf_error_free(err);
182 err = atf_libc_error(EPERM, "%s message %d", "Test", 2);
183 ATF_REQUIRE(atf_error_is(err, "libc"));
184 ATF_REQUIRE_EQ(atf_libc_error_code(err), EPERM);
185 ATF_REQUIRE(strcmp(atf_libc_error_msg(err), "Test message 2") == 0);
186 atf_error_free(err);
189 ATF_TC(libc_format);
190 ATF_TC_HEAD(libc_format, tc)
192 atf_tc_set_md_var(tc, "descr", "Checks the formatting of libc errors");
194 ATF_TC_BODY(libc_format, tc)
196 atf_error_t err;
197 char buf[1024];
199 err = atf_libc_error(ENOMEM, "Test message 1");
200 atf_error_format(err, buf, sizeof(buf));
201 ATF_REQUIRE(strstr(buf, strerror(ENOMEM)) != NULL);
202 ATF_REQUIRE(strstr(buf, "Test message 1") != NULL);
203 atf_error_free(err);
205 err = atf_libc_error(EPERM, "Test message 2");
206 atf_error_format(err, buf, sizeof(buf));
207 ATF_REQUIRE(strstr(buf, strerror(EPERM)) != NULL);
208 ATF_REQUIRE(strstr(buf, "Test message 2") != NULL);
209 atf_error_free(err);
211 err = atf_libc_error(EPERM, "%s message %d", "Test", 3);
212 atf_error_format(err, buf, sizeof(buf));
213 ATF_REQUIRE(strstr(buf, strerror(EPERM)) != NULL);
214 ATF_REQUIRE(strstr(buf, "Test message 3") != NULL);
215 atf_error_free(err);
218 /* ---------------------------------------------------------------------
219 * Tests for the "no_memory" error.
220 * --------------------------------------------------------------------- */
222 ATF_TC(no_memory_new);
223 ATF_TC_HEAD(no_memory_new, tc)
225 atf_tc_set_md_var(tc, "descr", "Checks the construction of no_memory "
226 "errors");
228 ATF_TC_BODY(no_memory_new, tc)
230 atf_error_t err;
232 err = atf_no_memory_error();
233 ATF_REQUIRE(atf_error_is(err, "no_memory"));
234 ATF_REQUIRE(atf_error_data(err) == NULL);
235 atf_error_free(err);
238 ATF_TC(no_memory_format);
239 ATF_TC_HEAD(no_memory_format, tc)
241 atf_tc_set_md_var(tc, "descr", "Checks the formatting of no_memory "
242 "errors");
244 ATF_TC_BODY(no_memory_format, tc)
246 atf_error_t err;
247 char buf[1024];
249 err = atf_no_memory_error();
250 atf_error_format(err, buf, sizeof(buf));
251 ATF_REQUIRE(strcmp(buf, "Not enough memory") == 0);
252 atf_error_free(err);
255 ATF_TC(no_memory_twice);
256 ATF_TC_HEAD(no_memory_twice, tc)
258 atf_tc_set_md_var(tc, "descr", "Checks the construction of no_memory "
259 "errors multiple times, as this error is initialized "
260 "statically");
262 ATF_TC_BODY(no_memory_twice, tc)
265 atf_error_t err = atf_no_memory_error();
266 ATF_REQUIRE(atf_error_is(err, "no_memory"));
267 ATF_REQUIRE(atf_error_data(err) == NULL);
268 atf_error_free(err);
272 atf_error_t err = atf_no_memory_error();
273 ATF_REQUIRE(atf_error_is(err, "no_memory"));
274 ATF_REQUIRE(atf_error_data(err) == NULL);
275 atf_error_free(err);
279 /* ---------------------------------------------------------------------
280 * Tests cases for the header file.
281 * --------------------------------------------------------------------- */
283 HEADER_TC(include, "atf-c/error.h");
284 HEADER_TC(include_fwd, "atf-c/error_fwd.h");
286 /* ---------------------------------------------------------------------
287 * Main.
288 * --------------------------------------------------------------------- */
290 ATF_TP_ADD_TCS(tp)
292 /* Add the tests for the "atf_error" type. */
293 ATF_TP_ADD_TC(tp, error_new);
294 ATF_TP_ADD_TC(tp, error_new_wo_memory);
295 ATF_TP_ADD_TC(tp, no_error);
296 ATF_TP_ADD_TC(tp, is_error);
297 ATF_TP_ADD_TC(tp, format);
299 /* Add the tests for the "libc" error. */
300 ATF_TP_ADD_TC(tp, libc_new);
301 ATF_TP_ADD_TC(tp, libc_format);
303 /* Add the tests for the "no_memory" error. */
304 ATF_TP_ADD_TC(tp, no_memory_new);
305 ATF_TP_ADD_TC(tp, no_memory_format);
306 ATF_TP_ADD_TC(tp, no_memory_twice);
308 /* Add the test cases for the header file. */
309 ATF_TP_ADD_TC(tp, include);
310 ATF_TP_ADD_TC(tp, include_fwd);
312 return atf_no_error();