Skip to content

Commit

Permalink
auto merge of #12718 : thestinger/rust/min_max, r=alexcrichton
Browse files Browse the repository at this point in the history
  • Loading branch information
bors committed Mar 6, 2014
2 parents ff22e47 + a871068 commit 67c5d79
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 3 deletions.
14 changes: 11 additions & 3 deletions src/libextra/stats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

#[allow(missing_doc)];

use std::cmp;
use std::hash::Hash;
use std::io;
use std::mem;
Expand Down Expand Up @@ -203,12 +202,12 @@ impl<'a> Stats for &'a [f64] {

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

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

fn mean(self) -> f64 {
Expand Down Expand Up @@ -442,6 +441,7 @@ mod tests {
use stats::write_boxplot;
use std::io;
use std::str;
use std::f64;

macro_rules! assert_approx_eq(
($a:expr, $b:expr) => ({
Expand Down Expand Up @@ -481,6 +481,14 @@ mod tests {
assert_eq!(summ.iqr, summ2.iqr);
}

#[test]
fn test_min_max_nan() {
let xs = &[1.0, 2.0, f64::NAN, 3.0, 4.0];
let summary = Summary::new(xs);
assert_eq!(summary.min, 1.0);
assert_eq!(summary.max, 4.0);
}

#[test]
fn test_norm2() {
let val = &[
Expand Down
12 changes: 12 additions & 0 deletions src/libstd/num/f32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -866,6 +866,18 @@ mod tests {
use num::*;
use num;

#[test]
fn test_min_nan() {
assert_eq!(NAN.min(2.0), 2.0);
assert_eq!(2.0f32.min(NAN), 2.0);
}

#[test]
fn test_max_nan() {
assert_eq!(NAN.max(2.0), 2.0);
assert_eq!(2.0f32.max(NAN), 2.0);
}

#[test]
fn test_num() {
num::test_num(10f32, 2f32);
Expand Down
12 changes: 12 additions & 0 deletions src/libstd/num/f64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -865,6 +865,18 @@ mod tests {
use num::*;
use num;

#[test]
fn test_min_nan() {
assert_eq!(NAN.min(2.0), 2.0);
assert_eq!(2.0f64.min(NAN), 2.0);
}

#[test]
fn test_max_nan() {
assert_eq!(NAN.max(2.0), 2.0);
assert_eq!(2.0f64.max(NAN), 2.0);
}

#[test]
fn test_num() {
num::test_num(10f64, 2f64);
Expand Down

0 comments on commit 67c5d79

Please sign in to comment.