Skip to content

Commit d47af7a

Browse files
committed
Tests for opaque C types in namepsaces
1 parent 585bb0b commit d47af7a

File tree

4 files changed

+27
-0
lines changed

4 files changed

+27
-0
lines changed

tests/ffi/extra.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ pub mod ffi2 {
2525
#[namespace (namespace = G)]
2626
type G = crate::other::G;
2727

28+
#[namespace(namespace = H)]
29+
type H;
30+
2831
fn c_take_trivial_ptr(d: UniquePtr<D>);
2932
fn c_take_trivial_ref(d: &D);
3033
fn c_take_trivial(d: D);
@@ -41,5 +44,7 @@ pub mod ffi2 {
4144
fn c_return_trivial_ns() -> G;
4245
fn c_return_opaque_ptr() -> UniquePtr<E>;
4346
fn c_return_ns_opaque_ptr() -> UniquePtr<F>;
47+
fn c_return_ns_unique_ptr() -> UniquePtr<H>;
48+
fn c_take_ref_ns_c(h: &H);
4449
}
4550
}

tests/ffi/tests.cc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ std::unique_ptr<C> c_return_unique_ptr() {
5555
return std::unique_ptr<C>(new C{2020});
5656
}
5757

58+
std::unique_ptr<::H::H> c_return_ns_unique_ptr() {
59+
return std::unique_ptr<::H::H>(new ::H::H{"hello"});
60+
}
61+
5862
const size_t &c_return_ref(const Shared &shared) { return shared.z; }
5963

6064
const size_t &c_return_ns_ref(const ::A::AShared &shared) { return shared.z; }
@@ -220,6 +224,12 @@ void c_take_ref_c(const C &c) {
220224
}
221225
}
222226

227+
void c_take_ref_ns_c(const ::H::H &h) {
228+
if (h.h == "hello") {
229+
cxx_test_suite_set_correct();
230+
}
231+
}
232+
223233
void c_take_str(rust::Str s) {
224234
if (std::string(s) == "2020") {
225235
cxx_test_suite_set_correct();

tests/ffi/tests.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,13 @@ namespace G {
2525
};
2626
}
2727

28+
namespace H {
29+
class H {
30+
public:
31+
std::string h;
32+
};
33+
}
34+
2835
namespace tests {
2936

3037
struct R;
@@ -70,6 +77,7 @@ ::A::AShared c_return_ns_shared();
7077
::A::B::ABShared c_return_nested_ns_shared();
7178
rust::Box<R> c_return_box();
7279
std::unique_ptr<C> c_return_unique_ptr();
80+
std::unique_ptr<::H::H> c_return_ns_unique_ptr();
7381
const size_t &c_return_ref(const Shared &shared);
7482
const size_t &c_return_ns_ref(const ::A::AShared &shared);
7583
const size_t &c_return_nested_ns_ref(const ::A::B::ABShared &shared);
@@ -103,6 +111,7 @@ void c_take_box(rust::Box<R> r);
103111
void c_take_unique_ptr(std::unique_ptr<C> c);
104112
void c_take_ref_r(const R &r);
105113
void c_take_ref_c(const C &c);
114+
void c_take_ref_ns_c(const ::H::H &h);
106115
void c_take_str(rust::Str s);
107116
void c_take_sliceu8(rust::Slice<uint8_t> s);
108117
void c_take_rust_string(rust::String s);

tests/test.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ fn test_c_return() {
3030
assert_eq!(2020, ffi::c_return_shared().z);
3131
assert_eq!(2020, *ffi::c_return_box());
3232
ffi::c_return_unique_ptr();
33+
ffi2::c_return_ns_unique_ptr();
3334
assert_eq!(2020, *ffi::c_return_ref(&shared));
3435
assert_eq!(2020, *ffi::c_return_ns_ref(&ns_shared));
3536
assert_eq!(2020, *ffi::c_return_nested_ns_ref(&nested_ns_shared));
@@ -97,13 +98,15 @@ fn test_c_try_return() {
9798
#[test]
9899
fn test_c_take() {
99100
let unique_ptr = ffi::c_return_unique_ptr();
101+
let unique_ptr_ns = ffi2::c_return_ns_unique_ptr();
100102

101103
check!(ffi::c_take_primitive(2020));
102104
check!(ffi::c_take_shared(ffi::Shared { z: 2020 }));
103105
check!(ffi::c_take_ns_shared(ffi::AShared { z: 2020 }));
104106
check!(ffi::c_take_nested_ns_shared(ffi::ABShared { z: 2020 }));
105107
check!(ffi::c_take_box(Box::new(2020)));
106108
check!(ffi::c_take_ref_c(&unique_ptr));
109+
check!(ffi2::c_take_ref_ns_c(&unique_ptr_ns));
107110
check!(cxx_test_suite::module::ffi::c_take_unique_ptr(unique_ptr));
108111
check!(ffi::c_take_str("2020"));
109112
check!(ffi::c_take_sliceu8(b"2020"));

0 commit comments

Comments
 (0)