From 7a40307a7ca44d8ff4035c59022647cddc5b6699 Mon Sep 17 00:00:00 2001 From: Simon Sapin Date: Thu, 6 Jul 2017 17:13:29 +0200 Subject: [PATCH] Add tests for Range*::nth --- src/libcore/tests/iter.rs | 49 +++++++++++++++++++++++++++++++++++++++ src/libcore/tests/lib.rs | 2 ++ 2 files changed, 51 insertions(+) diff --git a/src/libcore/tests/iter.rs b/src/libcore/tests/iter.rs index 14f0260f571..748eac10ac0 100644 --- a/src/libcore/tests/iter.rs +++ b/src/libcore/tests/iter.rs @@ -1076,6 +1076,55 @@ fn test_range() { (isize::MAX as usize + 2, Some(isize::MAX as usize + 2))); } +#[test] +fn test_range_nth() { + assert_eq!((10..15).nth(0), Some(10)); + assert_eq!((10..15).nth(1), Some(11)); + assert_eq!((10..15).nth(4), Some(14)); + assert_eq!((10..15).nth(5), None); + + let mut r = 10..20; + assert_eq!(r.nth(2), Some(12)); + assert_eq!(r, 13..20); + assert_eq!(r.nth(2), Some(15)); + assert_eq!(r, 16..20); + assert_eq!(r.nth(10), None); + assert_eq!(r, 20..20); +} + +#[test] +fn test_range_from_nth() { + assert_eq!((10..).nth(0), Some(10)); + assert_eq!((10..).nth(1), Some(11)); + assert_eq!((10..).nth(4), Some(14)); + + let mut r = 10..; + assert_eq!(r.nth(2), Some(12)); + assert_eq!(r, 13..); + assert_eq!(r.nth(2), Some(15)); + assert_eq!(r, 16..); + assert_eq!(r.nth(10), Some(26)); + assert_eq!(r, 27..); +} + +#[test] +fn test_range_inclusive_nth() { + assert_eq!((10...15).nth(0), Some(10)); + assert_eq!((10...15).nth(1), Some(11)); + assert_eq!((10...15).nth(5), Some(15)); + assert_eq!((10...15).nth(6), None); + + let mut r = 10_u8...20; + assert_eq!(r.nth(2), Some(12)); + assert_eq!(r, 13...20); + assert_eq!(r.nth(2), Some(15)); + assert_eq!(r, 16...20); + assert_eq!(r.is_empty(), false); + assert_eq!(r.nth(10), None); + assert_eq!(r.is_empty(), true); + assert_eq!(r, 1...0); // We may not want to document/promise this detail +} + #[test] fn test_range_step() { #![allow(deprecated)] diff --git a/src/libcore/tests/lib.rs b/src/libcore/tests/lib.rs index 8d3e367d237..26e4c21dc8f 100644 --- a/src/libcore/tests/lib.rs +++ b/src/libcore/tests/lib.rs @@ -18,12 +18,14 @@ #![feature(core_private_diy_float)] #![feature(dec2flt)] #![feature(decode_utf8)] +#![feature(exact_size_is_empty)] #![feature(fixed_size_array)] #![feature(flt2dec)] #![feature(fmt_internals)] #![feature(iterator_step_by)] #![feature(i128_type)] #![feature(inclusive_range)] +#![feature(inclusive_range_syntax)] #![feature(iter_rfind)] #![feature(libc)] #![feature(nonzero)]