]> git.lizzy.rs Git - rust.git/blob - src/tools/miri/test-cargo-miri/src/main.rs
Rollup merge of #104641 - tshepang:grammar, r=Mark-Simulacrum
[rust.git] / src / tools / miri / 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 byteorder_2::{BigEndian, ByteOrder};
49
50     // Make sure in-crate tests with dev-dependencies work
51     #[test]
52     fn dev_dependency() {
53         let _n = <BigEndian as ByteOrder>::read_u64(&[1, 2, 3, 4, 5, 6, 7, 8]);
54     }
55
56     #[test]
57     fn exported_symbol() {
58         extern crate cargo_miri_test;
59         extern crate exported_symbol;
60         extern crate issue_rust_86261;
61         // Test calling exported symbols in (transitive) dependencies.
62         // Repeat calls to make sure the `Instance` cache is not broken.
63         for _ in 0..3 {
64             extern "Rust" {
65                 fn exported_symbol() -> i32;
66                 fn assoc_fn_as_exported_symbol() -> i32;
67                 fn make_true() -> bool;
68                 fn NoMangleStruct();
69                 fn no_mangle_generic();
70             }
71             assert_eq!(unsafe { exported_symbol() }, 123456);
72             assert_eq!(unsafe { assoc_fn_as_exported_symbol() }, -123456);
73             assert!(unsafe { make_true() });
74             unsafe { NoMangleStruct() }
75             unsafe { no_mangle_generic() }
76         }
77     }
78 }