Skip to content

Commit f16a86a

Browse files
committed
rust: kunit: add KUnit case and suite macros
Add a couple of Rust macros to allow to develop KUnit tests without relying on generated C code: - The `kunit_unsafe_test_suite!` Rust macro is similar to the `kunit_test_suite` C macro. - The `kunit_case!` Rust macro is similar to the `KUNIT_CASE` C macro. It can be used with parameters to generate a test case or without parameters to be used as delimiter in `kunit_test_suite!`. While these macros can be used on their own, a future patch will introduce another macro to create KUnit tests using a user-space like syntax. Co-developed-by: David Gow <[email protected]> Signed-off-by: David Gow <[email protected]> Signed-off-by: José Expósito <[email protected]>
1 parent 84b0fc2 commit f16a86a

File tree

1 file changed

+92
-0
lines changed

1 file changed

+92
-0
lines changed

rust/kernel/kunit.rs

+92
Original file line numberDiff line numberDiff line change
@@ -88,3 +88,95 @@ macro_rules! kunit_assert_eq {
8888
$crate::kunit_assert!($test, $left == $right);
8989
}};
9090
}
91+
92+
/// Represents an individual test case.
93+
///
94+
/// The test case should have the signature
95+
/// `unsafe extern "C" fn test_case(test: *mut crate::bindings::kunit)`.
96+
///
97+
/// The `kunit_unsafe_test_suite!` macro expects a NULL-terminated list of test cases. This macro
98+
/// can be invoked without parameters to generate the delimiter.
99+
#[macro_export]
100+
macro_rules! kunit_case {
101+
() => {
102+
$crate::bindings::kunit_case {
103+
run_case: None,
104+
name: core::ptr::null_mut(),
105+
generate_params: None,
106+
status: $crate::bindings::kunit_status_KUNIT_SUCCESS,
107+
log: core::ptr::null_mut(),
108+
}
109+
};
110+
($name:ident, $run_case:ident) => {
111+
$crate::bindings::kunit_case {
112+
run_case: Some($run_case),
113+
name: $crate::c_str!(core::stringify!($name)).as_char_ptr(),
114+
generate_params: None,
115+
status: $crate::bindings::kunit_status_KUNIT_SUCCESS,
116+
log: core::ptr::null_mut(),
117+
}
118+
};
119+
}
120+
121+
/// Registers a KUnit test suite.
122+
///
123+
/// # Safety
124+
///
125+
/// `test_cases` must be a NULL terminated array of test cases.
126+
///
127+
/// # Examples
128+
///
129+
/// ```ignore
130+
/// unsafe extern "C" fn test_fn(_test: *mut crate::bindings::kunit) {
131+
/// let actual = 1 + 1;
132+
/// let expected = 2;
133+
/// assert_eq!(actual, expected);
134+
/// }
135+
///
136+
/// static mut KUNIT_TEST_CASE: crate::bindings::kunit_case = crate::kunit_case!(name, test_fn);
137+
/// static mut KUNIT_NULL_CASE: crate::bindings::kunit_case = crate::kunit_case!();
138+
/// static mut KUNIT_TEST_CASES: &mut[crate::bindings::kunit_case] = unsafe {
139+
/// &mut[KUNIT_TEST_CASE, KUNIT_NULL_CASE]
140+
/// };
141+
/// crate::kunit_unsafe_test_suite!(suite_name, KUNIT_TEST_CASES);
142+
/// ```
143+
#[macro_export]
144+
macro_rules! kunit_unsafe_test_suite {
145+
($name:ident, $test_cases:ident) => {
146+
const _: () = {
147+
static KUNIT_TEST_SUITE_NAME: [i8; 256] = {
148+
let name_u8 = core::stringify!($name).as_bytes();
149+
let mut ret = [0; 256];
150+
151+
let mut i = 0;
152+
while i < name_u8.len() {
153+
ret[i] = name_u8[i] as i8;
154+
i += 1;
155+
}
156+
157+
ret
158+
};
159+
160+
// SAFETY: `test_cases` is valid as it should be static.
161+
static mut KUNIT_TEST_SUITE: $crate::bindings::kunit_suite =
162+
$crate::bindings::kunit_suite {
163+
name: KUNIT_TEST_SUITE_NAME,
164+
test_cases: unsafe { $test_cases.as_mut_ptr() },
165+
suite_init: None,
166+
suite_exit: None,
167+
init: None,
168+
exit: None,
169+
status_comment: [0; 256usize],
170+
debugfs: core::ptr::null_mut(),
171+
log: core::ptr::null_mut(),
172+
suite_init_err: 0,
173+
};
174+
175+
// SAFETY: `KUNIT_TEST_SUITE` is static.
176+
#[used]
177+
#[link_section = ".kunit_test_suites"]
178+
static mut KUNIT_TEST_SUITE_ENTRY: *const $crate::bindings::kunit_suite =
179+
unsafe { &KUNIT_TEST_SUITE };
180+
};
181+
};
182+
}

0 commit comments

Comments
 (0)