-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
137 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
[build] | ||
target = "../x86_64-bootimage-example-kernels.json" | ||
|
||
[target.'cfg(target_os = "none")'] | ||
runner = "bootimage runner" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
/target/ | ||
**/*.rs.bk |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
[package] | ||
name = "runner-doctest" | ||
version = "0.1.0" | ||
authors = ["Philipp Oppermann <[email protected]>"] | ||
edition = "2018" | ||
|
||
[dependencies] | ||
bootloader = "0.6.4" | ||
x86_64 = "0.5.3" | ||
|
||
[package.metadata.bootimage] | ||
test-success-exit-code = 33 # (0x10 << 1) | 1 | ||
test-args = ["-device", "isa-debug-exit,iobase=0xf4,iosize=0x04", "-display", "none"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
#![no_std] | ||
#![cfg_attr(test, no_main)] | ||
#![feature(custom_test_frameworks)] | ||
#![test_runner(crate::test_runner)] | ||
#![reexport_test_harness_main = "test_main"] | ||
|
||
/// add two numbers | ||
/// | ||
/// ``` | ||
/// #![no_std] | ||
/// #![no_main] | ||
/// use runner_doctest::{add, exit_qemu, ExitCode}; | ||
/// #[export_name = "_start"] | ||
/// extern "C" fn main() { | ||
/// assert_eq!(add(1, 2), 3); | ||
/// unsafe { exit_qemu(ExitCode::Success); } | ||
/// } | ||
/// ``` | ||
pub fn add(a: u32, b: u32) -> u32 { | ||
a + b | ||
} | ||
|
||
/// multiply two numbers | ||
/// | ||
/// ``` | ||
/// #![no_std] | ||
/// #![no_main] | ||
/// use runner_doctest::{mul, exit_qemu, ExitCode}; | ||
/// #[export_name = "_start"] | ||
/// extern "C" fn main() { | ||
/// assert_eq!(mul(2, 3), 6); | ||
/// unsafe { exit_qemu(ExitCode::Success); } | ||
/// } | ||
/// ``` | ||
pub fn mul(a: u32, b: u32) -> u32 { | ||
a * b | ||
} | ||
|
||
fn test_runner(tests: &[&dyn Fn()]) { | ||
for test in tests.iter() { | ||
test(); | ||
} | ||
|
||
unsafe { | ||
exit_qemu(ExitCode::Success); | ||
} | ||
} | ||
|
||
pub enum ExitCode { | ||
Success, | ||
Failed, | ||
} | ||
|
||
impl ExitCode { | ||
fn code(&self) -> u32 { | ||
match self { | ||
ExitCode::Success => 0x10, | ||
ExitCode::Failed => 0x11, | ||
} | ||
} | ||
} | ||
|
||
/// exit QEMU (see https://os.phil-opp.com/integration-tests/#shutting-down-qemu) | ||
pub unsafe fn exit_qemu(exit_code: ExitCode) { | ||
use x86_64::instructions::port::Port; | ||
|
||
let mut port = Port::<u32>::new(0xf4); | ||
port.write(exit_code.code()); | ||
} | ||
|
||
#[cfg(test)] | ||
#[no_mangle] | ||
pub extern "C" fn _start() -> ! { | ||
test_main(); | ||
|
||
unsafe { | ||
exit_qemu(ExitCode::Failed); | ||
} | ||
|
||
loop {} | ||
} | ||
|
||
#[panic_handler] | ||
fn panic(_info: &core::panic::PanicInfo) -> ! { | ||
unsafe { | ||
exit_qemu(ExitCode::Failed); | ||
} | ||
loop {} | ||
} |