test nth better

This commit is contained in:
Ralf Jung 2018-07-19 12:28:43 +02:00
parent cbdba2b4c2
commit 60b06369ee

View File

@ -395,7 +395,7 @@ fn test_windows_zip() {
fn test_iter_ref_consistency() {
use std::fmt::Debug;
fn helper<T : Copy + Debug + PartialEq>(x : T) {
fn test<T : Copy + Debug + PartialEq>(x : T) {
let v : &[T] = &[x, x, x];
let v_ptrs : [*const T; 3] = match v {
[ref v1, ref v2, ref v3] => [v1 as *const _, v2 as *const _, v3 as *const _],
@ -403,6 +403,7 @@ fn test_iter_ref_consistency() {
};
let len = v.len();
// nth(i)
for i in 0..len {
assert_eq!(&v[i] as *const _, v_ptrs[i]); // check the v_ptrs array, just to be sure
let nth = v.iter().nth(i).unwrap();
@ -410,9 +411,20 @@ fn test_iter_ref_consistency() {
}
assert_eq!(v.iter().nth(len), None, "nth(len) should return None");
// stepping through with nth(0)
{
let mut it = v.iter();
for i in 0..len{
for i in 0..len {
let next = it.nth(0).unwrap();
assert_eq!(next as *const _, v_ptrs[i]);
}
assert_eq!(it.nth(0), None);
}
// next()
{
let mut it = v.iter();
for i in 0..len {
let remaining = len - i;
assert_eq!(it.size_hint(), (remaining, Some(remaining)));
@ -423,9 +435,10 @@ fn test_iter_ref_consistency() {
assert_eq!(it.next(), None, "The final call to next() should return None");
}
// next_back()
{
let mut it = v.iter();
for i in 0..len{
for i in 0..len {
let remaining = len - i;
assert_eq!(it.size_hint(), (remaining, Some(remaining)));
@ -437,7 +450,7 @@ fn test_iter_ref_consistency() {
}
}
fn helper_mut<T : Copy + Debug + PartialEq>(x : T) {
fn test_mut<T : Copy + Debug + PartialEq>(x : T) {
let v : &mut [T] = &mut [x, x, x];
let v_ptrs : [*mut T; 3] = match v {
[ref v1, ref v2, ref v3] =>
@ -446,6 +459,7 @@ fn test_iter_ref_consistency() {
};
let len = v.len();
// nth(i)
for i in 0..len {
assert_eq!(&mut v[i] as *mut _, v_ptrs[i]); // check the v_ptrs array, just to be sure
let nth = v.iter_mut().nth(i).unwrap();
@ -453,6 +467,17 @@ fn test_iter_ref_consistency() {
}
assert_eq!(v.iter().nth(len), None, "nth(len) should return None");
// stepping through with nth(0)
{
let mut it = v.iter();
for i in 0..len {
let next = it.nth(0).unwrap();
assert_eq!(next as *const _, v_ptrs[i]);
}
assert_eq!(it.nth(0), None);
}
// next()
{
let mut it = v.iter_mut();
for i in 0..len {
@ -466,9 +491,10 @@ fn test_iter_ref_consistency() {
assert_eq!(it.next(), None, "The final call to next() should return None");
}
// next_back()
{
let mut it = v.iter_mut();
for i in 0..len{
for i in 0..len {
let remaining = len - i;
assert_eq!(it.size_hint(), (remaining, Some(remaining)));
@ -482,12 +508,12 @@ fn test_iter_ref_consistency() {
// Make sure iterators and slice patterns yield consistent addresses for various types,
// including ZSTs.
helper(0u32);
helper(());
helper([0u32; 0]); // ZST with alignment > 0
helper_mut(0u32);
helper_mut(());
helper_mut([0u32; 0]); // ZST with alignment > 0
test(0u32);
test(());
test([0u32; 0]); // ZST with alignment > 0
test_mut(0u32);
test_mut(());
test_mut([0u32; 0]); // ZST with alignment > 0
}
// The current implementation of SliceIndex fails to handle methods