summaryrefslogtreecommitdiff
path: root/tests/test_struct.rs
diff options
context:
space:
mode:
authorChristopher Chambers <chris.chambers@peanutcode.com>2014-12-29 16:12:44 -0600
committerChristopher Chambers <chris.chambers@peanutcode.com>2014-12-29 16:33:48 -0600
commit97945161d298bb89f7f1ea015c5cc54acc9e7c43 (patch)
tree2b9540747f1caac3355d00a36fa2f1954691a647 /tests/test_struct.rs
parentdd96a0c5f416c6d0e0689079e228de536a643153 (diff)
Adds support for function prototypes, improves tests.
Fixes #79. Fixes #100. Cargo.toml - Adds `[[test]]` section pointing to tests.rs, where all former tests are now imported as modules (cf Servo's tests). gen.rs - Breaks up `TFunc` handling into `TFuncPtr` and `TFuncProto`. - Eliminates empty `extern "C" { }` block that was sometimes generated. lib.rs - Eliminates `dead_code` warnings and one about `LinkType` being able to implement `Copy`. parser.rs - Breaks up function handling into `TFuncPtr` and `TFuncProto`. - Uses the `FuncSig` type to capture information about function signatures. - Adds support for pointers to arrays at the global scope. types.rs - Adds types related to function prototype and pointer handling: `FuncSig`, `TFuncPtr` and `TFuncProto`. tests - Adds quote-based testing. Using the `assert_bind_eq!` macro, it is now possible--without too much ceremony--to test bindgen output of an input file against inline source created with the `quote_item!` (and other `quote_*` macros). - Removes stringify-based testing. - Combines test running into a single entry point, `test.rs`. - Shortens test names where possible without losing context.
Diffstat (limited to 'tests/test_struct.rs')
-rw-r--r--tests/test_struct.rs102
1 files changed, 102 insertions, 0 deletions
diff --git a/tests/test_struct.rs b/tests/test_struct.rs
new file mode 100644
index 00000000..da7547e4
--- /dev/null
+++ b/tests/test_struct.rs
@@ -0,0 +1,102 @@
+#[test]
+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 } };
+
+ x.bar.a = 1;
+ x.bar.b = 2;
+
+ assert_eq!(x.bar.a, 1);
+ assert_eq!(x.bar.b, 2);
+}
+
+#[test]
+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] } };
+
+ 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 { _bindgen_data_1_: [0, 0] };
+
+ 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 { _bindgen_data_1_: [0] };
+
+ 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 { a: 0, _bindgen_data_1_: [0] };
+
+ 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 with_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,
+ }
+ ));
+}