Skip to content

Commit 60b0636

Browse files
committed
test nth better
1 parent cbdba2b commit 60b0636

File tree

1 file changed

+37
-11
lines changed

1 file changed

+37
-11
lines changed

src/libcore/tests/slice.rs

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -395,24 +395,36 @@ fn test_windows_zip() {
395395
fn test_iter_ref_consistency() {
396396
use std::fmt::Debug;
397397

398-
fn helper<T : Copy + Debug + PartialEq>(x : T) {
398+
fn test<T : Copy + Debug + PartialEq>(x : T) {
399399
let v : &[T] = &[x, x, x];
400400
let v_ptrs : [*const T; 3] = match v {
401401
[ref v1, ref v2, ref v3] => [v1 as *const _, v2 as *const _, v3 as *const _],
402402
_ => unreachable!()
403403
};
404404
let len = v.len();
405405

406+
// nth(i)
406407
for i in 0..len {
407408
assert_eq!(&v[i] as *const _, v_ptrs[i]); // check the v_ptrs array, just to be sure
408409
let nth = v.iter().nth(i).unwrap();
409410
assert_eq!(nth as *const _, v_ptrs[i]);
410411
}
411412
assert_eq!(v.iter().nth(len), None, "nth(len) should return None");
412413

414+
// stepping through with nth(0)
413415
{
414416
let mut it = v.iter();
415-
for i in 0..len{
417+
for i in 0..len {
418+
let next = it.nth(0).unwrap();
419+
assert_eq!(next as *const _, v_ptrs[i]);
420+
}
421+
assert_eq!(it.nth(0), None);
422+
}
423+
424+
// next()
425+
{
426+
let mut it = v.iter();
427+
for i in 0..len {
416428
let remaining = len - i;
417429
assert_eq!(it.size_hint(), (remaining, Some(remaining)));
418430

@@ -423,9 +435,10 @@ fn test_iter_ref_consistency() {
423435
assert_eq!(it.next(), None, "The final call to next() should return None");
424436
}
425437

438+
// next_back()
426439
{
427440
let mut it = v.iter();
428-
for i in 0..len{
441+
for i in 0..len {
429442
let remaining = len - i;
430443
assert_eq!(it.size_hint(), (remaining, Some(remaining)));
431444

@@ -437,7 +450,7 @@ fn test_iter_ref_consistency() {
437450
}
438451
}
439452

440-
fn helper_mut<T : Copy + Debug + PartialEq>(x : T) {
453+
fn test_mut<T : Copy + Debug + PartialEq>(x : T) {
441454
let v : &mut [T] = &mut [x, x, x];
442455
let v_ptrs : [*mut T; 3] = match v {
443456
[ref v1, ref v2, ref v3] =>
@@ -446,13 +459,25 @@ fn test_iter_ref_consistency() {
446459
};
447460
let len = v.len();
448461

462+
// nth(i)
449463
for i in 0..len {
450464
assert_eq!(&mut v[i] as *mut _, v_ptrs[i]); // check the v_ptrs array, just to be sure
451465
let nth = v.iter_mut().nth(i).unwrap();
452466
assert_eq!(nth as *mut _, v_ptrs[i]);
453467
}
454468
assert_eq!(v.iter().nth(len), None, "nth(len) should return None");
455469

470+
// stepping through with nth(0)
471+
{
472+
let mut it = v.iter();
473+
for i in 0..len {
474+
let next = it.nth(0).unwrap();
475+
assert_eq!(next as *const _, v_ptrs[i]);
476+
}
477+
assert_eq!(it.nth(0), None);
478+
}
479+
480+
// next()
456481
{
457482
let mut it = v.iter_mut();
458483
for i in 0..len {
@@ -466,9 +491,10 @@ fn test_iter_ref_consistency() {
466491
assert_eq!(it.next(), None, "The final call to next() should return None");
467492
}
468493

494+
// next_back()
469495
{
470496
let mut it = v.iter_mut();
471-
for i in 0..len{
497+
for i in 0..len {
472498
let remaining = len - i;
473499
assert_eq!(it.size_hint(), (remaining, Some(remaining)));
474500

@@ -482,12 +508,12 @@ fn test_iter_ref_consistency() {
482508

483509
// Make sure iterators and slice patterns yield consistent addresses for various types,
484510
// including ZSTs.
485-
helper(0u32);
486-
helper(());
487-
helper([0u32; 0]); // ZST with alignment > 0
488-
helper_mut(0u32);
489-
helper_mut(());
490-
helper_mut([0u32; 0]); // ZST with alignment > 0
511+
test(0u32);
512+
test(());
513+
test([0u32; 0]); // ZST with alignment > 0
514+
test_mut(0u32);
515+
test_mut(());
516+
test_mut([0u32; 0]); // ZST with alignment > 0
491517
}
492518

493519
// The current implementation of SliceIndex fails to handle methods

0 commit comments

Comments
 (0)