xref: /freebsd/tools/regression/posixsem/test.c (revision 783d3ff6d7fae619db8a7990b8a6387de0c677b5)
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 #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