Skip to content

Commit e05eb20

Browse files
committed
Add simple io test
1 parent abd2008 commit e05eb20

3 files changed

Lines changed: 227 additions & 0 deletions

File tree

‎tests/unit/fd_io.c‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#include <assert.h>
2+
#include <fcntl.h>
3+
#include <string.h>
4+
#include <unistd.h>
5+
6+
int main(void) {
7+
const char *path = "/tmp/cpp2rust_fd_io_test.tmp";
8+
int fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
9+
assert(fd >= 0);
10+
assert(write(fd, "hello world", 11) == 11);
11+
assert(close(fd) == 0);
12+
fd = open(path, O_RDONLY);
13+
assert(fd >= 0);
14+
char buf[16];
15+
memset(buf, 0, sizeof(buf));
16+
assert(read(fd, buf, sizeof(buf)) == 11);
17+
assert(strcmp(buf, "hello world") == 0);
18+
assert(read(fd, buf, sizeof(buf)) == 0);
19+
assert(close(fd) == 0);
20+
assert(unlink(path) == 0);
21+
return 0;
22+
}

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

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
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_fd_io_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(((1 | 64) | 512)),
24+
__mode,
25+
) {
26+
Ok(__ofd) => FdRegistry::register(__ofd),
27+
Err(__e) => {
28+
libcc2rs::cpp2rust_errno().write(__e as i32);
29+
-1
30+
}
31+
}
32+
}));
33+
assert!(((((*fd.borrow()) >= 0) as i32) != 0));
34+
assert!(
35+
(((match FdRegistry::with_fd((*fd.borrow()), |__fd| {
36+
Ptr::from_string_literal(b"hello world")
37+
.to_any()
38+
.reinterpret_cast::<u8>()
39+
.with_slice(11_usize, |__buf| nix::unistd::write(__fd, __buf))
40+
}) {
41+
Ok(__n) => __n as isize,
42+
Err(__e) => {
43+
libcc2rs::cpp2rust_errno().write(__e as i32);
44+
-1
45+
}
46+
} == 11_isize) as i32)
47+
!= 0)
48+
);
49+
assert!((((FdRegistry::close((*fd.borrow())) == 0) as i32) != 0));
50+
(*fd.borrow_mut()) = {
51+
let __mode = match &[].first() {
52+
Some(__m) => nix::sys::stat::Mode::from_bits_truncate(i32::get(__m) as ::libc::mode_t),
53+
None => nix::sys::stat::Mode::empty(),
54+
};
55+
match nix::fcntl::open(
56+
(*path.borrow()).to_rust_string().as_str(),
57+
nix::fcntl::OFlag::from_bits_retain(0),
58+
__mode,
59+
) {
60+
Ok(__ofd) => FdRegistry::register(__ofd),
61+
Err(__e) => {
62+
libcc2rs::cpp2rust_errno().write(__e as i32);
63+
-1
64+
}
65+
}
66+
};
67+
assert!(((((*fd.borrow()) >= 0) as i32) != 0));
68+
let buf: Value<Box<[u8]>> = Rc::new(RefCell::new(
69+
(0..16).map(|_| <u8>::default()).collect::<Box<[u8]>>(),
70+
));
71+
{
72+
((buf.as_pointer() as Ptr<u8>) as Ptr<u8>)
73+
.to_any()
74+
.memset((0) as u8, ::std::mem::size_of::<[u8; 16]>() as usize);
75+
((buf.as_pointer() as Ptr<u8>) as Ptr<u8>).to_any().clone()
76+
};
77+
assert!(
78+
(((match FdRegistry::with_fd((*fd.borrow()), |__fd| {
79+
((buf.as_pointer() as Ptr<u8>) as Ptr<u8>)
80+
.to_any()
81+
.reinterpret_cast::<u8>()
82+
.with_slice_mut(::std::mem::size_of::<[u8; 16]>(), |__buf| {
83+
nix::unistd::read(__fd, __buf)
84+
})
85+
}) {
86+
Ok(__n) => __n as isize,
87+
Err(__e) => {
88+
libcc2rs::cpp2rust_errno().write(__e as i32);
89+
-1
90+
}
91+
} == 11_isize) as i32)
92+
!= 0)
93+
);
94+
assert!(
95+
((({
96+
let mut __it1 = (buf.as_pointer() as Ptr<u8>).to_c_string_iterator();
97+
let mut __it2 = Ptr::from_string_literal(b"hello world").to_c_string_iterator();
98+
loop {
99+
let __c1 = __it1.next();
100+
let __c2 = __it2.next();
101+
if __c1 != __c2 {
102+
break (__c1.unwrap_or(0) as i32) - (__c2.unwrap_or(0) as i32);
103+
}
104+
if __c1.is_none() {
105+
break 0;
106+
}
107+
}
108+
} == 0) as i32)
109+
!= 0)
110+
);
111+
assert!(
112+
(((match FdRegistry::with_fd((*fd.borrow()), |__fd| {
113+
((buf.as_pointer() as Ptr<u8>) as Ptr<u8>)
114+
.to_any()
115+
.reinterpret_cast::<u8>()
116+
.with_slice_mut(::std::mem::size_of::<[u8; 16]>(), |__buf| {
117+
nix::unistd::read(__fd, __buf)
118+
})
119+
}) {
120+
Ok(__n) => __n as isize,
121+
Err(__e) => {
122+
libcc2rs::cpp2rust_errno().write(__e as i32);
123+
-1
124+
}
125+
} == 0_isize) as i32)
126+
!= 0)
127+
);
128+
assert!((((FdRegistry::close((*fd.borrow())) == 0) as i32) != 0));
129+
assert!(
130+
(((match nix::unistd::unlink((*path.borrow()).to_rust_string().as_str()) {
131+
Ok(()) => 0,
132+
Err(__e) => {
133+
libcc2rs::cpp2rust_errno().write(__e as i32);
134+
-1
135+
}
136+
} == 0) as i32)
137+
!= 0)
138+
);
139+
return 0;
140+
}

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

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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_fd_io_test.tmp".as_ptr().cast_mut()).cast_const();
17+
let mut fd: i32 =
18+
(unsafe { libc::open(path as *const i8, (((1) | (64)) | (512)) as i32, (420)) });
19+
assert!(((((fd) >= (0)) as i32) != 0));
20+
assert!(
21+
((((libc::write(
22+
fd,
23+
(c"hello world".as_ptr().cast_mut() as *const libc::c_char as *const ::libc::c_void),
24+
11_usize
25+
)) == (11_isize)) as i32)
26+
!= 0)
27+
);
28+
assert!(((((libc::close(fd)) == (0)) as i32) != 0));
29+
fd = (unsafe { libc::open(path as *const i8, 0 as i32) });
30+
assert!(((((fd) >= (0)) as i32) != 0));
31+
let mut buf: [libc::c_char; 16] = [(0 as libc::c_char); 16];
32+
{
33+
let byte_0 = (buf.as_mut_ptr() as *mut libc::c_char as *mut ::libc::c_void) as *mut u8;
34+
for offset in 0..::std::mem::size_of::<[libc::c_char; 16]>() {
35+
*byte_0.offset(offset as isize) = 0 as u8;
36+
}
37+
(buf.as_mut_ptr() as *mut libc::c_char as *mut ::libc::c_void)
38+
};
39+
assert!(
40+
((((libc::read(
41+
fd,
42+
(buf.as_mut_ptr() as *mut libc::c_char as *mut ::libc::c_void),
43+
::std::mem::size_of::<[libc::c_char; 16]>()
44+
)) == (11_isize)) as i32)
45+
!= 0)
46+
);
47+
assert!(
48+
((((libc::strcmp(
49+
(buf.as_mut_ptr()).cast_const(),
50+
(c"hello world".as_ptr().cast_mut()).cast_const()
51+
)) == (0)) as i32)
52+
!= 0)
53+
);
54+
assert!(
55+
((((libc::read(
56+
fd,
57+
(buf.as_mut_ptr() as *mut libc::c_char as *mut ::libc::c_void),
58+
::std::mem::size_of::<[libc::c_char; 16]>()
59+
)) == (0_isize)) as i32)
60+
!= 0)
61+
);
62+
assert!(((((libc::close(fd)) == (0)) as i32) != 0));
63+
assert!(((((libc::unlink(path)) == (0)) as i32) != 0));
64+
return 0;
65+
}

0 commit comments

Comments
 (0)