xref: /linux/drivers/gpu/nova-core/selftest.rs (revision cb8fef4b07f71da01843457b66e579ad248a625e)
1 // SPDX-License-Identifier: GPL-2.0
2 // SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
3 
4 //! Assertion macros for driver self-tests.
5 //!
6 //! Self-tests run against live hardware during probe, so a failed assertion should not panic. These
7 //! macros log the failure on the device and fail the enclosing test by returning
8 //! [`EIO`](kernel::error::code::EIO) instead.
9 
10 /// Like [`assert!`], but logs the failure via `dev` and fails the enclosing test instead of
11 /// panicking.
12 ///
13 /// As with [`assert!`], a custom message with format arguments can follow the condition.
14 #[macro_export]
15 macro_rules! selftest_assert {
16     ($dev:expr, $cond:expr $(,)?) => {
17         $crate::selftest_assert!($dev, $cond, "assertion failed: {}", ::core::stringify!($cond))
18     };
19     ($dev:expr, $cond:expr, $($arg:tt)+) => {{
20         if !$cond {
21             ::kernel::dev_err!(
22                 $dev,
23                 "Selftest: {}:{}: {}\n",
24                 ::core::file!(),
25                 ::core::line!(),
26                 ::kernel::prelude::fmt!($($arg)+)
27             );
28             return Err(::kernel::error::code::EIO);
29         }
30     }};
31 }
32 
33 /// Like [`assert_eq!`], but logs the failure via `dev` and fails the enclosing test instead of
34 /// panicking.
35 ///
36 /// As with [`assert_eq!`], a custom message with format arguments can follow the compared values.
37 #[macro_export]
38 macro_rules! selftest_assert_eq {
39     ($dev:expr, $left:expr, $right:expr $(,)?) => {
40         match (&$left, &$right) {
41             (left, right) => $crate::selftest_assert!(
42                 $dev,
43                 left == right,
44                 "assertion `{} == {}` failed: left {:?}, right {:?}",
45                 ::core::stringify!($left),
46                 ::core::stringify!($right),
47                 left,
48                 right
49             ),
50         }
51     };
52     ($dev:expr, $left:expr, $right:expr, $($arg:tt)+) => {
53         match (&$left, &$right) {
54             (left, right) => $crate::selftest_assert!(
55                 $dev,
56                 left == right,
57                 "assertion `left == right` failed: {}: left {:?}, right {:?}",
58                 ::kernel::prelude::fmt!($($arg)+),
59                 left,
60                 right
61             ),
62         }
63     };
64 }
65