Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

various testing improvements #9

Merged
merged 3 commits into from
Apr 22, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/bin/miri.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#![feature(rustc_private)]
#![feature(rustc_private, custom_attribute)]
#![allow(unused_attributes)]

extern crate miri;
extern crate rustc;
Expand All @@ -23,6 +24,7 @@ impl<'a> CompilerCalls<'a> for MiriCompilerCalls {
}
}

#[miri_run]
fn main() {
let args: Vec<String> = std::env::args().collect();
rustc_driver::run_compiler(&args, &mut MiriCompilerCalls);
Expand Down
9 changes: 9 additions & 0 deletions tests/compile-fail/bugs/discriminant_value.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#![feature(custom_attribute)]
#![allow(dead_code, unused_attributes)]

// error-pattern:can't handle intrinsic: discriminant_value

#[miri_run]
fn main() {
assert_eq!(None::<i32>, None);
}
11 changes: 11 additions & 0 deletions tests/compile-fail/bugs/memcmp.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#![feature(custom_attribute)]
#![allow(dead_code, unused_attributes)]

// error-pattern:can't handle intrinsic: size_of_val

#[miri_run]
fn memcmp() {
assert_eq!("", "");
}

fn main() {}
13 changes: 13 additions & 0 deletions tests/compile-fail/bugs/option_box_transmute_ptr.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#![feature(custom_attribute)]
#![allow(dead_code, unused_attributes)]

#[miri_run]
fn option_box_deref() -> i32 {
let val = Some(Box::new(42));
unsafe {
let ptr: *const i32 = std::mem::transmute(val); //~ ERROR: pointer offset outside bounds of allocation
*ptr
}
}

fn main() {}
12 changes: 12 additions & 0 deletions tests/compile-fail/bugs/slice_index.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#![feature(custom_attribute)]
#![allow(dead_code, unused_attributes)]

// error-pattern:assertion failed

#[miri_run]
fn slice() -> u8 {
let arr: &[_] = &[101, 102, 103, 104, 105, 106];
arr[5]
}

fn main() {}
11 changes: 11 additions & 0 deletions tests/compile-fail/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,15 @@ fn dangling_pointer_deref() -> i32 {
unsafe { *p } //~ ERROR: dangling pointer was dereferenced
}

#[miri_run]
fn wild_pointer_deref() -> i32 {
let p = 42 as *const i32;
unsafe { *p } //~ ERROR: attempted to interpret some raw bytes as a pointer address
}

#[miri_run]
fn null_pointer_deref() -> i32 {
unsafe { *std::ptr::null() } //~ ERROR: attempted to interpret some raw bytes as a pointer address
}

fn main() {}
1 change: 1 addition & 0 deletions tests/compiletest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ fn run_mode(mode: &'static str) {
fn compile_test() {
run_mode("compile-fail");
run_mode("run-pass");
run_mode("run-fail");
}
32 changes: 32 additions & 0 deletions tests/run-fail/inception.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// error-pattern:no mir for DefId
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's the std::env::args() that are blocking this

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hilarious test. :)

But yeah, it would need crate metadata to contain MIR for every function. And later it would fall down on some FFI call, but it would be fun to see how far it can go.


use std::env;
use std::process::{Command, Output};

fn run_miri(file: &str, sysroot: &str) -> Output {
let path = env::current_dir().unwrap();
let libpath = path.join("target").join("debug");
let libpath = libpath.to_str().unwrap();
let libpath2 = path.join("target").join("debug").join("deps");
let libpath2 = libpath2.to_str().unwrap();
Command::new("cargo")
.args(&[
"run", "--",
"--sysroot", sysroot,
"-L", libpath,
"-L", libpath2,
file
])
.output()
.unwrap_or_else(|e| panic!("failed to execute process: {}", e))
}

fn main() {
let sysroot = env::var("RUST_SYSROOT").expect("env variable `RUST_SYSROOT` not set");
let test_run = run_miri("src/bin/miri.rs", &sysroot);

if test_run.status.code().unwrap_or(-1) != 0 {
println!("{}", String::from_utf8(test_run.stdout).unwrap());
panic!("{}", String::from_utf8(test_run.stderr).unwrap());
}
}
17 changes: 16 additions & 1 deletion tests/run-pass/arrays.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ fn empty_array() -> [u16; 0] {
[]
}

#[miri_run]
fn mini_array() -> [u16; 1] {
[42]
}

#[miri_run]
fn big_array() -> [u16; 5] {
[5, 4, 3, 2, 1]
Expand Down Expand Up @@ -33,4 +38,14 @@ fn array_repeat() -> [u8; 8] {
[42; 8]
}

fn main() {}
#[miri_run]
fn main() {
//assert_eq!(empty_array(), []);
assert_eq!(index_unsafe(), 20);
assert_eq!(index(), 20);
/*
assert_eq!(big_array(), [5, 4, 3, 2, 1]);
assert_eq!(array_array(), [[5, 4], [3, 2], [1, 0]]);
assert_eq!(array_repeat(), [42; 8]);
*/
}
8 changes: 7 additions & 1 deletion tests/run-pass/bools.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,10 @@ fn match_bool() -> i16 {
}
}

fn main() {}
#[miri_run]
fn main() {
assert!(boolean());
assert_eq!(if_false(), 0);
assert_eq!(if_true(), 1);
assert_eq!(match_bool(), 1);
}
5 changes: 4 additions & 1 deletion tests/run-pass/c_enums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,7 @@ fn unsafe_match() -> bool {
}
}

fn main() {}
#[miri_run]
fn main() {
assert!(unsafe_match());
}
9 changes: 8 additions & 1 deletion tests/run-pass/calls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,11 @@ fn test_size_of() -> usize {
::std::mem::size_of::<Option<i32>>()
}

fn main() {}
#[miri_run]
fn main() {
assert_eq!(call(), 2);
assert_eq!(factorial_recursive(), 3628800);
//assert_eq!(call_generic(), (42, true));
assert_eq!(cross_crate_fn_call(), 1);
//assert_eq!(test_size_of(), 8);
}
5 changes: 4 additions & 1 deletion tests/run-pass/closures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,7 @@ fn crazy_closure() -> (i32, i32, i32) {
// y
// }

fn main() {}
#[miri_run]
fn main() {
assert_eq!(simple(), 12);
}
6 changes: 5 additions & 1 deletion tests/run-pass/heap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,8 @@ fn make_box_syntax() -> Box<(i16, i16)> {
box (1, 2)
}

fn main() {}
#[miri_run]
fn main() {
assert_eq!(*make_box(), (1, 2));
assert_eq!(*make_box_syntax(), (1, 2));
}
11 changes: 10 additions & 1 deletion tests/run-pass/ints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,13 @@ fn match_int_range() -> i64 {
}
}

fn main() {}
#[miri_run]
fn main() {
assert_eq!(ret(), 1);
assert_eq!(neg(), -1);
assert_eq!(add(), 3);
assert_eq!(indirect_add(), 3);
assert_eq!(arith(), 5*5);
assert_eq!(match_int(), 20);
assert_eq!(match_int_range(), 4);
}
7 changes: 6 additions & 1 deletion tests/run-pass/loops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,9 @@ fn for_loop() -> usize {
sum
}

fn main() {}
#[miri_run]
fn main() {
assert_eq!(factorial_loop(), 3628800);
assert_eq!(index_for_loop(), 60);
assert_eq!(for_loop(), 60);
}
12 changes: 11 additions & 1 deletion tests/run-pass/pointers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,14 @@ fn dangling_pointer() -> *const i32 {
&*b as *const i32
}

fn main() {}
#[miri_run]
fn main() {
assert_eq!(one_line_ref(), 1);
assert_eq!(basic_ref(), 1);
assert_eq!(basic_ref_mut(), 3);
assert_eq!(basic_ref_mut_var(), 3);
assert_eq!(tuple_ref_mut(), (10, 22));
assert_eq!(match_ref_mut(), 42);
// FIXME: improve this test... how?
assert!(dangling_pointer() != std::ptr::null());
}
10 changes: 9 additions & 1 deletion tests/run-pass/products.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ fn tuple_5() -> (i16, i16, i16, i16, i16) {
(1, 2, 3, 4, 5)
}

#[derive(Debug, PartialEq)]
struct Pair { x: i8, y: i8 }

#[miri_run]
Expand All @@ -30,4 +31,11 @@ fn field_access() -> (i8, i8) {
(p.x, p.y)
}

fn main() {}
#[miri_run]
fn main() {
assert_eq!(tuple(), (1,));
assert_eq!(tuple_2(), (1, 2));
assert_eq!(tuple_5(), (1, 2, 3, 4, 5));
assert_eq!(pair(), Pair { x: 10, y: 20} );
assert_eq!(field_access(), (15, 20));
}
4 changes: 3 additions & 1 deletion tests/run-pass/specialization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,6 @@ fn specialization() -> (bool, bool) {
(i32::is_unit(), <()>::is_unit())
}

fn main() {}
fn main() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing #[miri_run] here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nope, it doesn't work, miri can't compare tuples

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not? It's just a PartialEq implementation, isn't it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Haha, true == false doesn't work in miri?

assert_eq!(specialization(), (false, true));
}
8 changes: 7 additions & 1 deletion tests/run-pass/std.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,10 @@ fn true_assert() {
assert_eq!(1, 1);
}

fn main() {}
#[miri_run]
fn main() {
//let x = rc_reference_cycle().0;
//assert!(x.borrow().is_some());
assert_eq!(*arc(), 42);
assert_eq!(rc_cell().get(), 84);
}
14 changes: 13 additions & 1 deletion tests/run-pass/sums.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
#![feature(custom_attribute)]
#![allow(dead_code, unused_attributes)]

#[derive(Debug, PartialEq)]
enum Unit { Unit }

#[miri_run]
fn return_unit() -> Unit {
Unit::Unit
}

#[derive(Debug, PartialEq)]
enum MyBool { False, True }

#[miri_run]
Expand Down Expand Up @@ -53,4 +55,14 @@ fn two_nones() -> (Option<i16>, Option<i16>) {
(None, None)
}

fn main() {}
#[miri_run]
fn main() {
//assert_eq!(two_nones(), (None, None));
assert_eq!(match_opt_some(), 13);
assert_eq!(match_opt_none(), 42);
//assert_eq!(return_some(), Some(42));
//assert_eq!(return_none(), None);
//assert_eq!(return_false(), MyBool::False);
//assert_eq!(return_true(), MyBool::True);
//assert_eq!(return_unit(), Unit::Unit);
}
7 changes: 6 additions & 1 deletion tests/run-pass/vecs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,9 @@ fn vec_reallocate() -> Vec<u8> {
v
}

fn main() {}
#[miri_run]
fn main() {
assert_eq!(vec_reallocate().len(), 5);
assert_eq!(vec_into_iter(), 30);
assert_eq!(make_vec().capacity(), 4);
}