]> git.lizzy.rs Git - rust.git/blob - test-cargo-miri/src/main.rs
Auto merge of #1710 - hyd-dev:crate-types, r=RalfJung
[rust.git] / test-cargo-miri / src / main.rs
1 use byteorder::{BigEndian, ByteOrder};
2 use std::env;
3 #[cfg(unix)]
4 use std::io::{self, BufRead};
5
6 fn main() {
7     // Check env var set by `build.rs`.
8     assert_eq!(env!("MIRITESTVAR"), "testval");
9
10     // Exercise external crate, printing to stdout.
11     let buf = &[1,2,3,4];
12     let n = <BigEndian as ByteOrder>::read_u32(buf);
13     assert_eq!(n, 0x01020304);
14     println!("{:#010x}", n);
15
16     // Access program arguments, printing to stderr.
17     for arg in std::env::args() {
18         eprintln!("{}", arg);
19     }
20
21     // If there were no arguments, access stdin and test working dir.
22     // (We rely on the test runner to always disable isolation when passing no arguments.)
23     if std::env::args().len() <= 1 {
24         // CWD should be crate root.
25         // We have to normalize slashes, as the env var might be set for a different target's conventions.
26         let env_dir = env::current_dir().unwrap();
27         let env_dir = env_dir.to_string_lossy().replace("\\", "/");
28         let crate_dir = env::var_os("CARGO_MANIFEST_DIR").unwrap();
29         let crate_dir = crate_dir.to_string_lossy().replace("\\", "/");
30         assert_eq!(env_dir, crate_dir);
31
32         #[cfg(unix)]
33         for line in io::stdin().lock().lines() {
34             let num: i32 = line.unwrap().parse().unwrap();
35             println!("{}", 2*num);
36         }
37         // On non-Unix, reading from stdin is not supported. So we hard-code the right answer.
38         #[cfg(not(unix))]
39         {
40             println!("24");
41             println!("42");
42         }
43     }
44 }
45
46 #[cfg(test)]
47 mod test {
48     use rand::{Rng, SeedableRng};
49
50     // Make sure in-crate tests with dev-dependencies work
51     #[test]
52     fn rng() {
53         let mut rng = rand::rngs::StdRng::seed_from_u64(0xcafebeef);
54         let x: u32 = rng.gen();
55         let y: usize = rng.gen();
56         let z: u128 = rng.gen();
57         assert_ne!(x as usize, y);
58         assert_ne!(y as u128, z);
59     }
60 }