xref: /freebsd/tools/regression/posixsem/test.c (revision 39ee7a7a6bdd1557b1c3532abf60d139798ac88b)
1 /*-
2  * Copyright (c) 2008 Yahoo!, Inc.
3  * All rights reserved.
4  * Written by: John Baldwin <jhb@FreeBSD.org>
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
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 copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. Neither the name of the author nor the names of any co-contributors
15  *    may be used to endorse or promote products derived from this software
16  *    without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  */
30 
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
33 
34 #include <stdarg.h>
35 #include <stdio.h>
36 
37 #include "test.h"
38 
39 static int test_index;
40 static struct regression_test *test;
41 static int test_acknowleged;
42 
43 SET_DECLARE(regression_tests_set, struct regression_test);
44 
45 /*
46  * Outputs a test summary of the following:
47  *
48  * <status> <test #> [name] [# <fmt> [fmt args]]
49  */
50 static void
51 vprint_status(const char *status, const char *fmt, va_list ap)
52 {
53 
54 	printf("%s %d", status, test_index);
55 	if (test->rt_name)
56 		printf(" - %s", test->rt_name);
57 	if (fmt) {
58 		printf(" # ");
59 		vprintf(fmt, ap);
60 	}
61 	printf("\n");
62 	test_acknowleged = 1;
63 }
64 
65 static void
66 print_status(const char *status, const char *fmt, ...)
67 {
68 	va_list ap;
69 
70 	va_start(ap, fmt);
71 	vprint_status(status, fmt, ap);
72 	va_end(ap);
73 }
74 
75 void
76 pass(void)
77 {
78 
79 	print_status("ok", NULL);
80 }
81 
82 void
83 fail(void)
84 {
85 
86 	print_status("not ok", NULL);
87 }
88 
89 void
90 fail_err(const char *fmt, ...)
91 {
92 	va_list ap;
93 
94 	va_start(ap, fmt);
95 	vprint_status("not ok", fmt, ap);
96 	va_end(ap);
97 }
98 
99 void
100 skip(const char *reason)
101 {
102 
103 	print_status("ok", "skip %s", reason);
104 }
105 
106 void
107 todo(const char *reason)
108 {
109 
110 	print_status("not ok", "TODO %s", reason);
111 }
112 
113 void
114 run_tests(void)
115 {
116 	struct regression_test **testp;
117 
118 	printf("1..%td\n", SET_COUNT(regression_tests_set));
119 	test_index = 1;
120 	SET_FOREACH(testp, regression_tests_set) {
121 		test_acknowleged = 0;
122 		test = *testp;
123 		test->rt_function();
124 		if (!test_acknowleged)
125 			print_status("not ok", "unknown status");
126 		test_index++;
127 	}
128 }
129