Skip to content

Commit 2dc7ae0

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_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: José Expósito <[email protected]>
1 parent 84b0fc2 commit 2dc7ae0

File tree

1 file changed

+86
-0
lines changed

1 file changed

+86
-0
lines changed

rust/kernel/kunit.rs

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,3 +88,89 @@ 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_test_suite!` macro expects a NULL-terminated list of test cases. This macro can be
98+
/// 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+
/// # Examples
124+
///
125+
/// ```ignore
126+
/// unsafe extern "C" fn test_fn(_test: *mut crate::bindings::kunit) {
127+
/// let actual = 1 + 1;
128+
/// let expected = 2;
129+
/// assert_eq!(actual, expected);
130+
/// }
131+
///
132+
/// static mut KUNIT_TEST_CASE: crate::bindings::kunit_case = crate::kunit_case!(test_name, test_fn);
133+
/// static mut KUNIT_NULL_CASE: crate::bindings::kunit_case = crate::kunit_case!();
134+
/// static mut KUNIT_TEST_CASES: &mut[crate::bindings::kunit_case] = unsafe {
135+
/// &mut[KUNIT_TEST_CASE, KUNIT_NULL_CASE]
136+
/// };
137+
/// crate::kunit_test_suite!(suite_name, KUNIT_TEST_CASES);
138+
/// ```
139+
#[macro_export]
140+
macro_rules! kunit_test_suite {
141+
($name:ident, $test_cases:ident) => {
142+
static KUNIT_TEST_SUITE_NAME: [i8; 256] = {
143+
let name_u8 = core::stringify!($name).as_bytes();
144+
let mut ret = [0; 256];
145+
146+
let mut i = 0;
147+
while i < name_u8.len() {
148+
ret[i] = name_u8[i] as i8;
149+
i += 1;
150+
}
151+
152+
ret
153+
};
154+
155+
// SAFETY: `test_cases` is valid as it should be static.
156+
static mut KUNIT_TEST_SUITE: $crate::bindings::kunit_suite =
157+
$crate::bindings::kunit_suite {
158+
name: KUNIT_TEST_SUITE_NAME,
159+
test_cases: unsafe { $test_cases.as_mut_ptr() },
160+
suite_init: None,
161+
suite_exit: None,
162+
init: None,
163+
exit: None,
164+
status_comment: [0; 256usize],
165+
debugfs: core::ptr::null_mut(),
166+
log: core::ptr::null_mut(),
167+
suite_init_err: 0,
168+
};
169+
170+
// SAFETY: `KUNIT_TEST_SUITE` is static.
171+
#[used]
172+
#[link_section = ".kunit_test_suites"]
173+
static mut KUNIT_TEST_SUITE_ENTRY: *const $crate::bindings::kunit_suite =
174+
unsafe { &KUNIT_TEST_SUITE };
175+
};
176+
}

0 commit comments

Comments
 (0)