-
Notifications
You must be signed in to change notification settings - Fork 14.1k
Add Range[Inclusive]::is_empty #48087
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
4f8049a
7fe182f
b5cb393
6f70a11
22b0489
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1322,42 +1322,84 @@ fn test_range() { | |
| (isize::MAX as usize + 2, Some(isize::MAX as usize + 2))); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_range_exhaustion() { | ||
| let mut r = 10..10; | ||
| assert!(r.is_empty()); | ||
| assert_eq!(r.next(), None); | ||
| assert_eq!(r.next_back(), None); | ||
| assert_eq!(r, 10..10); | ||
|
|
||
| let mut r = 10..12; | ||
| assert_eq!(r.next(), Some(10)); | ||
| assert_eq!(r.next(), Some(11)); | ||
| assert!(r.is_empty()); | ||
| assert_eq!(r, 12..12); | ||
| assert_eq!(r.next(), None); | ||
|
|
||
| let mut r = 10..12; | ||
| assert_eq!(r.next_back(), Some(11)); | ||
| assert_eq!(r.next_back(), Some(10)); | ||
| assert!(r.is_empty()); | ||
| assert_eq!(r, 10..10); | ||
| assert_eq!(r.next_back(), None); | ||
|
|
||
| let mut r = 100..10; | ||
| assert!(r.is_empty()); | ||
| assert_eq!(r.next(), None); | ||
| assert_eq!(r.next_back(), None); | ||
| assert_eq!(r, 100..10); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_range_inclusive_exhaustion() { | ||
| let mut r = 10..=10; | ||
| assert_eq!(r.next(), Some(10)); | ||
| assert_eq!(r, 1..=0); | ||
| assert!(r.is_empty()); | ||
| assert_eq!(r.next(), None); | ||
| assert_eq!(r.next(), None); | ||
|
|
||
| let mut r = 10..=10; | ||
| assert_eq!(r.next_back(), Some(10)); | ||
| assert_eq!(r, 1..=0); | ||
| assert!(r.is_empty()); | ||
| assert_eq!(r.next_back(), None); | ||
|
|
||
| let mut r = 10..=12; | ||
| assert_eq!(r.next(), Some(10)); | ||
| assert_eq!(r.next(), Some(11)); | ||
| assert_eq!(r.next(), Some(12)); | ||
| assert_eq!(r, 1..=0); | ||
| assert!(r.is_empty()); | ||
| assert_eq!(r.next(), None); | ||
|
|
||
| let mut r = 10..=12; | ||
| assert_eq!(r.next_back(), Some(12)); | ||
| assert_eq!(r.next_back(), Some(11)); | ||
| assert_eq!(r.next_back(), Some(10)); | ||
| assert_eq!(r, 1..=0); | ||
| assert!(r.is_empty()); | ||
| assert_eq!(r.next_back(), None); | ||
|
|
||
| let mut r = 10..=12; | ||
| assert_eq!(r.nth(2), Some(12)); | ||
| assert_eq!(r, 1..=0); | ||
| assert!(r.is_empty()); | ||
| assert_eq!(r.next(), None); | ||
|
|
||
| let mut r = 10..=12; | ||
| assert_eq!(r.nth(5), None); | ||
| assert_eq!(r, 1..=0); | ||
| assert!(r.is_empty()); | ||
| assert_eq!(r.next(), None); | ||
|
|
||
| let mut r = 100..=10; | ||
| assert_eq!(r.next(), None); | ||
| assert!(r.is_empty()); | ||
| assert_eq!(r.next(), None); | ||
| assert_eq!(r.next(), None); | ||
| assert_eq!(r, 100..=10); | ||
|
|
||
| let mut r = 100..=10; | ||
| assert_eq!(r.next_back(), None); | ||
| assert!(r.is_empty()); | ||
| assert_eq!(r.next_back(), None); | ||
| assert_eq!(r.next_back(), None); | ||
| assert_eq!(r, 100..=10); | ||
| } | ||
|
|
||
|
|
@@ -1428,9 +1470,10 @@ fn test_range_inclusive_nth() { | |
| assert_eq!(r.nth(2), Some(15)); | ||
| assert_eq!(r, 16..=20); | ||
| assert_eq!(r.is_empty(), false); | ||
| assert_eq!(ExactSizeIterator::is_empty(&r), 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 | ||
| assert_eq!(ExactSizeIterator::is_empty(&r), true); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this going to be a concern for end users?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
ah, nvm
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Changed |
||
| } | ||
|
|
||
| #[test] | ||
|
|
@@ -1514,11 +1557,11 @@ fn test_range_inclusive_folds() { | |
|
|
||
| let mut it = 10..=20; | ||
| assert_eq!(it.try_fold(0, |a,b| Some(a+b)), Some(165)); | ||
| assert_eq!(it, 1..=0); | ||
| assert!(it.is_empty()); | ||
|
|
||
| let mut it = 10..=20; | ||
| assert_eq!(it.try_rfold(0, |a,b| Some(a+b)), Some(165)); | ||
| assert_eq!(it, 1..=0); | ||
| assert!(it.is_empty()); | ||
| } | ||
|
|
||
| #[test] | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -68,3 +68,27 @@ fn test_range_inclusive() { | |
| assert_eq!(r.size_hint(), (0, Some(0))); | ||
| assert_eq!(r.next(), None); | ||
| } | ||
|
|
||
|
|
||
| #[test] | ||
| fn test_range_is_empty() { | ||
| use core::f32::*; | ||
|
|
||
| assert!(!(0.0 .. 10.0).is_empty()); | ||
| assert!( (-0.0 .. 0.0).is_empty()); | ||
| assert!( (10.0 .. 0.0).is_empty()); | ||
|
|
||
| assert!(!(NEG_INFINITY .. INFINITY).is_empty()); | ||
| assert!( (EPSILON .. NAN).is_empty()); | ||
| assert!( (NAN .. EPSILON).is_empty()); | ||
| assert!( (NAN .. NAN).is_empty()); | ||
|
|
||
| assert!(!(0.0 ..= 10.0).is_empty()); | ||
| assert!(!(-0.0 ..= 0.0).is_empty()); | ||
| assert!( (10.0 ..= 0.0).is_empty()); | ||
|
|
||
| assert!(!(NEG_INFINITY ..= INFINITY).is_empty()); | ||
| assert!( (EPSILON ..= NAN).is_empty()); | ||
| assert!( (NAN ..= EPSILON).is_empty()); | ||
| assert!( (NAN ..= NAN).is_empty()); | ||
| } | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. WDYT about a test that calls
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good thought; I'll change all of these to look for is_empty instead of |
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is this related to this PR?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because using
0..1means the code wasn't actually usingExactSizeIterator; the inherent method was getting picked up instead. For normal code that's not a problem--they do exactly the same thing--but I figured the doctest should actually use the method it's documenting.