Skip to content

Commit

Permalink
Rollup merge of rust-lang#76718 - poliorcetics:vec-ui-to-unit-test, r…
Browse files Browse the repository at this point in the history
…=jyn514

Move Vec UI tests to unit tests when possible

Helps with rust-lang#76268.

I'm moving the tests using `Vec` or `VecDeque`.

`@rustbot` modify labels: A-testsuite C-cleanup T-libs
  • Loading branch information
JohnTitor authored Nov 5, 2020
2 parents 0fb0025 + 1bdee96 commit 30bb1f9
Show file tree
Hide file tree
Showing 8 changed files with 193 additions and 208 deletions.
120 changes: 120 additions & 0 deletions library/alloc/tests/slice.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::cell::Cell;
use std::cmp::Ordering::{self, Equal, Greater, Less};
use std::convert::identity;
use std::mem;
use std::panic;
use std::rc::Rc;
Expand Down Expand Up @@ -1778,3 +1779,122 @@ fn repeat_generic_slice() {
assert_eq!([1, 2, 3, 4].repeat(1), vec![1, 2, 3, 4]);
assert_eq!([1, 2, 3, 4].repeat(3), vec![1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4]);
}

#[test]
#[allow(unreachable_patterns)]
fn subslice_patterns() {
// This test comprehensively checks the passing static and dynamic semantics
// of subslice patterns `..`, `x @ ..`, `ref x @ ..`, and `ref mut @ ..`
// in slice patterns `[$($pat), $(,)?]` .

#[derive(PartialEq, Debug, Clone)]
struct N(u8);

macro_rules! n {
($($e:expr),* $(,)?) => {
[$(N($e)),*]
}
}

macro_rules! c {
($inp:expr, $typ:ty, $out:expr $(,)?) => {
assert_eq!($out, identity::<$typ>($inp));
};
}

macro_rules! m {
($e:expr, $p:pat => $b:expr) => {
match $e {
$p => $b,
_ => panic!(),
}
};
}

// == Slices ==

// Matching slices using `ref` patterns:
let mut v = vec![N(0), N(1), N(2), N(3), N(4)];
let mut vc = (0..=4).collect::<Vec<u8>>();

let [..] = v[..]; // Always matches.
m!(v[..], [N(0), ref sub @ .., N(4)] => c!(sub, &[N], n![1, 2, 3]));
m!(v[..], [N(0), ref sub @ ..] => c!(sub, &[N], n![1, 2, 3, 4]));
m!(v[..], [ref sub @ .., N(4)] => c!(sub, &[N], n![0, 1, 2, 3]));
m!(v[..], [ref sub @ .., _, _, _, _, _] => c!(sub, &[N], &n![] as &[N]));
m!(v[..], [_, _, _, _, _, ref sub @ ..] => c!(sub, &[N], &n![] as &[N]));
m!(vc[..], [x, .., y] => c!((x, y), (u8, u8), (0, 4)));

// Matching slices using `ref mut` patterns:
let [..] = v[..]; // Always matches.
m!(v[..], [N(0), ref mut sub @ .., N(4)] => c!(sub, &mut [N], n![1, 2, 3]));
m!(v[..], [N(0), ref mut sub @ ..] => c!(sub, &mut [N], n![1, 2, 3, 4]));
m!(v[..], [ref mut sub @ .., N(4)] => c!(sub, &mut [N], n![0, 1, 2, 3]));
m!(v[..], [ref mut sub @ .., _, _, _, _, _] => c!(sub, &mut [N], &mut n![] as &mut [N]));
m!(v[..], [_, _, _, _, _, ref mut sub @ ..] => c!(sub, &mut [N], &mut n![] as &mut [N]));
m!(vc[..], [x, .., y] => c!((x, y), (u8, u8), (0, 4)));

// Matching slices using default binding modes (&):
let [..] = &v[..]; // Always matches.
m!(&v[..], [N(0), sub @ .., N(4)] => c!(sub, &[N], n![1, 2, 3]));
m!(&v[..], [N(0), sub @ ..] => c!(sub, &[N], n![1, 2, 3, 4]));
m!(&v[..], [sub @ .., N(4)] => c!(sub, &[N], n![0, 1, 2, 3]));
m!(&v[..], [sub @ .., _, _, _, _, _] => c!(sub, &[N], &n![] as &[N]));
m!(&v[..], [_, _, _, _, _, sub @ ..] => c!(sub, &[N], &n![] as &[N]));
m!(&vc[..], [x, .., y] => c!((x, y), (&u8, &u8), (&0, &4)));

// Matching slices using default binding modes (&mut):
let [..] = &mut v[..]; // Always matches.
m!(&mut v[..], [N(0), sub @ .., N(4)] => c!(sub, &mut [N], n![1, 2, 3]));
m!(&mut v[..], [N(0), sub @ ..] => c!(sub, &mut [N], n![1, 2, 3, 4]));
m!(&mut v[..], [sub @ .., N(4)] => c!(sub, &mut [N], n![0, 1, 2, 3]));
m!(&mut v[..], [sub @ .., _, _, _, _, _] => c!(sub, &mut [N], &mut n![] as &mut [N]));
m!(&mut v[..], [_, _, _, _, _, sub @ ..] => c!(sub, &mut [N], &mut n![] as &mut [N]));
m!(&mut vc[..], [x, .., y] => c!((x, y), (&mut u8, &mut u8), (&mut 0, &mut 4)));

// == Arrays ==
let mut v = n![0, 1, 2, 3, 4];
let vc = [0, 1, 2, 3, 4];

// Matching arrays by value:
m!(v.clone(), [N(0), sub @ .., N(4)] => c!(sub, [N; 3], n![1, 2, 3]));
m!(v.clone(), [N(0), sub @ ..] => c!(sub, [N; 4], n![1, 2, 3, 4]));
m!(v.clone(), [sub @ .., N(4)] => c!(sub, [N; 4], n![0, 1, 2, 3]));
m!(v.clone(), [sub @ .., _, _, _, _, _] => c!(sub, [N; 0], n![] as [N; 0]));
m!(v.clone(), [_, _, _, _, _, sub @ ..] => c!(sub, [N; 0], n![] as [N; 0]));
m!(v.clone(), [x, .., y] => c!((x, y), (N, N), (N(0), N(4))));
m!(v.clone(), [..] => ());

// Matching arrays by ref patterns:
m!(v, [N(0), ref sub @ .., N(4)] => c!(sub, &[N; 3], &n![1, 2, 3]));
m!(v, [N(0), ref sub @ ..] => c!(sub, &[N; 4], &n![1, 2, 3, 4]));
m!(v, [ref sub @ .., N(4)] => c!(sub, &[N; 4], &n![0, 1, 2, 3]));
m!(v, [ref sub @ .., _, _, _, _, _] => c!(sub, &[N; 0], &n![] as &[N; 0]));
m!(v, [_, _, _, _, _, ref sub @ ..] => c!(sub, &[N; 0], &n![] as &[N; 0]));
m!(vc, [x, .., y] => c!((x, y), (u8, u8), (0, 4)));

// Matching arrays by ref mut patterns:
m!(v, [N(0), ref mut sub @ .., N(4)] => c!(sub, &mut [N; 3], &mut n![1, 2, 3]));
m!(v, [N(0), ref mut sub @ ..] => c!(sub, &mut [N; 4], &mut n![1, 2, 3, 4]));
m!(v, [ref mut sub @ .., N(4)] => c!(sub, &mut [N; 4], &mut n![0, 1, 2, 3]));
m!(v, [ref mut sub @ .., _, _, _, _, _] => c!(sub, &mut [N; 0], &mut n![] as &mut [N; 0]));
m!(v, [_, _, _, _, _, ref mut sub @ ..] => c!(sub, &mut [N; 0], &mut n![] as &mut [N; 0]));

// Matching arrays by default binding modes (&):
m!(&v, [N(0), sub @ .., N(4)] => c!(sub, &[N; 3], &n![1, 2, 3]));
m!(&v, [N(0), sub @ ..] => c!(sub, &[N; 4], &n![1, 2, 3, 4]));
m!(&v, [sub @ .., N(4)] => c!(sub, &[N; 4], &n![0, 1, 2, 3]));
m!(&v, [sub @ .., _, _, _, _, _] => c!(sub, &[N; 0], &n![] as &[N; 0]));
m!(&v, [_, _, _, _, _, sub @ ..] => c!(sub, &[N; 0], &n![] as &[N; 0]));
m!(&v, [..] => ());
m!(&v, [x, .., y] => c!((x, y), (&N, &N), (&N(0), &N(4))));

// Matching arrays by default binding modes (&mut):
m!(&mut v, [N(0), sub @ .., N(4)] => c!(sub, &mut [N; 3], &mut n![1, 2, 3]));
m!(&mut v, [N(0), sub @ ..] => c!(sub, &mut [N; 4], &mut n![1, 2, 3, 4]));
m!(&mut v, [sub @ .., N(4)] => c!(sub, &mut [N; 4], &mut n![0, 1, 2, 3]));
m!(&mut v, [sub @ .., _, _, _, _, _] => c!(sub, &mut [N; 0], &mut n![] as &[N; 0]));
m!(&mut v, [_, _, _, _, _, sub @ ..] => c!(sub, &mut [N; 0], &mut n![] as &[N; 0]));
m!(&mut v, [..] => ());
m!(&mut v, [x, .., y] => c!((x, y), (&mut N, &mut N), (&mut N(0), &mut N(4))));
}
44 changes: 43 additions & 1 deletion library/alloc/tests/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::cell::Cell;
use std::collections::TryReserveError::*;
use std::fmt::Debug;
use std::iter::InPlaceIterable;
use std::mem::size_of;
use std::mem::{size_of, swap};
use std::ops::Bound::*;
use std::panic::{catch_unwind, AssertUnwindSafe};
use std::rc::Rc;
Expand Down Expand Up @@ -1912,3 +1912,45 @@ fn test_vec_cycle_wrapped() {
c3.refs.v[0].set(Some(&c1));
c3.refs.v[1].set(Some(&c2));
}

#[test]
fn test_zero_sized_vec_push() {
const N: usize = 8;

for len in 0..N {
let mut tester = Vec::with_capacity(len);
assert_eq!(tester.len(), 0);
assert!(tester.capacity() >= len);
for _ in 0..len {
tester.push(());
}
assert_eq!(tester.len(), len);
assert_eq!(tester.iter().count(), len);
tester.clear();
}
}

#[test]
fn test_vec_macro_repeat() {
assert_eq!(vec![1; 3], vec![1, 1, 1]);
assert_eq!(vec![1; 2], vec![1, 1]);
assert_eq!(vec![1; 1], vec![1]);
assert_eq!(vec![1; 0], vec![]);

// from_elem syntax (see RFC 832)
let el = Box::new(1);
let n = 3;
assert_eq!(vec![el; n], vec![Box::new(1), Box::new(1), Box::new(1)]);
}

#[test]
fn test_vec_swap() {
let mut a: Vec<isize> = vec![0, 1, 2, 3, 4, 5, 6];
a.swap(2, 4);
assert_eq!(a[2], 4);
assert_eq!(a[4], 2);
let mut n = 42;
swap(&mut n, &mut a[0]);
assert_eq!(a[0], 42);
assert_eq!(n, 0);
}
30 changes: 30 additions & 0 deletions library/alloc/tests/vec_deque.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1698,3 +1698,33 @@ fn test_binary_search_by_key() {
assert_eq!(deque.binary_search_by_key(&3, |&(v,)| v), Ok(2));
assert_eq!(deque.binary_search_by_key(&4, |&(v,)| v), Err(3));
}

#[test]
fn test_zero_sized_push() {
const N: usize = 8;

// Zero sized type
struct Zst;

// Test that for all possible sequences of push_front / push_back,
// we end up with a deque of the correct size

for len in 0..N {
let mut tester = VecDeque::with_capacity(len);
assert_eq!(tester.len(), 0);
assert!(tester.capacity() >= len);
for case in 0..(1 << len) {
assert_eq!(tester.len(), 0);
for bit in 0..len {
if case & (1 << bit) != 0 {
tester.push_front(Zst);
} else {
tester.push_back(Zst);
}
}
assert_eq!(tester.len(), len);
assert_eq!(tester.iter().count(), len);
tester.clear();
}
}
}
126 changes: 0 additions & 126 deletions src/test/ui/array-slice-vec/subslice-patterns-pass.rs

This file was deleted.

15 changes: 0 additions & 15 deletions src/test/ui/array-slice-vec/vec-macro-repeat.rs

This file was deleted.

14 changes: 0 additions & 14 deletions src/test/ui/swap-2.rs

This file was deleted.

Loading

0 comments on commit 30bb1f9

Please sign in to comment.