Skip to content

Commit 1d154dd

Browse files
committed
Simplify tests
1 parent 589376f commit 1d154dd

File tree

8 files changed

+44
-105
lines changed

8 files changed

+44
-105
lines changed

riscv-peripheral/src/aclint.rs

+1-22
Original file line numberDiff line numberDiff line change
@@ -92,28 +92,7 @@ impl<C: Clint> CLINT<C> {
9292

9393
#[cfg(test)]
9494
pub(crate) mod test {
95-
use riscv_pac::{result::Error, HartIdNumber};
96-
97-
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
98-
#[riscv::pac_enum(unsafe HartIdNumber)]
99-
pub(super) enum HartId {
100-
H0 = 0,
101-
H1 = 1,
102-
H2 = 2,
103-
}
104-
105-
#[test]
106-
fn check_hart_id_enum() {
107-
assert_eq!(HartId::H0.number(), 0);
108-
assert_eq!(HartId::H1.number(), 1);
109-
assert_eq!(HartId::H2.number(), 2);
110-
111-
assert_eq!(HartId::from_number(0), Ok(HartId::H0));
112-
assert_eq!(HartId::from_number(1), Ok(HartId::H1));
113-
assert_eq!(HartId::from_number(2), Ok(HartId::H2));
114-
115-
assert_eq!(HartId::from_number(3), Err(Error::InvalidVariant(3)));
116-
}
95+
use crate::test::HartId;
11796

11897
#[allow(dead_code)]
11998
#[test]

riscv-peripheral/src/lib.rs

+29
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,32 @@ pub mod macros; // macros for easing the definition of peripherals in PACs
1212

1313
pub mod aclint; // ACLINT and CLINT peripherals
1414
pub mod plic; // PLIC peripheral
15+
16+
#[cfg(test)]
17+
mod test {
18+
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
19+
#[riscv::pac_enum(unsafe ExternalInterruptNumber)]
20+
pub(crate) enum Interrupt {
21+
I1 = 1,
22+
I2 = 2,
23+
I3 = 3,
24+
I4 = 4,
25+
}
26+
27+
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
28+
#[riscv::pac_enum(unsafe PriorityNumber)]
29+
pub(crate) enum Priority {
30+
P0 = 0,
31+
P1 = 1,
32+
P2 = 2,
33+
P3 = 3,
34+
}
35+
36+
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
37+
#[riscv::pac_enum(unsafe HartIdNumber)]
38+
pub(crate) enum HartId {
39+
H0 = 0,
40+
H1 = 1,
41+
H2 = 2,
42+
}
43+
}

riscv-peripheral/src/plic.rs

+9-78
Original file line numberDiff line numberDiff line change
@@ -186,84 +186,15 @@ impl<P: Plic> CTX<P> {
186186

187187
#[cfg(test)]
188188
pub(crate) mod test {
189-
use riscv_pac::{result::Error, HartIdNumber, InterruptNumber, PriorityNumber};
190-
191-
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
192-
#[riscv::pac_enum(unsafe ExternalInterruptNumber)]
193-
pub(crate) enum Interrupt {
194-
I1 = 1,
195-
I2 = 2,
196-
I3 = 3,
197-
I4 = 4,
198-
}
199-
200-
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
201-
#[riscv::pac_enum(unsafe PriorityNumber)]
202-
pub(crate) enum Priority {
203-
P0 = 0,
204-
P1 = 1,
205-
P2 = 2,
206-
P3 = 3,
207-
}
208-
209-
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
210-
#[riscv::pac_enum(unsafe HartIdNumber)]
211-
pub(crate) enum Context {
212-
C0 = 0,
213-
C1 = 1,
214-
C2 = 2,
215-
}
216-
217-
#[test]
218-
fn check_interrupt_enum() {
219-
assert_eq!(Interrupt::I1.number(), 1);
220-
assert_eq!(Interrupt::I2.number(), 2);
221-
assert_eq!(Interrupt::I3.number(), 3);
222-
assert_eq!(Interrupt::I4.number(), 4);
223-
224-
assert_eq!(Interrupt::from_number(1), Ok(Interrupt::I1));
225-
assert_eq!(Interrupt::from_number(2), Ok(Interrupt::I2));
226-
assert_eq!(Interrupt::from_number(3), Ok(Interrupt::I3));
227-
assert_eq!(Interrupt::from_number(4), Ok(Interrupt::I4));
228-
229-
assert_eq!(Interrupt::from_number(0), Err(Error::InvalidVariant(0)),);
230-
assert_eq!(Interrupt::from_number(5), Err(Error::InvalidVariant(5)),);
231-
}
232-
233-
#[test]
234-
fn check_priority_enum() {
235-
assert_eq!(Priority::P0.number(), 0);
236-
assert_eq!(Priority::P1.number(), 1);
237-
assert_eq!(Priority::P2.number(), 2);
238-
assert_eq!(Priority::P3.number(), 3);
239-
240-
assert_eq!(Priority::from_number(0), Ok(Priority::P0));
241-
assert_eq!(Priority::from_number(1), Ok(Priority::P1));
242-
assert_eq!(Priority::from_number(2), Ok(Priority::P2));
243-
assert_eq!(Priority::from_number(3), Ok(Priority::P3));
244-
245-
assert_eq!(Priority::from_number(4), Err(Error::InvalidVariant(4)),);
246-
}
247-
248-
#[test]
249-
fn check_context_enum() {
250-
assert_eq!(Context::C0.number(), 0);
251-
assert_eq!(Context::C1.number(), 1);
252-
assert_eq!(Context::C2.number(), 2);
253-
254-
assert_eq!(Context::from_number(0), Ok(Context::C0));
255-
assert_eq!(Context::from_number(1), Ok(Context::C1));
256-
assert_eq!(Context::from_number(2), Ok(Context::C2));
257-
258-
assert_eq!(Context::from_number(3), Err(Error::InvalidVariant(3)),);
259-
}
189+
use crate::test::HartId;
190+
use riscv_pac::HartIdNumber;
260191

261192
#[allow(dead_code)]
262193
#[test]
263194
fn check_plic() {
264195
crate::plic_codegen!(
265196
base 0x0C00_0000,
266-
harts [Context::C0 => 0, Context::C1 => 1, Context::C2 => 2],
197+
harts [HartId::H0 => 0, HartId::H1 => 1, HartId::H2 => 2],
267198
);
268199

269200
let plic = PLIC::new();
@@ -273,10 +204,10 @@ pub(crate) mod test {
273204
assert_eq!(priorities.address(), 0x0C00_0000);
274205
assert_eq!(pendings.address(), 0x0C00_1000);
275206

276-
for i in 0..=Context::MAX_HART_ID_NUMBER {
277-
let context = Context::from_number(i).unwrap();
207+
for i in 0..=HartId::MAX_HART_ID_NUMBER {
208+
let hart_id = HartId::from_number(i).unwrap();
278209

279-
let ctx = plic.ctx(context);
210+
let ctx = plic.ctx(hart_id);
280211

281212
assert_eq!(ctx.enables().address(), 0x0C00_0000 + 0x2000 + i * 0x80);
282213
assert_eq!(
@@ -289,8 +220,8 @@ pub(crate) mod test {
289220
);
290221
}
291222

292-
assert_eq!(plic.ctx0(), plic.ctx(Context::C0));
293-
assert_eq!(plic.ctx1(), plic.ctx(Context::C1));
294-
assert_eq!(plic.ctx2(), plic.ctx(Context::C2));
223+
assert_eq!(plic.ctx0(), plic.ctx(HartId::H0));
224+
assert_eq!(plic.ctx1(), plic.ctx(HartId::H1));
225+
assert_eq!(plic.ctx2(), plic.ctx(HartId::H2));
295226
}
296227
}

riscv-peripheral/src/plic/claim.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ impl CLAIM {
3030

3131
#[cfg(test)]
3232
mod test {
33-
use super::super::test::Interrupt;
3433
use super::*;
34+
use crate::test::Interrupt;
3535
use riscv_pac::InterruptNumber;
3636

3737
#[test]

riscv-peripheral/src/plic/enables.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,8 @@ impl ENABLES {
145145

146146
#[cfg(test)]
147147
mod test {
148-
use super::super::test::Interrupt;
149148
use super::*;
149+
use crate::test::Interrupt;
150150

151151
#[test]
152152
fn test_enables() {

riscv-peripheral/src/plic/pendings.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ impl PENDINGS {
4040

4141
#[cfg(test)]
4242
mod test {
43-
use super::super::test::Interrupt;
4443
use super::*;
44+
use crate::test::Interrupt;
4545

4646
#[test]
4747
fn test_pendings() {

riscv-peripheral/src/plic/priorities.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ impl PRIORITIES {
6969

7070
#[cfg(test)]
7171
mod test {
72-
use super::super::test::{Interrupt, Priority};
7372
use super::*;
73+
use crate::test::{Interrupt, Priority};
7474
use riscv_pac::InterruptNumber;
7575

7676
#[test]

riscv-peripheral/src/plic/threshold.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ impl THRESHOLD {
3535

3636
#[cfg(test)]
3737
mod test {
38-
use super::super::test::Priority;
3938
use super::*;
39+
use crate::test::Priority;
4040

4141
#[test]
4242
fn test_threshold() {

0 commit comments

Comments
 (0)