Skip to content

Commit

Permalink
add test for doctest
Browse files Browse the repository at this point in the history
  • Loading branch information
Freax13 committed Apr 29, 2020
1 parent 293f7da commit 89ed3b0
Show file tree
Hide file tree
Showing 7 changed files with 132 additions and 23 deletions.
4 changes: 4 additions & 0 deletions azure-pipelines.yml
Expand Up @@ -191,6 +191,10 @@ jobs:
workingDirectory: example-kernels/runner
displayName: 'Run `cargo xrun` for "runner" kernel'
- script: cargo xtest -Z doctest-xcompile
workingDirectory: example-kernels/runner-doctest
displayName: 'Run `cargo xtest -Z doctest-xcompile` for "runner-doctest" kernel'

- script: cargo xtest
workingDirectory: example-kernels/runner-test
displayName: 'Run `cargo xtest` for "runner-test" kernel'
Expand Down
46 changes: 23 additions & 23 deletions example-kernels/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions example-kernels/Cargo.toml
Expand Up @@ -4,6 +4,7 @@ members = [
"default-target-bootimage",
"default-target-cargo",
"runner",
"runner-doctest",
"runner-test",
"testing-qemu-exit-code",
"testing-serial-result",
Expand Down
5 changes: 5 additions & 0 deletions example-kernels/runner-doctest/.cargo/config
@@ -0,0 +1,5 @@
[build]
target = "../x86_64-bootimage-example-kernels.json"

[target.'cfg(target_os = "none")']
runner = "bootimage runner"
2 changes: 2 additions & 0 deletions example-kernels/runner-doctest/.gitignore
@@ -0,0 +1,2 @@
/target/
**/*.rs.bk
13 changes: 13 additions & 0 deletions example-kernels/runner-doctest/Cargo.toml
@@ -0,0 +1,13 @@
[package]
name = "runner-doctest"
version = "0.1.0"
authors = ["Philipp Oppermann <dev@phil-opp.com>"]
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"]
84 changes: 84 additions & 0 deletions example-kernels/runner-doctest/src/lib.rs
@@ -0,0 +1,84 @@
#![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 {}
}

0 comments on commit 89ed3b0

Please sign in to comment.