• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #![allow(warnings)]
2 
3 mod bindings {
4     include!(concat!(env!("OUT_DIR"), "/test.rs"));
5 }
6 
7 mod extern_bindings {
8     include!(concat!(env!("OUT_DIR"), "/extern.rs"));
9 }
10 
11 use std::ffi::CStr;
12 use std::mem;
13 use std::os::raw::c_int;
14 
15 use bindings::testing::Bar; // This type is generated from module_raw_line.
16 
17 type MacroInteger = isize;
18 
19 #[test]
test_static_array()20 fn test_static_array() {
21     let mut test = unsafe { bindings::Test_COUNTDOWN.as_ptr() };
22     let expected = unsafe { bindings::Test_countdown() };
23     let also_expected = unsafe { bindings::Test_COUNTDOWN_PTR };
24     assert!(!test.is_null());
25     assert_eq!(also_expected, expected);
26     assert_eq!(test, also_expected);
27 
28     let mut expected = 10;
29     unsafe {
30         loop {
31             assert_eq!(*test, expected);
32             if *test == 0 {
33                 break;
34             }
35             test = test.offset(1);
36             expected -= 1;
37         }
38     }
39 }
40 
41 #[test]
test_static_method()42 fn test_static_method() {
43     let c_str = unsafe { bindings::Test::name() };
44     let name = unsafe { CStr::from_ptr(c_str).to_string_lossy().into_owned() };
45     assert_eq!(name, "Test", "Calling a static C++ method works!");
46 }
47 
48 #[test]
test_constructor()49 fn test_constructor() {
50     let test = unsafe { bindings::Test::new(5) };
51     assert_eq!(test.m_int, 5);
52     assert_eq!(test.m_double, 0.0);
53 }
54 
55 #[test]
test_overload()56 fn test_overload() {
57     let test = unsafe { bindings::Test::new1(5.0) };
58     assert_eq!(test.m_int, 0);
59     assert_eq!(test.m_double, 5.0);
60 }
61 
62 #[test]
test_bitfields_first()63 fn test_bitfields_first() {
64     let mut first: bindings::bitfields::First = unsafe { mem::zeroed() };
65     assert!(unsafe { first.assert(0, 0, 0) });
66     first.set_three_bits_byte_one(2);
67     first.set_six_bits_byte_two(42);
68     first.set_two_bits_byte_two(1);
69     assert!(unsafe { first.assert(2, 42, 1) });
70 }
71 
72 #[test]
test_bitfields_second()73 fn test_bitfields_second() {
74     let mut second: bindings::bitfields::Second = unsafe { mem::zeroed() };
75     assert!(unsafe { second.assert(0, false) });
76     second.set_thirty_one_bits(1337);
77     second.set_one_bit(true);
78     assert!(unsafe { second.assert(1337, true) });
79 }
80 
81 #[test]
test_bitfields_third()82 fn test_bitfields_third() {
83     let mut third: bindings::bitfields::Third = unsafe { mem::zeroed() };
84     assert!(unsafe {
85         third.assert(0, false, bindings::bitfields::ItemKind::ITEM_KIND_UNO)
86     });
87     third.set_flags(12345);
88     third.set_is_whatever(true);
89     third.set_kind(bindings::bitfields::ItemKind::ITEM_KIND_TRES);
90     assert!(unsafe {
91         third.assert(12345, true, bindings::bitfields::ItemKind::ITEM_KIND_TRES)
92     });
93 }
94 
95 #[test]
test_bitfields_fourth()96 fn test_bitfields_fourth() {
97     let mut fourth: bindings::bitfields::Fourth = unsafe { mem::zeroed() };
98     assert!(unsafe { fourth.assert(bindings::bitfields::MyEnum::ONE, 0) });
99 
100     fourth.set_tag(bindings::bitfields::MyEnum::THREE);
101     fourth.set_ptr(0xdeadbeef);
102     assert!(unsafe {
103         fourth.assert(bindings::bitfields::MyEnum::THREE, 0xdeadbeef)
104     });
105 }
106 
107 #[test]
test_bitfields_date2()108 fn test_bitfields_date2() {
109     let mut date: bindings::bitfields::Date2 = unsafe { mem::zeroed() };
110     assert!(unsafe { date.assert(0, 0, 0, 0, 0) });
111 
112     date.set_nWeekDay(6); // saturdays are the best
113     date.set_nMonthDay(20);
114     date.set_nMonth(11);
115     date.set_nYear(95);
116     date.set_byte(255);
117     assert!(unsafe { date.assert(6, 20, 11, 95, 255) });
118 }
119 
120 #[test]
test_bitfields_fifth()121 fn test_bitfields_fifth() {
122     let mut date: bindings::bitfields::Fifth = unsafe { mem::zeroed() };
123 
124     assert!(unsafe { date.assert(0, 0, 0, 0, 0) });
125 
126     date.byte = 255; // Set this first, to ensure we don't override it.
127 
128     date.set_nWeekDay(6); // saturdays are the best
129     date.set_nMonthDay(20);
130     date.set_nMonth(11);
131     date.set_nYear(95);
132 
133     assert!(unsafe { date.assert(6, 20, 11, 95, 255) });
134 }
135 
136 #[test]
test_bitfields_sixth()137 fn test_bitfields_sixth() {
138     let mut date: bindings::bitfields::Sixth = unsafe { mem::zeroed() };
139 
140     assert!(unsafe { date.assert(0, 0, 0, 0) });
141 
142     date.byte = 255;
143     date.set_nWeekDay(6); // saturdays are the best
144     date.set_nMonthDay(20);
145     date.set_nMonth(11);
146 
147     assert!(unsafe { date.assert(255, 6, 11, 20) });
148 }
149 
150 #[test]
test_bitfields_seventh()151 fn test_bitfields_seventh() {
152     let mut large: bindings::bitfields::Seventh = unsafe { mem::zeroed() };
153 
154     assert!(unsafe { large.assert(false, 0, 0, 0, 0, false, 0) });
155 
156     large.set_first_one_bit(true);
157     large.set_second_thirty_bits(375028802);
158     large.set_third_two_bits(2);
159     large.set_fourth_thirty_bits(643472885);
160     large.set_fifth_two_bits(3);
161     large.set_sixth_one_bit(true);
162     large.set_seventh_thirty_bits(1061657575);
163 
164     assert!(unsafe {
165         large.assert(true, 375028802, 2, 643472885, 3, true, 1061657575)
166     });
167 
168     assert_eq!(large.first_one_bit(), true);
169     assert_eq!(large.second_thirty_bits(), 375028802);
170     assert_eq!(large.third_two_bits(), 2);
171     assert_eq!(large.fourth_thirty_bits(), 643472885);
172     assert_eq!(large.fifth_two_bits(), 3);
173     assert_eq!(large.sixth_one_bit(), true);
174     assert_eq!(large.seventh_thirty_bits(), 1061657575);
175 }
176 
177 #[test]
test_bitfield_constructors()178 fn test_bitfield_constructors() {
179     use std::mem;
180     let mut first = bindings::bitfields::First {
181         _bitfield_align_1: [],
182         _bitfield_1: bindings::bitfields::First::new_bitfield_1(1, 2, 3),
183     };
184     assert!(unsafe { first.assert(1, 2, 3) });
185 
186     let mut second = bindings::bitfields::Second {
187         _bitfield_align_1: [],
188         _bitfield_1: bindings::bitfields::Second::new_bitfield_1(1337, true),
189     };
190     assert!(unsafe { second.assert(1337, true) });
191 
192     let mut third = bindings::bitfields::Third {
193         _bitfield_align_1: [],
194         _bitfield_1: bindings::bitfields::Third::new_bitfield_1(
195             42,
196             false,
197             bindings::bitfields::ItemKind::ITEM_KIND_TRES,
198         ),
199     };
200     assert!(unsafe {
201         third.assert(42, false, bindings::bitfields::ItemKind::ITEM_KIND_TRES)
202     });
203 }
204 
205 impl Drop for bindings::AutoRestoreBool {
drop(&mut self)206     fn drop(&mut self) {
207         unsafe { bindings::AutoRestoreBool::destruct(self) }
208     }
209 }
210 
211 #[test]
test_destructors()212 fn test_destructors() {
213     let mut v = true;
214 
215     {
216         let auto_restore = unsafe { bindings::AutoRestoreBool::new(&mut v) };
217         v = false;
218     }
219 
220     assert!(v, "Should've been restored when going out of scope");
221 }
222 
223 impl Drop for bindings::InheritsFromVirtualDestructor {
drop(&mut self)224     fn drop(&mut self) {
225         unsafe {
226             bindings::InheritsFromVirtualDestructor_InheritsFromVirtualDestructor_destructor(self)
227         }
228     }
229 }
230 
231 #[test]
test_virtual_dtor()232 fn test_virtual_dtor() {
233     unsafe {
234         {
235             let b = bindings::InheritsFromVirtualDestructor::new();
236             // Let it go out of scope.
237         }
238 
239         assert_eq!(bindings::InheritsFromVirtualDestructor_sDestructorCount, 1);
240         assert_eq!(bindings::VirtualDestructor_sDestructorCount, 1);
241     }
242 }
243 
244 #[test]
test_item_rename()245 fn test_item_rename() {
246     assert_eq!(bindings::CONST_VALUE, 3);
247     assert_eq!(unsafe { bindings::function_name() }, 4);
248 
249     let _foo = bindings::foo {
250         member: bindings::bar { foo: 2 },
251     };
252 }
253 
254 #[test]
test_matching_with_rename()255 fn test_matching_with_rename() {
256     assert_eq!(bindings::enum_to_be_constified_THREE, 3);
257     assert_eq!(unsafe { bindings::TEMPLATED_CONST_VALUE.len() }, 30);
258 }
259 
260 #[test]
test_macro_customintkind_path()261 fn test_macro_customintkind_path() {
262     let v: &std::any::Any = &bindings::TESTMACRO_CUSTOMINTKIND_PATH;
263     assert!(v.is::<MacroInteger>())
264 }
265 
266 #[test]
test_homogeneous_aggregate_float_union()267 fn test_homogeneous_aggregate_float_union() {
268     unsafe {
269         let coord = &bindings::coord(1., 2., 3., 4.);
270         assert_eq!([1., 2., 3., 4.], coord.v)
271     }
272 }
273 
274 #[test]
test_custom_derive()275 fn test_custom_derive() {
276     // The `add_derives` callback should have added `#[derive(PartialEq)]`
277     // to the `Test` struct. If it didn't, this will fail to compile.
278     let test1 = unsafe { bindings::Test::new(5) };
279     let test2 = unsafe { bindings::Test::new(6) };
280     assert_ne!(test1, test2);
281 
282     // The `add_derives` callback should have added `#[derive(PartialOrd)]`
283     // to the `MyOrderedEnum` enum. If it didn't, this will fail to compile.
284 
285     let micron = unsafe { bindings::MyOrderedEnum::MICRON };
286     let meter = unsafe { bindings::MyOrderedEnum::METER };
287     let lightyear = unsafe { bindings::MyOrderedEnum::LIGHTYEAR };
288 
289     assert!(meter < lightyear);
290     assert!(meter > micron);
291 
292     // The `add_derives` callback should have added `#[derive(PartialEq, PartialOrd)]`
293     // to the `TestDeriveOnAlias` new-type alias. If it didn't, this will fail to compile.
294     let test1 = unsafe { bindings::TestDeriveOnAlias(5) };
295     let test2 = unsafe { bindings::TestDeriveOnAlias(6) };
296     assert!(test1 < test2);
297     assert!(!(test1 > test2));
298 }
299 
300 #[test]
test_wrap_static_fns()301 fn test_wrap_static_fns() {
302     // GH-1090: https://github.com/rust-lang/rust-bindgen/issues/1090
303     unsafe {
304         let f = extern_bindings::foo();
305         assert_eq!(11, f);
306 
307         let b = extern_bindings::bar();
308         assert_eq!(1, b);
309 
310         let t = extern_bindings::takes_ptr(&mut 1);
311         assert_eq!(2, t);
312 
313         extern "C" fn function(x: i32) -> i32 {
314             x + 1
315         }
316 
317         let tp = extern_bindings::takes_fn_ptr(Some(function));
318         assert_eq!(2, tp);
319 
320         let tf = extern_bindings::takes_fn(Some(function));
321         assert_eq!(3, tf);
322 
323         let ta = extern_bindings::takes_alias(Some(function));
324         assert_eq!(4, ta);
325 
326         let tq =
327             extern_bindings::takes_qualified(&(&5 as *const _) as *const _);
328         assert_eq!(5, tq);
329 
330         let wv1 = extern_bindings::wrap_as_variadic_fn1_wrapped(0);
331         assert_eq!(0, wv1);
332 
333         let wv1 = extern_bindings::wrap_as_variadic_fn1_wrapped(2, 5, 3);
334         assert_eq!(8, wv1);
335 
336         extern_bindings::wrap_as_variadic_fn2_wrapped(1, 2);
337     }
338 }
339