]> git.lizzy.rs Git - rust.git/blob - test-cargo-miri/src/main.rs
test reading from stdin
[rust.git] / test-cargo-miri / src / main.rs
1 use byteorder::{BigEndian, ByteOrder};
2 #[cfg(unix)]
3 use std::io::{self, BufRead};
4
5 fn main() {
6     // Exercise external crate, printing to stdout.
7     let buf = &[1,2,3,4];
8     let n = <BigEndian as ByteOrder>::read_u32(buf);
9     assert_eq!(n, 0x01020304);
10     println!("{:#010x}", n);
11
12     // Access program arguments, printing to stderr.
13     for arg in std::env::args() {
14         eprintln!("{}", arg);
15     }
16
17     // If there were no arguments, access stdin.
18     if std::env::args().len() <= 1 {
19         #[cfg(unix)]
20         for line in io::stdin().lock().lines() {
21             let num: i32 = line.unwrap().parse().unwrap();
22             println!("{}", 2*num);
23         }
24         // On non-Unix, reading from stdin is not support. So we hard-code the right answer.
25         #[cfg(not(unix))]
26         {
27             println!("24");
28             println!("42");
29         }
30     }
31
32 }
33
34 #[cfg(test)]
35 mod test {
36     use rand::{Rng, SeedableRng};
37
38     // Make sure in-crate tests with dev-dependencies work
39     #[test]
40     fn rng() {
41         let mut rng = rand::rngs::StdRng::seed_from_u64(0xcafebeef);
42         let x: u32 = rng.gen();
43         let y: usize = rng.gen();
44         let z: u128 = rng.gen();
45         assert_ne!(x as usize, y);
46         assert_ne!(y as u128, z);
47     }
48 }