Skip to content

Commit

Permalink
Add tests for dynamic binding generation
Browse files Browse the repository at this point in the history
  • Loading branch information
Joe Ellis committed Oct 27, 2020
1 parent a086e56 commit 3594e93
Show file tree
Hide file tree
Showing 11 changed files with 491 additions and 1 deletion.
3 changes: 2 additions & 1 deletion tests/expectations/Cargo.toml
Expand Up @@ -10,4 +10,5 @@ authors = [

[dependencies]
objc = "0.2"
block = "0.1"
block = "0.1"
libloading = "0.6.2"
81 changes: 81 additions & 0 deletions tests/expectations/tests/dynamic_loading_simple.rs
@@ -0,0 +1,81 @@
#![allow(
dead_code,
non_snake_case,
non_camel_case_types,
non_upper_case_globals
)]

extern crate libloading;
pub struct TestLib {
__library: ::libloading::Library,
foo: Result<
unsafe extern "C" fn(
x: ::std::os::raw::c_int,
y: ::std::os::raw::c_int,
) -> ::std::os::raw::c_int,
::libloading::Error,
>,
bar: Result<
unsafe extern "C" fn(
x: *mut ::std::os::raw::c_void,
) -> ::std::os::raw::c_int,
::libloading::Error,
>,
baz: Result<
unsafe extern "C" fn() -> ::std::os::raw::c_int,
::libloading::Error,
>,
}
impl TestLib {
pub unsafe fn new<P>(path: P) -> Result<Self, ::libloading::Error>
where
P: AsRef<::std::ffi::OsStr>,
{
let __library = ::libloading::Library::new(path)?;
let foo = __library.get("foo".as_bytes()).map(|sym| *sym);
let bar = __library.get("bar".as_bytes()).map(|sym| *sym);
let baz = __library.get("baz".as_bytes()).map(|sym| *sym);
Ok(TestLib {
__library: __library,
foo,
bar,
baz,
})
}
pub fn can_call(&self) -> CheckTestLib {
CheckTestLib { __library: self }
}
pub unsafe fn foo(
&self,
x: ::std::os::raw::c_int,
y: ::std::os::raw::c_int,
) -> ::std::os::raw::c_int {
let sym = self.foo.as_ref().expect("Expected function, got error.");
(sym)(x, y)
}
pub unsafe fn bar(
&self,
x: *mut ::std::os::raw::c_void,
) -> ::std::os::raw::c_int {
let sym = self.bar.as_ref().expect("Expected function, got error.");
(sym)(x)
}
pub unsafe fn baz(&self) -> ::std::os::raw::c_int {
let sym = self.baz.as_ref().expect("Expected function, got error.");
(sym)()
}
}
pub struct CheckTestLib<'a> {
__library: &'a TestLib,
}
impl<'a> CheckTestLib<'a> {
pub fn foo(&self) -> Result<(), &'a ::libloading::Error> {
self.__library.foo.as_ref().map(|_| ())
}
pub fn bar(&self) -> Result<(), &'a ::libloading::Error> {
self.__library.bar.as_ref().map(|_| ())
}
pub fn baz(&self) -> Result<(), &'a ::libloading::Error> {
self.__library.baz.as_ref().map(|_| ())
}
}
56 changes: 56 additions & 0 deletions tests/expectations/tests/dynamic_loading_template.rs
@@ -0,0 +1,56 @@
#![allow(
dead_code,
non_snake_case,
non_camel_case_types,
non_upper_case_globals
)]

extern crate libloading;
pub struct TestLib {
__library: ::libloading::Library,
foo: Result<
unsafe extern "C" fn(x: ::std::os::raw::c_int) -> ::std::os::raw::c_int,
::libloading::Error,
>,
foo1: Result<unsafe extern "C" fn(x: f32) -> f32, ::libloading::Error>,
}
impl TestLib {
pub unsafe fn new<P>(path: P) -> Result<Self, ::libloading::Error>
where
P: AsRef<::std::ffi::OsStr>,
{
let __library = ::libloading::Library::new(path)?;
let foo = __library.get("foo".as_bytes()).map(|sym| *sym);
let foo1 = __library.get("foo1".as_bytes()).map(|sym| *sym);
Ok(TestLib {
__library: __library,
foo,
foo1,
})
}
pub fn can_call(&self) -> CheckTestLib {
CheckTestLib { __library: self }
}
pub unsafe fn foo(
&self,
x: ::std::os::raw::c_int,
) -> ::std::os::raw::c_int {
let sym = self.foo.as_ref().expect("Expected function, got error.");
(sym)(x)
}
pub unsafe fn foo1(&self, x: f32) -> f32 {
let sym = self.foo1.as_ref().expect("Expected function, got error.");
(sym)(x)
}
}
pub struct CheckTestLib<'a> {
__library: &'a TestLib,
}
impl<'a> CheckTestLib<'a> {
pub fn foo(&self) -> Result<(), &'a ::libloading::Error> {
self.__library.foo.as_ref().map(|_| ())
}
pub fn foo1(&self) -> Result<(), &'a ::libloading::Error> {
self.__library.foo1.as_ref().map(|_| ())
}
}
117 changes: 117 additions & 0 deletions tests/expectations/tests/dynamic_loading_with_blacklist.rs
@@ -0,0 +1,117 @@
#![allow(
dead_code,
non_snake_case,
non_camel_case_types,
non_upper_case_globals
)]

#[repr(C)]
#[derive(Debug, Default, Copy, Clone)]
pub struct X {
pub _x: ::std::os::raw::c_int,
}
#[test]
fn bindgen_test_layout_X() {
assert_eq!(
::std::mem::size_of::<X>(),
4usize,
concat!("Size of: ", stringify!(X))
);
assert_eq!(
::std::mem::align_of::<X>(),
4usize,
concat!("Alignment of ", stringify!(X))
);
assert_eq!(
unsafe { &(*(::std::ptr::null::<X>()))._x as *const _ as usize },
0usize,
concat!("Offset of field: ", stringify!(X), "::", stringify!(_x))
);
}
extern "C" {
#[link_name = "\u{1}_ZN1X13some_functionEv"]
pub fn X_some_function(this: *mut X);
}
extern "C" {
#[link_name = "\u{1}_ZN1X19some_other_functionEv"]
pub fn X_some_other_function(this: *mut X);
}
extern "C" {
#[link_name = "\u{1}_ZN1XC1Ei"]
pub fn X_X(this: *mut X, x: ::std::os::raw::c_int);
}
impl X {
#[inline]
pub unsafe fn some_function(&mut self) {
X_some_function(self)
}
#[inline]
pub unsafe fn some_other_function(&mut self) {
X_some_other_function(self)
}
#[inline]
pub unsafe fn new(x: ::std::os::raw::c_int) -> Self {
let mut __bindgen_tmp = ::std::mem::MaybeUninit::uninit();
X_X(__bindgen_tmp.as_mut_ptr(), x);
__bindgen_tmp.assume_init()
}
}
extern crate libloading;
pub struct TestLib {
__library: ::libloading::Library,
foo: Result<
unsafe extern "C" fn(
x: *mut ::std::os::raw::c_void,
) -> ::std::os::raw::c_int,
::libloading::Error,
>,
bar: Result<
unsafe extern "C" fn(
x: *mut ::std::os::raw::c_void,
) -> ::std::os::raw::c_int,
::libloading::Error,
>,
}
impl TestLib {
pub unsafe fn new<P>(path: P) -> Result<Self, ::libloading::Error>
where
P: AsRef<::std::ffi::OsStr>,
{
let __library = ::libloading::Library::new(path)?;
let foo = __library.get("foo".as_bytes()).map(|sym| *sym);
let bar = __library.get("bar".as_bytes()).map(|sym| *sym);
Ok(TestLib {
__library: __library,
foo,
bar,
})
}
pub fn can_call(&self) -> CheckTestLib {
CheckTestLib { __library: self }
}
pub unsafe fn foo(
&self,
x: *mut ::std::os::raw::c_void,
) -> ::std::os::raw::c_int {
let sym = self.foo.as_ref().expect("Expected function, got error.");
(sym)(x)
}
pub unsafe fn bar(
&self,
x: *mut ::std::os::raw::c_void,
) -> ::std::os::raw::c_int {
let sym = self.bar.as_ref().expect("Expected function, got error.");
(sym)(x)
}
}
pub struct CheckTestLib<'a> {
__library: &'a TestLib,
}
impl<'a> CheckTestLib<'a> {
pub fn foo(&self) -> Result<(), &'a ::libloading::Error> {
self.__library.foo.as_ref().map(|_| ())
}
pub fn bar(&self) -> Result<(), &'a ::libloading::Error> {
self.__library.bar.as_ref().map(|_| ())
}
}
109 changes: 109 additions & 0 deletions tests/expectations/tests/dynamic_loading_with_class.rs
@@ -0,0 +1,109 @@
#![allow(
dead_code,
non_snake_case,
non_camel_case_types,
non_upper_case_globals
)]

#[repr(C)]
#[derive(Debug, Default, Copy, Clone)]
pub struct A {
pub _x: ::std::os::raw::c_int,
}
#[test]
fn bindgen_test_layout_A() {
assert_eq!(
::std::mem::size_of::<A>(),
4usize,
concat!("Size of: ", stringify!(A))
);
assert_eq!(
::std::mem::align_of::<A>(),
4usize,
concat!("Alignment of ", stringify!(A))
);
assert_eq!(
unsafe { &(*(::std::ptr::null::<A>()))._x as *const _ as usize },
0usize,
concat!("Offset of field: ", stringify!(A), "::", stringify!(_x))
);
}
extern "C" {
#[link_name = "\u{1}_ZN1A13some_functionEv"]
pub fn A_some_function(this: *mut A);
}
extern "C" {
#[link_name = "\u{1}_ZN1A19some_other_functionEv"]
pub fn A_some_other_function(this: *mut A);
}
extern "C" {
#[link_name = "\u{1}_ZN1AC1Ei"]
pub fn A_A(this: *mut A, x: ::std::os::raw::c_int);
}
impl A {
#[inline]
pub unsafe fn some_function(&mut self) {
A_some_function(self)
}
#[inline]
pub unsafe fn some_other_function(&mut self) {
A_some_other_function(self)
}
#[inline]
pub unsafe fn new(x: ::std::os::raw::c_int) -> Self {
let mut __bindgen_tmp = ::std::mem::MaybeUninit::uninit();
A_A(__bindgen_tmp.as_mut_ptr(), x);
__bindgen_tmp.assume_init()
}
}
extern crate libloading;
pub struct TestLib {
__library: ::libloading::Library,
foo: Result<
unsafe extern "C" fn(
x: *mut ::std::os::raw::c_void,
) -> ::std::os::raw::c_int,
::libloading::Error,
>,
bar: Result<unsafe extern "C" fn(), ::libloading::Error>,
}
impl TestLib {
pub unsafe fn new<P>(path: P) -> Result<Self, ::libloading::Error>
where
P: AsRef<::std::ffi::OsStr>,
{
let __library = ::libloading::Library::new(path)?;
let foo = __library.get("foo".as_bytes()).map(|sym| *sym);
let bar = __library.get("bar".as_bytes()).map(|sym| *sym);
Ok(TestLib {
__library: __library,
foo,
bar,
})
}
pub fn can_call(&self) -> CheckTestLib {
CheckTestLib { __library: self }
}
pub unsafe fn foo(
&self,
x: *mut ::std::os::raw::c_void,
) -> ::std::os::raw::c_int {
let sym = self.foo.as_ref().expect("Expected function, got error.");
(sym)(x)
}
pub unsafe fn bar(&self) -> () {
let sym = self.bar.as_ref().expect("Expected function, got error.");
(sym)()
}
}
pub struct CheckTestLib<'a> {
__library: &'a TestLib,
}
impl<'a> CheckTestLib<'a> {
pub fn foo(&self) -> Result<(), &'a ::libloading::Error> {
self.__library.foo.as_ref().map(|_| ())
}
pub fn bar(&self) -> Result<(), &'a ::libloading::Error> {
self.__library.bar.as_ref().map(|_| ())
}
}

0 comments on commit 3594e93

Please sign in to comment.