rust/test-cargo-miri/src/main.rs

61 lines
1.9 KiB
Rust
Raw Normal View History

2017-06-29 12:59:47 -05:00
use byteorder::{BigEndian, ByteOrder};
use std::env;
2020-09-09 02:18:10 -05:00
#[cfg(unix)]
use std::io::{self, BufRead};
2017-06-29 12:59:47 -05:00
fn main() {
// Check env var set by `build.rs`.
assert_eq!(env!("MIRITESTVAR"), "testval");
2019-06-29 06:33:47 -05:00
// Exercise external crate, printing to stdout.
2017-06-29 12:59:47 -05:00
let buf = &[1,2,3,4];
let n = <BigEndian as ByteOrder>::read_u32(buf);
assert_eq!(n, 0x01020304);
println!("{:#010x}", n);
2019-06-29 06:33:47 -05:00
// Access program arguments, printing to stderr.
for arg in std::env::args() {
eprintln!("{}", arg);
}
2020-09-09 02:18:10 -05:00
// If there were no arguments, access stdin and test working dir.
2020-09-18 05:12:02 -05:00
// (We rely on the test runner to always disable isolation when passing no arguments.)
2020-09-09 02:18:10 -05:00
if std::env::args().len() <= 1 {
// CWD should be crate root.
// We have to normalize slashes, as the env var might be set for a different target's conventions.
let env_dir = env::current_dir().unwrap();
let env_dir = env_dir.to_string_lossy().replace("\\", "/");
let crate_dir = env::var_os("CARGO_MANIFEST_DIR").unwrap();
let crate_dir = crate_dir.to_string_lossy().replace("\\", "/");
assert_eq!(env_dir, crate_dir);
2020-09-09 02:18:10 -05:00
#[cfg(unix)]
for line in io::stdin().lock().lines() {
let num: i32 = line.unwrap().parse().unwrap();
println!("{}", 2*num);
}
2020-09-18 05:12:02 -05:00
// On non-Unix, reading from stdin is not supported. So we hard-code the right answer.
2020-09-09 02:18:10 -05:00
#[cfg(not(unix))]
{
println!("24");
println!("42");
}
}
}
2019-02-07 06:00:42 -06:00
#[cfg(test)]
mod test {
use rand::{Rng, SeedableRng};
// Make sure in-crate tests with dev-dependencies work
#[test]
fn rng() {
let mut rng = rand::rngs::StdRng::seed_from_u64(0xcafebeef);
let x: u32 = rng.gen();
2019-06-12 11:19:50 -05:00
let y: usize = rng.gen();
let z: u128 = rng.gen();
assert_ne!(x as usize, y);
assert_ne!(y as u128, z);
2019-02-07 06:00:42 -06:00
}
}