Skip to content

tests: Add an integration test for static arrays. #457

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 29, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions bindgen-integration/cpp/Test.cc
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
#include "Test.h"

const int Test::COUNTDOWN[] = { 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 };
const int* Test::COUNTDOWN_PTR = Test::COUNTDOWN;

const int* Test::countdown() {
return COUNTDOWN;
}

const char* Test::name() {
return "Test";
}
Expand Down
4 changes: 4 additions & 0 deletions bindgen-integration/cpp/Test.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ class Test {
static const char* name();
Test(int foo);
Test(double foo);

static const int COUNTDOWN[];
static const int* COUNTDOWN_PTR;
static const int* countdown();
};

namespace testing {
Expand Down
23 changes: 23 additions & 0 deletions bindgen-integration/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,29 @@ mod bindings {
}

use std::ffi::CStr;
use std::os::raw::c_int;

#[test]
fn test_static_array() {
let mut test = unsafe { bindings::Test_COUNTDOWN.as_ptr() };
let expected = unsafe { bindings::Test_countdown()};
let also_expected = unsafe { bindings::Test_COUNTDOWN_PTR };
assert!(!test.is_null());
assert_eq!(also_expected, expected);
assert_eq!(test, also_expected);

let mut expected = 10;
unsafe {
loop {
assert_eq!(*test, expected);
if *test == 0 {
break;
}
test = test.offset(1);
expected -= 1;
}
}
}

#[test]
fn test_static_method() {
Expand Down
27 changes: 22 additions & 5 deletions src/codegen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2385,31 +2385,48 @@ mod utils {

#[inline]
pub unsafe fn as_slice(&self, len: usize) -> &[T] {
::std::slice::from_raw_parts(self.as_ptr(), len)
::$prefix::slice::from_raw_parts(self.as_ptr(), len)
}

#[inline]
pub unsafe fn as_mut_slice(&mut self, len: usize) -> &mut [T] {
::std::slice::from_raw_parts_mut(self.as_mut_ptr(), len)
::$prefix::slice::from_raw_parts_mut(self.as_mut_ptr(), len)
}
}
)
.unwrap();

let incomplete_array_debug_impl = quote_item!(ctx.ext_cx(),
impl<T> ::std::fmt::Debug for __IncompleteArrayField<T> {
fn fmt(&self, fmt: &mut ::std::fmt::Formatter)
-> ::std::fmt::Result {
impl<T> ::$prefix::fmt::Debug for __IncompleteArrayField<T> {
fn fmt(&self, fmt: &mut ::$prefix::fmt::Formatter)
-> ::$prefix::fmt::Result {
fmt.write_str("__IncompleteArrayField")
}
}
)
.unwrap();

let incomplete_array_clone_impl = quote_item!(&ctx.ext_cx(),
impl<T> ::$prefix::clone::Clone for __IncompleteArrayField<T> {
#[inline]
fn clone(&self) -> Self {
Self::new()
}
}
)
.unwrap();

let incomplete_array_copy_impl = quote_item!(&ctx.ext_cx(),
impl<T> ::$prefix::marker::Copy for __IncompleteArrayField<T> {}
)
.unwrap();

let items = vec![
incomplete_array_decl,
incomplete_array_impl,
incomplete_array_debug_impl,
incomplete_array_clone_impl,
incomplete_array_copy_impl,
];

let old_items = mem::replace(result, items);
Expand Down
15 changes: 15 additions & 0 deletions tests/expectations/tests/class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ impl <T> ::std::fmt::Debug for __IncompleteArrayField<T> {
fmt.write_str("__IncompleteArrayField")
}
}
impl <T> ::std::clone::Clone for __IncompleteArrayField<T> {
#[inline]
fn clone(&self) -> Self { Self::new() }
}
impl <T> ::std::marker::Copy for __IncompleteArrayField<T> { }
#[repr(C)]
pub struct __BindgenUnionField<T>(::std::marker::PhantomData<T>);
impl <T> __BindgenUnionField<T> {
Expand Down Expand Up @@ -112,6 +117,16 @@ fn bindgen_test_layout_WithDtor() {
assert_eq!(::std::mem::align_of::<WithDtor>() , 4usize);
}
#[repr(C)]
pub struct IncompleteArrayNonCopiable {
pub whatever: *mut ::std::os::raw::c_void,
pub incomplete_array: __IncompleteArrayField<C>,
}
#[test]
fn bindgen_test_layout_IncompleteArrayNonCopiable() {
assert_eq!(::std::mem::size_of::<IncompleteArrayNonCopiable>() , 8usize);
assert_eq!(::std::mem::align_of::<IncompleteArrayNonCopiable>() , 8usize);
}
#[repr(C)]
#[derive(Debug, Copy)]
pub struct Union {
pub d: __BindgenUnionField<f32>,
Expand Down
5 changes: 5 additions & 0 deletions tests/headers/class.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ class WithDtor {
~WithDtor() {}
};

class IncompleteArrayNonCopiable {
void* whatever;
C incomplete_array[];
};

union Union {
float d;
int i;
Expand Down