Skip to content

Commit 71bb0e7

Browse files
committed
Port sort-research-rs test suite Rust stdlib tests
This commit is a followup to rust-lang#124032. It replaces the tests that test the various sort functions in the standard library with a test-suite developed as part of https://github.com/Voultapher/sort-research-rs. The current tests suffer a couple of problems: - They don't cover important real world patterns that the implementations take advantage of and execute special code for. - The input lengths tested miss out on code paths. For example, important safety property tests never reach the quicksort part of the implementation. - The miri side is often limited to `len <= 20` which means it very thoroughly tests the insertion sort, which accounts for 19 out of 1.5k LoC. - They are split into to core and alloc, causing code duplication and uneven coverage. - The randomness is not repeatable, as it relies on `std::hash::RandomState::new().build_hasher()`. Most of these issues existed before rust-lang#124032, but they are intensified by it. One thing that is new and requires additional testing, is that the new sort implementations specialize based on type properties. For example `Freeze` and non `Freeze` execute different code paths. Effectively there are three dimensions that matter: - Input type - Input length - Input pattern The ported test-suite tests various properties along all three dimensions, greatly improving test coverage. It side-steps the miri issue by preferring sampled approaches. For example the test that checks if after a panic the set of elements is still the original one, doesn't do so for every single possible panic opportunity but rather it picks one at random, and performs this test across a range of input length, which varies the panic point across them. This allows regular execution to easily test inputs of length 10k, and miri execution up to 100 which covers significantly more code. The randomness used is tied to a fixed - but random per process execution - seed. This allows for fully repeatable tests and fuzzer like exploration across multiple runs. Structure wise, the tests are previously found in the core integration tests for `sort_unstable` and alloc unit tests for `sort`. The new test-suite was developed to be a purely black-box approach, which makes integration testing the better place, because it can't accidentally rely on internal access. Because unwinding support is required the tests can't be in core, even if the implementation is, so they are now part of the alloc integration tests. Are there architectures that can only build and test core and not alloc? If so, do such platforms require sort testing? For what it's worth the current implementation state passes miri `--target mips64-unknown-linux-gnuabi64` which is big endian. The test-suite also contains tests for properties that were and are given by the current and previous implementations, and likely relied upon by users but weren't tested. For example `self_cmp` tests that the two parameters `a` and `b` passed into the comparison function are never references to the same object, which if the user is sorting for example a `&mut [Mutex<i32>]` could lead to a deadlock. Instead of using the hashed caller location as rand seed, it uses seconds since unix epoch / 10, which given timestamps in the CI should be reasonably easy to reproduce, but also allows fuzzer like space exploration.
1 parent e9df22f commit 71bb0e7

File tree

10 files changed

+1955
-434
lines changed

10 files changed

+1955
-434
lines changed

library/alloc/src/slice.rs

+10-14
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,6 @@ use core::cmp::Ordering::{self, Less};
1919
use core::mem::{self, MaybeUninit};
2020
#[cfg(not(no_global_oom_handling))]
2121
use core::ptr;
22-
#[cfg(not(no_global_oom_handling))]
23-
use core::slice::sort;
24-
25-
use crate::alloc::Allocator;
26-
#[cfg(not(no_global_oom_handling))]
27-
use crate::alloc::Global;
28-
#[cfg(not(no_global_oom_handling))]
29-
use crate::borrow::ToOwned;
30-
use crate::boxed::Box;
31-
use crate::vec::Vec;
32-
33-
#[cfg(test)]
34-
mod tests;
35-
3622
#[unstable(feature = "array_chunks", issue = "74985")]
3723
pub use core::slice::ArrayChunks;
3824
#[unstable(feature = "array_chunks", issue = "74985")]
@@ -43,6 +29,8 @@ pub use core::slice::ArrayWindows;
4329
pub use core::slice::EscapeAscii;
4430
#[stable(feature = "slice_get_slice", since = "1.28.0")]
4531
pub use core::slice::SliceIndex;
32+
#[cfg(not(no_global_oom_handling))]
33+
use core::slice::sort;
4634
#[stable(feature = "slice_group_by", since = "1.77.0")]
4735
pub use core::slice::{ChunkBy, ChunkByMut};
4836
#[stable(feature = "rust1", since = "1.0.0")]
@@ -83,6 +71,14 @@ pub use hack::into_vec;
8371
#[cfg(test)]
8472
pub use hack::to_vec;
8573

74+
use crate::alloc::Allocator;
75+
#[cfg(not(no_global_oom_handling))]
76+
use crate::alloc::Global;
77+
#[cfg(not(no_global_oom_handling))]
78+
use crate::borrow::ToOwned;
79+
use crate::boxed::Box;
80+
use crate::vec::Vec;
81+
8682
// HACK(japaric): With cfg(test) `impl [T]` is not available, these three
8783
// functions are actually methods that are in `impl [T]` but not in
8884
// `core::slice::SliceExt` - we need to supply these functions for the

0 commit comments

Comments
 (0)