mirror of
https://gitee.com/openharmony/third_party_rust_cxx
synced 2024-11-27 01:11:38 +00:00
Update tests to use c_char where appropriate
This commit is contained in:
parent
b3873dcd10
commit
36bd61615d
@ -12,6 +12,7 @@ rust_test(
|
||||
rust_library(
|
||||
name = "ffi",
|
||||
srcs = [
|
||||
"ffi/cast.rs",
|
||||
"ffi/lib.rs",
|
||||
"ffi/module.rs",
|
||||
],
|
||||
|
@ -15,6 +15,7 @@ rust_test(
|
||||
rust_library(
|
||||
name = "cxx_test_suite",
|
||||
srcs = [
|
||||
"ffi/cast.rs",
|
||||
"ffi/lib.rs",
|
||||
"ffi/module.rs",
|
||||
],
|
||||
|
14
tests/ffi/cast.rs
Normal file
14
tests/ffi/cast.rs
Normal file
@ -0,0 +1,14 @@
|
||||
use std::os::raw::c_char;
|
||||
use std::slice;
|
||||
|
||||
pub fn c_char_to_unsigned(slice: &[c_char]) -> &[u8] {
|
||||
let ptr = slice.as_ptr().cast::<u8>();
|
||||
let len = slice.len();
|
||||
unsafe { slice::from_raw_parts(ptr, len) }
|
||||
}
|
||||
|
||||
pub fn unsigned_to_c_char(slice: &[u8]) -> &[c_char] {
|
||||
let ptr = slice.as_ptr().cast::<c_char>();
|
||||
let len = slice.len();
|
||||
unsafe { slice::from_raw_parts(ptr, len) }
|
||||
}
|
@ -6,10 +6,12 @@
|
||||
clippy::unnecessary_wraps
|
||||
)]
|
||||
|
||||
pub mod cast;
|
||||
pub mod module;
|
||||
|
||||
use cxx::{CxxString, CxxVector, UniquePtr};
|
||||
use std::fmt::{self, Display};
|
||||
use std::os::raw::c_char;
|
||||
|
||||
#[cxx::bridge(namespace = "tests")]
|
||||
pub mod ffi {
|
||||
@ -81,7 +83,7 @@ pub mod ffi {
|
||||
fn c_return_ref(shared: &Shared) -> &usize;
|
||||
fn c_return_mut(shared: &mut Shared) -> &mut usize;
|
||||
fn c_return_str(shared: &Shared) -> &str;
|
||||
fn c_return_sliceu8(shared: &Shared) -> &[u8];
|
||||
fn c_return_slice_char(shared: &Shared) -> &[c_char];
|
||||
fn c_return_mutsliceu8(slice: &mut [u8]) -> &mut [u8];
|
||||
fn c_return_rust_string() -> String;
|
||||
fn c_return_unique_ptr_string() -> UniquePtr<CxxString>;
|
||||
@ -110,7 +112,7 @@ pub mod ffi {
|
||||
fn c_take_ref_r(r: &R);
|
||||
fn c_take_ref_c(c: &C);
|
||||
fn c_take_str(s: &str);
|
||||
fn c_take_sliceu8(s: &[u8]);
|
||||
fn c_take_slice_char(s: &[c_char]);
|
||||
fn c_take_slice_shared(s: &[Shared]);
|
||||
fn c_take_rust_string(s: String);
|
||||
fn c_take_unique_ptr_string(s: UniquePtr<CxxString>);
|
||||
@ -214,7 +216,7 @@ pub mod ffi {
|
||||
fn r_take_ref_r(r: &R);
|
||||
fn r_take_ref_c(c: &C);
|
||||
fn r_take_str(s: &str);
|
||||
fn r_take_sliceu8(s: &[u8]);
|
||||
fn r_take_slice_char(s: &[c_char]);
|
||||
fn r_take_rust_string(s: String);
|
||||
fn r_take_unique_ptr_string(s: UniquePtr<CxxString>);
|
||||
fn r_take_ref_vector(v: &CxxVector<u8>);
|
||||
@ -472,8 +474,9 @@ fn r_take_rust_string(s: String) {
|
||||
assert_eq!(s, "2020");
|
||||
}
|
||||
|
||||
fn r_take_sliceu8(s: &[u8]) {
|
||||
fn r_take_slice_char(s: &[c_char]) {
|
||||
assert_eq!(s.len(), 5);
|
||||
let s = cast::c_char_to_unsigned(s);
|
||||
assert_eq!(std::str::from_utf8(s).unwrap(), "2020\0");
|
||||
}
|
||||
|
||||
|
@ -75,10 +75,9 @@ rust::Str c_return_str(const Shared &shared) {
|
||||
return "2020";
|
||||
}
|
||||
|
||||
rust::Slice<const uint8_t> c_return_sliceu8(const Shared &shared) {
|
||||
rust::Slice<const char> c_return_slice_char(const Shared &shared) {
|
||||
(void)shared;
|
||||
return rust::Slice<const uint8_t>(
|
||||
reinterpret_cast<const uint8_t *>(SLICE_DATA), sizeof(SLICE_DATA));
|
||||
return rust::Slice<const char>(SLICE_DATA, sizeof(SLICE_DATA));
|
||||
}
|
||||
|
||||
rust::Slice<uint8_t> c_return_mutsliceu8(rust::Slice<uint8_t> slice) {
|
||||
@ -243,9 +242,8 @@ void c_take_str(rust::Str s) {
|
||||
}
|
||||
}
|
||||
|
||||
void c_take_sliceu8(rust::Slice<const uint8_t> s) {
|
||||
if (std::string(reinterpret_cast<const char *>(s.data()), s.size()) ==
|
||||
"2020") {
|
||||
void c_take_slice_char(rust::Slice<const char> s) {
|
||||
if (std::string(s.data(), s.size()) == "2020") {
|
||||
cxx_test_suite_set_correct();
|
||||
}
|
||||
}
|
||||
@ -626,8 +624,7 @@ extern "C" const char *cxx_run_test() noexcept {
|
||||
r_take_unique_ptr(std::unique_ptr<C>(new C{2020}));
|
||||
r_take_ref_c(C{2020});
|
||||
r_take_str(rust::Str("2020"));
|
||||
r_take_sliceu8(rust::Slice<const uint8_t>(
|
||||
reinterpret_cast<const uint8_t *>(SLICE_DATA), sizeof(SLICE_DATA)));
|
||||
r_take_slice_char(rust::Slice<const char>(SLICE_DATA, sizeof(SLICE_DATA)));
|
||||
r_take_rust_string(rust::String("2020"));
|
||||
r_take_unique_ptr_string(
|
||||
std::unique_ptr<std::string>(new std::string("2020")));
|
||||
|
@ -83,7 +83,7 @@ const size_t &c_return_ns_ref(const ::A::AShared &shared);
|
||||
const size_t &c_return_nested_ns_ref(const ::A::B::ABShared &shared);
|
||||
size_t &c_return_mut(Shared &shared);
|
||||
rust::Str c_return_str(const Shared &shared);
|
||||
rust::Slice<const uint8_t> c_return_sliceu8(const Shared &shared);
|
||||
rust::Slice<const char> c_return_slice_char(const Shared &shared);
|
||||
rust::Slice<uint8_t> c_return_mutsliceu8(rust::Slice<uint8_t> slice);
|
||||
rust::String c_return_rust_string();
|
||||
std::unique_ptr<std::string> c_return_unique_ptr_string();
|
||||
@ -114,7 +114,7 @@ void c_take_ref_r(const R &r);
|
||||
void c_take_ref_c(const C &c);
|
||||
void c_take_ref_ns_c(const ::H::H &h);
|
||||
void c_take_str(rust::Str s);
|
||||
void c_take_sliceu8(rust::Slice<const uint8_t> s);
|
||||
void c_take_slice_char(rust::Slice<const char> s);
|
||||
void c_take_slice_shared(rust::Slice<const Shared> s);
|
||||
void c_take_rust_string(rust::String s);
|
||||
void c_take_unique_ptr_string(std::unique_ptr<std::string> s);
|
||||
|
@ -1,7 +1,7 @@
|
||||
#![allow(clippy::assertions_on_constants, clippy::float_cmp, clippy::unit_cmp)]
|
||||
|
||||
use cxx_test_suite::module::ffi2;
|
||||
use cxx_test_suite::{ffi, R};
|
||||
use cxx_test_suite::{cast, ffi, R};
|
||||
use std::cell::Cell;
|
||||
use std::ffi::CStr;
|
||||
|
||||
@ -41,7 +41,10 @@ fn test_c_return() {
|
||||
assert_eq!(2020, *ffi::c_return_ns_ref(&ns_shared));
|
||||
assert_eq!(2020, *ffi::c_return_nested_ns_ref(&nested_ns_shared));
|
||||
assert_eq!("2020", ffi::c_return_str(&shared));
|
||||
assert_eq!(b"2020\0", ffi::c_return_sliceu8(&shared));
|
||||
assert_eq!(
|
||||
b"2020\0",
|
||||
cast::c_char_to_unsigned(ffi::c_return_slice_char(&shared)),
|
||||
);
|
||||
assert_eq!("2020", ffi::c_return_rust_string());
|
||||
assert_eq!("2020", ffi::c_return_unique_ptr_string().to_str().unwrap());
|
||||
assert_eq!(4, ffi::c_return_unique_ptr_vector_u8().len());
|
||||
@ -116,7 +119,7 @@ fn test_c_take() {
|
||||
check!(ffi2::c_take_ref_ns_c(&unique_ptr_ns));
|
||||
check!(cxx_test_suite::module::ffi::c_take_unique_ptr(unique_ptr));
|
||||
check!(ffi::c_take_str("2020"));
|
||||
check!(ffi::c_take_sliceu8(b"2020"));
|
||||
check!(ffi::c_take_slice_char(cast::unsigned_to_c_char(b"2020")));
|
||||
check!(ffi::c_take_slice_shared(&[
|
||||
ffi::Shared { z: 2020 },
|
||||
ffi::Shared { z: 2021 },
|
||||
|
Loading…
Reference in New Issue
Block a user