Skip to content

Commit 44a6d61

Browse files
committed
Add call to fread through function pointer
1 parent d7709d5 commit 44a6d61

3 files changed

Lines changed: 125 additions & 0 deletions

File tree

tests/unit/fn_ptr_stdlib_compare.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include <assert.h>
22
#include <stdio.h>
33
#include <stdlib.h>
4+
#include <string.h>
45

56
typedef size_t (*fread_t)(void *, size_t, size_t, FILE *);
67

@@ -19,5 +20,19 @@ int main() {
1920
fread_t f3 = (fread_t)my_alternative_fread;
2021
assert((*f3)(nullptr, 0, 0, nullptr) == 22);
2122

23+
FILE *stream = fopen("/dev/zero", "rb");
24+
assert(stream != nullptr);
25+
char buf[16];
26+
memset(buf, 'X', sizeof(buf));
27+
size_t n = (*fn1)(buf, 1, 10, stream);
28+
assert(n == 10);
29+
for (int i = 0; i < 10; ++i) {
30+
assert(buf[i] == 0);
31+
}
32+
for (int i = 10; i < 16; ++i) {
33+
assert(buf[i] == 'X');
34+
}
35+
fclose(stream);
36+
2237
return 0;
2338
}

tests/unit/out/refcount/fn_ptr_stdlib_compare.rs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,5 +66,57 @@ fn main_0() -> i32 {
6666
(*(*f3.borrow()))(_arg0, _arg1, _arg2, _arg3)
6767
}) == 22_u64)
6868
);
69+
let stream: Value<Ptr<::std::fs::File>> = Rc::new(RefCell::new(
70+
match Ptr::from_string_literal("rb").to_rust_string() {
71+
v if v == "rb" => std::fs::OpenOptions::new()
72+
.read(true)
73+
.open(Ptr::from_string_literal("/dev/zero").to_rust_string())
74+
.ok()
75+
.map_or(Ptr::null(), |f| Ptr::alloc(f)),
76+
v if v == "wb" => std::fs::OpenOptions::new()
77+
.write(true)
78+
.create(true)
79+
.truncate(true)
80+
.open(Ptr::from_string_literal("/dev/zero").to_rust_string())
81+
.ok()
82+
.map_or(Ptr::null(), |f| Ptr::alloc(f)),
83+
_ => panic!("unsupported mode"),
84+
},
85+
));
86+
assert!(!((*stream.borrow()).is_null()));
87+
let buf: Value<Box<[u8]>> = Rc::new(RefCell::new(
88+
(0..16).map(|_| <u8>::default()).collect::<Box<[u8]>>(),
89+
));
90+
{
91+
((buf.as_pointer() as Ptr<u8>) as Ptr<u8>).to_any().memset(
92+
(('X' as u8) as i32) as u8,
93+
::std::mem::size_of::<[u8; 16]>() as u64 as usize,
94+
);
95+
((buf.as_pointer() as Ptr<u8>) as Ptr<u8>).to_any().clone()
96+
};
97+
let n: Value<u64> = Rc::new(RefCell::new(
98+
({
99+
let _arg0: AnyPtr = ((buf.as_pointer() as Ptr<u8>) as Ptr<u8>).to_any();
100+
let _arg1: u64 = 1_u64;
101+
let _arg2: u64 = 10_u64;
102+
let _arg3: Ptr<::std::fs::File> = (*stream.borrow()).clone();
103+
(*(*fn1.borrow()))(_arg0, _arg1, _arg2, _arg3)
104+
}),
105+
));
106+
assert!(((*n.borrow()) == 10_u64));
107+
let i: Value<i32> = Rc::new(RefCell::new(0));
108+
'loop_: while ((*i.borrow()) < 10) {
109+
assert!((((*buf.borrow())[(*i.borrow()) as usize] as i32) == 0));
110+
(*i.borrow_mut()).prefix_inc();
111+
}
112+
let i: Value<i32> = Rc::new(RefCell::new(10));
113+
'loop_: while ((*i.borrow()) < 16) {
114+
assert!((((*buf.borrow())[(*i.borrow()) as usize] as i32) == (('X' as u8) as i32)));
115+
(*i.borrow_mut()).prefix_inc();
116+
}
117+
{
118+
(*stream.borrow()).delete();
119+
0
120+
};
69121
return 0;
70122
}

tests/unit/out/unsafe/fn_ptr_stdlib_compare.rs

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,5 +50,63 @@ unsafe fn main_0() -> i32 {
5050
(f3).unwrap()(_arg0, _arg1, _arg2, _arg3)
5151
}) == (22_u64))
5252
);
53+
let mut stream: *mut ::std::fs::File =
54+
match std::ffi::CStr::from_ptr(b"rb\0".as_ptr() as *const i8)
55+
.to_str()
56+
.expect("invalid c-string")
57+
{
58+
v if v == "rb" => std::fs::OpenOptions::new()
59+
.read(true)
60+
.open(
61+
std::ffi::CStr::from_ptr(b"/dev/zero\0".as_ptr() as *const i8)
62+
.to_str()
63+
.expect("invalid c-string"),
64+
)
65+
.ok()
66+
.map_or(std::ptr::null_mut(), |f| Box::into_raw(Box::new(f))),
67+
v if v == "wb" => std::fs::OpenOptions::new()
68+
.write(true)
69+
.create(true)
70+
.truncate(true)
71+
.open(
72+
std::ffi::CStr::from_ptr(b"/dev/zero\0".as_ptr() as *const i8)
73+
.to_str()
74+
.expect("invalid c-string"),
75+
)
76+
.ok()
77+
.map_or(std::ptr::null_mut(), |f| Box::into_raw(Box::new(f))),
78+
_ => panic!("unsupported mode"),
79+
};
80+
assert!(!((stream).is_null()));
81+
let mut buf: [u8; 16] = [0_u8; 16];
82+
{
83+
let byte_0 = (buf.as_mut_ptr() as *mut u8 as *mut ::libc::c_void) as *mut u8;
84+
for offset in 0..::std::mem::size_of::<[u8; 16]>() as u64 {
85+
*byte_0.offset(offset as isize) = (('X' as u8) as i32) as u8;
86+
}
87+
(buf.as_mut_ptr() as *mut u8 as *mut ::libc::c_void)
88+
};
89+
let mut n: u64 = (unsafe {
90+
let _arg0: *mut ::libc::c_void = (buf.as_mut_ptr() as *mut u8 as *mut ::libc::c_void);
91+
let _arg1: u64 = 1_u64;
92+
let _arg2: u64 = 10_u64;
93+
let _arg3: *mut ::std::fs::File = stream;
94+
(fn1).unwrap()(_arg0, _arg1, _arg2, _arg3)
95+
});
96+
assert!(((n) == (10_u64)));
97+
let mut i: i32 = 0;
98+
'loop_: while ((i) < (10)) {
99+
assert!(((buf[(i) as usize] as i32) == (0)));
100+
i.prefix_inc();
101+
}
102+
let mut i: i32 = 10;
103+
'loop_: while ((i) < (16)) {
104+
assert!(((buf[(i) as usize] as i32) == (('X' as u8) as i32)));
105+
i.prefix_inc();
106+
}
107+
{
108+
Box::from_raw(stream);
109+
0
110+
};
53111
return 0;
54112
}

0 commit comments

Comments
 (0)