Skip to content

Commit

Permalink
Add a couple sanity tests for zero sized types
Browse files Browse the repository at this point in the history
  • Loading branch information
fitzgen committed Oct 25, 2017
1 parent 379bb16 commit d018f42
Show file tree
Hide file tree
Showing 4 changed files with 186 additions and 0 deletions.
56 changes: 56 additions & 0 deletions tests/expectations/tests/array-of-zero-sized-types.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/* automatically generated by rust-bindgen */


#![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)]



/// This should get an `_address` byte.
#[repr(C)]
#[derive(Debug, Default, Copy, Clone)]
pub struct Empty {
pub _address: u8,
}
#[test]
fn bindgen_test_layout_Empty() {
assert_eq!(
::std::mem::size_of::<Empty>(),
1usize,
concat!("Size of: ", stringify!(Empty))
);
assert_eq!(
::std::mem::align_of::<Empty>(),
1usize,
concat!("Alignment of ", stringify!(Empty))
);
}
/// This should not get an `_address` byte, since each `Empty` gets one, meaning
/// that this object is addressable.
#[repr(C)]
#[derive(Debug, Default, Copy, Clone)]
pub struct HasArrayOfEmpty {
pub empties: [Empty; 10usize],
}
#[test]
fn bindgen_test_layout_HasArrayOfEmpty() {
assert_eq!(
::std::mem::size_of::<HasArrayOfEmpty>(),
10usize,
concat!("Size of: ", stringify!(HasArrayOfEmpty))
);
assert_eq!(
::std::mem::align_of::<HasArrayOfEmpty>(),
1usize,
concat!("Alignment of ", stringify!(HasArrayOfEmpty))
);
assert_eq!(
unsafe { &(*(0 as *const HasArrayOfEmpty)).empties as *const _ as usize },
0usize,
concat!(
"Alignment of field: ",
stringify!(HasArrayOfEmpty),
"::",
stringify!(empties)
)
);
}
97 changes: 97 additions & 0 deletions tests/expectations/tests/contains-vs-inherits-zero-sized.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/* automatically generated by rust-bindgen */


#![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)]



/// This should get an `_address` byte.
#[repr(C)]
#[derive(Debug, Default, Copy, Clone)]
pub struct Empty {
pub _address: u8,
}
#[test]
fn bindgen_test_layout_Empty() {
assert_eq!(
::std::mem::size_of::<Empty>(),
1usize,
concat!("Size of: ", stringify!(Empty))
);
assert_eq!(
::std::mem::align_of::<Empty>(),
1usize,
concat!("Alignment of ", stringify!(Empty))
);
}
/// This should not get an `_address` byte, so `sizeof(Inherits)` should be
/// `1`.
#[repr(C)]
#[derive(Debug, Default, Copy, Clone)]
pub struct Inherits {
pub b: bool,
}
#[test]
fn bindgen_test_layout_Inherits() {
assert_eq!(
::std::mem::size_of::<Inherits>(),
1usize,
concat!("Size of: ", stringify!(Inherits))
);
assert_eq!(
::std::mem::align_of::<Inherits>(),
1usize,
concat!("Alignment of ", stringify!(Inherits))
);
assert_eq!(
unsafe { &(*(0 as *const Inherits)).b as *const _ as usize },
0usize,
concat!(
"Alignment of field: ",
stringify!(Inherits),
"::",
stringify!(b)
)
);
}
/// This should not get an `_address` byte, but contains `Empty` which *does* get
/// one, so `sizeof(Contains)` should be `1 + 1`.
#[repr(C)]
#[derive(Debug, Default, Copy, Clone)]
pub struct Contains {
pub empty: Empty,
pub b: bool,
}
#[test]
fn bindgen_test_layout_Contains() {
assert_eq!(
::std::mem::size_of::<Contains>(),
2usize,
concat!("Size of: ", stringify!(Contains))
);
assert_eq!(
::std::mem::align_of::<Contains>(),
1usize,
concat!("Alignment of ", stringify!(Contains))
);
assert_eq!(
unsafe { &(*(0 as *const Contains)).empty as *const _ as usize },
0usize,
concat!(
"Alignment of field: ",
stringify!(Contains),
"::",
stringify!(empty)
)
);
assert_eq!(
unsafe { &(*(0 as *const Contains)).b as *const _ as usize },
1usize,
concat!(
"Alignment of field: ",
stringify!(Contains),
"::",
stringify!(b)
)
);
}
12 changes: 12 additions & 0 deletions tests/headers/array-of-zero-sized-types.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
* This should get an `_address` byte.
*/
struct Empty {};

/**
* This should not get an `_address` byte, since each `Empty` gets one, meaning
* that this object is addressable.
*/
struct HasArrayOfEmpty {
Empty empties[10];
};
21 changes: 21 additions & 0 deletions tests/headers/contains-vs-inherits-zero-sized.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* This should get an `_address` byte.
*/
struct Empty {};

/**
* This should not get an `_address` byte, so `sizeof(Inherits)` should be
* `1`.
*/
struct Inherits : public Empty {
bool b;
};

/**
* This should not get an `_address` byte, but contains `Empty` which *does* get
* one, so `sizeof(Contains)` should be `1 + 1`.
*/
struct Contains {
Empty empty;
bool b;
};

0 comments on commit d018f42

Please sign in to comment.