Skip to content

Commit

Permalink
Trivial unit test runs with utest
Browse files Browse the repository at this point in the history
  • Loading branch information
robert-w-gries committed Dec 27, 2017
1 parent f6883f2 commit 0d0151d
Show file tree
Hide file tree
Showing 11 changed files with 239 additions and 11 deletions.
11 changes: 11 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,14 @@ default-features = false
default = ["serial"]
serial = []
vga = []

[dev-dependencies.test]
#git = "https://github.com/japaric/utest"
path = "/home/rob/utest"

[dev-dependencies.utest-macros]
#git = "https://github.com/japaric/utest"
path = "/home/rob/utest/macros"

[dev-dependencies.utest_rxinu]
path = "utest-rxinu"
8 changes: 1 addition & 7 deletions Xargo.toml
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
[target.i686-rxinu.dependencies]
alloc = {}

[target.x86_64-rxinu.dependencies]
alloc = {}

[dependencies.core]
[dependencies.alloc]
stage = 0

[dependencies.compiler_builtins]
Expand Down
14 changes: 14 additions & 0 deletions i686-rxinu-utest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"llvm-target": "i686-unknown-linux-gnu",
"data-layout": "e-m:e-p:32:32-f64:32:64-f80:32-n8:16:32-S128",
"target-endian": "little",
"target-pointer-width": "32",
"target-c-int-width": "32",
"arch": "x86",
"os": "linux",
"executables": true,
"features": "-mmx,-sse,+soft-float",
"linker-flavor": "ld",
"linker": "ld.lld-4.0",
"panic": "abort"
}
8 changes: 4 additions & 4 deletions src/arch/x86/memory/paging/mapper/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ mod tests {

#[test]
fn map() {
use memory::paging::page::Page;
use memory::paging::entry::EntryFlags;
use arch::x86::memory::paging::page::Page;
use arch::x86::memory::paging::entry::EntryFlags;

let page_table = unsafe { Mapper::new() };

Expand Down Expand Up @@ -96,8 +96,8 @@ mod tests {

#[test]
fn unmap() {
use memory::paging::page::Page;
use memory::paging::entry::EntryFlags;
use arch::x86::memory::paging::page::Page;
use arch::x86::memory::paging::entry::EntryFlags;

let page_table = unsafe { Mapper::new() };

Expand Down
17 changes: 17 additions & 0 deletions tests/foo.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#![no_std]

#[macro_use]
extern crate utest_macros;

extern crate utest_rxinu;

macro_rules! panic {
($($tt:tt)*) => {
upanic!($($tt)*);
};
}

#[test]
fn foo() {
assert!(true);
}
8 changes: 8 additions & 0 deletions utest-rxinu/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "utest_rxinu"
version = "0.1.0"
authors = ["Rob Gries <robert.w.gries@gmail.com>"]

[dependencies]
sc = "0.2.0"
rlibc = "1.0.0"
43 changes: 43 additions & 0 deletions utest-rxinu/src/io.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
use core::{fmt, slice};
use core::fmt::Write;

/// STDERR File descriptor
const STDERR: usize = 2;

struct Stderr;

impl Stderr {
fn write_all(&mut self, mut buffer: &[u8]) {
while !buffer.is_empty() {
match unsafe {
syscall!(WRITE, STDERR, buffer.as_ptr(), buffer.len()) as
isize
} {
n if n >= 0 => {
buffer =
unsafe {
slice::from_raw_parts(buffer.as_ptr().offset(n),
buffer.len() - n as usize)
}
}
// ignore errors
_ => return,
}
}
}
}

impl Write for Stderr {
fn write_str(&mut self, s: &str) -> fmt::Result {
self.write_all(s.as_bytes());
Ok(())
}
}

pub fn write_fmt(args: fmt::Arguments) {
Stderr.write_fmt(args).ok();
}

pub fn write_str(string: &str) {
Stderr.write_all(string.as_bytes())
}
31 changes: 31 additions & 0 deletions utest-rxinu/src/lang_items.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use core::intrinsics;

#[lang = "start"]
extern "C" fn start(main: fn(),
_argc: isize,
_argv: *const *const u8)
-> isize {
main();

0
}

#[lang = "panic_fmt"]
unsafe extern "C" fn panic_fmt(args: ::core::fmt::Arguments,
file: &'static str,
line: u32)
-> ! {
#[allow(improper_ctypes)]
extern "Rust" {
fn __test_panic_fmt(args: ::core::fmt::Arguments,
file: &'static str,
line: u32);
}

__test_panic_fmt(args, file, line);

intrinsics::abort()
}

#[lang = "eh_personality"]
pub extern "C" fn eh_personality() {}
86 changes: 86 additions & 0 deletions utest-rxinu/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
#![feature(compiler_builtins_lib)]
#![feature(core_intrinsics)]
#![feature(lang_items)]
#![no_std]

extern crate compiler_builtins;

#[macro_use]
extern crate sc;

extern crate rlibc;

use core::intrinsics;

#[macro_use]
mod macros;

mod lang_items;
mod io;

#[no_mangle]
pub fn __test_start(ntests: usize) {
eprintln!("running {} tests", ntests)
}

#[no_mangle]
pub fn __test_ignored(name: &str) {
eprintln!("test {} ... ignored", name);
}

#[no_mangle]
pub fn __test_before_run(name: &str) {
eprint!("test {} ... ", name);
}

#[no_mangle]
pub fn __test_panic_fmt(args: ::core::fmt::Arguments,
file: &'static str,
line: u32) {
eprint!("\npanicked at '");
io::write_fmt(args);
eprintln!("', {}:{}", file, line);
}

#[no_mangle]
pub fn __test_failed(_name: &str) {
eprintln!("FAILED");
}

#[no_mangle]
pub fn __test_success(_name: &str) {
eprintln!("OK");
}

#[no_mangle]
pub fn __test_summary(passed: usize, failed: usize, ignored: usize) {
eprintln!("\ntest result: {}. {} passed; {} failed; {} ignored",
if failed == 0 { "OK" } else { "FAILED" },
passed,
failed,
ignored);

if failed != 0 {
exit(101);
}
}

/// Entry point
#[no_mangle]
pub unsafe extern "C" fn _start() -> ! {
extern "C" {
fn main(argc: isize, argv: *const *const u8) -> isize;
}

main(0, ::core::ptr::null());

exit(0);
}

fn exit(code: i32) -> ! {
unsafe {
syscall!(EXIT, code as usize);

intrinsics::unreachable()
}
}
10 changes: 10 additions & 0 deletions utest-rxinu/src/macros.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
macro_rules! eprint {
($s:expr) => ($crate::io::write_str($s));
($($arg:tt)*) => ($crate::io::write_fmt(format_args!($($arg)*)));
}

macro_rules! eprintln {
() => (eprint!("\n"));
($fmt:expr) => (eprint!(concat!($fmt, "\n")));
($fmt:expr, $($arg:tt)*) => (eprint!(concat!($fmt, "\n"), $($arg)*));
}
14 changes: 14 additions & 0 deletions x86_64-rxinu-utest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"llvm-target": "x86_64-none-eabi",
"data-layout": "e-m:e-i64:64-f80:128-n8:16:32:64-S128",
"target-endian": "little",
"target-pointer-width": "64",
"target-c-int-width": "32",
"arch": "x86_64",
"os": "linux",
"executables": true,
"features": "-mmx,-sse,+soft-float",
"linker-flavor": "ld",
"linker": "ld.lld-4.0",
"panic": "abort"
}

0 comments on commit 0d0151d

Please sign in to comment.