Add test with qsort on a mut slice of opaque Rust types

This commit is contained in:
David Tolnay 2020-12-27 19:26:52 -08:00
parent f62458f075
commit fe67bf4b6a
No known key found for this signature in database
GPG Key ID: F9BA143B95FF6D82
4 changed files with 24 additions and 2 deletions

View File

@ -122,6 +122,7 @@ pub mod ffi {
fn c_take_slice_shared(s: &[Shared]);
fn c_take_slice_shared_sort(s: &mut [Shared]);
fn c_take_slice_r(s: &[R]);
fn c_take_slice_r_sort(s: &mut [R]);
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

@ -1,5 +1,6 @@
#include "tests/ffi/tests.h"
#include "tests/ffi/lib.rs.h"
#include <cstdlib>
#include <cstring>
#include <iterator>
#include <memory>
@ -290,7 +291,21 @@ void c_take_slice_shared_sort(rust::Slice<Shared> s) {
}
void c_take_slice_r(rust::Slice<const R> s) {
if (s.size() == 2 && s[0].get() == 2020 && s[1].get() == 2021) {
if (s.size() == 3 && s[0].get() == 2020 && s[1].get() == 2050) {
cxx_test_suite_set_correct();
}
}
bool operator<(const R &a, const R &b) noexcept { return a.get() < b.get(); }
void c_take_slice_r_sort(rust::Slice<R> s) {
std::qsort(s.data(), s.size(), rust::size_of<decltype(s)::value_type>(),
[](const void *fst, const void *snd) {
auto &a = *static_cast<const R *>(fst);
auto &b = *static_cast<const R *>(snd);
return a < b ? -1 : b < a ? 1 : 0;
});
if (s[0].get() == 2020 && s[1].get() == 2021 && s[2].get() == 2050) {
cxx_test_suite_set_correct();
}
}

View File

@ -125,6 +125,7 @@ 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_slice_r(rust::Slice<const R> s);
void c_take_slice_r_sort(rust::Slice<R> 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

@ -132,11 +132,16 @@ fn test_c_take() {
ffi::Shared { z: 4 },
];
check!(ffi::c_take_slice_shared_sort(shared_sort_slice));
check!(ffi::c_take_slice_r(&[R(2020), R(2021)]));
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);
let r_sort_slice = &mut [R(2020), R(2050), R(2021)];
check!(ffi::c_take_slice_r(r_sort_slice));
check!(ffi::c_take_slice_r_sort(r_sort_slice));
assert_eq!(r_sort_slice[0].0, 2020);
assert_eq!(r_sort_slice[1].0, 2021);
assert_eq!(r_sort_slice[2].0, 2050);
check!(ffi::c_take_rust_string("2020".to_owned()));
check!(ffi::c_take_unique_ptr_string(
ffi::c_return_unique_ptr_string()