-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.rs
More file actions
91 lines (76 loc) · 2.28 KB
/
Copy pathmain.rs
File metadata and controls
91 lines (76 loc) · 2.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#![no_std]
#![no_main]
#![feature(custom_test_frameworks)]
#![test_runner(crate::test_runner)]
#![reexport_test_harness_main = "test_main"] // specify test_main to be called in test contexts
use core::panic::PanicInfo;
mod serial;
mod vga_buffer;
// TESTS
// The runner prints a short debug message and then calls each test function in the list.
// The argument type &[&dyn Fn()] is a slice of a trait object references on the Fn() trait
// basically a list of references to types that can be called like a function
#[cfg(test)] // only include function for tests
pub fn test_runner(tests: &[&dyn Testable]) {
serial_println!("Running {} tests", tests.len());
for test in tests {
test.run();
}
exit_qemu(QemuExitCode::Success);
}
#[test_case]
fn trivial_assertion() {
assert_eq!(1, 1);
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(u32)]
pub enum QemuExitCode {
Success = 0x10,
Failed = 0x11,
}
pub fn exit_qemu(exit_code: QemuExitCode) {
use x86_64::instructions::port::Port;
unsafe {
let mut port = Port::new(0xf4);
port.write(exit_code as u32);
}
}
// create a new Testable Trait
pub trait Testable {
fn run(&self) -> ();
}
// we implement the run function by first printing the function name using the any::type_name function
// this function is implemented directly in the compiler and returns a string description of every type
// for functions, the type is their name, so this is exactly what we want in this case
impl<T> Testable for T where T: Fn() {
fn run(&self) -> () {
serial_print!("{}...\t", core::any::type_name::<T>());
self();
serial_println!("[ok]");
}
}
// we mark this as a diverging function by having it return the never type
#[cfg(not(test))] // new attribute
#[panic_handler]
fn panic(info: &PanicInfo) -> ! {
println!("{}", info);
loop {}
}
// our panic handler in test mode
#[cfg(test)]
#[panic_handler]
fn panic(info: &PanicInfo) -> ! {
serial_println!("[failed]\n");
serial_println!("Error: {}\n", info);
exit_qemu(QemuExitCode::Failed);
loop {}
}
#[no_mangle]
pub extern "C" fn _start() -> ! {
println!("Hello World{}", "!");
// panic!("Some panic message"); for testing panic
// only loaded on test
#[cfg(test)]
test_main();
loop {}
}