diff options
Diffstat (limited to 'tests')
-rw-r--r-- | tests/forward_declared_struct.rs | 23 | ||||
-rw-r--r-- | tests/func_ptr.rs | 17 | ||||
-rw-r--r-- | tests/headers/decl_ptr_to_array.h | 1 | ||||
-rw-r--r-- | tests/headers/func_proto.h | 1 | ||||
-rw-r--r-- | tests/headers/func_with_func_ptr_arg.h | 1 | ||||
-rw-r--r-- | tests/headers/struct_containing_forward_declared_struct.h | 2 | ||||
-rw-r--r-- | tests/headers/unnamed_bitfields.h | 13 | ||||
-rw-r--r-- | tests/support.rs | 220 | ||||
-rw-r--r-- | tests/test_builtins.rs (renamed from tests/builtins.rs) | 0 | ||||
-rw-r--r-- | tests/test_cmath.rs (renamed from tests/cmath.rs) | 14 | ||||
-rw-r--r-- | tests/test_decl.rs | 11 | ||||
-rw-r--r-- | tests/test_func.rs | 45 | ||||
-rw-r--r-- | tests/test_struct.rs (renamed from tests/struct.rs) | 66 | ||||
-rw-r--r-- | tests/test_union.rs (renamed from tests/union.rs) | 17 | ||||
-rw-r--r-- | tests/tests.rs | 21 | ||||
-rw-r--r-- | tests/unnamed_bitfields.rs | 12 | ||||
-rw-r--r-- | tests/util.rs | 58 |
17 files changed, 367 insertions, 155 deletions
diff --git a/tests/forward_declared_struct.rs b/tests/forward_declared_struct.rs deleted file mode 100644 index efd48e30..00000000 --- a/tests/forward_declared_struct.rs +++ /dev/null @@ -1,23 +0,0 @@ -#![feature(phase)] - -#[phase(plugin)] -extern crate bindgen; - -extern crate libc; - -#[test] -fn test_struct_containing_forward_declared_struct() { - mod ffi { bindgen!("headers/struct_containing_forward_declared_struct.h"); } - // Check that struct b is not duplicated -} - -#[test] -fn test_forward_declared_struct() { - mod ffi { bindgen!("headers/forward_declared_struct.h"); } - - let a = ffi::Struct_a { b: 1 }; - let c = ffi::Struct_c { d: 1 }; - - a.b; - c.d; -}
\ No newline at end of file diff --git a/tests/func_ptr.rs b/tests/func_ptr.rs deleted file mode 100644 index 7851c4ff..00000000 --- a/tests/func_ptr.rs +++ /dev/null @@ -1,17 +0,0 @@ -extern crate bindgen; -extern crate regex; -extern crate syntax; - -mod util; - -#[test] -fn test_func_ptr() { - let output = util::generate_unpretty_output("tests/headers/func_ptr.h"); - assert_eq!(output, r##"extern "C" { pub static mut foo: ::std::option::Option<extern "C" fn(x: ::libc::c_int, y: ::libc::c_int) -> ::libc::c_int>; }"##); -} - -#[test] -fn test_func_ptr_in_struct() { - let output = util::generate_unpretty_output("tests/headers/func_ptr_in_struct.h"); - assert_eq!(output, r##"#[repr(C)] #[deriving(Copy)] pub struct Struct_Foo { pub bar: ::std::option::Option<extern "C" fn(x: ::libc::c_int, y: ::libc::c_int) -> Enum_baz>, } extern "C" { }"##); -} diff --git a/tests/headers/decl_ptr_to_array.h b/tests/headers/decl_ptr_to_array.h new file mode 100644 index 00000000..3222cbd4 --- /dev/null +++ b/tests/headers/decl_ptr_to_array.h @@ -0,0 +1 @@ +int (*foo)[1]; diff --git a/tests/headers/func_proto.h b/tests/headers/func_proto.h new file mode 100644 index 00000000..51139ca9 --- /dev/null +++ b/tests/headers/func_proto.h @@ -0,0 +1 @@ +typedef int foo(int bar); diff --git a/tests/headers/func_with_func_ptr_arg.h b/tests/headers/func_with_func_ptr_arg.h new file mode 100644 index 00000000..629c84ab --- /dev/null +++ b/tests/headers/func_with_func_ptr_arg.h @@ -0,0 +1 @@ +void foo(void (*bar)()); diff --git a/tests/headers/struct_containing_forward_declared_struct.h b/tests/headers/struct_containing_forward_declared_struct.h index c68af042..d38aca2f 100644 --- a/tests/headers/struct_containing_forward_declared_struct.h +++ b/tests/headers/struct_containing_forward_declared_struct.h @@ -4,4 +4,4 @@ struct a { struct b { int val_b; -};
\ No newline at end of file +}; diff --git a/tests/headers/unnamed_bitfields.h b/tests/headers/unnamed_bitfields.h index 6ca9ec76..229bd728 100644 --- a/tests/headers/unnamed_bitfields.h +++ b/tests/headers/unnamed_bitfields.h @@ -1,10 +1,9 @@ -struct bitfield -{ +struct bitfield { unsigned short - a :1, - b :1, - c :1, + a :1, + b :1, + c :1, :1, :2, - d :2 -};
\ No newline at end of file + d :2; +}; diff --git a/tests/support.rs b/tests/support.rs new file mode 100644 index 00000000..a9b6dc4d --- /dev/null +++ b/tests/support.rs @@ -0,0 +1,220 @@ +use bindgen; +use bindgen::{Logger, BindgenOptions}; +use std::default::Default; +use std::fmt; +use syntax::ast; +use syntax::print::pprust; +use syntax::ptr::P; + +use syntax::codemap; +use syntax::codemap::{Span, DUMMY_SP}; +use syntax::parse; +use syntax::parse::token; + +struct TestLogger; + +impl Logger for TestLogger { + fn error(&self, msg: &str) { + println!("err: {}", msg); + } + + fn warn(&self, msg: &str) { + println!("warn: {}", msg); + } +} + +pub fn generate_bindings(filename: &str) -> Result<Vec<P<ast::Item>>, ()> { + let mut options:BindgenOptions = Default::default(); + options.clang_args.push(filename.to_string()); + + let logger = TestLogger; + Ok(try!(bindgen::Bindings::generate(&options, Some(&logger as &Logger), None)).into_ast()) +} + +pub fn test_bind_eq(filename: &str, f:|ext_cx: DummyExtCtxt| -> Vec<Option<P<ast::Item>>>) { + let ext_cx = mk_dummy_ext_ctxt(); + let items = normalize_attr_ids(generate_bindings(filename).unwrap()); + let quoted = normalize_attr_ids(f(ext_cx).into_iter().map(|x| x.unwrap()).collect()); + assert_eq!(PrettyItems { items: items }, + PrettyItems { items: quoted }); +} + +macro_rules! assert_bind_eq { + ($filename:expr, $ext_cx:ident, $($quote:expr),*) => { + ::support::test_bind_eq(concat!("tests/", $filename), |ext_cx| { + let $ext_cx = &ext_cx; + vec!($($quote),*) + }); + } +} + +pub struct DummyExtCtxt { + sess: parse::ParseSess, +} + +impl DummyExtCtxt { + pub fn cfg(&self) -> ast::CrateConfig { + vec!() + } + pub fn parse_sess(&self) -> &parse::ParseSess { + &self.sess + } + pub fn call_site(&self) -> codemap::Span { + codemap::Span { + lo: codemap::BytePos(0), + hi: codemap::BytePos(0), + expn_id: codemap::NO_EXPANSION + } + } + pub fn ident_of(&self, s: &str) -> ast::Ident { + token::str_to_ident(s) + } + pub fn name_of(&self, s: &str) -> ast::Name { + token::intern(s) + } +} + +fn mk_dummy_ext_ctxt<'a>() -> DummyExtCtxt { + DummyExtCtxt { sess: parse::new_parse_sess() } +} + +#[deriving(PartialEq)] +struct PrettyItems { + pub items: Vec<P<ast::Item>>, +} + +impl fmt::Show for PrettyItems { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + let output = pprust::to_string(|s| { + let module = ast::Mod { + inner: DUMMY_SP, + view_items: Vec::new(), + items: self.items.clone(), + }; + s.print_mod(&module, &[]) + }); + write!(f, "\n{}\n", output) + } +} + +// libsyntax uses a thread-local variable to create unique AttrId values during +// parsing and quoting. normalize_attr_ids makes sure that all AttrId values +// for the passed `items` start at zero and proceed upward. This is necessary +// to correctly compare otherwise-identical ASTs. +fn normalize_attr_ids(items: Vec<P<ast::Item>>) -> Vec<P<ast::Item>> { + use std::mem; + use syntax::visit::*; + + struct Vis<'a> { + attr_id: uint, + items: Vec<P<ast::Item>>, + } + + impl<'a> Vis<'a> { + // TODO: visit_crate? ast::visit::Visitor does not deal with them directly, + // but concievably, it could eventually come up. + + fn rewrite_attrs(&mut self, attrs: Vec<ast::Attribute>) -> Vec<ast::Attribute> { + attrs.into_iter().map(|mut attr| { + attr.node.id = ast::AttrId(self.attr_id); + self.attr_id += 1; + attr + }).collect() + } + } + + unsafe fn force_mutable<T>(x: &T) -> &mut T { + mem::transmute(x) + } + + macro_rules! rewrite_attrs { + ($self_:ident, $item:expr) => { + unsafe { + let unsafe_item = force_mutable($item); + let new_attrs = mem::replace(&mut unsafe_item.attrs, vec!()); + let new_attrs = $self_.rewrite_attrs(new_attrs); + mem::replace(&mut unsafe_item.attrs, new_attrs); + } + } + } + + impl<'a, 'v> Visitor<'v> for Vis<'a> { + fn visit_item(&mut self, i: &ast::Item) { + rewrite_attrs!(self, i); + + match i.node { + ast::ItemImpl(_, _, _, _, ref impl_items) => { + for impl_item in impl_items.iter() { + match *impl_item { + ast::ImplItem::MethodImplItem(_) => { } + ast::ImplItem::TypeImplItem(ref typedef) => { + rewrite_attrs!(self, typedef.deref()); + } + } + } + } + _ => { } + } + + walk_item(self, i); + } + + fn visit_foreign_item(&mut self, i: &ast::ForeignItem) { + rewrite_attrs!(self, i); + walk_foreign_item(self, i); + } + + fn visit_fn(&mut self, fk: FnKind, fd: &ast::FnDecl, b: &ast::Block, s: Span, _: ast::NodeId) { + match fk { + FkItemFn(_, _, _, _) | FkFnBlock => { } + FkMethod(_, _, method) => { + rewrite_attrs!(self, method); + } + } + walk_fn(self, fk, fd, b, s); + } + + fn visit_arm(&mut self, a: &ast::Arm) { + rewrite_attrs!(self, a); + walk_arm(self, a); + } + + fn visit_ty_method(&mut self, t: &ast::TypeMethod) { + rewrite_attrs!(self, t); + walk_ty_method(self, t); + } + + fn visit_variant(&mut self, v: &ast::Variant, g: &ast::Generics) { + rewrite_attrs!(self, &v.node); + walk_variant(self, v, g); + } + + fn visit_view_item(&mut self, i: &ast::ViewItem) { + rewrite_attrs!(self, i); + walk_view_item(self, i); + } + + fn visit_struct_field(&mut self, s: &ast::StructField) { + rewrite_attrs!(self, &s.node); + walk_struct_field(self, s); + } + + fn visit_trait_item(&mut self, t: &ast::TraitItem) { + match *t { + ast::TraitItem::RequiredMethod(_) | + ast::TraitItem::ProvidedMethod(_) => { } + ast::TraitItem::TypeTraitItem(ref assoc_ty) => { + rewrite_attrs!(self, assoc_ty.deref()); + } + } + walk_trait_item(self, t); + } + } + + let mut visitor = Vis { attr_id: 0, items: vec!() }; + for item in items.into_iter() { + visitor.visit_item(item.deref()); + visitor.items.push(item); + } + visitor.items +} diff --git a/tests/builtins.rs b/tests/test_builtins.rs index 35cd573c..35cd573c 100644 --- a/tests/builtins.rs +++ b/tests/test_builtins.rs diff --git a/tests/cmath.rs b/tests/test_cmath.rs index f3fa1840..73e2cef1 100644 --- a/tests/cmath.rs +++ b/tests/test_cmath.rs @@ -1,20 +1,12 @@ -#![feature(phase)] - -#[phase(plugin)] -extern crate bindgen; - -extern crate libc; - #[allow(dead_code)] #[allow(non_snake_case)] #[allow(non_camel_case_types)] #[allow(non_upper_case_globals)] -pub mod ffi { - bindgen!("/usr/include/math.h", link = "m"); -} +#[allow(raw_pointer_deriving)] +pub mod ffi { bindgen!("/usr/include/math.h", link = "m"); } #[test] -fn test_floor_is_bound_and_callable() { +fn floor_is_bound_and_callable() { unsafe { assert_eq!(ffi::floor( 2.7), 2.0); assert_eq!(ffi::floor(-2.7), -3.0); diff --git a/tests/test_decl.rs b/tests/test_decl.rs new file mode 100644 index 00000000..5f83d00b --- /dev/null +++ b/tests/test_decl.rs @@ -0,0 +1,11 @@ +#[test] +fn ptr_to_array() { + assert_bind_eq!("headers/decl_ptr_to_array.h", cx, + quote_item!(cx, + extern "C" { + pub static mut foo: [::libc::c_int, ..1u]; + } + ) + ); + +} diff --git a/tests/test_func.rs b/tests/test_func.rs new file mode 100644 index 00000000..83bc2696 --- /dev/null +++ b/tests/test_func.rs @@ -0,0 +1,45 @@ +#[test] +fn func_ptr() { + assert_bind_eq!("headers/func_ptr.h", cx, + quote_item!(cx, + extern "C" { + pub static mut foo: ::std::option::Option< + extern "C" fn(x: ::libc::c_int, + y: ::libc::c_int) -> ::libc::c_int>; + } + ) + ); +} + +#[test] +fn func_ptr_in_struct() { + assert_bind_eq!("headers/func_ptr_in_struct.h", cx, + quote_item!(cx, + #[repr(C)] + #[deriving(Copy)] + pub struct Struct_Foo { + pub bar: ::std::option::Option< + extern "C" fn(x: ::libc::c_int, + y: ::libc::c_int) -> Enum_baz>, + } + ) + ); +} + +#[test] +fn func_proto() { + assert_bind_eq!("headers/func_proto.h", cx, + quote_item!(cx, + pub type foo = extern "C" fn(bar: ::libc::c_int) -> ::libc::c_int; + )); +} + +#[test] +fn with_func_ptr_arg() { + assert_bind_eq!("headers/func_with_func_ptr_arg.h", cx, + quote_item!(cx, + extern "C" { + pub fn foo(bar: ::std::option::Option<extern "C" fn()>); + } + )); +} diff --git a/tests/struct.rs b/tests/test_struct.rs index f9900349..d3c52aef 100644 --- a/tests/struct.rs +++ b/tests/test_struct.rs @@ -1,12 +1,5 @@ -#![feature(phase)] - -#[phase(plugin)] -extern crate bindgen; - -extern crate libc; - #[test] -fn test_struct_with_anon_struct() { +fn with_anon_struct() { mod ffi { bindgen!("headers/struct_with_anon_struct.h"); } let mut x = ffi::Struct_foo { bar: ffi::Struct_Unnamed1 { a: 0, b: 0 } }; @@ -18,7 +11,7 @@ fn test_struct_with_anon_struct() { } #[test] -fn test_struct_with_anon_struct_array() { +fn with_anon_struct_array() { mod ffi { bindgen!("headers/struct_with_anon_struct_array.h"); } let mut x = ffi::Struct_foo { bar: [ ffi::Struct_Unnamed1 { a: 0, b: 0 }, @@ -33,7 +26,7 @@ fn test_struct_with_anon_struct_array() { } #[test] -fn test_struct_with_anon_struct_pointer() { +fn with_anon_struct_pointer() { mod ffi { bindgen!("headers/struct_with_anon_struct_pointer.h"); } let mut unnamed = box ffi::Struct_Unnamed1 { a: 0, b: 0 }; @@ -51,7 +44,7 @@ fn test_struct_with_anon_struct_pointer() { } #[test] -fn test_struct_with_anon_union() { +fn with_anon_union() { mod ffi { bindgen!("headers/struct_with_anon_union.h"); } let mut x = ffi::Struct_foo { bar: ffi::Union_Unnamed1 { _bindgen_data_: [0] } }; @@ -63,7 +56,7 @@ fn test_struct_with_anon_union() { } #[test] -fn test_struct_with_anon_unnamed_struct() { +fn with_anon_unnamed_struct() { mod ffi { bindgen!("headers/struct_with_anon_unnamed_struct.h"); } let mut x = ffi::Struct_foo { _bindgen_data_1_: [0, 0] }; @@ -76,7 +69,7 @@ fn test_struct_with_anon_unnamed_struct() { } #[test] -fn test_struct_with_anon_unnamed_union() { +fn with_anon_unnamed_union() { mod ffi { bindgen!("headers/struct_with_anon_unnamed_union.h"); } let mut x = ffi::Struct_foo { _bindgen_data_1_: [0] }; @@ -88,7 +81,7 @@ fn test_struct_with_anon_unnamed_union() { } #[test] -fn test_struct_with_nesting() { +fn with_nesting() { mod ffi { bindgen!("headers/struct_with_nesting.h"); } let mut x = ffi::Struct_foo { a: 0, _bindgen_data_1_: [0] }; @@ -106,3 +99,48 @@ fn test_struct_with_nesting() { assert_eq!(*x.d4(), 0x87); } } + +#[test] +fn with_contain_fwd_decl_struct() { + assert_bind_eq!("headers/struct_containing_forward_declared_struct.h", cx, + quote_item!(cx, + #[repr(C)] + #[deriving(Copy)] + pub struct Struct_a { + pub val_a: *mut Struct_b, + } + ), + quote_item!(cx, + #[repr(C)] + #[deriving(Copy)] + pub struct Struct_b; + )); +} + +#[test] +fn with_unnamed_bitfields() { + assert_bind_eq!("headers/unnamed_bitfields.h", cx, + quote_item!(cx, + #[repr(C)] + #[deriving(Copy)] + pub struct Struct_bitfield { + pub a: ::libc::c_ushort, + pub b: ::libc::c_ushort, + pub c: ::libc::c_ushort, + pub unnamed_field1: ::libc::c_ushort, + pub unnamed_field2: ::libc::c_ushort, + pub d: ::libc::c_ushort, + } + )); +} + +#[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 }; + + a.b; + c.d; +} diff --git a/tests/union.rs b/tests/test_union.rs index 2bdb313e..33a3713d 100644 --- a/tests/union.rs +++ b/tests/test_union.rs @@ -1,12 +1,5 @@ -#![feature(phase)] - -#[phase(plugin)] -extern crate bindgen; - -extern crate libc; - #[test] -fn test_union_with_anon_struct() { +fn with_anon_struct() { // XXX: Rustc thinks that the anonymous struct, bar, is unused. #[allow(dead_code)] mod ffi { bindgen!("headers/union_with_anon_struct.h"); } @@ -22,7 +15,7 @@ fn test_union_with_anon_struct() { } #[test] -fn test_union_with_anon_union() { +fn with_anon_union() { mod ffi { bindgen!("headers/union_with_anon_union.h"); } let mut x = ffi::Union_foo { _bindgen_data_: [0] }; @@ -35,7 +28,7 @@ fn test_union_with_anon_union() { } #[test] -fn test_union_with_anon_unnamed_struct() { +fn with_anon_unnamed_struct() { mod ffi { bindgen!("headers/union_with_anon_unnamed_struct.h"); } let mut x = ffi::Union_pixel { _bindgen_data_: [0] }; @@ -54,7 +47,7 @@ fn test_union_with_anon_unnamed_struct() { } #[test] -fn test_union_with_anon_unnamed_union() { +fn with_anon_unnamed_union() { mod ffi { bindgen!("headers/union_with_anon_unnamed_union.h"); } let mut x = ffi::Union_foo { _bindgen_data_: [0] }; @@ -68,7 +61,7 @@ fn test_union_with_anon_unnamed_union() { } #[test] -fn test_union_with_nesting() { +fn with_nesting() { mod ffi { bindgen!("headers/union_with_nesting.h"); } let mut x = ffi::Union_foo { _bindgen_data_: [0] }; diff --git a/tests/tests.rs b/tests/tests.rs new file mode 100644 index 00000000..6515f6de --- /dev/null +++ b/tests/tests.rs @@ -0,0 +1,21 @@ +#![feature(globs)] +#![feature(macro_rules)] +#![feature(phase)] +#![feature(quote)] + +#[phase(plugin)] +extern crate bindgen; + +extern crate bindgen; +extern crate libc; +extern crate syntax; + +#[macro_escape] +mod support; + +mod test_cmath; +mod test_decl; +mod test_func; +mod test_struct; +mod test_union; +mod test_builtins; diff --git a/tests/unnamed_bitfields.rs b/tests/unnamed_bitfields.rs deleted file mode 100644 index a5bb1332..00000000 --- a/tests/unnamed_bitfields.rs +++ /dev/null @@ -1,12 +0,0 @@ -#![feature(phase)] - -#[phase(plugin)] -extern crate bindgen; - -extern crate libc; - -#[test] -fn test_unnamed_bitfields() { - mod ffi { bindgen!("headers/unnamed_bitfields.h"); } - // Check that there are no unnamed struct fields -}
\ No newline at end of file diff --git a/tests/util.rs b/tests/util.rs deleted file mode 100644 index 8ee3d213..00000000 --- a/tests/util.rs +++ /dev/null @@ -1,58 +0,0 @@ -extern crate bindgen; -extern crate regex; -extern crate syntax; - -use bindgen::{Logger, BindgenOptions}; -use regex::Regex; -use std::default::Default; -use syntax::ast; -use syntax::codemap::DUMMY_SP; -use syntax::print::pp; -use syntax::print::pprust; -use syntax::ptr::P; - -struct TestLogger; - -impl Logger for TestLogger { - fn error(&self, msg: &str) { - println!("err: {}", msg); - } - - fn warn(&self, msg: &str) { - println!("warn: {}", msg); - } -} - -pub fn generate_bindings(filename: &str) -> Result<Vec<P<ast::Item>>, ()> { - let mut options:BindgenOptions = Default::default(); - options.clang_args.push(filename.to_string()); - - let logger = TestLogger; - Ok(try!(bindgen::Bindings::generate(&options, Some(&logger as &Logger), None)).into_ast()) -} - -pub fn generate_unpretty_output(filename: &str) -> String { - let output = pprust::to_string(|s| { - // HACK: Replace the default printer with a very wide printer. This - // makes it easier to reason about how the unpretty'd output - // will look, at the cost of tying us to the implementation - // details of pprust::to_string. - s.s = pp::mk_printer(box Vec::new(), 4096); - - let items = generate_bindings(filename).unwrap(); - - let module = ast::Mod { - inner: DUMMY_SP, - view_items: Vec::new(), - items: items, - }; - s.print_mod(&module, &[]) - }); - - unpretty(output.as_slice()) -} - -pub fn unpretty(s: &str) -> String { - let re = Regex::new(r"\s+").unwrap(); - re.replace_all(s, " ") -} |