Skip to content

Commit 8e10f74

Browse files
author
lif
committed
move test_harness to mod
1 parent 9fafc14 commit 8e10f74

File tree

2 files changed

+113
-118
lines changed

2 files changed

+113
-118
lines changed

src/lib.rs

Lines changed: 4 additions & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,9 @@ use prelude::{GbaCell, IrqFn};
9898

9999
mod macros;
100100

101+
#[cfg(test)]
102+
mod test_harness;
103+
101104
#[cfg(feature = "on_gba")]
102105
mod asm_runtime;
103106
#[cfg(feature = "on_gba")]
@@ -183,121 +186,6 @@ macro_rules! include_aligned_bytes {
183186
}};
184187
}
185188

186-
#[cfg(test)]
187-
mod test_harness {
188-
use crate::prelude::*;
189-
use crate::{bios, mem, mgba};
190-
use core::fmt::Write;
191-
192-
#[panic_handler]
193-
fn panic(info: &core::panic::PanicInfo) -> ! {
194-
DISPSTAT.write(DisplayStatus::new().with_irq_vblank(true));
195-
BG_PALETTE.index(0).write(Color::from_rgb(25, 10, 5));
196-
IE.write(IrqBits::VBLANK);
197-
IME.write(true);
198-
VBlankIntrWait();
199-
VBlankIntrWait();
200-
VBlankIntrWait();
201-
202-
// the Fatal one kills emulation after one line / 256 bytes
203-
// so emit all the information as Error first
204-
if let Ok(mut log) =
205-
mgba::MgbaBufferedLogger::try_new(mgba::MgbaMessageLevel::Error)
206-
{
207-
writeln!(log, "[failed]").ok();
208-
write!(log, "{}", info).ok();
209-
}
210-
211-
if let Ok(mut log) =
212-
mgba::MgbaBufferedLogger::try_new(mgba::MgbaMessageLevel::Fatal)
213-
{
214-
if let Some(loc) = info.location() {
215-
write!(log, "panic at {loc}! see mgba error log for details.").ok();
216-
} else {
217-
write!(log, "panic! see mgba error log for details.").ok();
218-
}
219-
}
220-
221-
IE.write(IrqBits::new());
222-
bios::IntrWait(true, IrqBits::new());
223-
loop {}
224-
}
225-
226-
pub(crate) trait UnitTest {
227-
fn run(&self);
228-
}
229-
230-
impl<T: Fn()> UnitTest for T {
231-
fn run(&self) {
232-
if let Ok(mut log) =
233-
mgba::MgbaBufferedLogger::try_new(mgba::MgbaMessageLevel::Info)
234-
{
235-
write!(log, "{}...", core::any::type_name::<T>()).ok();
236-
}
237-
238-
self();
239-
240-
if let Ok(mut log) =
241-
mgba::MgbaBufferedLogger::try_new(mgba::MgbaMessageLevel::Info)
242-
{
243-
writeln!(log, "[ok]").ok();
244-
}
245-
}
246-
}
247-
248-
pub(crate) fn test_runner(tests: &[&dyn UnitTest]) {
249-
if let Ok(mut log) =
250-
mgba::MgbaBufferedLogger::try_new(mgba::MgbaMessageLevel::Info)
251-
{
252-
write!(log, "Running {} tests", tests.len()).ok();
253-
}
254-
255-
for test in tests {
256-
test.run();
257-
}
258-
if let Ok(mut log) =
259-
mgba::MgbaBufferedLogger::try_new(mgba::MgbaMessageLevel::Info)
260-
{
261-
write!(log, "Tests finished successfully").ok();
262-
}
263-
}
264-
265-
#[no_mangle]
266-
extern "C" fn main() {
267-
DISPCNT.write(DisplayControl::new().with_video_mode(VideoMode::_0));
268-
BG_PALETTE.index(0).write(Color::new());
269-
270-
crate::test_main();
271-
272-
BG_PALETTE.index(0).write(Color::from_rgb(5, 15, 25));
273-
BG_PALETTE.index(1).write(Color::new());
274-
BG0CNT
275-
.write(BackgroundControl::new().with_charblock(0).with_screenblock(31));
276-
DISPCNT.write(
277-
DisplayControl::new().with_video_mode(VideoMode::_0).with_show_bg0(true),
278-
);
279-
280-
// some niceties for people without mgba-test-runner
281-
let tsb = TEXT_SCREENBLOCKS.get_frame(31).unwrap();
282-
unsafe {
283-
mem::set_u32x80_unchecked(
284-
tsb.into_block::<1024>().as_mut_ptr().cast(),
285-
0,
286-
12,
287-
);
288-
}
289-
Cga8x8Thick.bitunpack_4bpp(CHARBLOCK0_4BPP.as_region(), 0);
290-
291-
let row = tsb.get_row(9).unwrap().iter().skip(6);
292-
for (addr, ch) in row.zip(b"all tests passed!") {
293-
addr.write(TextEntry::new().with_tile(*ch as u16));
294-
}
295-
296-
DISPSTAT.write(DisplayStatus::new());
297-
bios::IntrWait(true, IrqBits::new());
298-
}
299-
}
300-
301189
#[cfg(test)]
302190
mod test {
303191
use super::Align4;
@@ -309,7 +197,6 @@ mod test {
309197
assert_eq!(a.as_u32_slice(), &[0x3020100_u32.to_le()]);
310198
}
311199

312-
313200
#[test_case]
314201
fn align4_as_generic() {
315202
// with padding
@@ -335,9 +222,8 @@ mod test {
335222
.as_slice::<ThreeHalfWords>(),
336223
&[
337224
ThreeHalfWords(0x1111, 0x2222, 0x3333),
338-
ThreeHalfWords(0x4444 0x5555, 0x6666)
225+
ThreeHalfWords(0x4444, 0x5555, 0x6666)
339226
]
340227
);
341228
}
342-
343229
}

src/test_harness.rs

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
use crate::{bios, mem, mgba, prelude::*};
2+
use core::fmt::Write;
3+
4+
#[panic_handler]
5+
fn panic(info: &core::panic::PanicInfo) -> ! {
6+
DISPSTAT.write(DisplayStatus::new().with_irq_vblank(true));
7+
BG_PALETTE.index(0).write(Color::from_rgb(25, 10, 5));
8+
IE.write(IrqBits::VBLANK);
9+
IME.write(true);
10+
VBlankIntrWait();
11+
VBlankIntrWait();
12+
VBlankIntrWait();
13+
14+
// the Fatal one kills emulation after one line / 256 bytes
15+
// so emit all the information as Error first
16+
if let Ok(mut log) =
17+
mgba::MgbaBufferedLogger::try_new(mgba::MgbaMessageLevel::Error)
18+
{
19+
writeln!(log, "[failed]").ok();
20+
write!(log, "{}", info).ok();
21+
}
22+
23+
if let Ok(mut log) =
24+
mgba::MgbaBufferedLogger::try_new(mgba::MgbaMessageLevel::Fatal)
25+
{
26+
if let Some(loc) = info.location() {
27+
write!(log, "panic at {loc}! see mgba error log for details.").ok();
28+
} else {
29+
write!(log, "panic! see mgba error log for details.").ok();
30+
}
31+
}
32+
33+
IE.write(IrqBits::new());
34+
bios::IntrWait(true, IrqBits::new());
35+
loop {}
36+
}
37+
38+
pub(crate) trait UnitTest {
39+
fn run(&self);
40+
}
41+
42+
impl<T: Fn()> UnitTest for T {
43+
fn run(&self) {
44+
if let Ok(mut log) =
45+
mgba::MgbaBufferedLogger::try_new(mgba::MgbaMessageLevel::Info)
46+
{
47+
write!(log, "{}...", core::any::type_name::<T>()).ok();
48+
}
49+
50+
self();
51+
52+
if let Ok(mut log) =
53+
mgba::MgbaBufferedLogger::try_new(mgba::MgbaMessageLevel::Info)
54+
{
55+
writeln!(log, "[ok]").ok();
56+
}
57+
}
58+
}
59+
60+
pub(crate) fn test_runner(tests: &[&dyn UnitTest]) {
61+
if let Ok(mut log) =
62+
mgba::MgbaBufferedLogger::try_new(mgba::MgbaMessageLevel::Info)
63+
{
64+
write!(log, "Running {} tests", tests.len()).ok();
65+
}
66+
67+
for test in tests {
68+
test.run();
69+
}
70+
if let Ok(mut log) =
71+
mgba::MgbaBufferedLogger::try_new(mgba::MgbaMessageLevel::Info)
72+
{
73+
write!(log, "Tests finished successfully").ok();
74+
}
75+
}
76+
77+
#[no_mangle]
78+
extern "C" fn main() {
79+
DISPCNT.write(DisplayControl::new().with_video_mode(VideoMode::_0));
80+
BG_PALETTE.index(0).write(Color::new());
81+
82+
crate::test_main();
83+
84+
BG_PALETTE.index(0).write(Color::from_rgb(5, 15, 25));
85+
BG_PALETTE.index(1).write(Color::new());
86+
BG0CNT.write(BackgroundControl::new().with_charblock(0).with_screenblock(31));
87+
DISPCNT.write(
88+
DisplayControl::new().with_video_mode(VideoMode::_0).with_show_bg0(true),
89+
);
90+
91+
// some niceties for people without mgba-test-runner
92+
let tsb = TEXT_SCREENBLOCKS.get_frame(31).unwrap();
93+
unsafe {
94+
mem::set_u32x80_unchecked(
95+
tsb.into_block::<1024>().as_mut_ptr().cast(),
96+
0,
97+
12,
98+
);
99+
}
100+
Cga8x8Thick.bitunpack_4bpp(CHARBLOCK0_4BPP.as_region(), 0);
101+
102+
let row = tsb.get_row(9).unwrap().iter().skip(6);
103+
for (addr, ch) in row.zip(b"all tests passed!") {
104+
addr.write(TextEntry::new().with_tile(*ch as u16));
105+
}
106+
107+
DISPSTAT.write(DisplayStatus::new());
108+
bios::IntrWait(true, IrqBits::new());
109+
}

0 commit comments

Comments
 (0)