Skip to content

Commit 9279f48

Browse files
Add expected value checking to test_csr_field macro for read-only CSR fields
1 parent b28ca8f commit 9279f48

File tree

2 files changed

+20
-26
lines changed

2 files changed

+20
-26
lines changed

riscv/src/register/macros.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1048,6 +1048,11 @@ macro_rules! test_csr_field {
10481048
assert_eq!($reg.$field(), exp_val);
10491049
}};
10501050

1051+
// test a multi-bit bitfield for read-only CSR with expected value
1052+
($reg:ident, $field:ident: [$start:expr, $end:expr], $expected:expr) => {{
1053+
assert_eq!($reg.$field(), $expected);
1054+
}};
1055+
10511056
// test an enum bit field
10521057
($reg:ident, $field:ident: $var:expr) => {{
10531058
$crate::paste! {

riscv/src/register/mtopi.rs

Lines changed: 15 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -61,37 +61,26 @@ mod tests {
6161

6262
#[test]
6363
fn test_mtopi_fields() {
64-
// Test using helper macros as requested - follows mcounteren.rs pattern
65-
let mut mtopi = Mtopi::from_bits(0);
64+
let mtopi = Mtopi::from_bits(0);
6665

67-
// Test iid field [16:27] - using test helper macro
68-
test_csr_field!(mtopi, iid: [16, 27]);
69-
// Test ipid field [0:7] - using test helper macro
70-
test_csr_field!(mtopi, ipid: [0, 7]);
66+
test_csr_field!(mtopi, iid: [16, 27], 0x0);
67+
test_csr_field!(mtopi, iprio: [0, 7], 0x0);
7168

72-
// Test helper methods
73-
assert!(!mtopi.has_interrupt());
69+
let mtopi = Mtopi::from_bits((11 << 16) | 5);
70+
test_csr_field!(mtopi, iid: [16, 27], 0xB);
71+
test_csr_field!(mtopi, iprio: [0, 7], 0x5);
7472

75-
// Test with some interrupt pending (IID = 11, IPID = 5)
76-
mtopi = Mtopi::from_bits((11 << 16) | 5);
77-
test_csr_field!(mtopi, iid: [16, 27]);
78-
test_csr_field!(mtopi, ipid: [0, 7]);
79-
assert!(mtopi.has_interrupt());
73+
let mtopi = Mtopi::from_bits((0xFFF << 16) | 0xFF);
74+
test_csr_field!(mtopi, iid: [16, 27], 0xFFF);
75+
test_csr_field!(mtopi, iprio: [0, 7], 0xFF);
8076

81-
// Test maximum values for each field
82-
mtopi = Mtopi::from_bits((0xFFF << 16) | 0xFF);
83-
test_csr_field!(mtopi, iid: [16, 27]);
84-
test_csr_field!(mtopi, ipid: [0, 7]);
85-
assert!(mtopi.has_interrupt());
77+
let mtopi = Mtopi::from_bits(1 << 16);
78+
test_csr_field!(mtopi, iid: [16, 27], 0x1);
79+
test_csr_field!(mtopi, iprio: [0, 7], 0x0);
8680

87-
// Test field boundaries
88-
mtopi = Mtopi::from_bits(1 << 16);
89-
test_csr_field!(mtopi, iid: [16, 27]);
90-
test_csr_field!(mtopi, ipid: [0, 7]);
91-
92-
mtopi = Mtopi::from_bits(1);
93-
test_csr_field!(mtopi, iid: [16, 27]);
94-
test_csr_field!(mtopi, ipid: [0, 7]);
81+
let mtopi = Mtopi::from_bits(1);
82+
test_csr_field!(mtopi, iid: [16, 27], 0x0);
83+
test_csr_field!(mtopi, iprio: [0, 7], 0x1);
9584
}
9685

9786
#[test]

0 commit comments

Comments
 (0)