Skip to content

Commit 67c5d79

Browse files
committed
auto merge of #12718 : thestinger/rust/min_max, r=alexcrichton
2 parents ff22e47 + a871068 commit 67c5d79

File tree

3 files changed

+35
-3
lines changed

3 files changed

+35
-3
lines changed

src/libextra/stats.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
#[allow(missing_doc)];
1212

13-
use std::cmp;
1413
use std::hash::Hash;
1514
use std::io;
1615
use std::mem;
@@ -203,12 +202,12 @@ impl<'a> Stats for &'a [f64] {
203202

204203
fn min(self) -> f64 {
205204
assert!(self.len() != 0);
206-
self.iter().fold(self[0], |p,q| cmp::min(p, *q))
205+
self.iter().fold(self[0], |p, q| p.min(*q))
207206
}
208207

209208
fn max(self) -> f64 {
210209
assert!(self.len() != 0);
211-
self.iter().fold(self[0], |p,q| cmp::max(p, *q))
210+
self.iter().fold(self[0], |p, q| p.max(*q))
212211
}
213212

214213
fn mean(self) -> f64 {
@@ -442,6 +441,7 @@ mod tests {
442441
use stats::write_boxplot;
443442
use std::io;
444443
use std::str;
444+
use std::f64;
445445

446446
macro_rules! assert_approx_eq(
447447
($a:expr, $b:expr) => ({
@@ -481,6 +481,14 @@ mod tests {
481481
assert_eq!(summ.iqr, summ2.iqr);
482482
}
483483

484+
#[test]
485+
fn test_min_max_nan() {
486+
let xs = &[1.0, 2.0, f64::NAN, 3.0, 4.0];
487+
let summary = Summary::new(xs);
488+
assert_eq!(summary.min, 1.0);
489+
assert_eq!(summary.max, 4.0);
490+
}
491+
484492
#[test]
485493
fn test_norm2() {
486494
let val = &[

src/libstd/num/f32.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -866,6 +866,18 @@ mod tests {
866866
use num::*;
867867
use num;
868868

869+
#[test]
870+
fn test_min_nan() {
871+
assert_eq!(NAN.min(2.0), 2.0);
872+
assert_eq!(2.0f32.min(NAN), 2.0);
873+
}
874+
875+
#[test]
876+
fn test_max_nan() {
877+
assert_eq!(NAN.max(2.0), 2.0);
878+
assert_eq!(2.0f32.max(NAN), 2.0);
879+
}
880+
869881
#[test]
870882
fn test_num() {
871883
num::test_num(10f32, 2f32);

src/libstd/num/f64.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -865,6 +865,18 @@ mod tests {
865865
use num::*;
866866
use num;
867867

868+
#[test]
869+
fn test_min_nan() {
870+
assert_eq!(NAN.min(2.0), 2.0);
871+
assert_eq!(2.0f64.min(NAN), 2.0);
872+
}
873+
874+
#[test]
875+
fn test_max_nan() {
876+
assert_eq!(NAN.max(2.0), 2.0);
877+
assert_eq!(2.0f64.max(NAN), 2.0);
878+
}
879+
868880
#[test]
869881
fn test_num() {
870882
num::test_num(10f64, 2f64);

0 commit comments

Comments
 (0)