Skip to content

Commit

Permalink
riscv: add basic stvec unit tests
Browse files Browse the repository at this point in the history
Adds basic unit tests for the `stvec` CSR.
  • Loading branch information
rmsyn committed Feb 12, 2025
1 parent 0856abb commit 2ff31df
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions riscv/src/register/stvec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,44 @@ impl Stvec {
}
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_stvec() {
let mut stvec = Stvec::from_bits(0);

[TrapMode::Direct, TrapMode::Vectored]
.into_iter()
.for_each(|trap_mode| {
test_csr_field!(stvec, trap_mode: trap_mode);
});

(1..=usize::BITS)
.map(|r| (((1u128 << r) - 1) as usize) & !TRAP_MASK)
.for_each(|address| {
stvec.set_address(address);
assert_eq!(stvec.address(), address);

assert_eq!(stvec.try_set_address(address), Ok(()));
assert_eq!(stvec.address(), address);
});

(1..=usize::BITS)
.filter_map(|r| match ((1u128 << r) - 1) as usize {
addr if (addr & TRAP_MASK) != 0 => Some(addr),
_ => None,
})
.for_each(|address| {
assert_eq!(
stvec.try_set_address(address),
Err(Error::InvalidFieldVariant {
field: "stvec::address",
value: address,
})
);
});
}
}

0 comments on commit 2ff31df

Please sign in to comment.