Skip to content

Commit 96e2f22

Browse files
committed
Add fstat and termios tests
1 parent 006ed07 commit 96e2f22

6 files changed

Lines changed: 246 additions & 0 deletions

File tree

‎tests/unit/fstat.c‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#include <assert.h>
2+
#include <fcntl.h>
3+
#include <sys/stat.h>
4+
#include <unistd.h>
5+
6+
int main(void) {
7+
const char *path = "/tmp/cpp2rust_fstat_test.tmp";
8+
int fd = open(path, O_RDWR | O_CREAT | O_TRUNC, 0644);
9+
assert(fd >= 0);
10+
assert(write(fd, "hello", 5) == 5);
11+
struct stat st;
12+
assert(fstat(fd, &st) == 0);
13+
assert(st.st_size == 5);
14+
assert((st.st_mode & S_IFMT) == S_IFREG);
15+
assert(close(fd) == 0);
16+
assert(unlink(path) == 0);
17+
return 0;
18+
}

‎tests/unit/out/refcount/fstat.rs‎

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
extern crate libcc2rs;
2+
use libcc2rs::*;
3+
use std::cell::RefCell;
4+
use std::collections::BTreeMap;
5+
use std::io::prelude::*;
6+
use std::io::{Read, Seek, Write};
7+
use std::os::fd::AsFd;
8+
use std::rc::{Rc, Weak};
9+
pub fn main() {
10+
std::process::exit(main_0());
11+
}
12+
fn main_0() -> i32 {
13+
let path: Value<Ptr<u8>> = Rc::new(RefCell::new(Ptr::from_string_literal(
14+
b"/tmp/cpp2rust_fstat_test.tmp",
15+
)));
16+
let fd: Value<i32> = Rc::new(RefCell::new({
17+
let __mode = match &[(420).into()].first() {
18+
Some(__m) => nix::sys::stat::Mode::from_bits_truncate(i32::get(__m) as ::libc::mode_t),
19+
None => nix::sys::stat::Mode::empty(),
20+
};
21+
match nix::fcntl::open(
22+
(*path.borrow()).to_rust_string().as_str(),
23+
nix::fcntl::OFlag::from_bits_retain(
24+
((::libc::O_RDWR | ::libc::O_CREAT) | ::libc::O_TRUNC),
25+
),
26+
__mode,
27+
) {
28+
Ok(__ofd) => FdRegistry::register(__ofd),
29+
Err(__e) => {
30+
libcc2rs::cpp2rust_errno().write(__e as i32);
31+
-1
32+
}
33+
}
34+
}));
35+
assert!(((((*fd.borrow()) >= 0) as i32) != 0));
36+
assert!(
37+
(((match FdRegistry::with_fd((*fd.borrow()), |__fd| {
38+
Ptr::from_string_literal(b"hello")
39+
.to_any()
40+
.reinterpret_cast::<u8>()
41+
.with_slice(5_usize, |__buf| nix::unistd::write(__fd, __buf))
42+
}) {
43+
Ok(__n) => __n as isize,
44+
Err(__e) => {
45+
libcc2rs::cpp2rust_errno().write(__e as i32);
46+
-1
47+
}
48+
} == 5_isize) as i32)
49+
!= 0)
50+
);
51+
let st: Value<libcc2rs::Stat> = Rc::new(RefCell::new(Default::default()));
52+
assert!(
53+
(((match FdRegistry::with_fd((*fd.borrow()), |__fd| nix::sys::stat::fstat(__fd)) {
54+
Ok(__s) => {
55+
(st.as_pointer()).with_mut(|__st| *__st = Stat::from_libc(&__s));
56+
0
57+
}
58+
Err(__e) => {
59+
libcc2rs::cpp2rust_errno().write(__e as i32);
60+
-1
61+
}
62+
} == 0) as i32)
63+
!= 0)
64+
);
65+
assert!(((((*(*st.borrow()).st_size.borrow()) == 5_i64) as i32) != 0));
66+
assert!((((((*(*st.borrow()).st_mode.borrow()) & 61440_u32) == 32768_u32) as i32) != 0));
67+
assert!((((FdRegistry::close((*fd.borrow())) == 0) as i32) != 0));
68+
assert!(
69+
(((match nix::unistd::unlink((*path.borrow()).to_rust_string().as_str()) {
70+
Ok(()) => 0,
71+
Err(__e) => {
72+
libcc2rs::cpp2rust_errno().write(__e as i32);
73+
-1
74+
}
75+
} == 0) as i32)
76+
!= 0)
77+
);
78+
return 0;
79+
}

‎tests/unit/out/refcount/termios.rs‎

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
extern crate libcc2rs;
2+
use libcc2rs::*;
3+
use std::cell::RefCell;
4+
use std::collections::BTreeMap;
5+
use std::io::prelude::*;
6+
use std::io::{Read, Seek, Write};
7+
use std::os::fd::AsFd;
8+
use std::rc::{Rc, Weak};
9+
pub fn main() {
10+
std::process::exit(main_0());
11+
}
12+
fn main_0() -> i32 {
13+
let path: Value<Ptr<u8>> = Rc::new(RefCell::new(Ptr::from_string_literal(
14+
b"/tmp/cpp2rust_termios_test.tmp",
15+
)));
16+
let fd: Value<i32> = Rc::new(RefCell::new({
17+
let __mode = match &[(420).into()].first() {
18+
Some(__m) => nix::sys::stat::Mode::from_bits_truncate(i32::get(__m) as ::libc::mode_t),
19+
None => nix::sys::stat::Mode::empty(),
20+
};
21+
match nix::fcntl::open(
22+
(*path.borrow()).to_rust_string().as_str(),
23+
nix::fcntl::OFlag::from_bits_retain(
24+
((::libc::O_RDWR | ::libc::O_CREAT) | ::libc::O_TRUNC),
25+
),
26+
__mode,
27+
) {
28+
Ok(__ofd) => FdRegistry::register(__ofd),
29+
Err(__e) => {
30+
libcc2rs::cpp2rust_errno().write(__e as i32);
31+
-1
32+
}
33+
}
34+
}));
35+
assert!(((((*fd.borrow()) >= 0) as i32) != 0));
36+
let tio: Value<libcc2rs::Termios> = Rc::new(RefCell::new(Default::default()));
37+
assert!(
38+
(((match FdRegistry::with_fd((*fd.borrow()), |__fd| nix::sys::termios::tcgetattr(__fd)) {
39+
Ok(__t) => {
40+
(tio.as_pointer()).with_mut(|__dst| *__dst = Termios::from_libc(&__t.into()));
41+
0
42+
}
43+
Err(__e) => {
44+
libcc2rs::cpp2rust_errno().write(__e as i32);
45+
-1
46+
}
47+
} == -1_i32) as i32)
48+
!= 0)
49+
);
50+
assert!((((FdRegistry::close((*fd.borrow())) == 0) as i32) != 0));
51+
assert!(
52+
(((match nix::unistd::unlink((*path.borrow()).to_rust_string().as_str()) {
53+
Ok(()) => 0,
54+
Err(__e) => {
55+
libcc2rs::cpp2rust_errno().write(__e as i32);
56+
-1
57+
}
58+
} == 0) as i32)
59+
!= 0)
60+
);
61+
return 0;
62+
}

‎tests/unit/out/unsafe/fstat.rs‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
extern crate libc;
2+
use libc::*;
3+
extern crate libcc2rs;
4+
use libcc2rs::*;
5+
use std::collections::BTreeMap;
6+
use std::io::{Read, Seek, Write};
7+
use std::os::fd::{AsFd, FromRawFd, IntoRawFd};
8+
use std::rc::Rc;
9+
pub fn main() {
10+
unsafe {
11+
std::process::exit(main_0() as i32);
12+
}
13+
}
14+
unsafe fn main_0() -> i32 {
15+
let mut path: *const libc::c_char =
16+
(c"/tmp/cpp2rust_fstat_test.tmp".as_ptr().cast_mut()).cast_const();
17+
let mut fd: i32 = (unsafe {
18+
libc::open(
19+
path as *const i8,
20+
(((::libc::O_RDWR) | (::libc::O_CREAT)) | (::libc::O_TRUNC)) as i32,
21+
(420),
22+
)
23+
});
24+
assert!(((((fd) >= (0)) as i32) != 0));
25+
assert!(
26+
((((libc::write(
27+
fd,
28+
(c"hello".as_ptr().cast_mut() as *const libc::c_char as *const ::libc::c_void),
29+
5_usize
30+
)) == (5_isize)) as i32)
31+
!= 0)
32+
);
33+
let mut st: ::libc::stat = unsafe { std::mem::zeroed() };
34+
assert!(((((libc::fstat(fd, (&mut st as *mut ::libc::stat))) == (0)) as i32) != 0));
35+
assert!(((((st.st_size) == (5_i64)) as i32) != 0));
36+
assert!((((((st.st_mode) & (61440_u32)) == (32768_u32)) as i32) != 0));
37+
assert!(((((libc::close(fd)) == (0)) as i32) != 0));
38+
assert!(((((libc::unlink(path)) == (0)) as i32) != 0));
39+
return 0;
40+
}

‎tests/unit/out/unsafe/termios.rs‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
extern crate libc;
2+
use libc::*;
3+
extern crate libcc2rs;
4+
use libcc2rs::*;
5+
use std::collections::BTreeMap;
6+
use std::io::{Read, Seek, Write};
7+
use std::os::fd::{AsFd, FromRawFd, IntoRawFd};
8+
use std::rc::Rc;
9+
pub fn main() {
10+
unsafe {
11+
std::process::exit(main_0() as i32);
12+
}
13+
}
14+
unsafe fn main_0() -> i32 {
15+
let mut path: *const libc::c_char =
16+
(c"/tmp/cpp2rust_termios_test.tmp".as_ptr().cast_mut()).cast_const();
17+
let mut fd: i32 = (unsafe {
18+
libc::open(
19+
path as *const i8,
20+
(((::libc::O_RDWR) | (::libc::O_CREAT)) | (::libc::O_TRUNC)) as i32,
21+
(420),
22+
)
23+
});
24+
assert!(((((fd) >= (0)) as i32) != 0));
25+
let mut tio: ::libc::termios = unsafe { std::mem::zeroed() };
26+
assert!(
27+
((((libc::tcgetattr(fd, (&mut tio as *mut ::libc::termios))) == (-1_i32)) as i32) != 0)
28+
);
29+
assert!(((((libc::close(fd)) == (0)) as i32) != 0));
30+
assert!(((((libc::unlink(path)) == (0)) as i32) != 0));
31+
return 0;
32+
}

‎tests/unit/termios.c‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#include <assert.h>
2+
#include <fcntl.h>
3+
#include <termios.h>
4+
#include <unistd.h>
5+
6+
int main(void) {
7+
const char *path = "/tmp/cpp2rust_termios_test.tmp";
8+
int fd = open(path, O_RDWR | O_CREAT | O_TRUNC, 0644);
9+
assert(fd >= 0);
10+
struct termios tio;
11+
assert(tcgetattr(fd, &tio) == -1);
12+
assert(close(fd) == 0);
13+
assert(unlink(path) == 0);
14+
return 0;
15+
}

0 commit comments

Comments
 (0)