Skip to content

Commit eb19e52

Browse files
committed
Add var args tests
1 parent 582d5d3 commit eb19e52

2 files changed

Lines changed: 149 additions & 7 deletions

File tree

tests/unit/out/unsafe/unistd.rs

Lines changed: 105 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -225,19 +225,115 @@ pub unsafe fn test_ftruncate_5() {
225225
assert!(((((libc::fclose(fp)) == (0)) as i32) != 0));
226226
libc::unlink(path as *const i8);
227227
}
228-
pub unsafe fn test_isatty_6() {
228+
pub unsafe fn test_open_6() {
229+
let mut path: *const u8 = (b"/tmp/cpp2rust_open_test.tmp\0".as_ptr().cast_mut()).cast_const();
230+
let mut fd: i32 =
231+
(unsafe { libc::open(path as *const i8, (((1) | (64)) | (512)) as i32, (420)) });
232+
assert!(((((fd) >= (0)) as i32) != 0));
233+
assert!(
234+
((((libc::write(
235+
fd,
236+
(b"hello world\0".as_ptr().cast_mut() as *const u8 as *const ::libc::c_void),
237+
11_u64 as usize
238+
) as i64)
239+
== (11_i64)) as i32)
240+
!= 0)
241+
);
242+
assert!(((((libc::close(fd)) == (0)) as i32) != 0));
243+
fd = (unsafe { libc::open(path as *const i8, 0 as i32) });
244+
assert!(((((fd) >= (0)) as i32) != 0));
245+
let mut buf: [u8; 16] = [
246+
0_u8, 0_u8, 0_u8, 0_u8, 0_u8, 0_u8, 0_u8, 0_u8, 0_u8, 0_u8, 0_u8, 0_u8, 0_u8, 0_u8, 0_u8,
247+
0_u8,
248+
];
249+
assert!(
250+
((((libc::read(
251+
fd,
252+
(buf.as_mut_ptr() as *mut u8 as *mut ::libc::c_void),
253+
16_u64 as usize
254+
) as i64)
255+
== (11_i64)) as i32)
256+
!= 0)
257+
);
258+
assert!(
259+
(((({
260+
let sa = core::slice::from_raw_parts(
261+
(buf.as_mut_ptr() as *const u8 as *const ::libc::c_void) as *const u8,
262+
11_u64 as usize,
263+
);
264+
let sb = core::slice::from_raw_parts(
265+
(b"hello world\0".as_ptr().cast_mut() as *const u8 as *const ::libc::c_void)
266+
as *const u8,
267+
11_u64 as usize,
268+
);
269+
let mut diff = 0_i32;
270+
for (x, y) in sa.iter().zip(sb.iter()) {
271+
if x != y {
272+
diff = (*x as i32) - (*y as i32);
273+
break;
274+
}
275+
}
276+
diff
277+
}) == (0)) as i32)
278+
!= 0)
279+
);
280+
assert!(((((libc::close(fd)) == (0)) as i32) != 0));
281+
libc::unlink(path as *const i8);
282+
}
283+
pub unsafe fn test_fcntl_7() {
284+
let mut path: *const u8 = (b"/tmp/cpp2rust_fcntl_test.tmp\0".as_ptr().cast_mut()).cast_const();
285+
let mut fd: i32 =
286+
(unsafe { libc::open(path as *const i8, (((1) | (64)) | (512)) as i32, (420)) });
287+
assert!(((((fd) >= (0)) as i32) != 0));
288+
let mut flags: i32 = (unsafe { libc::fcntl(fd as i32, 3 as i32) });
289+
assert!(((((flags) != (-1_i32)) as i32) != 0));
290+
assert!(
291+
((((unsafe { libc::fcntl(fd as i32, 4 as i32, ((flags) | (1024)),) }) == (0)) as i32) != 0)
292+
);
293+
assert!((((((unsafe { libc::fcntl(fd as i32, 3 as i32,) }) & (1024)) != (0)) as i32) != 0));
294+
assert!(((((libc::close(fd)) == (0)) as i32) != 0));
295+
libc::unlink(path as *const i8);
296+
}
297+
pub unsafe fn test_ioctl_8() {
298+
let mut fds: [i32; 2] = [0_i32; 2];
299+
assert!(((((libc::pipe(fds.as_mut_ptr())) == (0)) as i32) != 0));
300+
assert!(
301+
((((libc::write(
302+
fds[(1) as usize],
303+
(b"abcd\0".as_ptr().cast_mut() as *const u8 as *const ::libc::c_void),
304+
4_u64 as usize
305+
) as i64)
306+
== (4_i64)) as i32)
307+
!= 0)
308+
);
309+
let mut navail: i32 = 0;
310+
assert!(
311+
((((unsafe {
312+
libc::ioctl(
313+
fds[(0) as usize] as i32,
314+
21531_u64 as u64,
315+
(&mut navail as *mut i32),
316+
)
317+
}) == (0)) as i32)
318+
!= 0)
319+
);
320+
assert!(((((navail) == (4)) as i32) != 0));
321+
assert!(((((libc::close(fds[(0) as usize])) == (0)) as i32) != 0));
322+
assert!(((((libc::close(fds[(1) as usize])) == (0)) as i32) != 0));
323+
}
324+
pub unsafe fn test_isatty_9() {
229325
printf(
230326
(b"%d\n\0".as_ptr().cast_mut()).cast_const() as *const i8,
231327
libc::isatty(0),
232328
);
233329
}
234-
pub unsafe fn test_geteuid_7() {
330+
pub unsafe fn test_geteuid_10() {
235331
printf(
236332
(b"%u\n\0".as_ptr().cast_mut()).cast_const() as *const i8,
237333
libc::geteuid(),
238334
);
239335
}
240-
pub unsafe fn test_gethostname_8() {
336+
pub unsafe fn test_gethostname_11() {
241337
let mut name: [u8; 256] = [0_u8; 256];
242338
assert!(
243339
((((libc::gethostname(
@@ -263,8 +359,11 @@ unsafe fn main_0() -> i32 {
263359
(unsafe { test_unlink_3() });
264360
(unsafe { test_pipe_4() });
265361
(unsafe { test_ftruncate_5() });
266-
(unsafe { test_isatty_6() });
267-
(unsafe { test_geteuid_7() });
268-
(unsafe { test_gethostname_8() });
362+
(unsafe { test_open_6() });
363+
(unsafe { test_fcntl_7() });
364+
(unsafe { test_ioctl_8() });
365+
(unsafe { test_isatty_9() });
366+
(unsafe { test_geteuid_10() });
367+
(unsafe { test_gethostname_11() });
269368
return 0;
270369
}

tests/unit/unistd.c

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
// no-compile: refcount
1+
// translation-fail: refcount
22
#include <assert.h>
3+
#include <fcntl.h>
34
#include <stdio.h>
45
#include <string.h>
6+
#include <sys/ioctl.h>
57
#include <sys/types.h>
68
#include <unistd.h>
79

@@ -87,6 +89,44 @@ static void test_ftruncate(void) {
8789
unlink(path);
8890
}
8991

92+
static void test_open(void) {
93+
const char *path = "/tmp/cpp2rust_open_test.tmp";
94+
int fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
95+
assert(fd >= 0);
96+
assert(write(fd, "hello world", 11) == 11);
97+
assert(close(fd) == 0);
98+
fd = open(path, O_RDONLY);
99+
assert(fd >= 0);
100+
char buf[16] = {0};
101+
assert(read(fd, buf, 16) == 11);
102+
assert(memcmp(buf, "hello world", 11) == 0);
103+
assert(close(fd) == 0);
104+
unlink(path);
105+
}
106+
107+
static void test_fcntl(void) {
108+
const char *path = "/tmp/cpp2rust_fcntl_test.tmp";
109+
int fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
110+
assert(fd >= 0);
111+
int flags = fcntl(fd, F_GETFL);
112+
assert(flags != -1);
113+
assert(fcntl(fd, F_SETFL, flags | O_APPEND) == 0);
114+
assert((fcntl(fd, F_GETFL) & O_APPEND) != 0);
115+
assert(close(fd) == 0);
116+
unlink(path);
117+
}
118+
119+
static void test_ioctl(void) {
120+
int fds[2];
121+
assert(pipe(fds) == 0);
122+
assert(write(fds[1], "abcd", 4) == 4);
123+
int navail = 0;
124+
assert(ioctl(fds[0], FIONREAD, &navail) == 0);
125+
assert(navail == 4);
126+
assert(close(fds[0]) == 0);
127+
assert(close(fds[1]) == 0);
128+
}
129+
90130
static void test_isatty(void) { printf("%d\n", isatty(0)); }
91131

92132
static void test_geteuid(void) { printf("%u\n", geteuid()); }
@@ -104,6 +144,9 @@ int main(void) {
104144
test_unlink();
105145
test_pipe();
106146
test_ftruncate();
147+
test_open();
148+
test_fcntl();
149+
test_ioctl();
107150
test_isatty();
108151
test_geteuid();
109152
test_gethostname();

0 commit comments

Comments
 (0)