]> git.lizzy.rs Git - rust.git/blob - tests/run-pass/libc.rs
test harness informs tests about suitable temp dir
[rust.git] / tests / run-pass / libc.rs
1 // ignore-windows: No libc on Windows
2 // compile-flags: -Zmiri-disable-isolation
3
4 #![feature(rustc_private)]
5 #![allow(unused)] // necessary on macos due to conditional compilation
6
7 use std::path::PathBuf;
8
9 extern crate libc;
10
11 fn tmp() -> PathBuf {
12     std::env::var("MIRI_TEMP").map(PathBuf::from).unwrap_or_else(|_| std::env::temp_dir())
13 }
14
15 #[cfg(not(target_os = "macos"))]
16 fn test_posix_fadvise() {
17     use std::convert::TryInto;
18     use std::fs::{File, remove_file};
19     use std::io::Write;
20     use std::os::unix::io::AsRawFd;
21
22     let path = tmp().join("miri_test_libc.txt");
23     // Cleanup before test
24     remove_file(&path).ok();
25
26     // Set up an open file
27     let mut file = File::create(&path).unwrap();
28     let bytes = b"Hello, World!\n";
29     file.write(bytes).unwrap();
30
31     // Test calling posix_fadvise on a file.
32     let result = unsafe {
33         libc::posix_fadvise(
34             file.as_raw_fd(),
35             0,
36             bytes.len().try_into().unwrap(),
37             libc::POSIX_FADV_DONTNEED,
38         )
39     };
40     drop(file);
41     remove_file(&path).unwrap();
42     assert_eq!(result, 0);
43 }
44
45 fn main() {
46     #[cfg(not(target_os = "macos"))]
47     test_posix_fadvise();
48 }