Skip to content

Commit

Permalink
Add more comprehensive tests for is_sorted and friends
Browse files Browse the repository at this point in the history
See #53485 and #55045.
  • Loading branch information
bluebear94 committed Jun 16, 2023
1 parent c84d5e7 commit c2e4e98
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 1 deletion.
31 changes: 30 additions & 1 deletion library/core/tests/iter/traits/iterator.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use core::cmp::Ordering;
use core::num::NonZeroUsize;

/// A wrapper struct that implements `Eq` and `Ord` based on the wrapped
Expand Down Expand Up @@ -371,11 +372,39 @@ fn test_by_ref() {

#[test]
fn test_is_sorted() {
// Tests on integers
assert!([1, 2, 2, 9].iter().is_sorted());
assert!(![1, 3, 2].iter().is_sorted());
assert!([0].iter().is_sorted());
assert!(std::iter::empty::<i32>().is_sorted());
assert!([0, 0].iter().is_sorted());
assert!(core::iter::empty::<i32>().is_sorted());

// Tests on floats
assert!([1.0f32, 2.0, 2.0, 9.0].iter().is_sorted());
assert!(![1.0f32, 3.0f32, 2.0f32].iter().is_sorted());
assert!([0.0f32].iter().is_sorted());
assert!([0.0f32, 0.0f32].iter().is_sorted());
// Test cases with NaNs
assert!([f32::NAN].iter().is_sorted());
assert!(![f32::NAN, f32::NAN].iter().is_sorted());
assert!(![0.0, 1.0, f32::NAN].iter().is_sorted());
// Tests from <https://github.com/rust-lang/rust/pull/55045#discussion_r229689884>
assert!(![f32::NAN, f32::NAN, f32::NAN].iter().is_sorted());
assert!(![1.0, f32::NAN, 2.0].iter().is_sorted());
assert!(![2.0, f32::NAN, 1.0].iter().is_sorted());
assert!(![2.0, f32::NAN, 1.0, 7.0].iter().is_sorted());
assert!(![2.0, f32::NAN, 1.0, 0.0].iter().is_sorted());
assert!(![-f32::NAN, -1.0, 0.0, 1.0, f32::NAN].iter().is_sorted());
assert!(![f32::NAN, -f32::NAN, -1.0, 0.0, 1.0].iter().is_sorted());
assert!(![1.0, f32::NAN, -f32::NAN, -1.0, 0.0].iter().is_sorted());
assert!(![0.0, 1.0, f32::NAN, -f32::NAN, -1.0].iter().is_sorted());
assert!(![-1.0, 0.0, 1.0, f32::NAN, -f32::NAN].iter().is_sorted());

// Tests for is_sorted_by
assert!(![6, 2, 8, 5, 1, -60, 1337].iter().is_sorted());
assert!([6, 2, 8, 5, 1, -60, 1337].iter().is_sorted_by(|_, _| Some(Ordering::Less)));

// Tests for is_sorted_by_key
assert!([-2, -1, 0, 3].iter().is_sorted());
assert!(![-2i32, -1, 0, 3].iter().is_sorted_by_key(|n| n.abs()));
assert!(!["c", "bb", "aaa"].iter().is_sorted());
Expand Down
28 changes: 28 additions & 0 deletions library/core/tests/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2278,11 +2278,39 @@ fn test_copy_within_panics_src_out_of_bounds() {
fn test_is_sorted() {
let empty: [i32; 0] = [];

// Tests on integers
assert!([1, 2, 2, 9].is_sorted());
assert!(![1, 3, 2].is_sorted());
assert!([0].is_sorted());
assert!([0, 0].is_sorted());
assert!(empty.is_sorted());

// Tests on floats
assert!([1.0f32, 2.0, 2.0, 9.0].is_sorted());
assert!(![1.0f32, 3.0f32, 2.0f32].is_sorted());
assert!([0.0f32].is_sorted());
assert!([0.0f32, 0.0f32].is_sorted());
// Test cases with NaNs
assert!([f32::NAN].is_sorted());
assert!(![f32::NAN, f32::NAN].is_sorted());
assert!(![0.0, 1.0, f32::NAN].is_sorted());
// Tests from <https://github.com/rust-lang/rust/pull/55045#discussion_r229689884>
assert!(![f32::NAN, f32::NAN, f32::NAN].is_sorted());
assert!(![1.0, f32::NAN, 2.0].is_sorted());
assert!(![2.0, f32::NAN, 1.0].is_sorted());
assert!(![2.0, f32::NAN, 1.0, 7.0].is_sorted());
assert!(![2.0, f32::NAN, 1.0, 0.0].is_sorted());
assert!(![-f32::NAN, -1.0, 0.0, 1.0, f32::NAN].is_sorted());
assert!(![f32::NAN, -f32::NAN, -1.0, 0.0, 1.0].is_sorted());
assert!(![1.0, f32::NAN, -f32::NAN, -1.0, 0.0].is_sorted());
assert!(![0.0, 1.0, f32::NAN, -f32::NAN, -1.0].is_sorted());
assert!(![-1.0, 0.0, 1.0, f32::NAN, -f32::NAN].is_sorted());

// Tests for is_sorted_by
assert!(![6, 2, 8, 5, 1, -60, 1337].is_sorted());
assert!([6, 2, 8, 5, 1, -60, 1337].is_sorted_by(|_, _| Some(Ordering::Less)));

// Tests for is_sorted_by_key
assert!([-2, -1, 0, 3].is_sorted());
assert!(![-2i32, -1, 0, 3].is_sorted_by_key(|n| n.abs()));
assert!(!["c", "bb", "aaa"].is_sorted());
Expand Down

0 comments on commit c2e4e98

Please sign in to comment.