Skip to content

Commit cf39bf2

Browse files
committed
Add C struct test
1 parent f9cdc9c commit cf39bf2

3 files changed

Lines changed: 231 additions & 0 deletions

File tree

tests/unit/c_struct.c

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#include <assert.h>
2+
3+
struct Point {
4+
int x;
5+
int y;
6+
};
7+
8+
struct Line {
9+
struct Point start;
10+
struct Point end;
11+
};
12+
13+
struct Node {
14+
int value;
15+
struct Node *next;
16+
};
17+
18+
struct Container {
19+
struct Inner {
20+
int a;
21+
int b;
22+
} inner;
23+
enum Color { RED, GREEN, BLUE } color;
24+
int count;
25+
};
26+
27+
int main() {
28+
struct Point p = {10, 20};
29+
assert(p.x == 10);
30+
assert(p.y == 20);
31+
32+
struct Line l = {{1, 2}, {3, 4}};
33+
assert(l.start.x == 1);
34+
assert(l.end.y == 4);
35+
36+
struct Node a = {1, 0};
37+
struct Node b = {2, &a};
38+
assert(b.next->value == 1);
39+
40+
struct Container c = {{5, 6}, GREEN, 42};
41+
assert(c.inner.a == 5);
42+
assert(c.inner.b == 6);
43+
assert(c.color == GREEN);
44+
assert(c.count == 42);
45+
46+
struct Container c2;
47+
c2.color = BLUE;
48+
assert(c2.color == 2);
49+
50+
return 0;
51+
}
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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::Seek;
7+
use std::io::{Read, Write};
8+
use std::os::fd::AsFd;
9+
use std::rc::{Rc, Weak};
10+
#[derive(Clone, Default)]
11+
pub struct Point {
12+
pub x: Value<i32>,
13+
pub y: Value<i32>,
14+
}
15+
impl ByteRepr for Point {}
16+
#[derive(Clone, Default)]
17+
pub struct Line {
18+
pub start: Value<Point>,
19+
pub end: Value<Point>,
20+
}
21+
impl ByteRepr for Line {}
22+
#[derive(Clone, Default)]
23+
pub struct Node {
24+
pub value: Value<i32>,
25+
pub next: Value<Ptr<Node>>,
26+
}
27+
impl ByteRepr for Node {}
28+
#[derive(Clone, Copy, PartialEq, Debug, Default)]
29+
enum Color {
30+
#[default]
31+
RED = 0,
32+
GREEN = 1,
33+
BLUE = 2,
34+
}
35+
#[derive(Clone, Default)]
36+
pub struct Inner {
37+
pub a: Value<i32>,
38+
pub b: Value<i32>,
39+
}
40+
impl ByteRepr for Inner {}
41+
#[derive(Clone, Default)]
42+
pub struct Container {
43+
pub inner: Value<Inner>,
44+
pub color: Value<Color>,
45+
pub count: Value<i32>,
46+
}
47+
impl ByteRepr for Container {}
48+
pub fn main() {
49+
std::process::exit(main_0());
50+
}
51+
fn main_0() -> i32 {
52+
let p: Value<Point> = Rc::new(RefCell::new(Point {
53+
x: Rc::new(RefCell::new(10)),
54+
y: Rc::new(RefCell::new(20)),
55+
}));
56+
assert!(((*(*p.borrow()).x.borrow()) == 10));
57+
assert!(((*(*p.borrow()).y.borrow()) == 20));
58+
let l: Value<Line> = Rc::new(RefCell::new(Line {
59+
start: Rc::new(RefCell::new(Point {
60+
x: Rc::new(RefCell::new(1)),
61+
y: Rc::new(RefCell::new(2)),
62+
})),
63+
end: Rc::new(RefCell::new(Point {
64+
x: Rc::new(RefCell::new(3)),
65+
y: Rc::new(RefCell::new(4)),
66+
})),
67+
}));
68+
assert!(((*(*(*l.borrow()).start.borrow()).x.borrow()) == 1));
69+
assert!(((*(*(*l.borrow()).end.borrow()).y.borrow()) == 4));
70+
let a: Value<Node> = Rc::new(RefCell::new(Node {
71+
value: Rc::new(RefCell::new(1)),
72+
next: Rc::new(RefCell::new(Default::default())),
73+
}));
74+
let b: Value<Node> = Rc::new(RefCell::new(Node {
75+
value: Rc::new(RefCell::new(2)),
76+
next: Rc::new(RefCell::new((a.as_pointer()))),
77+
}));
78+
assert!(
79+
((*(*(*(*b.borrow()).next.borrow()).upgrade().deref())
80+
.value
81+
.borrow())
82+
== 1)
83+
);
84+
let c: Value<Container> = Rc::new(RefCell::new(Container {
85+
inner: Rc::new(RefCell::new(Inner {
86+
a: Rc::new(RefCell::new(5)),
87+
b: Rc::new(RefCell::new(6)),
88+
})),
89+
color: Rc::new(RefCell::new((Color::GREEN as Color))),
90+
count: Rc::new(RefCell::new(42)),
91+
}));
92+
assert!(((*(*(*c.borrow()).inner.borrow()).a.borrow()) == 5));
93+
assert!(((*(*(*c.borrow()).inner.borrow()).b.borrow()) == 6));
94+
assert!((((*(*c.borrow()).color.borrow()) as u32) == (Color::GREEN as u32)));
95+
assert!(((*(*c.borrow()).count.borrow()) == 42));
96+
let c2: Value<Container> = <Value<Container>>::default();
97+
(*(*c2.borrow()).color.borrow_mut()) = (Color::BLUE as Color);
98+
assert!((((*(*c2.borrow()).color.borrow()) as u32) == 2_u32));
99+
return 0;
100+
}

tests/unit/out/unsafe/c_struct.rs

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
extern crate libc;
2+
use libc::*;
3+
extern crate libcc2rs;
4+
use libcc2rs::*;
5+
use std::collections::BTreeMap;
6+
use std::io::Seek;
7+
use std::io::{Read, Write};
8+
use std::os::fd::{AsFd, FromRawFd, IntoRawFd};
9+
use std::rc::Rc;
10+
#[derive(Clone, Default)]
11+
pub struct Point {
12+
pub x: i32,
13+
pub y: i32,
14+
}
15+
#[derive(Clone, Default)]
16+
pub struct Line {
17+
pub start: Point,
18+
pub end: Point,
19+
}
20+
#[derive(Clone, Default)]
21+
pub struct Node {
22+
pub value: i32,
23+
pub next: *mut Node,
24+
}
25+
#[derive(Clone, Copy, PartialEq, Debug, Default)]
26+
enum Color {
27+
#[default]
28+
RED = 0,
29+
GREEN = 1,
30+
BLUE = 2,
31+
}
32+
#[derive(Clone, Default)]
33+
pub struct Inner {
34+
pub a: i32,
35+
pub b: i32,
36+
}
37+
#[derive(Clone, Default)]
38+
pub struct Container {
39+
pub inner: Inner,
40+
pub color: Color,
41+
pub count: i32,
42+
}
43+
pub fn main() {
44+
unsafe {
45+
std::process::exit(main_0() as i32);
46+
}
47+
}
48+
unsafe fn main_0() -> i32 {
49+
let mut p: Point = Point { x: 10, y: 20 };
50+
assert!(((p.x) == (10)));
51+
assert!(((p.y) == (20)));
52+
let mut l: Line = Line {
53+
start: Point { x: 1, y: 2 },
54+
end: Point { x: 3, y: 4 },
55+
};
56+
assert!(((l.start.x) == (1)));
57+
assert!(((l.end.y) == (4)));
58+
let mut a: Node = Node {
59+
value: 1,
60+
next: Default::default(),
61+
};
62+
let mut b: Node = Node {
63+
value: 2,
64+
next: (&mut a as *mut Node),
65+
};
66+
assert!((((*b.next).value) == (1)));
67+
let mut c: Container = Container {
68+
inner: Inner { a: 5, b: 6 },
69+
color: (Color::GREEN as Color),
70+
count: 42,
71+
};
72+
assert!(((c.inner.a) == (5)));
73+
assert!(((c.inner.b) == (6)));
74+
assert!(((c.color as u32) == (Color::GREEN as u32)));
75+
assert!(((c.count) == (42)));
76+
let mut c2: Container = <Container>::default();
77+
c2.color = (Color::BLUE as Color);
78+
assert!(((c2.color as u32) == (2_u32)));
79+
return 0;
80+
}

0 commit comments

Comments
 (0)