diff --git a/rules/stdio/tgt_refcount.rs b/rules/stdio/tgt_refcount.rs index 3b914c0c..0dba24cb 100644 --- a/rules/stdio/tgt_refcount.rs +++ b/rules/stdio/tgt_refcount.rs @@ -95,6 +95,124 @@ fn f10() -> Ptr<::std::fs::File> { libcc2rs::cin() } +fn f11(a0: i32, a1: Ptr<::std::fs::File>) -> i32 { + let __c = a0 as u8; + match a1.with_mut(|__f| ::std::io::Write::write_all(__f, &[__c])) { + Ok(()) => __c as i32, + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e.raw_os_error().unwrap_or(::libc::EIO)); + -1 + } + } +} + +fn f12(a0: Ptr, a1: Ptr<::std::fs::File>) -> i32 { + let __bytes: Vec = a0.to_c_string_iterator().collect(); + match a1.with_mut(|__f| ::std::io::Write::write_all(__f, &__bytes)) { + Ok(()) => 0, + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e.raw_os_error().unwrap_or(::libc::EIO)); + -1 + } + } +} + +fn f13(a0: Ptr) -> i32 { + let __bytes: Vec = a0.to_c_string_iterator().collect(); + match libcc2rs::cout().with_mut(|__f| { + ::std::io::Write::write_all(__f, &__bytes) + .and_then(|_| ::std::io::Write::write_all(__f, b"\n")) + }) { + Ok(()) => 0, + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e.raw_os_error().unwrap_or(::libc::EIO)); + -1 + } + } +} + +fn f17(a0: Ptr, a1: i32, a2: Ptr<::std::fs::File>) -> Ptr { + let __buf = a0.clone(); + let __n = a1; + let __stream = a2.clone(); + if __n <= 0 { + Ptr::null() + } else { + let __max = (__n - 1) as usize; + let mut __dst = __buf.clone(); + let mut __count: usize = 0; + let mut __failed = false; + while __count < __max { + let mut __b: [u8; 1] = [0]; + match __stream.with_mut(|__f| ::std::io::Read::read(__f, &mut __b)) { + Ok(0) => break, + Ok(_) => { + __dst.write(__b[0]); + __dst += 1; + __count += 1; + if __b[0] == b'\n' { + break; + } + } + Err(__e) => { + if __e.kind() != ::std::io::ErrorKind::Interrupted { + libcc2rs::cpp2rust_errno().write(__e.raw_os_error().unwrap_or(::libc::EIO)); + __failed = true; + break; + } + } + } + } + if __failed || __count == 0 { + Ptr::null() + } else { + __dst.write(0); + __buf + } + } +} + +fn f18(a0: Ptr, a1: Ptr, a2: Ptr<::std::fs::File>) -> Ptr<::std::fs::File> { + let __stream = a2.clone(); + let __new = match a1.to_rust_string().as_str() { + "rb" => ::std::fs::OpenOptions::new() + .read(true) + .open(a0.to_rust_string()), + "wb" => ::std::fs::OpenOptions::new() + .write(true) + .create(true) + .truncate(true) + .open(a0.to_rust_string()), + _ => panic!("unsupported mode"), + }; + match __new { + Ok(__f) => { + __stream.write(__f); + __stream + } + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e.raw_os_error().unwrap_or(::libc::EIO)); + Ptr::null() + } + } +} + +fn f19(a0: Ptr<::std::fs::File>, a1: i64, a2: i32) -> i32 { + let __r = a0.with_mut(|__f| match a2 { + 0 => ::std::io::Seek::seek(__f, ::std::io::SeekFrom::Start(a1 as u64)), + 1 => ::std::io::Seek::seek(__f, ::std::io::SeekFrom::Current(a1)), + 2 => ::std::io::Seek::seek(__f, ::std::io::SeekFrom::End(a1)), + _ => Err(::std::io::Error::other("unsupported whence for fseeko.")), + }); + match __r { + Ok(_) => 0, + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e.raw_os_error().unwrap_or(::libc::EINVAL)); + -1 + } + } +} + fn f21(a0: Ptr, a1: usize, a2: Ptr, va: &[VaArg]) -> i32 { panic!( "snprintf is not supported in the refcount model (buf_is_null={}, size={}, fmt={:?}, varargs={})", @@ -104,3 +222,30 @@ fn f21(a0: Ptr, a1: usize, a2: Ptr, va: &[VaArg]) -> i32 { va.len() ) } + +fn f22(a0: Ptr, a1: Ptr) -> i32 { + match ::std::fs::rename(a0.to_rust_string(), a1.to_rust_string()) { + Ok(()) => 0, + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e.raw_os_error().unwrap_or(::libc::EIO)); + -1 + } + } +} + +fn f23(a0: Ptr<::std::fs::File>) -> i32 { + let mut __b: [u8; 1] = [0]; + match a0.with_mut(|__f| ::std::io::Read::read(__f, &mut __b)) { + Ok(0) => -1, + Ok(_) => __b[0] as i32, + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e.raw_os_error().unwrap_or(::libc::EIO)); + -1 + } + } +} + +fn f24(a0: Ptr<::std::fs::File>, a1: Ptr, a2: i32, a3: usize) -> i32 { + /* std::fs::File is unbuffered */ + 0 +} diff --git a/tests/unit/out/refcount/stdio_nofd.rs b/tests/unit/out/refcount/stdio_nofd.rs new file mode 100644 index 00000000..7982071e --- /dev/null +++ b/tests/unit/out/refcount/stdio_nofd.rs @@ -0,0 +1,953 @@ +extern crate libcc2rs; +use libcc2rs::*; +use std::cell::RefCell; +use std::collections::BTreeMap; +use std::io::prelude::*; +use std::io::{Read, Seek, Write}; +use std::os::fd::AsFd; +use std::rc::{Rc, Weak}; +pub fn test_fputc_fputs_0() { + let path: Value> = Rc::new(RefCell::new(Ptr::from_string_literal( + b"/tmp/cpp2rust_stdio_nofd_puts.tmp", + ))); + let fp: Value> = Rc::new(RefCell::new( + match Ptr::from_string_literal(b"wb").to_rust_string() { + v if v == "rb" => std::fs::OpenOptions::new() + .read(true) + .open((*path.borrow()).to_rust_string()) + .ok() + .map_or(Ptr::null(), |f| Ptr::alloc(f)), + v if v == "wb" => std::fs::OpenOptions::new() + .write(true) + .create(true) + .truncate(true) + .open((*path.borrow()).to_rust_string()) + .ok() + .map_or(Ptr::null(), |f| Ptr::alloc(f)), + _ => panic!("unsupported mode"), + }, + )); + assert!((((!((*fp.borrow()).is_null())) as i32) != 0)); + assert!( + ((({ + let __c = ('A' as i32) as u8; + match (*fp.borrow()).with_mut(|__f| ::std::io::Write::write_all(__f, &[__c])) { + Ok(()) => __c as i32, + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e.raw_os_error().unwrap_or(::libc::EIO)); + -1 + } + } + } == ('A' as i32)) as i32) + != 0) + ); + assert!( + ((({ + let __bytes: Vec = Ptr::from_string_literal(b"BCD\n") + .to_c_string_iterator() + .collect(); + match (*fp.borrow()).with_mut(|__f| ::std::io::Write::write_all(__f, &__bytes)) { + Ok(()) => 0, + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e.raw_os_error().unwrap_or(::libc::EIO)); + -1 + } + } + } >= 0) as i32) + != 0) + ); + assert!( + ((({ + (*fp.borrow()).delete(); + 0 + } == 0) as i32) + != 0) + ); + (*fp.borrow_mut()) = match Ptr::from_string_literal(b"rb").to_rust_string() { + v if v == "rb" => std::fs::OpenOptions::new() + .read(true) + .open((*path.borrow()).to_rust_string()) + .ok() + .map_or(Ptr::null(), |f| Ptr::alloc(f)), + v if v == "wb" => std::fs::OpenOptions::new() + .write(true) + .create(true) + .truncate(true) + .open((*path.borrow()).to_rust_string()) + .ok() + .map_or(Ptr::null(), |f| Ptr::alloc(f)), + _ => panic!("unsupported mode"), + }; + assert!((((!((*fp.borrow()).is_null())) as i32) != 0)); + let buf: Value> = Rc::new(RefCell::new(Box::new([ + 0_u8, + ::default(), + ::default(), + ::default(), + ::default(), + ::default(), + ::default(), + ::default(), + ::default(), + ::default(), + ::default(), + ::default(), + ::default(), + ::default(), + ::default(), + ::default(), + ]))); + assert!( + ((({ + let __a0 = ((buf.as_pointer() as Ptr) as Ptr).to_any(); + let __a1 = 1_usize; + let __a2 = 16_usize; + let __a3 = (*fp.borrow()).clone(); + libcc2rs::fread_refcount(__a0, __a1, __a2, __a3) + } == 5_usize) as i32) + != 0) + ); + assert!( + (((((buf.as_pointer() as Ptr::) as Ptr::) + .to_any() + .memcmp(&Ptr::from_string_literal(b"ABCD\n").to_any(), 5_usize) + == 0) as i32) + != 0) + ); + assert!( + ((({ + (*fp.borrow()).delete(); + 0 + } == 0) as i32) + != 0) + ); + assert!( + (((match nix::unistd::unlink((*path.borrow()).to_rust_string().as_str()) { + Ok(()) => 0, + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } == 0) as i32) + != 0) + ); +} +pub fn test_puts_1() { + assert!( + ((({ + let __bytes: Vec = Ptr::from_string_literal(b"hello from puts") + .to_c_string_iterator() + .collect(); + match libcc2rs::cout().with_mut(|__f| { + ::std::io::Write::write_all(__f, &__bytes) + .and_then(|_| ::std::io::Write::write_all(__f, b"\n")) + }) { + Ok(()) => 0, + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e.raw_os_error().unwrap_or(::libc::EIO)); + -1 + } + } + } >= 0) as i32) + != 0) + ); +} +pub fn test_fgets_getc_2() { + let path: Value> = Rc::new(RefCell::new(Ptr::from_string_literal( + b"/tmp/cpp2rust_stdio_nofd_gets.tmp", + ))); + let fp: Value> = Rc::new(RefCell::new( + match Ptr::from_string_literal(b"wb").to_rust_string() { + v if v == "rb" => std::fs::OpenOptions::new() + .read(true) + .open((*path.borrow()).to_rust_string()) + .ok() + .map_or(Ptr::null(), |f| Ptr::alloc(f)), + v if v == "wb" => std::fs::OpenOptions::new() + .write(true) + .create(true) + .truncate(true) + .open((*path.borrow()).to_rust_string()) + .ok() + .map_or(Ptr::null(), |f| Ptr::alloc(f)), + _ => panic!("unsupported mode"), + }, + )); + assert!((((!((*fp.borrow()).is_null())) as i32) != 0)); + assert!( + ((({ + let __bytes: Vec = Ptr::from_string_literal(b"line1\nline2\n") + .to_c_string_iterator() + .collect(); + match (*fp.borrow()).with_mut(|__f| ::std::io::Write::write_all(__f, &__bytes)) { + Ok(()) => 0, + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e.raw_os_error().unwrap_or(::libc::EIO)); + -1 + } + } + } >= 0) as i32) + != 0) + ); + assert!( + ((({ + (*fp.borrow()).delete(); + 0 + } == 0) as i32) + != 0) + ); + (*fp.borrow_mut()) = match Ptr::from_string_literal(b"rb").to_rust_string() { + v if v == "rb" => std::fs::OpenOptions::new() + .read(true) + .open((*path.borrow()).to_rust_string()) + .ok() + .map_or(Ptr::null(), |f| Ptr::alloc(f)), + v if v == "wb" => std::fs::OpenOptions::new() + .write(true) + .create(true) + .truncate(true) + .open((*path.borrow()).to_rust_string()) + .ok() + .map_or(Ptr::null(), |f| Ptr::alloc(f)), + _ => panic!("unsupported mode"), + }; + assert!((((!((*fp.borrow()).is_null())) as i32) != 0)); + let buf: Value> = Rc::new(RefCell::new( + (0..8).map(|_| ::default()).collect::>(), + )); + assert!( + (((!(({ + let __buf = (buf.as_pointer() as Ptr).clone(); + let __n = 8; + let __stream = (*fp.borrow()).clone(); + if __n <= 0 { + Ptr::null() + } else { + let __max = (__n - 1) as usize; + let mut __dst = __buf.clone(); + let mut __count: usize = 0; + let mut __failed = false; + while __count < __max { + let mut __b: [u8; 1] = [0]; + match __stream.with_mut(|__f| ::std::io::Read::read(__f, &mut __b)) { + Ok(0) => break, + Ok(_) => { + __dst.write(__b[0]); + __dst += 1; + __count += 1; + if __b[0] == b'\n' { + break; + } + } + Err(__e) => { + if __e.kind() != ::std::io::ErrorKind::Interrupted { + libcc2rs::cpp2rust_errno() + .write(__e.raw_os_error().unwrap_or(::libc::EIO)); + __failed = true; + break; + } + } + } + } + if __failed || __count == 0 { + Ptr::null() + } else { + __dst.write(0); + __buf + } + } + }) + .is_null())) as i32) + != 0) + ); + assert!( + (((((buf.as_pointer() as Ptr::) as Ptr::) + .to_any() + .memcmp(&Ptr::from_string_literal(b"line1\n").to_any(), 7_usize) + == 0) as i32) + != 0) + ); + assert!( + ((({ + let mut __b: [u8; 1] = [0]; + match (*fp.borrow()).with_mut(|__f| ::std::io::Read::read(__f, &mut __b)) { + Ok(0) => -1, + Ok(_) => __b[0] as i32, + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e.raw_os_error().unwrap_or(::libc::EIO)); + -1 + } + } + } == ('l' as i32)) as i32) + != 0) + ); + assert!( + (((!(({ + let __buf = (buf.as_pointer() as Ptr).clone(); + let __n = 4; + let __stream = (*fp.borrow()).clone(); + if __n <= 0 { + Ptr::null() + } else { + let __max = (__n - 1) as usize; + let mut __dst = __buf.clone(); + let mut __count: usize = 0; + let mut __failed = false; + while __count < __max { + let mut __b: [u8; 1] = [0]; + match __stream.with_mut(|__f| ::std::io::Read::read(__f, &mut __b)) { + Ok(0) => break, + Ok(_) => { + __dst.write(__b[0]); + __dst += 1; + __count += 1; + if __b[0] == b'\n' { + break; + } + } + Err(__e) => { + if __e.kind() != ::std::io::ErrorKind::Interrupted { + libcc2rs::cpp2rust_errno() + .write(__e.raw_os_error().unwrap_or(::libc::EIO)); + __failed = true; + break; + } + } + } + } + if __failed || __count == 0 { + Ptr::null() + } else { + __dst.write(0); + __buf + } + } + }) + .is_null())) as i32) + != 0) + ); + assert!( + (((((buf.as_pointer() as Ptr::) as Ptr::) + .to_any() + .memcmp(&Ptr::from_string_literal(b"ine").to_any(), 4_usize) + == 0) as i32) + != 0) + ); + assert!( + (((!(({ + let __buf = (buf.as_pointer() as Ptr).clone(); + let __n = 8; + let __stream = (*fp.borrow()).clone(); + if __n <= 0 { + Ptr::null() + } else { + let __max = (__n - 1) as usize; + let mut __dst = __buf.clone(); + let mut __count: usize = 0; + let mut __failed = false; + while __count < __max { + let mut __b: [u8; 1] = [0]; + match __stream.with_mut(|__f| ::std::io::Read::read(__f, &mut __b)) { + Ok(0) => break, + Ok(_) => { + __dst.write(__b[0]); + __dst += 1; + __count += 1; + if __b[0] == b'\n' { + break; + } + } + Err(__e) => { + if __e.kind() != ::std::io::ErrorKind::Interrupted { + libcc2rs::cpp2rust_errno() + .write(__e.raw_os_error().unwrap_or(::libc::EIO)); + __failed = true; + break; + } + } + } + } + if __failed || __count == 0 { + Ptr::null() + } else { + __dst.write(0); + __buf + } + } + }) + .is_null())) as i32) + != 0) + ); + assert!( + (((((buf.as_pointer() as Ptr::) as Ptr::) + .to_any() + .memcmp(&Ptr::from_string_literal(b"2\n").to_any(), 3_usize) + == 0) as i32) + != 0) + ); + assert!( + (((({ + let __buf = (buf.as_pointer() as Ptr).clone(); + let __n = 8; + let __stream = (*fp.borrow()).clone(); + if __n <= 0 { + Ptr::null() + } else { + let __max = (__n - 1) as usize; + let mut __dst = __buf.clone(); + let mut __count: usize = 0; + let mut __failed = false; + while __count < __max { + let mut __b: [u8; 1] = [0]; + match __stream.with_mut(|__f| ::std::io::Read::read(__f, &mut __b)) { + Ok(0) => break, + Ok(_) => { + __dst.write(__b[0]); + __dst += 1; + __count += 1; + if __b[0] == b'\n' { + break; + } + } + Err(__e) => { + if __e.kind() != ::std::io::ErrorKind::Interrupted { + libcc2rs::cpp2rust_errno() + .write(__e.raw_os_error().unwrap_or(::libc::EIO)); + __failed = true; + break; + } + } + } + } + if __failed || __count == 0 { + Ptr::null() + } else { + __dst.write(0); + __buf + } + } + }) + .is_null()) as i32) + != 0) + ); + assert!( + ((({ + let mut __b: [u8; 1] = [0]; + match (*fp.borrow()).with_mut(|__f| ::std::io::Read::read(__f, &mut __b)) { + Ok(0) => -1, + Ok(_) => __b[0] as i32, + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e.raw_os_error().unwrap_or(::libc::EIO)); + -1 + } + } + } == (-1_i32)) as i32) + != 0) + ); + assert!( + ((({ + (*fp.borrow()).delete(); + 0 + } == 0) as i32) + != 0) + ); + assert!( + (((match nix::unistd::unlink((*path.borrow()).to_rust_string().as_str()) { + Ok(()) => 0, + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } == 0) as i32) + != 0) + ); +} +pub fn test_freopen_3() { + let path: Value> = Rc::new(RefCell::new(Ptr::from_string_literal( + b"/tmp/cpp2rust_stdio_nofd_reopen.tmp", + ))); + let fp: Value> = Rc::new(RefCell::new( + match Ptr::from_string_literal(b"wb").to_rust_string() { + v if v == "rb" => std::fs::OpenOptions::new() + .read(true) + .open((*path.borrow()).to_rust_string()) + .ok() + .map_or(Ptr::null(), |f| Ptr::alloc(f)), + v if v == "wb" => std::fs::OpenOptions::new() + .write(true) + .create(true) + .truncate(true) + .open((*path.borrow()).to_rust_string()) + .ok() + .map_or(Ptr::null(), |f| Ptr::alloc(f)), + _ => panic!("unsupported mode"), + }, + )); + assert!((((!((*fp.borrow()).is_null())) as i32) != 0)); + assert!( + ((({ + let __bytes: Vec = Ptr::from_string_literal(b"hello") + .to_c_string_iterator() + .collect(); + match (*fp.borrow()).with_mut(|__f| ::std::io::Write::write_all(__f, &__bytes)) { + Ok(()) => 0, + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e.raw_os_error().unwrap_or(::libc::EIO)); + -1 + } + } + } >= 0) as i32) + != 0) + ); + let fp2: Value> = Rc::new(RefCell::new({ + let __stream = (*fp.borrow()).clone(); + let __new = match Ptr::from_string_literal(b"rb").to_rust_string().as_str() { + "rb" => ::std::fs::OpenOptions::new() + .read(true) + .open((*path.borrow()).to_rust_string()), + "wb" => ::std::fs::OpenOptions::new() + .write(true) + .create(true) + .truncate(true) + .open((*path.borrow()).to_rust_string()), + _ => panic!("unsupported mode"), + }; + match __new { + Ok(__f) => { + __stream.write(__f); + __stream + } + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e.raw_os_error().unwrap_or(::libc::EIO)); + Ptr::null() + } + } + })); + assert!((((!((*fp2.borrow()).is_null())) as i32) != 0)); + let buf: Value> = Rc::new(RefCell::new(Box::new([ + 0_u8, + ::default(), + ::default(), + ::default(), + ::default(), + ::default(), + ::default(), + ::default(), + ]))); + assert!( + ((({ + let __a0 = ((buf.as_pointer() as Ptr) as Ptr).to_any(); + let __a1 = 1_usize; + let __a2 = 8_usize; + let __a3 = (*fp2.borrow()).clone(); + libcc2rs::fread_refcount(__a0, __a1, __a2, __a3) + } == 5_usize) as i32) + != 0) + ); + assert!( + (((((buf.as_pointer() as Ptr::) as Ptr::) + .to_any() + .memcmp(&Ptr::from_string_literal(b"hello").to_any(), 5_usize) + == 0) as i32) + != 0) + ); + assert!( + ((({ + (*fp2.borrow()).delete(); + 0 + } == 0) as i32) + != 0) + ); + assert!( + (((match nix::unistd::unlink((*path.borrow()).to_rust_string().as_str()) { + Ok(()) => 0, + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } == 0) as i32) + != 0) + ); +} +pub fn test_fseeko_4() { + let path: Value> = Rc::new(RefCell::new(Ptr::from_string_literal( + b"/tmp/cpp2rust_stdio_nofd_seek.tmp", + ))); + let fp: Value> = Rc::new(RefCell::new( + match Ptr::from_string_literal(b"wb").to_rust_string() { + v if v == "rb" => std::fs::OpenOptions::new() + .read(true) + .open((*path.borrow()).to_rust_string()) + .ok() + .map_or(Ptr::null(), |f| Ptr::alloc(f)), + v if v == "wb" => std::fs::OpenOptions::new() + .write(true) + .create(true) + .truncate(true) + .open((*path.borrow()).to_rust_string()) + .ok() + .map_or(Ptr::null(), |f| Ptr::alloc(f)), + _ => panic!("unsupported mode"), + }, + )); + assert!((((!((*fp.borrow()).is_null())) as i32) != 0)); + assert!( + ((({ + let __bytes: Vec = Ptr::from_string_literal(b"hello world") + .to_c_string_iterator() + .collect(); + match (*fp.borrow()).with_mut(|__f| ::std::io::Write::write_all(__f, &__bytes)) { + Ok(()) => 0, + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e.raw_os_error().unwrap_or(::libc::EIO)); + -1 + } + } + } >= 0) as i32) + != 0) + ); + assert!( + ((({ + (*fp.borrow()).delete(); + 0 + } == 0) as i32) + != 0) + ); + (*fp.borrow_mut()) = match Ptr::from_string_literal(b"rb").to_rust_string() { + v if v == "rb" => std::fs::OpenOptions::new() + .read(true) + .open((*path.borrow()).to_rust_string()) + .ok() + .map_or(Ptr::null(), |f| Ptr::alloc(f)), + v if v == "wb" => std::fs::OpenOptions::new() + .write(true) + .create(true) + .truncate(true) + .open((*path.borrow()).to_rust_string()) + .ok() + .map_or(Ptr::null(), |f| Ptr::alloc(f)), + _ => panic!("unsupported mode"), + }; + assert!((((!((*fp.borrow()).is_null())) as i32) != 0)); + assert!( + ((({ + let __r = (*fp.borrow()).with_mut(|__f| match 0 { + 0 => ::std::io::Seek::seek(__f, ::std::io::SeekFrom::Start(6_i64 as u64)), + 1 => ::std::io::Seek::seek(__f, ::std::io::SeekFrom::Current(6_i64)), + 2 => ::std::io::Seek::seek(__f, ::std::io::SeekFrom::End(6_i64)), + _ => Err(::std::io::Error::other("unsupported whence for fseeko.")), + }); + match __r { + Ok(_) => 0, + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e.raw_os_error().unwrap_or(::libc::EINVAL)); + -1 + } + } + } == 0) as i32) + != 0) + ); + let buf: Value> = Rc::new(RefCell::new(Box::new([ + 0_u8, + ::default(), + ::default(), + ::default(), + ::default(), + ::default(), + ::default(), + ::default(), + ]))); + assert!( + ((({ + let __a0 = ((buf.as_pointer() as Ptr) as Ptr).to_any(); + let __a1 = 1_usize; + let __a2 = 5_usize; + let __a3 = (*fp.borrow()).clone(); + libcc2rs::fread_refcount(__a0, __a1, __a2, __a3) + } == 5_usize) as i32) + != 0) + ); + assert!( + (((((buf.as_pointer() as Ptr::) as Ptr::) + .to_any() + .memcmp(&Ptr::from_string_literal(b"world").to_any(), 5_usize) + == 0) as i32) + != 0) + ); + assert!( + ((({ + let __r = (*fp.borrow()).with_mut(|__f| match 2 { + 0 => ::std::io::Seek::seek(__f, ::std::io::SeekFrom::Start((-5_i32 as i64) as u64)), + 1 => ::std::io::Seek::seek(__f, ::std::io::SeekFrom::Current((-5_i32 as i64))), + 2 => ::std::io::Seek::seek(__f, ::std::io::SeekFrom::End((-5_i32 as i64))), + _ => Err(::std::io::Error::other("unsupported whence for fseeko.")), + }); + match __r { + Ok(_) => 0, + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e.raw_os_error().unwrap_or(::libc::EINVAL)); + -1 + } + } + } == 0) as i32) + != 0) + ); + assert!( + ((({ + let mut __b: [u8; 1] = [0]; + match (*fp.borrow()).with_mut(|__f| ::std::io::Read::read(__f, &mut __b)) { + Ok(0) => -1, + Ok(_) => __b[0] as i32, + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e.raw_os_error().unwrap_or(::libc::EIO)); + -1 + } + } + } == ('w' as i32)) as i32) + != 0) + ); + assert!( + ((({ + let __r = (*fp.borrow()).with_mut(|__f| match 1 { + 0 => ::std::io::Seek::seek(__f, ::std::io::SeekFrom::Start(1_i64 as u64)), + 1 => ::std::io::Seek::seek(__f, ::std::io::SeekFrom::Current(1_i64)), + 2 => ::std::io::Seek::seek(__f, ::std::io::SeekFrom::End(1_i64)), + _ => Err(::std::io::Error::other("unsupported whence for fseeko.")), + }); + match __r { + Ok(_) => 0, + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e.raw_os_error().unwrap_or(::libc::EINVAL)); + -1 + } + } + } == 0) as i32) + != 0) + ); + assert!( + ((({ + let mut __b: [u8; 1] = [0]; + match (*fp.borrow()).with_mut(|__f| ::std::io::Read::read(__f, &mut __b)) { + Ok(0) => -1, + Ok(_) => __b[0] as i32, + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e.raw_os_error().unwrap_or(::libc::EIO)); + -1 + } + } + } == ('r' as i32)) as i32) + != 0) + ); + assert!( + ((({ + (*fp.borrow()).delete(); + 0 + } == 0) as i32) + != 0) + ); + assert!( + (((match nix::unistd::unlink((*path.borrow()).to_rust_string().as_str()) { + Ok(()) => 0, + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } == 0) as i32) + != 0) + ); +} +pub fn test_rename_5() { + let from: Value> = Rc::new(RefCell::new(Ptr::from_string_literal( + b"/tmp/cpp2rust_stdio_nofd_from.tmp", + ))); + let to: Value> = Rc::new(RefCell::new(Ptr::from_string_literal( + b"/tmp/cpp2rust_stdio_nofd_to.tmp", + ))); + let fp: Value> = Rc::new(RefCell::new( + match Ptr::from_string_literal(b"wb").to_rust_string() { + v if v == "rb" => std::fs::OpenOptions::new() + .read(true) + .open((*from.borrow()).to_rust_string()) + .ok() + .map_or(Ptr::null(), |f| Ptr::alloc(f)), + v if v == "wb" => std::fs::OpenOptions::new() + .write(true) + .create(true) + .truncate(true) + .open((*from.borrow()).to_rust_string()) + .ok() + .map_or(Ptr::null(), |f| Ptr::alloc(f)), + _ => panic!("unsupported mode"), + }, + )); + assert!((((!((*fp.borrow()).is_null())) as i32) != 0)); + assert!( + ((({ + let __bytes: Vec = Ptr::from_string_literal(b"data") + .to_c_string_iterator() + .collect(); + match (*fp.borrow()).with_mut(|__f| ::std::io::Write::write_all(__f, &__bytes)) { + Ok(()) => 0, + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e.raw_os_error().unwrap_or(::libc::EIO)); + -1 + } + } + } >= 0) as i32) + != 0) + ); + assert!( + ((({ + (*fp.borrow()).delete(); + 0 + } == 0) as i32) + != 0) + ); + assert!( + (((match ::std::fs::rename( + (*from.borrow()).to_rust_string(), + (*to.borrow()).to_rust_string() + ) { + Ok(()) => 0, + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e.raw_os_error().unwrap_or(::libc::EIO)); + -1 + } + } == 0) as i32) + != 0) + ); + assert!( + ((((match Ptr::from_string_literal(b"rb").to_rust_string() { + v if v == "rb" => std::fs::OpenOptions::new() + .read(true) + .open((*from.borrow()).to_rust_string()) + .ok() + .map_or(Ptr::null(), |f| Ptr::alloc(f)), + v if v == "wb" => std::fs::OpenOptions::new() + .write(true) + .create(true) + .truncate(true) + .open((*from.borrow()).to_rust_string()) + .ok() + .map_or(Ptr::null(), |f| Ptr::alloc(f)), + _ => panic!("unsupported mode"), + }) + .is_null()) as i32) + != 0) + ); + (*fp.borrow_mut()) = match Ptr::from_string_literal(b"rb").to_rust_string() { + v if v == "rb" => std::fs::OpenOptions::new() + .read(true) + .open((*to.borrow()).to_rust_string()) + .ok() + .map_or(Ptr::null(), |f| Ptr::alloc(f)), + v if v == "wb" => std::fs::OpenOptions::new() + .write(true) + .create(true) + .truncate(true) + .open((*to.borrow()).to_rust_string()) + .ok() + .map_or(Ptr::null(), |f| Ptr::alloc(f)), + _ => panic!("unsupported mode"), + }; + assert!((((!((*fp.borrow()).is_null())) as i32) != 0)); + assert!( + ((({ + (*fp.borrow()).delete(); + 0 + } == 0) as i32) + != 0) + ); + assert!( + (((match ::std::fs::rename( + (*from.borrow()).to_rust_string(), + (*to.borrow()).to_rust_string() + ) { + Ok(()) => 0, + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e.raw_os_error().unwrap_or(::libc::EIO)); + -1 + } + } == -1_i32) as i32) + != 0) + ); + assert!( + (((match nix::unistd::unlink((*to.borrow()).to_rust_string().as_str()) { + Ok(()) => 0, + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } == 0) as i32) + != 0) + ); +} +pub fn test_setvbuf_6() { + let path: Value> = Rc::new(RefCell::new(Ptr::from_string_literal( + b"/tmp/cpp2rust_stdio_nofd_vbuf.tmp", + ))); + let fp: Value> = Rc::new(RefCell::new( + match Ptr::from_string_literal(b"wb").to_rust_string() { + v if v == "rb" => std::fs::OpenOptions::new() + .read(true) + .open((*path.borrow()).to_rust_string()) + .ok() + .map_or(Ptr::null(), |f| Ptr::alloc(f)), + v if v == "wb" => std::fs::OpenOptions::new() + .write(true) + .create(true) + .truncate(true) + .open((*path.borrow()).to_rust_string()) + .ok() + .map_or(Ptr::null(), |f| Ptr::alloc(f)), + _ => panic!("unsupported mode"), + }, + )); + assert!((((!((*fp.borrow()).is_null())) as i32) != 0)); + assert!((((/* std::fs::File is unbuffered */0 == 0) as i32) != 0)); + assert!( + ((({ + let __bytes: Vec = Ptr::from_string_literal(b"x") + .to_c_string_iterator() + .collect(); + match (*fp.borrow()).with_mut(|__f| ::std::io::Write::write_all(__f, &__bytes)) { + Ok(()) => 0, + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e.raw_os_error().unwrap_or(::libc::EIO)); + -1 + } + } + } >= 0) as i32) + != 0) + ); + assert!( + ((({ + (*fp.borrow()).delete(); + 0 + } == 0) as i32) + != 0) + ); + assert!( + (((match nix::unistd::unlink((*path.borrow()).to_rust_string().as_str()) { + Ok(()) => 0, + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } == 0) as i32) + != 0) + ); +} +pub fn main() { + std::process::exit(main_0()); +} +fn main_0() -> i32 { + ({ test_fputc_fputs_0() }); + ({ test_puts_1() }); + ({ test_fgets_getc_2() }); + ({ test_freopen_3() }); + ({ test_fseeko_4() }); + ({ test_rename_5() }); + ({ test_setvbuf_6() }); + return 0; +} diff --git a/tests/unit/out/unsafe/stdio_nofd.rs b/tests/unit/out/unsafe/stdio_nofd.rs new file mode 100644 index 00000000..b9595ed3 --- /dev/null +++ b/tests/unit/out/unsafe/stdio_nofd.rs @@ -0,0 +1,326 @@ +extern crate libc; +use libc::*; +extern crate libcc2rs; +use libcc2rs::*; +use std::collections::BTreeMap; +use std::io::{Read, Seek, Write}; +use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; +use std::rc::Rc; +pub unsafe fn test_fputc_fputs_0() { + let mut path: *const libc::c_char = + (c"/tmp/cpp2rust_stdio_nofd_puts.tmp".as_ptr().cast_mut()).cast_const(); + let mut fp: *mut ::libc::FILE = libc::fopen(path, (c"wb".as_ptr().cast_mut()).cast_const()); + assert!((((!((fp).is_null())) as i32) != 0)); + assert!(((((libc::fputc(('A' as i32), fp)) == ('A' as i32)) as i32) != 0)); + assert!( + ((((libc::fputs((c"BCD\n".as_ptr().cast_mut()).cast_const(), fp)) >= (0)) as i32) != 0) + ); + assert!(((((libc::fclose(fp)) == (0)) as i32) != 0)); + fp = libc::fopen(path, (c"rb".as_ptr().cast_mut()).cast_const()); + assert!((((!((fp).is_null())) as i32) != 0)); + let mut buf: [libc::c_char; 16] = [ + (0 as libc::c_char), + (0 as libc::c_char), + (0 as libc::c_char), + (0 as libc::c_char), + (0 as libc::c_char), + (0 as libc::c_char), + (0 as libc::c_char), + (0 as libc::c_char), + (0 as libc::c_char), + (0 as libc::c_char), + (0 as libc::c_char), + (0 as libc::c_char), + (0 as libc::c_char), + (0 as libc::c_char), + (0 as libc::c_char), + (0 as libc::c_char), + ]; + assert!( + ((((libcc2rs::fread_unsafe( + (buf.as_mut_ptr() as *mut libc::c_char as *mut ::libc::c_void), + 1_usize, + 16_usize, + fp + )) == (5_usize)) as i32) + != 0) + ); + assert!( + (((({ + let sa = core::slice::from_raw_parts( + (buf.as_mut_ptr() as *const libc::c_char as *const ::libc::c_void) as *const u8, + 5_usize as usize, + ); + let sb = core::slice::from_raw_parts( + (c"ABCD\n".as_ptr().cast_mut() as *const libc::c_char as *const ::libc::c_void) + as *const u8, + 5_usize as usize, + ); + let mut diff = 0_i32; + for (x, y) in sa.iter().zip(sb.iter()) { + if x != y { + diff = (*x as i32) - (*y as i32); + break; + } + } + diff + }) == (0)) as i32) + != 0) + ); + assert!(((((libc::fclose(fp)) == (0)) as i32) != 0)); + assert!(((((libc::unlink(path)) == (0)) as i32) != 0)); +} +pub unsafe fn test_puts_1() { + assert!( + ((((libc::puts((c"hello from puts".as_ptr().cast_mut()).cast_const())) >= (0)) as i32) + != 0) + ); +} +pub unsafe fn test_fgets_getc_2() { + let mut path: *const libc::c_char = + (c"/tmp/cpp2rust_stdio_nofd_gets.tmp".as_ptr().cast_mut()).cast_const(); + let mut fp: *mut ::libc::FILE = libc::fopen(path, (c"wb".as_ptr().cast_mut()).cast_const()); + assert!((((!((fp).is_null())) as i32) != 0)); + assert!( + ((((libc::fputs((c"line1\nline2\n".as_ptr().cast_mut()).cast_const(), fp)) >= (0)) as i32) + != 0) + ); + assert!(((((libc::fclose(fp)) == (0)) as i32) != 0)); + fp = libc::fopen(path, (c"rb".as_ptr().cast_mut()).cast_const()); + assert!((((!((fp).is_null())) as i32) != 0)); + let mut buf: [libc::c_char; 8] = [(0 as libc::c_char); 8]; + assert!((((!((libc::fgets(buf.as_mut_ptr(), 8, fp)).is_null())) as i32) != 0)); + assert!( + (((({ + let sa = core::slice::from_raw_parts( + (buf.as_mut_ptr() as *const libc::c_char as *const ::libc::c_void) as *const u8, + 7_usize as usize, + ); + let sb = core::slice::from_raw_parts( + (c"line1\n".as_ptr().cast_mut() as *const libc::c_char as *const ::libc::c_void) + as *const u8, + 7_usize as usize, + ); + let mut diff = 0_i32; + for (x, y) in sa.iter().zip(sb.iter()) { + if x != y { + diff = (*x as i32) - (*y as i32); + break; + } + } + diff + }) == (0)) as i32) + != 0) + ); + assert!(((((libc::fgetc(fp)) == ('l' as i32)) as i32) != 0)); + assert!((((!((libc::fgets(buf.as_mut_ptr(), 4, fp)).is_null())) as i32) != 0)); + assert!( + (((({ + let sa = core::slice::from_raw_parts( + (buf.as_mut_ptr() as *const libc::c_char as *const ::libc::c_void) as *const u8, + 4_usize as usize, + ); + let sb = core::slice::from_raw_parts( + (c"ine".as_ptr().cast_mut() as *const libc::c_char as *const ::libc::c_void) + as *const u8, + 4_usize as usize, + ); + let mut diff = 0_i32; + for (x, y) in sa.iter().zip(sb.iter()) { + if x != y { + diff = (*x as i32) - (*y as i32); + break; + } + } + diff + }) == (0)) as i32) + != 0) + ); + assert!((((!((libc::fgets(buf.as_mut_ptr(), 8, fp)).is_null())) as i32) != 0)); + assert!( + (((({ + let sa = core::slice::from_raw_parts( + (buf.as_mut_ptr() as *const libc::c_char as *const ::libc::c_void) as *const u8, + 3_usize as usize, + ); + let sb = core::slice::from_raw_parts( + (c"2\n".as_ptr().cast_mut() as *const libc::c_char as *const ::libc::c_void) + as *const u8, + 3_usize as usize, + ); + let mut diff = 0_i32; + for (x, y) in sa.iter().zip(sb.iter()) { + if x != y { + diff = (*x as i32) - (*y as i32); + break; + } + } + diff + }) == (0)) as i32) + != 0) + ); + assert!(((((libc::fgets(buf.as_mut_ptr(), 8, fp)).is_null()) as i32) != 0)); + assert!(((((libc::fgetc(fp)) == (-1_i32)) as i32) != 0)); + assert!(((((libc::fclose(fp)) == (0)) as i32) != 0)); + assert!(((((libc::unlink(path)) == (0)) as i32) != 0)); +} +pub unsafe fn test_freopen_3() { + let mut path: *const libc::c_char = + (c"/tmp/cpp2rust_stdio_nofd_reopen.tmp".as_ptr().cast_mut()).cast_const(); + let mut fp: *mut ::libc::FILE = libc::fopen(path, (c"wb".as_ptr().cast_mut()).cast_const()); + assert!((((!((fp).is_null())) as i32) != 0)); + assert!( + ((((libc::fputs((c"hello".as_ptr().cast_mut()).cast_const(), fp)) >= (0)) as i32) != 0) + ); + let mut fp2: *mut ::libc::FILE = + libc::freopen(path, (c"rb".as_ptr().cast_mut()).cast_const(), fp); + assert!((((!((fp2).is_null())) as i32) != 0)); + let mut buf: [libc::c_char; 8] = [ + (0 as libc::c_char), + (0 as libc::c_char), + (0 as libc::c_char), + (0 as libc::c_char), + (0 as libc::c_char), + (0 as libc::c_char), + (0 as libc::c_char), + (0 as libc::c_char), + ]; + assert!( + ((((libcc2rs::fread_unsafe( + (buf.as_mut_ptr() as *mut libc::c_char as *mut ::libc::c_void), + 1_usize, + 8_usize, + fp2 + )) == (5_usize)) as i32) + != 0) + ); + assert!( + (((({ + let sa = core::slice::from_raw_parts( + (buf.as_mut_ptr() as *const libc::c_char as *const ::libc::c_void) as *const u8, + 5_usize as usize, + ); + let sb = core::slice::from_raw_parts( + (c"hello".as_ptr().cast_mut() as *const libc::c_char as *const ::libc::c_void) + as *const u8, + 5_usize as usize, + ); + let mut diff = 0_i32; + for (x, y) in sa.iter().zip(sb.iter()) { + if x != y { + diff = (*x as i32) - (*y as i32); + break; + } + } + diff + }) == (0)) as i32) + != 0) + ); + assert!(((((libc::fclose(fp2)) == (0)) as i32) != 0)); + assert!(((((libc::unlink(path)) == (0)) as i32) != 0)); +} +pub unsafe fn test_fseeko_4() { + let mut path: *const libc::c_char = + (c"/tmp/cpp2rust_stdio_nofd_seek.tmp".as_ptr().cast_mut()).cast_const(); + let mut fp: *mut ::libc::FILE = libc::fopen(path, (c"wb".as_ptr().cast_mut()).cast_const()); + assert!((((!((fp).is_null())) as i32) != 0)); + assert!( + ((((libc::fputs((c"hello world".as_ptr().cast_mut()).cast_const(), fp)) >= (0)) as i32) + != 0) + ); + assert!(((((libc::fclose(fp)) == (0)) as i32) != 0)); + fp = libc::fopen(path, (c"rb".as_ptr().cast_mut()).cast_const()); + assert!((((!((fp).is_null())) as i32) != 0)); + assert!(((((libc::fseeko(fp, 6_i64 as ::libc::off_t, 0)) == (0)) as i32) != 0)); + let mut buf: [libc::c_char; 8] = [ + (0 as libc::c_char), + (0 as libc::c_char), + (0 as libc::c_char), + (0 as libc::c_char), + (0 as libc::c_char), + (0 as libc::c_char), + (0 as libc::c_char), + (0 as libc::c_char), + ]; + assert!( + ((((libcc2rs::fread_unsafe( + (buf.as_mut_ptr() as *mut libc::c_char as *mut ::libc::c_void), + 1_usize, + 5_usize, + fp + )) == (5_usize)) as i32) + != 0) + ); + assert!( + (((({ + let sa = core::slice::from_raw_parts( + (buf.as_mut_ptr() as *const libc::c_char as *const ::libc::c_void) as *const u8, + 5_usize as usize, + ); + let sb = core::slice::from_raw_parts( + (c"world".as_ptr().cast_mut() as *const libc::c_char as *const ::libc::c_void) + as *const u8, + 5_usize as usize, + ); + let mut diff = 0_i32; + for (x, y) in sa.iter().zip(sb.iter()) { + if x != y { + diff = (*x as i32) - (*y as i32); + break; + } + } + diff + }) == (0)) as i32) + != 0) + ); + assert!(((((libc::fseeko(fp, (-5_i32 as i64) as ::libc::off_t, 2)) == (0)) as i32) != 0)); + assert!(((((libc::fgetc(fp)) == ('w' as i32)) as i32) != 0)); + assert!(((((libc::fseeko(fp, 1_i64 as ::libc::off_t, 1)) == (0)) as i32) != 0)); + assert!(((((libc::fgetc(fp)) == ('r' as i32)) as i32) != 0)); + assert!(((((libc::fclose(fp)) == (0)) as i32) != 0)); + assert!(((((libc::unlink(path)) == (0)) as i32) != 0)); +} +pub unsafe fn test_rename_5() { + let mut from: *const libc::c_char = + (c"/tmp/cpp2rust_stdio_nofd_from.tmp".as_ptr().cast_mut()).cast_const(); + let mut to: *const libc::c_char = + (c"/tmp/cpp2rust_stdio_nofd_to.tmp".as_ptr().cast_mut()).cast_const(); + let mut fp: *mut ::libc::FILE = libc::fopen(from, (c"wb".as_ptr().cast_mut()).cast_const()); + assert!((((!((fp).is_null())) as i32) != 0)); + assert!(((((libc::fputs((c"data".as_ptr().cast_mut()).cast_const(), fp)) >= (0)) as i32) != 0)); + assert!(((((libc::fclose(fp)) == (0)) as i32) != 0)); + assert!(((((libc::rename(from, to)) == (0)) as i32) != 0)); + assert!( + ((((libc::fopen(from, (c"rb".as_ptr().cast_mut()).cast_const())).is_null()) as i32) != 0) + ); + fp = libc::fopen(to, (c"rb".as_ptr().cast_mut()).cast_const()); + assert!((((!((fp).is_null())) as i32) != 0)); + assert!(((((libc::fclose(fp)) == (0)) as i32) != 0)); + assert!(((((libc::rename(from, to)) == (-1_i32)) as i32) != 0)); + assert!(((((libc::unlink(to)) == (0)) as i32) != 0)); +} +pub unsafe fn test_setvbuf_6() { + let mut path: *const libc::c_char = + (c"/tmp/cpp2rust_stdio_nofd_vbuf.tmp".as_ptr().cast_mut()).cast_const(); + let mut fp: *mut ::libc::FILE = libc::fopen(path, (c"wb".as_ptr().cast_mut()).cast_const()); + assert!((((!((fp).is_null())) as i32) != 0)); + assert!(((((libc::setvbuf(fp, std::ptr::null_mut(), 2, 0_usize)) == (0)) as i32) != 0)); + assert!(((((libc::fputs((c"x".as_ptr().cast_mut()).cast_const(), fp)) >= (0)) as i32) != 0)); + assert!(((((libc::fclose(fp)) == (0)) as i32) != 0)); + assert!(((((libc::unlink(path)) == (0)) as i32) != 0)); +} +pub fn main() { + unsafe { + std::process::exit(main_0() as i32); + } +} +unsafe fn main_0() -> i32 { + (unsafe { test_fputc_fputs_0() }); + (unsafe { test_puts_1() }); + (unsafe { test_fgets_getc_2() }); + (unsafe { test_freopen_3() }); + (unsafe { test_fseeko_4() }); + (unsafe { test_rename_5() }); + (unsafe { test_setvbuf_6() }); + return 0; +} diff --git a/tests/unit/stdio_nofd.c b/tests/unit/stdio_nofd.c new file mode 100644 index 00000000..070c39f2 --- /dev/null +++ b/tests/unit/stdio_nofd.c @@ -0,0 +1,115 @@ +#include +#include +#include +#include + +static void test_fputc_fputs(void) { + const char *path = "/tmp/cpp2rust_stdio_nofd_puts.tmp"; + FILE *fp = fopen(path, "wb"); + assert(fp != NULL); + assert(fputc('A', fp) == 'A'); + assert(fputs("BCD\n", fp) >= 0); + assert(fclose(fp) == 0); + fp = fopen(path, "rb"); + assert(fp != NULL); + char buf[16] = {0}; + assert(fread(buf, 1, 16, fp) == 5); + assert(memcmp(buf, "ABCD\n", 5) == 0); + assert(fclose(fp) == 0); + assert(unlink(path) == 0); +} + +static void test_puts(void) { assert(puts("hello from puts") >= 0); } + +static void test_fgets_getc(void) { + const char *path = "/tmp/cpp2rust_stdio_nofd_gets.tmp"; + FILE *fp = fopen(path, "wb"); + assert(fp != NULL); + assert(fputs("line1\nline2\n", fp) >= 0); + assert(fclose(fp) == 0); + fp = fopen(path, "rb"); + assert(fp != NULL); + char buf[8]; + assert(fgets(buf, 8, fp) != NULL); + assert(memcmp(buf, "line1\n", 7) == 0); + assert(getc(fp) == 'l'); + assert(fgets(buf, 4, fp) != NULL); + assert(memcmp(buf, "ine", 4) == 0); + assert(fgets(buf, 8, fp) != NULL); + assert(memcmp(buf, "2\n", 3) == 0); + assert(fgets(buf, 8, fp) == NULL); + assert(getc(fp) == EOF); + assert(fclose(fp) == 0); + assert(unlink(path) == 0); +} + +static void test_freopen(void) { + const char *path = "/tmp/cpp2rust_stdio_nofd_reopen.tmp"; + FILE *fp = fopen(path, "wb"); + assert(fp != NULL); + assert(fputs("hello", fp) >= 0); + FILE *fp2 = freopen(path, "rb", fp); + assert(fp2 != NULL); + char buf[8] = {0}; + assert(fread(buf, 1, 8, fp2) == 5); + assert(memcmp(buf, "hello", 5) == 0); + assert(fclose(fp2) == 0); + assert(unlink(path) == 0); +} + +static void test_fseeko(void) { + const char *path = "/tmp/cpp2rust_stdio_nofd_seek.tmp"; + FILE *fp = fopen(path, "wb"); + assert(fp != NULL); + assert(fputs("hello world", fp) >= 0); + assert(fclose(fp) == 0); + fp = fopen(path, "rb"); + assert(fp != NULL); + assert(fseeko(fp, 6, SEEK_SET) == 0); + char buf[8] = {0}; + assert(fread(buf, 1, 5, fp) == 5); + assert(memcmp(buf, "world", 5) == 0); + assert(fseeko(fp, -5, SEEK_END) == 0); + assert(getc(fp) == 'w'); + assert(fseeko(fp, 1, SEEK_CUR) == 0); + assert(getc(fp) == 'r'); + assert(fclose(fp) == 0); + assert(unlink(path) == 0); +} + +static void test_rename(void) { + const char *from = "/tmp/cpp2rust_stdio_nofd_from.tmp"; + const char *to = "/tmp/cpp2rust_stdio_nofd_to.tmp"; + FILE *fp = fopen(from, "wb"); + assert(fp != NULL); + assert(fputs("data", fp) >= 0); + assert(fclose(fp) == 0); + assert(rename(from, to) == 0); + assert(fopen(from, "rb") == NULL); + fp = fopen(to, "rb"); + assert(fp != NULL); + assert(fclose(fp) == 0); + assert(rename(from, to) == -1); + assert(unlink(to) == 0); +} + +static void test_setvbuf(void) { + const char *path = "/tmp/cpp2rust_stdio_nofd_vbuf.tmp"; + FILE *fp = fopen(path, "wb"); + assert(fp != NULL); + assert(setvbuf(fp, NULL, _IONBF, 0) == 0); + assert(fputs("x", fp) >= 0); + assert(fclose(fp) == 0); + assert(unlink(path) == 0); +} + +int main(void) { + test_fputc_fputs(); + test_puts(); + test_fgets_getc(); + test_freopen(); + test_fseeko(); + test_rename(); + test_setvbuf(); + return 0; +}