]> git.saurik.com Git - apple/xnu.git/blob - osfmk/tests/ktest.c
xnu-4903.231.4.tar.gz
[apple/xnu.git] / osfmk / tests / ktest.c
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
35 ktest_start(void) {
36 ktest_emit_start();
37 }
38
39 void
40 ktest_finish(void) {
41 ktest_emit_finish();
42 }
43
44 void
45 ktest_testbegin(const char * test_name) {
46 ktest_current_time = mach_absolute_time();
47 ktest_test_name = test_name;
48 ktest_emit_testbegin(test_name);
49 }
50
51 void
52 ktest_testend() {
53 ktest_current_time = mach_absolute_time();
54 ktest_emit_testend();
55 ktest_test_index++;
56 }
57
58 void
59 ktest_testskip(const char * msg, ...) {
60 va_list args;
61
62 ktest_current_time = mach_absolute_time();
63
64 va_start(args, msg);
65 ktest_emit_testskip(msg, args);
66 va_end(args);
67
68 }
69
70 void
71 ktest_log(const char * msg, ...) {
72 va_list args;
73
74 ktest_current_time = mach_absolute_time();
75
76 va_start(args, msg);
77 ktest_emit_log(msg, args);
78 va_end(args);
79 }
80
81 void
82 ktest_perf(const char * metric, const char * unit, double value, const char * desc)
83 {
84 ktest_current_time = mach_absolute_time();
85 ktest_emit_perfdata(metric, unit, value, desc);
86 }
87
88 void
89 ktest_testcase(int success)
90 {
91 ktest_current_time = mach_absolute_time();
92
93 if(success && !ktest_expectfail) {
94 /* PASS */
95 ktest_passcount++;
96 ktest_testcase_result = T_RESULT_PASS;
97 } else if(!success && !ktest_expectfail) {
98 /* FAIL */
99 ktest_failcount++;
100 ktest_testcase_result = T_RESULT_FAIL;
101 } else if(success && ktest_expectfail) {
102 /* UXPASS */
103 ktest_xpasscount++;
104 ktest_testcase_result = T_RESULT_UXPASS;
105 } else if(!success && ktest_expectfail) {
106 /* XFAIL */
107 ktest_xfailcount++;
108 ktest_testcase_result = T_RESULT_XFAIL;
109 }
110
111 ktest_update_test_result_state();
112 if(ktest_quiet == 0 ||
113 ktest_testcase_result == T_RESULT_FAIL ||
114 ktest_testcase_result == T_RESULT_UXPASS) {
115 ktest_emit_testcase();
116 }
117 ktest_expression_index++;
118
119 ktest_quiet = 0;
120 ktest_expectfail = 0;
121 ktest_output_buf[0] = '\0';
122 ktest_current_msg[0] = '\0';
123 ktest_current_expr[0] = '\0';
124 for(int i = 0; i < KTEST_MAXVARS; i++) {
125 ktest_current_var_names[i][0] = '\0';
126 ktest_current_var_values[i][0] = '\0';
127 }
128 ktest_current_var_index = 0;
129 }
130
131 void
132 ktest_update_test_result_state(void) {
133 ktest_test_result = ktest_test_result_statetab[ktest_test_result]
134 [ktest_testcase_result]
135 [ktest_testcase_mode];
136 }
137
138 void
139 ktest_assertion_check(void) {
140 if (ktest_testcase_result == T_RESULT_FAIL || ktest_testcase_result == T_RESULT_UXPASS) {
141 ktest_testend();
142 panic("XNUPOST: Assertion failed : %s : at %s:%d", ktest_test_name, ktest_current_file, ktest_current_line);
143 }
144 }