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