Skip to content

Commit b60ad3d

Browse files
committed
Add more tests for percentile
1 parent 9c6cec2 commit b60ad3d

File tree

1 file changed

+92
-4
lines changed

1 file changed

+92
-4
lines changed

tests/percentile.rs

Lines changed: 92 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,73 @@
1+
#[macro_use(array)]
12
extern crate ndarray;
23
extern crate ndarray_stats;
34

45
use ndarray::prelude::*;
56
use ndarray_stats::{
6-
interpolate::{Linear, Lower},
7+
interpolate::{Higher, Linear, Lower, Midpoint, Nearest},
78
PercentileExt,
89
};
910

11+
#[test]
12+
fn test_min() {
13+
let a = array![[1, 5, 3], [2, 0, 6]];
14+
assert_eq!(a.min(), &0);
15+
}
16+
17+
#[test]
18+
fn test_min_partialord() {
19+
let a = array![[1., 5., 3.], [2., 0., 6.]];
20+
assert_eq!(a.min_partialord(), Some(&0.));
21+
22+
let a = array![[1., 5., 3.], [2., ::std::f64::NAN, 6.]];
23+
assert_eq!(a.min_partialord(), None);
24+
}
25+
26+
#[test]
27+
fn test_min_skipnan() {
28+
let a = array![[1., 5., 3.], [2., 0., 6.]];
29+
assert_eq!(a.min_skipnan(), &0.);
30+
31+
let a = array![[1., 5., 3.], [2., ::std::f64::NAN, 6.]];
32+
assert_eq!(a.min_skipnan(), &1.);
33+
}
34+
35+
#[test]
36+
fn test_min_skipnan_all_nan() {
37+
let a = arr2(&[[::std::f64::NAN; 3]; 2]);
38+
assert!(a.min_skipnan().is_nan());
39+
}
40+
41+
#[test]
42+
fn test_max() {
43+
let a = array![[1, 5, 7], [2, 0, 6]];
44+
assert_eq!(a.max(), &7);
45+
}
46+
47+
#[test]
48+
fn test_max_partialord() {
49+
let a = array![[1., 5., 7.], [2., 0., 6.]];
50+
assert_eq!(a.max_partialord(), Some(&7.));
51+
52+
let a = array![[1., 5., 7.], [2., ::std::f64::NAN, 6.]];
53+
assert_eq!(a.max_partialord(), None);
54+
}
55+
56+
#[test]
57+
fn test_max_skipnan() {
58+
let a = array![[1., 5., 7.], [2., 0., 6.]];
59+
assert_eq!(a.max_skipnan(), &7.);
60+
61+
let a = array![[1., 5., 7.], [2., ::std::f64::NAN, 6.]];
62+
assert_eq!(a.max_skipnan(), &7.);
63+
}
64+
65+
#[test]
66+
fn test_max_skipnan_all_nan() {
67+
let a = arr2(&[[::std::f64::NAN; 3]; 2]);
68+
assert!(a.max_skipnan().is_nan());
69+
}
70+
1071
#[test]
1172
fn test_percentile_axis_mut_with_odd_axis_length() {
1273
let mut a = arr2(&[[1, 3, 2, 10], [2, 4, 3, 11], [3, 5, 6, 12]]);
@@ -35,9 +96,36 @@ fn test_percentile_axis_mut_to_get_maximum() {
3596
assert!(q == arr0(22));
3697
}
3798

99+
#[test]
100+
fn test_percentile_axis_skipnan_mut_higher_opt_i32() {
101+
let mut a = arr2(&[[Some(4), Some(2), None, Some(1), Some(5)], [None; 5]]);
102+
let q = a.percentile_axis_skipnan_mut::<Higher>(Axis(1), 0.6);
103+
assert_eq!(q.shape(), &[2]);
104+
assert_eq!(q[0], Some(4));
105+
assert!(q[1].is_none());
106+
}
107+
108+
#[test]
109+
fn test_percentile_axis_skipnan_mut_nearest_opt_i32() {
110+
let mut a = arr2(&[[Some(4), Some(2), None, Some(1), Some(5)], [None; 5]]);
111+
let q = a.percentile_axis_skipnan_mut::<Nearest>(Axis(1), 0.6);
112+
assert_eq!(q.shape(), &[2]);
113+
assert_eq!(q[0], Some(4));
114+
assert!(q[1].is_none());
115+
}
116+
117+
#[test]
118+
fn test_percentile_axis_skipnan_mut_midpoint_opt_i32() {
119+
let mut a = arr2(&[[Some(4), Some(2), None, Some(1), Some(5)], [None; 5]]);
120+
let q = a.percentile_axis_skipnan_mut::<Midpoint>(Axis(1), 0.6);
121+
assert_eq!(q.shape(), &[2]);
122+
assert_eq!(q[0], Some(3));
123+
assert!(q[1].is_none());
124+
}
125+
38126
// TODO: See https://github.com/SergiusIW/noisy_float-rs/pull/19
39127
// #[test]
40-
// fn test_percentile_axis_skipnan_mut_f64() {
128+
// fn test_percentile_axis_skipnan_mut_linear_f64() {
41129
// let mut a = arr2(&[[1., 2., ::std::f64::NAN, 3.], [::std::f64::NAN; 4]]);
42130
// let q = a.percentile_axis_skipnan_mut::<Linear>(Axis(1), 0.75);
43131
// assert_eq!(q.shape(), &[2]);
@@ -46,8 +134,8 @@ fn test_percentile_axis_mut_to_get_maximum() {
46134
// }
47135

48136
#[test]
49-
fn test_percentile_axis_skipnan_mut_opt_i32() {
50-
let mut a = arr2(&[[Some(1), Some(2), None, Some(4)], [None; 4]]);
137+
fn test_percentile_axis_skipnan_mut_linear_opt_i32() {
138+
let mut a = arr2(&[[Some(2), Some(4), None, Some(1)], [None; 4]]);
51139
let q = a.percentile_axis_skipnan_mut::<Linear>(Axis(1), 0.75);
52140
assert_eq!(q.shape(), &[2]);
53141
assert_eq!(q[0], Some(3));

0 commit comments

Comments
 (0)