Skip to content

Commit 15f8a16

Browse files
Use test helper macros for mtopi CSR field testing
Replace manual assertions with test_ro_csr_field! macro that follows the same pattern as test_csr_field! but adapted for read-only CSRs.
1 parent b2f88f8 commit 15f8a16

File tree

1 file changed

+26
-12
lines changed

1 file changed

+26
-12
lines changed

riscv/src/register/mtopi.rs

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -76,30 +76,44 @@ impl Mtopi {
7676
mod tests {
7777
use super::*;
7878

79+
macro_rules! test_ro_csr_field {
80+
// test a multi-bit bitfield for read-only CSR
81+
($reg:ident, $field:ident: [$start:expr, $end:expr]) => {{
82+
let bits = $reg.bits();
83+
let shift = $end - $start + 1;
84+
let mask = (1usize << shift) - 1;
85+
let exp_val = (bits >> $start) & mask;
86+
87+
// Test that field extraction matches expected value
88+
assert_eq!($reg.$field(), exp_val);
89+
}};
90+
}
91+
7992
#[test]
8093
fn test_mtopi_fields() {
81-
let mtopi = Mtopi::from_bits(0);
94+
let mut mtopi = Mtopi::from_bits(0);
95+
96+
// Test iid field [16:27] with zero bits
97+
test_ro_csr_field!(mtopi, iid: [16, 27]);
98+
// Test ipid field [0:7] with zero bits
99+
test_ro_csr_field!(mtopi, ipid: [0, 7]);
82100

83-
assert_eq!(mtopi.iid(), 0);
84-
assert_eq!(mtopi.ipid(), 0);
85101
assert!(!mtopi.has_interrupt());
86102
assert_eq!(mtopi.priority(), 0);
87103
assert_eq!(mtopi.interrupt_id(), 0);
88104

89105
// Test with some interrupt pending (IID = 11, IPID = 5)
90-
let mtopi = Mtopi::from_bits((11 << 16) | 5);
91-
92-
assert_eq!(mtopi.iid(), 11);
93-
assert_eq!(mtopi.ipid(), 5);
106+
mtopi = Mtopi::from_bits((11 << 16) | 5);
107+
test_ro_csr_field!(mtopi, iid: [16, 27]);
108+
test_ro_csr_field!(mtopi, ipid: [0, 7]);
94109
assert!(mtopi.has_interrupt());
95110
assert_eq!(mtopi.priority(), 5);
96111
assert_eq!(mtopi.interrupt_id(), 11);
97112

98-
// Test maximum values
99-
let mtopi = Mtopi::from_bits((0xFFF << 16) | 0xFF);
100-
101-
assert_eq!(mtopi.iid(), 0xFFF);
102-
assert_eq!(mtopi.ipid(), 0xFF);
113+
// Test maximum values for each field
114+
mtopi = Mtopi::from_bits((0xFFF << 16) | 0xFF);
115+
test_ro_csr_field!(mtopi, iid: [16, 27]);
116+
test_ro_csr_field!(mtopi, ipid: [0, 7]);
103117
assert!(mtopi.has_interrupt());
104118
}
105119

0 commit comments

Comments
 (0)