Skip to content

Commit fb7f030

Browse files
committed
Add memcpy_struct_bytes test
1 parent 3402042 commit fb7f030

3 files changed

Lines changed: 125 additions & 0 deletions

File tree

tests/unit/memcpy_struct_bytes.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#include <assert.h>
2+
#include <stdint.h>
3+
#include <string.h>
4+
5+
struct point {
6+
int32_t x;
7+
int32_t y;
8+
};
9+
10+
int main(void) {
11+
struct point src = {3, 7};
12+
13+
unsigned char buf[sizeof(struct point)];
14+
memcpy(buf, &src, sizeof(buf));
15+
16+
struct point dst;
17+
memcpy(&dst, buf, sizeof(dst));
18+
19+
assert(dst.x == 3);
20+
assert(dst.y == 7);
21+
22+
return 0;
23+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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+
#[derive(Default)]
10+
pub struct point {
11+
pub x: Value<i32>,
12+
pub y: Value<i32>,
13+
}
14+
impl ByteRepr for point {
15+
fn to_bytes(&self, buf: &mut [u8]) {
16+
(*self.x.borrow()).to_bytes(&mut buf[0..4]);
17+
(*self.y.borrow()).to_bytes(&mut buf[4..8]);
18+
}
19+
fn from_bytes(buf: &[u8]) -> Self {
20+
Self {
21+
x: Rc::new(RefCell::new(<i32>::from_bytes(&buf[0..4]))),
22+
y: Rc::new(RefCell::new(<i32>::from_bytes(&buf[4..8]))),
23+
}
24+
}
25+
}
26+
pub fn main() {
27+
std::process::exit(main_0());
28+
}
29+
fn main_0() -> i32 {
30+
let src: Value<point> = Rc::new(RefCell::new(point {
31+
x: Rc::new(RefCell::new(3)),
32+
y: Rc::new(RefCell::new(7)),
33+
}));
34+
let buf: Value<Box<[u8]>> = Rc::new(RefCell::new(
35+
(0..8).map(|_| <u8>::default()).collect::<Box<[u8]>>(),
36+
));
37+
{
38+
((buf.as_pointer() as Ptr<u8>) as Ptr<u8>).to_any().memcpy(
39+
&((src.as_pointer()) as Ptr<point>).to_any(),
40+
::std::mem::size_of::<[u8; 8]>() as usize,
41+
);
42+
((buf.as_pointer() as Ptr<u8>) as Ptr<u8>).to_any().clone()
43+
};
44+
let dst: Value<point> = <Value<point>>::default();
45+
{
46+
((dst.as_pointer()) as Ptr<point>).to_any().memcpy(
47+
&((buf.as_pointer() as Ptr<u8>) as Ptr<u8>).to_any(),
48+
::std::mem::size_of::<point>() as usize,
49+
);
50+
((dst.as_pointer()) as Ptr<point>).to_any().clone()
51+
};
52+
assert!(((((*(*dst.borrow()).x.borrow()) == 3) as i32) != 0));
53+
assert!(((((*(*dst.borrow()).y.borrow()) == 7) as i32) != 0));
54+
return 0;
55+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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+
#[repr(C)]
10+
#[derive(Copy, Clone, Default)]
11+
pub struct point {
12+
pub x: i32,
13+
pub y: i32,
14+
}
15+
pub fn main() {
16+
unsafe {
17+
std::process::exit(main_0() as i32);
18+
}
19+
}
20+
unsafe fn main_0() -> i32 {
21+
let mut src: point = point { x: 3, y: 7 };
22+
let mut buf: [u8; 8] = [0_u8; 8];
23+
{
24+
if ::std::mem::size_of::<[u8; 8]>() != 0 {
25+
::std::ptr::copy_nonoverlapping(
26+
((&mut src as *mut point) as *const point as *const ::libc::c_void),
27+
(buf.as_mut_ptr() as *mut u8 as *mut ::libc::c_void),
28+
::std::mem::size_of::<[u8; 8]>() as usize,
29+
)
30+
}
31+
(buf.as_mut_ptr() as *mut u8 as *mut ::libc::c_void)
32+
};
33+
let mut dst: point = <point>::default();
34+
{
35+
if ::std::mem::size_of::<point>() != 0 {
36+
::std::ptr::copy_nonoverlapping(
37+
(buf.as_mut_ptr() as *const u8 as *const ::libc::c_void),
38+
((&mut dst as *mut point) as *mut point as *mut ::libc::c_void),
39+
::std::mem::size_of::<point>() as usize,
40+
)
41+
}
42+
((&mut dst as *mut point) as *mut point as *mut ::libc::c_void)
43+
};
44+
assert!(((((dst.x) == (3)) as i32) != 0));
45+
assert!(((((dst.y) == (7)) as i32) != 0));
46+
return 0;
47+
}

0 commit comments

Comments
 (0)