Add test of sorting rust::Slice

This commit is contained in:
David Tolnay 2020-12-27 00:19:38 -08:00
parent e1ad7fab51
commit 8f2180ce23
No known key found for this signature in database
GPG Key ID: F9BA143B95FF6D82
4 changed files with 22 additions and 0 deletions

View File

@ -120,6 +120,7 @@ pub mod ffi {
fn c_take_str(s: &str);
fn c_take_slice_char(s: &[c_char]);
fn c_take_slice_shared(s: &[Shared]);
fn c_take_slice_shared_sort(s: &mut [Shared]);
fn c_take_rust_string(s: String);
fn c_take_unique_ptr_string(s: UniquePtr<CxxString>);
fn c_take_unique_ptr_vector_u8(v: UniquePtr<CxxVector<u8>>);

View File

@ -280,6 +280,15 @@ void c_take_slice_shared(rust::Slice<const Shared> s) {
}
}
void c_take_slice_shared_sort(rust::Slice<Shared> s) {
// Exercise requirements of RandomAccessIterator.
// https://en.cppreference.com/w/cpp/named_req/RandomAccessIterator
std::sort(s.begin(), s.end());
if (s[0].z == 0 && s[1].z == 2 && s[2].z == 4 && s[3].z == 7) {
cxx_test_suite_set_correct();
}
}
void c_take_rust_string(rust::String s) {
if (std::string(s) == "2020") {
cxx_test_suite_set_correct();

View File

@ -123,6 +123,7 @@ void c_take_ref_ns_c(const ::H::H &h);
void c_take_str(rust::Str s);
void c_take_slice_char(rust::Slice<const char> s);
void c_take_slice_shared(rust::Slice<const Shared> s);
void c_take_slice_shared_sort(rust::Slice<Shared> s);
void c_take_rust_string(rust::String s);
void c_take_unique_ptr_string(std::unique_ptr<std::string> s);
void c_take_unique_ptr_vector_u8(std::unique_ptr<std::vector<uint8_t>> v);

View File

@ -125,6 +125,17 @@ fn test_c_take() {
ffi::Shared { z: 2020 },
ffi::Shared { z: 2021 },
]));
let shared_sort_slice = &mut [
ffi::Shared { z: 2 },
ffi::Shared { z: 0 },
ffi::Shared { z: 7 },
ffi::Shared { z: 4 },
];
check!(ffi::c_take_slice_shared_sort(shared_sort_slice));
assert_eq!(shared_sort_slice[0].z, 0);
assert_eq!(shared_sort_slice[1].z, 2);
assert_eq!(shared_sort_slice[2].z, 4);
assert_eq!(shared_sort_slice[3].z, 7);
check!(ffi::c_take_rust_string("2020".to_owned()));
check!(ffi::c_take_unique_ptr_string(
ffi::c_return_unique_ptr_string()