-
Notifications
You must be signed in to change notification settings - Fork 698
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a couple sanity tests for zero sized types
- Loading branch information
Showing
4 changed files
with
186 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
97
tests/expectations/tests/contains-vs-inherits-zero-sized.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
) | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |