Skip to content

Commit

Permalink
tests: Improve enum tests to avoid duplication, and add a test for ru…
Browse files Browse the repository at this point in the history
  • Loading branch information
emilio authored and LoganBarnett committed Dec 2, 2023
1 parent 542e0d0 commit 0175c7b
Show file tree
Hide file tree
Showing 12 changed files with 234 additions and 47 deletions.
64 changes: 64 additions & 0 deletions tests/expectations/tests/enum-default-bitfield.rs
Expand Up @@ -5,6 +5,70 @@
non_upper_case_globals
)]

#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct foo {
pub member: foo__bindgen_ty_1,
}
pub const foo_FOO_A: foo__bindgen_ty_1 = foo__bindgen_ty_1(0);
pub const foo_FOO_B: foo__bindgen_ty_1 = foo__bindgen_ty_1(1);
impl ::std::ops::BitOr<foo__bindgen_ty_1> for foo__bindgen_ty_1 {
type Output = Self;
#[inline]
fn bitor(self, other: Self) -> Self {
foo__bindgen_ty_1(self.0 | other.0)
}
}
impl ::std::ops::BitOrAssign for foo__bindgen_ty_1 {
#[inline]
fn bitor_assign(&mut self, rhs: foo__bindgen_ty_1) {
self.0 |= rhs.0;
}
}
impl ::std::ops::BitAnd<foo__bindgen_ty_1> for foo__bindgen_ty_1 {
type Output = Self;
#[inline]
fn bitand(self, other: Self) -> Self {
foo__bindgen_ty_1(self.0 & other.0)
}
}
impl ::std::ops::BitAndAssign for foo__bindgen_ty_1 {
#[inline]
fn bitand_assign(&mut self, rhs: foo__bindgen_ty_1) {
self.0 &= rhs.0;
}
}
#[repr(transparent)]
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub struct foo__bindgen_ty_1(pub ::std::os::raw::c_uint);
#[test]
fn bindgen_test_layout_foo() {
assert_eq!(
::std::mem::size_of::<foo>(),
4usize,
concat!("Size of: ", stringify!(foo))
);
assert_eq!(
::std::mem::align_of::<foo>(),
4usize,
concat!("Alignment of ", stringify!(foo))
);
assert_eq!(
unsafe { &(*(::std::ptr::null::<foo>())).member as *const _ as usize },
0usize,
concat!(
"Offset of field: ",
stringify!(foo),
"::",
stringify!(member)
)
);
}
impl Default for foo {
fn default() -> Self {
unsafe { ::std::mem::zeroed() }
}
}
impl Foo {
pub const Bar: Foo = Foo(0);
}
Expand Down
36 changes: 36 additions & 0 deletions tests/expectations/tests/enum-default-consts.rs
Expand Up @@ -5,6 +5,42 @@
non_upper_case_globals
)]

#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct foo {
pub member: foo__bindgen_ty_1,
}
pub const foo_FOO_A: ::std::os::raw::c_uint = 0;
pub const foo_FOO_B: ::std::os::raw::c_uint = 1;
pub type foo__bindgen_ty_1 = ::std::os::raw::c_uint;
#[test]
fn bindgen_test_layout_foo() {
assert_eq!(
::std::mem::size_of::<foo>(),
4usize,
concat!("Size of: ", stringify!(foo))
);
assert_eq!(
::std::mem::align_of::<foo>(),
4usize,
concat!("Alignment of ", stringify!(foo))
);
assert_eq!(
unsafe { &(*(::std::ptr::null::<foo>())).member as *const _ as usize },
0usize,
concat!(
"Offset of field: ",
stringify!(foo),
"::",
stringify!(member)
)
);
}
impl Default for foo {
fn default() -> Self {
unsafe { ::std::mem::zeroed() }
}
}
pub const Foo_Bar: Foo = 0;
pub const Foo_Qux: Foo = 1;
pub type Foo = ::std::os::raw::c_uint;
Expand Down
38 changes: 38 additions & 0 deletions tests/expectations/tests/enum-default-module.rs
Expand Up @@ -5,6 +5,44 @@
non_upper_case_globals
)]

#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct foo {
pub member: foo__bindgen_ty_1::Type,
}
pub mod foo__bindgen_ty_1 {
pub type Type = ::std::os::raw::c_uint;
pub const FOO_A: Type = 0;
pub const FOO_B: Type = 1;
}
#[test]
fn bindgen_test_layout_foo() {
assert_eq!(
::std::mem::size_of::<foo>(),
4usize,
concat!("Size of: ", stringify!(foo))
);
assert_eq!(
::std::mem::align_of::<foo>(),
4usize,
concat!("Alignment of ", stringify!(foo))
);
assert_eq!(
unsafe { &(*(::std::ptr::null::<foo>())).member as *const _ as usize },
0usize,
concat!(
"Offset of field: ",
stringify!(foo),
"::",
stringify!(member)
)
);
}
impl Default for foo {
fn default() -> Self {
unsafe { ::std::mem::zeroed() }
}
}
pub mod Foo {
pub type Type = ::std::os::raw::c_uint;
pub const Bar: Type = 0;
Expand Down
41 changes: 41 additions & 0 deletions tests/expectations/tests/enum-default-rust.rs
Expand Up @@ -5,6 +5,47 @@
non_upper_case_globals
)]

#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct foo {
pub member: foo__bindgen_ty_1,
}
pub const foo_FOO_A: foo__bindgen_ty_1 = foo__bindgen_ty_1::FOO_A;
pub const foo_FOO_B: foo__bindgen_ty_1 = foo__bindgen_ty_1::FOO_B;
#[repr(u32)]
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub enum foo__bindgen_ty_1 {
FOO_A = 0,
FOO_B = 1,
}
#[test]
fn bindgen_test_layout_foo() {
assert_eq!(
::std::mem::size_of::<foo>(),
4usize,
concat!("Size of: ", stringify!(foo))
);
assert_eq!(
::std::mem::align_of::<foo>(),
4usize,
concat!("Alignment of ", stringify!(foo))
);
assert_eq!(
unsafe { &(*(::std::ptr::null::<foo>())).member as *const _ as usize },
0usize,
concat!(
"Offset of field: ",
stringify!(foo),
"::",
stringify!(member)
)
);
}
impl Default for foo {
fn default() -> Self {
unsafe { ::std::mem::zeroed() }
}
}
#[repr(u32)]
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub enum Foo {
Expand Down
50 changes: 40 additions & 10 deletions tests/expectations/tests/enum.rs
Expand Up @@ -5,15 +5,45 @@
non_upper_case_globals
)]

#[repr(u32)]
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub enum Foo {
Bar = 0,
Qux = 1,
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct foo {
pub member: foo__bindgen_ty_1,
}
#[repr(i32)]
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub enum Neg {
MinusOne = -1,
One = 1,
pub const foo_FOO_A: ::std::os::raw::c_uint = 0;
pub const foo_FOO_B: ::std::os::raw::c_uint = 1;
pub type foo__bindgen_ty_1 = ::std::os::raw::c_uint;
#[test]
fn bindgen_test_layout_foo() {
assert_eq!(
::std::mem::size_of::<foo>(),
4usize,
concat!("Size of: ", stringify!(foo))
);
assert_eq!(
::std::mem::align_of::<foo>(),
4usize,
concat!("Alignment of ", stringify!(foo))
);
assert_eq!(
unsafe { &(*(::std::ptr::null::<foo>())).member as *const _ as usize },
0usize,
concat!(
"Offset of field: ",
stringify!(foo),
"::",
stringify!(member)
)
);
}
impl Default for foo {
fn default() -> Self {
unsafe { ::std::mem::zeroed() }
}
}
pub const Foo_Bar: Foo = 0;
pub const Foo_Qux: Foo = 1;
pub type Foo = ::std::os::raw::c_uint;
pub const Neg_MinusOne: Neg = -1;
pub const Neg_One: Neg = 1;
pub type Neg = ::std::os::raw::c_int;
1 change: 1 addition & 0 deletions tests/expectations/tests/enum_explicit_type_constants.rs
Expand Up @@ -27,3 +27,4 @@ pub const BoolEnumsAreFun2_Value2: BoolEnumsAreFun2 = true;
pub type BoolEnumsAreFun2 = MyType;
pub const AnonymousVariantOne: ::std::os::raw::c_uchar = 0;
pub const AnonymousVariantTwo: ::std::os::raw::c_uchar = 1;
pub type _bindgen_ty_1 = ::std::os::raw::c_uchar;
1 change: 1 addition & 0 deletions tests/expectations/tests/issue-1025-unknown-enum-repr.rs
Expand Up @@ -10,3 +10,4 @@
pub struct a {
pub _address: u8,
}
pub type a__bindgen_ty_1 = i32;
10 changes: 1 addition & 9 deletions tests/headers/enum-default-bitfield.h
@@ -1,11 +1,3 @@
// bindgen-flags: --default-enum-style=bitfield --constified-enum-module=Neg

enum Foo {
Bar = 0,
Qux
};

enum Neg {
MinusOne = -1,
One = 1,
};
#include "enum.h"
10 changes: 1 addition & 9 deletions tests/headers/enum-default-consts.h
@@ -1,11 +1,3 @@
// bindgen-flags: --default-enum-style=consts --constified-enum-module=Neg

enum Foo {
Bar = 0,
Qux
};

enum Neg {
MinusOne = -1,
One = 1,
};
#include "enum.h"
10 changes: 1 addition & 9 deletions tests/headers/enum-default-module.h
@@ -1,11 +1,3 @@
// bindgen-flags: --default-enum-style=moduleconsts --constified-enum-module=Neg

enum Foo {
Bar = 0,
Qux
};

enum Neg {
MinusOne = -1,
One = 1,
};
#include "enum.h"
10 changes: 1 addition & 9 deletions tests/headers/enum-default-rust.h
@@ -1,11 +1,3 @@
// bindgen-flags: --default-enum-style=rust --constified-enum-module=Neg

enum Foo {
Bar = 0,
Qux
};

enum Neg {
MinusOne = -1,
One = 1,
};
#include "enum.h"
10 changes: 9 additions & 1 deletion tests/headers/enum.h
@@ -1,4 +1,12 @@
// bindgen-flags: --rustified-enum ".*"
// A few tests for enum-related issues that should be tested with all the enum
// representations.

struct foo {
enum {
FOO_A,
FOO_B,
} member;
};

enum Foo {
Bar = 0,
Expand Down

0 comments on commit 0175c7b

Please sign in to comment.