xref: /xnu-11215.41.3/osfmk/tests/ktest.c (revision 33de042d024d46de5ff4e89f2471de6608e37fa4)
1 /*
2  * Copyright (c) 2015 Apple Inc. All rights reserved.
3  *
4  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5  *
6  * This file contains Original Code and/or Modifications of Original Code
7  * as defined in and that are subject to the Apple Public Source License
8  * Version 2.0 (the 'License'). You may not use this file except in
9  * compliance with the License. The rights granted to you under the License
10  * may not be used to create, or enable the creation or redistribution of,
11  * unlawful or unlicensed copies of an Apple operating system, or to
12  * circumvent, violate, or enable the circumvention or violation of, any
13  * terms of an Apple operating system software license agreement.
14  *
15  * Please obtain a copy of the License at
16  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17  *
18  * The Original Code and all software distributed under the License are
19  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23  * Please see the License for the specific language governing rights and
24  * limitations under the License.
25  *
26  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27  */
28 
29 #include <tests/ktest.h>
30 #include <tests/ktest_internal.h>
31 #include <mach/mach_time.h>
32 #include <kern/misc_protos.h>
33 
34 void
ktest_start(void)35 ktest_start(void)
36 {
37 	ktest_emit_start();
38 }
39 
40 void
ktest_finish(void)41 ktest_finish(void)
42 {
43 	ktest_emit_finish();
44 }
45 
46 void
ktest_testbegin(const char * test_name)47 ktest_testbegin(const char * test_name)
48 {
49 	ktest_current_time = mach_absolute_time();
50 	ktest_test_name = test_name;
51 	ktest_emit_testbegin(test_name);
52 }
53 
54 void
ktest_testend()55 ktest_testend()
56 {
57 	ktest_current_time = mach_absolute_time();
58 	ktest_emit_testend();
59 	ktest_test_index++;
60 }
61 
62 void
ktest_testskip(const char * msg,...)63 ktest_testskip(const char * msg, ...)
64 {
65 	va_list args;
66 
67 	ktest_current_time = mach_absolute_time();
68 
69 	va_start(args, msg);
70 	ktest_emit_testskip(msg, args);
71 	va_end(args);
72 }
73 
74 void
ktest_log(const char * msg,...)75 ktest_log(const char * msg, ...)
76 {
77 	va_list args;
78 
79 	ktest_current_time = mach_absolute_time();
80 
81 	va_start(args, msg);
82 	ktest_emit_log(msg, args);
83 	va_end(args);
84 }
85 
86 void
ktest_perf(const char * metric,const char * unit,double value,const char * desc)87 ktest_perf(const char * metric, const char * unit, double value, const char * desc)
88 {
89 	ktest_current_time = mach_absolute_time();
90 	ktest_emit_perfdata(metric, unit, value, desc);
91 }
92 
93 void
ktest_testcase(int success)94 ktest_testcase(int success)
95 {
96 	ktest_current_time = mach_absolute_time();
97 
98 	if (success && !ktest_expectfail) {
99 		/* PASS */
100 		ktest_passcount++;
101 		ktest_testcase_result = T_RESULT_PASS;
102 	} else if (!success && !ktest_expectfail) {
103 		/* FAIL */
104 		ktest_failcount++;
105 		ktest_testcase_result = T_RESULT_FAIL;
106 	} else if (success && ktest_expectfail) {
107 		/* UXPASS */
108 		ktest_xpasscount++;
109 		ktest_testcase_result = T_RESULT_UXPASS;
110 	} else if (!success && ktest_expectfail) {
111 		/* XFAIL */
112 		ktest_xfailcount++;
113 		ktest_testcase_result = T_RESULT_XFAIL;
114 	}
115 
116 	ktest_update_test_result_state();
117 	if (ktest_quiet == 0 ||
118 	    ktest_testcase_result == T_RESULT_FAIL ||
119 	    ktest_testcase_result == T_RESULT_UXPASS) {
120 		ktest_emit_testcase();
121 	}
122 	ktest_expression_index++;
123 
124 	ktest_quiet = 0;
125 	ktest_expectfail = 0;
126 	ktest_output_buf[0] = '\0';
127 	ktest_current_msg[0] = '\0';
128 	ktest_current_expr[0] = '\0';
129 	for (int i = 0; i < KTEST_MAXVARS; i++) {
130 		ktest_current_var_names[i][0] = '\0';
131 		ktest_current_var_values[i][0] = '\0';
132 	}
133 	ktest_current_var_index = 0;
134 }
135 
136 void
ktest_update_test_result_state(void)137 ktest_update_test_result_state(void)
138 {
139 	ktest_test_result = ktest_test_result_statetab[ktest_test_result]
140 	    [ktest_testcase_result]
141 	    [ktest_testcase_mode];
142 }
143 
144 void
ktest_assertion_check(void)145 ktest_assertion_check(void)
146 {
147 	if (ktest_testcase_result == T_RESULT_FAIL || ktest_testcase_result == T_RESULT_UXPASS) {
148 		ktest_testend();
149 		panic("XNUPOST: Assertion failed : %s : at %s:%d", ktest_test_name, ktest_current_file, ktest_current_line);
150 	}
151 }
152