Skip to content

Commit 1531bd6

Browse files
committed
Merge pull request rust-lang#9 from oli-obk/improve_tests
various testing improvements
2 parents ae41877 + 5ea57cc commit 1531bd6

22 files changed

+201
-15
lines changed

src/bin/miri.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
#![feature(rustc_private)]
1+
#![feature(rustc_private, custom_attribute)]
2+
#![allow(unused_attributes)]
23

34
extern crate miri;
45
extern crate rustc;
@@ -23,6 +24,7 @@ impl<'a> CompilerCalls<'a> for MiriCompilerCalls {
2324
}
2425
}
2526

27+
#[miri_run]
2628
fn main() {
2729
let args: Vec<String> = std::env::args().collect();
2830
rustc_driver::run_compiler(&args, &mut MiriCompilerCalls);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#![feature(custom_attribute)]
2+
#![allow(dead_code, unused_attributes)]
3+
4+
// error-pattern:can't handle intrinsic: discriminant_value
5+
6+
#[miri_run]
7+
fn main() {
8+
assert_eq!(None::<i32>, None);
9+
}

tests/compile-fail/bugs/memcmp.rs

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#![feature(custom_attribute)]
2+
#![allow(dead_code, unused_attributes)]
3+
4+
// error-pattern:can't handle intrinsic: size_of_val
5+
6+
#[miri_run]
7+
fn memcmp() {
8+
assert_eq!("", "");
9+
}
10+
11+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#![feature(custom_attribute)]
2+
#![allow(dead_code, unused_attributes)]
3+
4+
#[miri_run]
5+
fn option_box_deref() -> i32 {
6+
let val = Some(Box::new(42));
7+
unsafe {
8+
let ptr: *const i32 = std::mem::transmute(val); //~ ERROR: pointer offset outside bounds of allocation
9+
*ptr
10+
}
11+
}
12+
13+
fn main() {}
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#![feature(custom_attribute)]
2+
#![allow(dead_code, unused_attributes)]
3+
4+
// error-pattern:assertion failed
5+
6+
#[miri_run]
7+
fn slice() -> u8 {
8+
let arr: &[_] = &[101, 102, 103, 104, 105, 106];
9+
arr[5]
10+
}
11+
12+
fn main() {}

tests/compile-fail/errors.rs

+11
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,15 @@ fn dangling_pointer_deref() -> i32 {
4646
unsafe { *p } //~ ERROR: dangling pointer was dereferenced
4747
}
4848

49+
#[miri_run]
50+
fn wild_pointer_deref() -> i32 {
51+
let p = 42 as *const i32;
52+
unsafe { *p } //~ ERROR: attempted to interpret some raw bytes as a pointer address
53+
}
54+
55+
#[miri_run]
56+
fn null_pointer_deref() -> i32 {
57+
unsafe { *std::ptr::null() } //~ ERROR: attempted to interpret some raw bytes as a pointer address
58+
}
59+
4960
fn main() {}

tests/compiletest.rs

+1
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,5 @@ fn run_mode(mode: &'static str) {
2020
fn compile_test() {
2121
run_mode("compile-fail");
2222
run_mode("run-pass");
23+
run_mode("run-fail");
2324
}

tests/run-fail/inception.rs

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// error-pattern:no mir for DefId
2+
3+
use std::env;
4+
use std::process::{Command, Output};
5+
6+
fn run_miri(file: &str, sysroot: &str) -> Output {
7+
let path = env::current_dir().unwrap();
8+
let libpath = path.join("target").join("debug");
9+
let libpath = libpath.to_str().unwrap();
10+
let libpath2 = path.join("target").join("debug").join("deps");
11+
let libpath2 = libpath2.to_str().unwrap();
12+
Command::new("cargo")
13+
.args(&[
14+
"run", "--",
15+
"--sysroot", sysroot,
16+
"-L", libpath,
17+
"-L", libpath2,
18+
file
19+
])
20+
.output()
21+
.unwrap_or_else(|e| panic!("failed to execute process: {}", e))
22+
}
23+
24+
fn main() {
25+
let sysroot = env::var("RUST_SYSROOT").expect("env variable `RUST_SYSROOT` not set");
26+
let test_run = run_miri("src/bin/miri.rs", &sysroot);
27+
28+
if test_run.status.code().unwrap_or(-1) != 0 {
29+
println!("{}", String::from_utf8(test_run.stdout).unwrap());
30+
panic!("{}", String::from_utf8(test_run.stderr).unwrap());
31+
}
32+
}

tests/run-pass/arrays.rs

+16-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ fn empty_array() -> [u16; 0] {
66
[]
77
}
88

9+
#[miri_run]
10+
fn mini_array() -> [u16; 1] {
11+
[42]
12+
}
13+
914
#[miri_run]
1015
fn big_array() -> [u16; 5] {
1116
[5, 4, 3, 2, 1]
@@ -33,4 +38,14 @@ fn array_repeat() -> [u8; 8] {
3338
[42; 8]
3439
}
3540

36-
fn main() {}
41+
#[miri_run]
42+
fn main() {
43+
//assert_eq!(empty_array(), []);
44+
assert_eq!(index_unsafe(), 20);
45+
assert_eq!(index(), 20);
46+
/*
47+
assert_eq!(big_array(), [5, 4, 3, 2, 1]);
48+
assert_eq!(array_array(), [[5, 4], [3, 2], [1, 0]]);
49+
assert_eq!(array_repeat(), [42; 8]);
50+
*/
51+
}

tests/run-pass/bools.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,10 @@ fn match_bool() -> i16 {
2727
}
2828
}
2929

30-
fn main() {}
30+
#[miri_run]
31+
fn main() {
32+
assert!(boolean());
33+
assert_eq!(if_false(), 0);
34+
assert_eq!(if_true(), 1);
35+
assert_eq!(match_bool(), 1);
36+
}

tests/run-pass/c_enums.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,7 @@ fn unsafe_match() -> bool {
2020
}
2121
}
2222

23-
fn main() {}
23+
#[miri_run]
24+
fn main() {
25+
assert!(unsafe_match());
26+
}

tests/run-pass/calls.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,11 @@ fn test_size_of() -> usize {
3939
::std::mem::size_of::<Option<i32>>()
4040
}
4141

42-
fn main() {}
42+
#[miri_run]
43+
fn main() {
44+
assert_eq!(call(), 2);
45+
assert_eq!(factorial_recursive(), 3628800);
46+
//assert_eq!(call_generic(), (42, true));
47+
assert_eq!(cross_crate_fn_call(), 1);
48+
//assert_eq!(test_size_of(), 8);
49+
}

tests/run-pass/closures.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,7 @@ fn crazy_closure() -> (i32, i32, i32) {
3737
// y
3838
// }
3939

40-
fn main() {}
40+
#[miri_run]
41+
fn main() {
42+
assert_eq!(simple(), 12);
43+
}

tests/run-pass/heap.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,8 @@ fn make_box_syntax() -> Box<(i16, i16)> {
1111
box (1, 2)
1212
}
1313

14-
fn main() {}
14+
#[miri_run]
15+
fn main() {
16+
assert_eq!(*make_box(), (1, 2));
17+
assert_eq!(*make_box_syntax(), (1, 2));
18+
}

tests/run-pass/ints.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,13 @@ fn match_int_range() -> i64 {
5353
}
5454
}
5555

56-
fn main() {}
56+
#[miri_run]
57+
fn main() {
58+
assert_eq!(ret(), 1);
59+
assert_eq!(neg(), -1);
60+
assert_eq!(add(), 3);
61+
assert_eq!(indirect_add(), 3);
62+
assert_eq!(arith(), 5*5);
63+
assert_eq!(match_int(), 20);
64+
assert_eq!(match_int_range(), 4);
65+
}

tests/run-pass/loops.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,9 @@ fn for_loop() -> usize {
3434
sum
3535
}
3636

37-
fn main() {}
37+
#[miri_run]
38+
fn main() {
39+
assert_eq!(factorial_loop(), 3628800);
40+
assert_eq!(index_for_loop(), 60);
41+
assert_eq!(for_loop(), 60);
42+
}

tests/run-pass/pointers.rs

+11-1
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,14 @@ fn dangling_pointer() -> *const i32 {
5858
&*b as *const i32
5959
}
6060

61-
fn main() {}
61+
#[miri_run]
62+
fn main() {
63+
assert_eq!(one_line_ref(), 1);
64+
assert_eq!(basic_ref(), 1);
65+
assert_eq!(basic_ref_mut(), 3);
66+
assert_eq!(basic_ref_mut_var(), 3);
67+
assert_eq!(tuple_ref_mut(), (10, 22));
68+
assert_eq!(match_ref_mut(), 42);
69+
// FIXME: improve this test... how?
70+
assert!(dangling_pointer() != std::ptr::null());
71+
}

tests/run-pass/products.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ fn tuple_5() -> (i16, i16, i16, i16, i16) {
1616
(1, 2, 3, 4, 5)
1717
}
1818

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

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

33-
fn main() {}
34+
#[miri_run]
35+
fn main() {
36+
assert_eq!(tuple(), (1,));
37+
assert_eq!(tuple_2(), (1, 2));
38+
assert_eq!(tuple_5(), (1, 2, 3, 4, 5));
39+
assert_eq!(pair(), Pair { x: 10, y: 20} );
40+
assert_eq!(field_access(), (15, 20));
41+
}

tests/run-pass/specialization.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,6 @@ fn specialization() -> (bool, bool) {
1818
(i32::is_unit(), <()>::is_unit())
1919
}
2020

21-
fn main() {}
21+
fn main() {
22+
assert_eq!(specialization(), (false, true));
23+
}

tests/run-pass/std.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,10 @@ fn true_assert() {
4444
assert_eq!(1, 1);
4545
}
4646

47-
fn main() {}
47+
#[miri_run]
48+
fn main() {
49+
//let x = rc_reference_cycle().0;
50+
//assert!(x.borrow().is_some());
51+
assert_eq!(*arc(), 42);
52+
assert_eq!(rc_cell().get(), 84);
53+
}

tests/run-pass/sums.rs

+13-1
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
#![feature(custom_attribute)]
22
#![allow(dead_code, unused_attributes)]
33

4+
#[derive(Debug, PartialEq)]
45
enum Unit { Unit }
56

67
#[miri_run]
78
fn return_unit() -> Unit {
89
Unit::Unit
910
}
1011

12+
#[derive(Debug, PartialEq)]
1113
enum MyBool { False, True }
1214

1315
#[miri_run]
@@ -53,4 +55,14 @@ fn two_nones() -> (Option<i16>, Option<i16>) {
5355
(None, None)
5456
}
5557

56-
fn main() {}
58+
#[miri_run]
59+
fn main() {
60+
//assert_eq!(two_nones(), (None, None));
61+
assert_eq!(match_opt_some(), 13);
62+
assert_eq!(match_opt_none(), 42);
63+
//assert_eq!(return_some(), Some(42));
64+
//assert_eq!(return_none(), None);
65+
//assert_eq!(return_false(), MyBool::False);
66+
//assert_eq!(return_true(), MyBool::True);
67+
//assert_eq!(return_unit(), Unit::Unit);
68+
}

tests/run-pass/vecs.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,9 @@ fn vec_reallocate() -> Vec<u8> {
3636
v
3737
}
3838

39-
fn main() {}
39+
#[miri_run]
40+
fn main() {
41+
assert_eq!(vec_reallocate().len(), 5);
42+
assert_eq!(vec_into_iter(), 30);
43+
assert_eq!(make_vec().capacity(), 4);
44+
}

0 commit comments

Comments
 (0)