summaryrefslogtreecommitdiff
path: root/tests/test_struct.rs
blob: 200eaf994c09a27965deda0e1325d6c91a6017bd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
use std::default::Default;

#[test]
fn with_anon_struct() {
    mod ffi { bindgen!("headers/struct_with_anon_struct.h"); }
    let mut x: ffi::Struct_foo = Default::default();

    x.bar.a = 1;
    x.bar.b = 2;

    assert_eq!(x.bar.a, 1);
    assert_eq!(x.bar.b, 2);
}

#[test]
fn with_anon_struct_array() {
    assert_bind_eq!("headers/struct_with_anon_struct_array.h", cx,
        quote_item!(cx,
            #[repr(C)]
            #[derive(Copy)]
            pub struct Struct_foo {
                pub bar: [Struct_Unnamed1; 2u],
                pub baz: [[[Struct_Unnamed2; 4u]; 3u]; 2u],
            }
        ),
        quote_item!(cx,
            impl ::std::default::Default for Struct_foo {
                fn default() -> Struct_foo { unsafe { ::std::mem::zeroed() } }
            }
        ),
        quote_item!(cx,
            #[repr(C)]
            #[derive(Copy)]
            pub struct Struct_Unnamed1 {
                pub a: ::libc::c_int,
                pub b: ::libc::c_int,
            }
        ),
        quote_item!(cx,
            impl ::std::default::Default for Struct_Unnamed1 {
                fn default() -> Struct_Unnamed1 { unsafe { ::std::mem::zeroed() } }
            }
        ),
        quote_item!(cx,
            #[repr(C)]
            #[derive(Copy)]
            pub struct Struct_Unnamed2 {
                pub a: ::libc::c_int,
                pub b: ::libc::c_int,
            }
        ),
        quote_item!(cx,
            impl ::std::default::Default for Struct_Unnamed2 {
                fn default() -> Struct_Unnamed2 { unsafe { ::std::mem::zeroed() } }
            }
        )
    );
}

#[test]
fn with_anon_struct_pointer() {
    mod ffi { bindgen!("headers/struct_with_anon_struct_pointer.h"); }
    let mut x: ffi::Struct_foo = Default::default();
    let mut unnamed: ffi::Struct_Unnamed1 = Default::default();

    unsafe {
        x.bar = &mut unnamed;

        (*x.bar).a = 1;
        (*x.bar).b = 2;

        assert_eq!((*x.bar).a, 1);
        assert_eq!((*x.bar).b, 2);

        ::std::ptr::read(x.bar);
    }
}

#[test]
fn with_anon_union() {
    mod ffi { bindgen!("headers/struct_with_anon_union.h"); }
    let mut x: ffi::Struct_foo = Default::default();

    unsafe {
        *x.bar.a() = 0x12345678;
        assert_eq!(*x.bar.a(), 0x12345678);
        assert_eq!(*x.bar.b(), 0x5678);
    }
}

#[test]
fn with_anon_unnamed_struct() {
    mod ffi { bindgen!("headers/struct_with_anon_unnamed_struct.h"); }
    let mut x: ffi::Struct_foo = Default::default();

    unsafe {
        *x.a() = 0x12345678;
        *x.b() = 0x87654321;
        assert_eq!(*x.a(), 0x12345678);
        assert_eq!(*x.b(), 0x87654321);
    }
}

#[test]
fn with_anon_unnamed_union() {
    mod ffi { bindgen!("headers/struct_with_anon_unnamed_union.h"); }
    let mut x: ffi::Struct_foo = Default::default();

    unsafe {
        *x.a() = 0x12345678;
        assert_eq!(*x.a(), 0x12345678);
        assert_eq!(*x.b(), 0x5678);
    }
}

#[test]
fn with_nesting() {
    mod ffi { bindgen!("headers/struct_with_nesting.h"); }
    let mut x: ffi::Struct_foo = Default::default();

    unsafe {
        x.a = 0x12345678;
        *x.b() = 0x87654321;

        assert_eq!(x.a, 0x12345678);
        assert_eq!(*x.b(), 0x87654321);
        assert_eq!(*x.c1(), 0x4321);
        assert_eq!(*x.c2(), 0x8765);
        assert_eq!(*x.d1(), 0x21);
        assert_eq!(*x.d2(), 0x43);
        assert_eq!(*x.d3(), 0x65);
        assert_eq!(*x.d4(), 0x87);
    }
}

#[test]
fn containing_fwd_decl_struct() {
    assert_bind_eq!("headers/struct_containing_forward_declared_struct.h", cx,
        quote_item!(cx,
            #[repr(C)]
            #[derive(Copy)]
            pub struct Struct_a {
                pub val_a: *mut Struct_b,
            }
        ),
        quote_item!(cx,
            impl ::std::default::Default for Struct_a {
                fn default() -> Struct_a { unsafe { ::std::mem::zeroed() } }
            }
        ),
        quote_item!(cx,
            #[repr(C)]
            #[derive(Copy)]
            pub struct Struct_b {
                pub val_b: ::libc::c_int,
            }
        ),
        quote_item!(cx,
            impl ::std::default::Default for Struct_b {
                fn default() -> Struct_b { unsafe { ::std::mem::zeroed() } }
            }
        )
    );
}

#[test]
fn with_bitfields() {
    assert_bind_eq!("headers/struct_with_bitfields.h", cx,
        quote_item!(cx,
            #[repr(C)]
            #[derive(Copy)]
            pub struct Struct_bitfield {
                pub _bindgen_bitfield_1_: ::libc::c_ushort,
                pub e: ::libc::c_int,
                pub _bindgen_bitfield_2_: ::libc::c_uint,
                pub _bindgen_bitfield_3_: ::libc::c_uint,
            }
        ),
        quote_item!(cx,
            impl ::std::default::Default for Struct_bitfield {
                fn default() -> Struct_bitfield { unsafe { ::std::mem::zeroed() } }
            }
        )
    );
}

#[test]
fn with_fwd_decl_struct() {
    mod ffi { bindgen!("headers/forward_declared_struct.h"); }

    let a = ffi::Struct_a { b: 1 };
    let c = ffi::Struct_c { d: 1 };

    assert_eq!(a.b, 1);
    assert_eq!(c.d, 1);
}

#[test]
fn default_impl() {
    mod ffi { bindgen!("headers/struct_with_nesting.h"); }

    let mut x: ffi::Struct_foo = Default::default();

    unsafe {
        assert_eq!(x.a, 0);
        assert_eq!(*x.b(), 0);
        assert_eq!(*x.c1(), 0);
        assert_eq!(*x.c2(), 0);
        assert_eq!(*x.d1(), 0);
        assert_eq!(*x.d2(), 0);
        assert_eq!(*x.d3(), 0);
        assert_eq!(*x.d4(), 0);
    }
}