]> git.lizzy.rs Git - rust.git/blob - tests/run-pass/libc.rs
5b7b37db327be78db0208d0ccf9d3df34464f27e
[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
6 #[allow(unused)] // necessary on macos due to conditional compilation
7 extern crate libc;
8
9 #[cfg(not(target_os = "macos"))]
10 fn test_posix_fadvise() {
11     use std::convert::TryInto;
12     use std::env::temp_dir;
13     use std::fs::{File, remove_file};
14     use std::io::Write;
15     use std::os::unix::io::AsRawFd;
16
17     let path = temp_dir().join("miri_test_libc.txt");
18     // Cleanup before test
19     remove_file(&path).ok();
20
21     // Set up an open file
22     let mut file = File::create(&path).unwrap();
23     let bytes = b"Hello, World!\n";
24     file.write(bytes).unwrap();
25
26     // Test calling posix_fadvise on a file.
27     let result = unsafe {
28         libc::posix_fadvise(
29             file.as_raw_fd(),
30             0,
31             bytes.len().try_into().unwrap(),
32             libc::POSIX_FADV_DONTNEED,
33         )
34     };
35     drop(file);
36     remove_file(&path).unwrap();
37     assert_eq!(result, 0);
38 }
39
40 fn main() {
41     #[cfg(not(target_os = "macos"))]
42     test_posix_fadvise();
43 }