mirror of
https://github.com/phil-opp/blog_os.git
synced 2025-12-16 14:27:49 +00:00
Merge branch 'post-05' into post-06
This commit is contained in:
@@ -5,7 +5,7 @@ authors = ["Philipp Oppermann <dev@phil-opp.com>"]
|
|||||||
edition = "2018"
|
edition = "2018"
|
||||||
|
|
||||||
[[test]]
|
[[test]]
|
||||||
name = "panic_handler"
|
name = "should_panic"
|
||||||
harness = false
|
harness = false
|
||||||
|
|
||||||
[[test]]
|
[[test]]
|
||||||
|
|||||||
@@ -1,74 +0,0 @@
|
|||||||
#![no_std]
|
|
||||||
#![no_main]
|
|
||||||
#![feature(panic_info_message)]
|
|
||||||
|
|
||||||
use blog_os::{exit_qemu, serial_print, serial_println, QemuExitCode};
|
|
||||||
use core::{
|
|
||||||
fmt::{self, Write},
|
|
||||||
panic::PanicInfo,
|
|
||||||
};
|
|
||||||
|
|
||||||
const MESSAGE: &str = "Example panic message from panic_handler test";
|
|
||||||
const PANIC_LINE: u32 = 17; // adjust this when moving the `panic!` call
|
|
||||||
|
|
||||||
#[no_mangle]
|
|
||||||
pub extern "C" fn _start() -> ! {
|
|
||||||
serial_print!("panic_handler... ");
|
|
||||||
panic!(MESSAGE); // must be in line `PANIC_LINE`
|
|
||||||
}
|
|
||||||
|
|
||||||
#[panic_handler]
|
|
||||||
fn panic(info: &PanicInfo) -> ! {
|
|
||||||
check_location(info);
|
|
||||||
check_message(info);
|
|
||||||
|
|
||||||
serial_println!("[ok]");
|
|
||||||
exit_qemu(QemuExitCode::Success);
|
|
||||||
loop {}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn fail(error: &str) -> ! {
|
|
||||||
serial_println!("[failed]");
|
|
||||||
serial_println!("{}", error);
|
|
||||||
exit_qemu(QemuExitCode::Failed);
|
|
||||||
loop {}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn check_location(info: &PanicInfo) {
|
|
||||||
let location = info.location().unwrap_or_else(|| fail("no location"));
|
|
||||||
if location.file() != file!() {
|
|
||||||
fail("file name wrong");
|
|
||||||
}
|
|
||||||
if location.line() != PANIC_LINE {
|
|
||||||
fail("file line wrong");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn check_message(info: &PanicInfo) {
|
|
||||||
let message = info.message().unwrap_or_else(|| fail("no message"));
|
|
||||||
let mut compare_message = CompareMessage { expected: MESSAGE };
|
|
||||||
write!(&mut compare_message, "{}", message).unwrap_or_else(|_| fail("write failed"));
|
|
||||||
if compare_message.expected.len() != 0 {
|
|
||||||
fail("message shorter than expected message");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Compares a `fmt::Arguments` instance with the `MESSAGE` string
|
|
||||||
///
|
|
||||||
/// To use this type, write the `fmt::Arguments` instance to it using the
|
|
||||||
/// `write` macro. If the message component matches `MESSAGE`, the `expected`
|
|
||||||
/// field is the empty string.
|
|
||||||
struct CompareMessage {
|
|
||||||
expected: &'static str,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl fmt::Write for CompareMessage {
|
|
||||||
fn write_str(&mut self, s: &str) -> fmt::Result {
|
|
||||||
if self.expected.starts_with(s) {
|
|
||||||
self.expected = &self.expected[s.len()..];
|
|
||||||
} else {
|
|
||||||
fail("message not equal to expected message");
|
|
||||||
}
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
25
tests/should_panic.rs
Normal file
25
tests/should_panic.rs
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
#![no_std]
|
||||||
|
#![no_main]
|
||||||
|
|
||||||
|
use blog_os::{exit_qemu, serial_print, serial_println, QemuExitCode};
|
||||||
|
use core::panic::PanicInfo;
|
||||||
|
|
||||||
|
#[no_mangle]
|
||||||
|
pub extern "C" fn _start() -> ! {
|
||||||
|
should_fail();
|
||||||
|
serial_println!("[test did not panic]");
|
||||||
|
exit_qemu(QemuExitCode::Failed);
|
||||||
|
loop {}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn should_fail() {
|
||||||
|
serial_print!("should_fail... ");
|
||||||
|
assert_eq!(0, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[panic_handler]
|
||||||
|
fn panic(_info: &PanicInfo) -> ! {
|
||||||
|
serial_println!("[ok]");
|
||||||
|
exit_qemu(QemuExitCode::Success);
|
||||||
|
loop {}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user