From 60b06369eea523b9d8fe606357177f126bef4b42 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Thu, 19 Jul 2018 12:28:43 +0200 Subject: [PATCH] test nth better --- src/libcore/tests/slice.rs | 48 +++++++++++++++++++++++++++++--------- 1 file changed, 37 insertions(+), 11 deletions(-) diff --git a/src/libcore/tests/slice.rs b/src/libcore/tests/slice.rs index e4230b08497..7968521f7b4 100644 --- a/src/libcore/tests/slice.rs +++ b/src/libcore/tests/slice.rs @@ -395,7 +395,7 @@ fn test_windows_zip() { fn test_iter_ref_consistency() { use std::fmt::Debug; - fn helper(x : T) { + fn test(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 helper(x : T) { }; 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 helper(x : T) { } 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 helper(x : T) { 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 helper(x : T) { } } - fn helper_mut(x : T) { + fn test_mut(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 helper_mut(x : T) { }; 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 helper_mut(x : T) { } 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 helper_mut(x : T) { 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 helper_mut(x : T) { // 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 -- 2.44.0