Skip to content

Commit 249dc9e

Browse files
committed
Add float NaN tests.
1 parent 16f30c2 commit 249dc9e

File tree

1 file changed

+59
-21
lines changed

1 file changed

+59
-21
lines changed

src/libcore/ops/range.rs

+59-21
Original file line numberDiff line numberDiff line change
@@ -100,13 +100,20 @@ impl<Idx: PartialOrd<Idx>> Range<Idx> {
100100
/// ```
101101
/// #![feature(range_contains)]
102102
///
103+
/// use std::f32;
104+
///
103105
/// assert!(!(3..5).contains(&2));
104106
/// assert!( (3..5).contains(&3));
105107
/// assert!( (3..5).contains(&4));
106108
/// assert!(!(3..5).contains(&5));
107109
///
108110
/// assert!(!(3..3).contains(&3));
109111
/// assert!(!(3..2).contains(&3));
112+
///
113+
/// assert!( (0.0..1.0).contains(&0.5));
114+
/// assert!(!(0.0..1.0).contains(&f32::NAN));
115+
/// assert!(!(0.0..f32::NAN).contains(&0.5));
116+
/// assert!(!(f32::NAN..1.0).contains(&0.5));
110117
/// ```
111118
#[unstable(feature = "range_contains", reason = "recently added as per RFC", issue = "32311")]
112119
pub fn contains<U>(&self, item: &U) -> bool
@@ -191,9 +198,15 @@ impl<Idx: PartialOrd<Idx>> RangeFrom<Idx> {
191198
/// ```
192199
/// #![feature(range_contains)]
193200
///
201+
/// use std::f32;
202+
///
194203
/// assert!(!(3..).contains(&2));
195204
/// assert!( (3..).contains(&3));
196205
/// assert!( (3..).contains(&1_000_000_000));
206+
///
207+
/// assert!( (0.0..).contains(&0.5));
208+
/// assert!(!(0.0..).contains(&f32::NAN));
209+
/// assert!(!(f32::NAN..).contains(&0.5));
197210
/// ```
198211
#[unstable(feature = "range_contains", reason = "recently added as per RFC", issue = "32311")]
199212
pub fn contains<U>(&self, item: &U) -> bool
@@ -266,9 +279,15 @@ impl<Idx: PartialOrd<Idx>> RangeTo<Idx> {
266279
/// ```
267280
/// #![feature(range_contains)]
268281
///
282+
/// use std::f32;
283+
///
269284
/// assert!( (..5).contains(&-1_000_000_000));
270285
/// assert!( (..5).contains(&4));
271286
/// assert!(!(..5).contains(&5));
287+
///
288+
/// assert!( (..1.0).contains(&0.5));
289+
/// assert!(!(..1.0).contains(&f32::NAN));
290+
/// assert!(!(..f32::NAN).contains(&0.5));
272291
/// ```
273292
#[unstable(feature = "range_contains", reason = "recently added as per RFC", issue = "32311")]
274293
pub fn contains<U>(&self, item: &U) -> bool
@@ -330,6 +349,8 @@ impl<Idx: PartialOrd<Idx>> RangeInclusive<Idx> {
330349
/// ```
331350
/// #![feature(range_contains)]
332351
///
352+
/// use std::f32;
353+
///
333354
/// assert!(!(3..=5).contains(&2));
334355
/// assert!( (3..=5).contains(&3));
335356
/// assert!( (3..=5).contains(&4));
@@ -338,6 +359,11 @@ impl<Idx: PartialOrd<Idx>> RangeInclusive<Idx> {
338359
///
339360
/// assert!( (3..=3).contains(&3));
340361
/// assert!(!(3..=2).contains(&3));
362+
///
363+
/// assert!( (0.0..=1.0).contains(&1.0));
364+
/// assert!(!(0.0..=1.0).contains(&f32::NAN));
365+
/// assert!(!(0.0..=f32::NAN).contains(&0.0));
366+
/// assert!(!(f32::NAN..=1.0).contains(&1.0));
341367
/// ```
342368
#[unstable(feature = "range_contains", reason = "recently added as per RFC", issue = "32311")]
343369
pub fn contains<U>(&self, item: &U) -> bool
@@ -447,9 +473,15 @@ impl<Idx: PartialOrd<Idx>> RangeToInclusive<Idx> {
447473
/// ```
448474
/// #![feature(range_contains)]
449475
///
476+
/// use std::f32;
477+
///
450478
/// assert!( (..=5).contains(&-1_000_000_000));
451479
/// assert!( (..=5).contains(&5));
452480
/// assert!(!(..=5).contains(&6));
481+
///
482+
/// assert!( (..=1.0).contains(&1.0));
483+
/// assert!(!(..=1.0).contains(&f32::NAN));
484+
/// assert!(!(..=f32::NAN).contains(&0.5));
453485
/// ```
454486
#[unstable(feature = "range_contains", reason = "recently added as per RFC", issue = "32311")]
455487
pub fn contains<U>(&self, item: &U) -> bool
@@ -559,34 +591,40 @@ pub trait RangeBounds<T: ?Sized> {
559591
/// ```
560592
fn end(&self) -> Bound<&T>;
561593

594+
562595
/// Returns `true` if `item` is contained in the range.
596+
///
597+
/// # Examples
598+
///
599+
/// ```
600+
/// #![feature(range_contains)]
601+
///
602+
/// use std::f32;
603+
///
604+
/// assert!( (3..5).contains(&4));
605+
/// assert!(!(3..5).contains(&2));
606+
///
607+
/// assert!( (0.0..1.0).contains(&0.5));
608+
/// assert!(!(0.0..1.0).contains(&f32::NAN));
609+
/// assert!(!(0.0..f32::NAN).contains(&0.5));
610+
/// assert!(!(f32::NAN..1.0).contains(&0.5));
563611
#[unstable(feature = "range_contains", reason = "recently added as per RFC", issue = "32311")]
564612
fn contains<U>(&self, item: &U) -> bool
565613
where
566614
T: PartialOrd<U>,
567615
U: ?Sized,
568616
{
569-
match self.start() {
570-
Included(ref start) => if *start > item {
571-
return false;
572-
},
573-
Excluded(ref start) => if *start >= item {
574-
return false;
575-
},
576-
Unbounded => (),
577-
};
578-
579-
match self.end() {
580-
Included(ref end) => if *end < item {
581-
return false;
582-
},
583-
Excluded(ref end) => if *end <= item {
584-
return false;
585-
},
586-
Unbounded => (),
587-
}
588-
589-
true
617+
(match self.start() {
618+
Included(ref start) => *start <= item,
619+
Excluded(ref start) => *start < item,
620+
Unbounded => true,
621+
})
622+
&&
623+
(match self.end() {
624+
Included(ref end) => *end >= item,
625+
Excluded(ref end) => *end > item,
626+
Unbounded => true,
627+
})
590628
}
591629
}
592630

0 commit comments

Comments
 (0)