Skip to content

Commit 47f309a

Browse files
Add test suite for mean
1 parent c65e457 commit 47f309a

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ serde = { version = "1.0", optional = true }
4747
defmac = "0.2"
4848
quickcheck = { version = "0.7.2", default-features = false }
4949
rawpointer = "0.1"
50+
approx = "0.3"
51+
noisy_float = "0.1.8"
5052

5153
[features]
5254
# Enable blas usage

tests/numeric.rs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
extern crate noisy_float;
2+
extern crate approx;
3+
use std::f64;
4+
use ndarray::{Array1, array};
5+
use noisy_float::types::N64;
6+
use approx::abs_diff_eq;
7+
8+
#[test]
9+
fn test_mean_with_nan_values() {
10+
let a = array![f64::NAN, 1.];
11+
assert!(a.mean().unwrap().is_nan());
12+
}
13+
14+
#[test]
15+
fn test_mean_with_empty_array_of_floats() {
16+
let a: Array1<f64> = array![];
17+
assert!(a.mean().is_none());
18+
}
19+
20+
#[test]
21+
fn test_mean_with_empty_array_of_noisy_floats() {
22+
let a: Array1<N64> = array![];
23+
assert!(a.mean().is_none());
24+
}
25+
26+
#[test]
27+
fn test_mean_with_array_of_floats() {
28+
let a: Array1<f64> = array![
29+
0.99889651, 0.0150731 , 0.28492482, 0.83819218, 0.48413156,
30+
0.80710412, 0.41762936, 0.22879429, 0.43997224, 0.23831807,
31+
0.02416466, 0.6269962 , 0.47420614, 0.56275487, 0.78995021,
32+
0.16060581, 0.64635041, 0.34876609, 0.78543249, 0.19938356,
33+
0.34429457, 0.88072369, 0.17638164, 0.60819363, 0.250392 ,
34+
0.69912532, 0.78855523, 0.79140914, 0.85084218, 0.31839879,
35+
0.63381769, 0.22421048, 0.70760302, 0.99216018, 0.80199153,
36+
0.19239188, 0.61356023, 0.31505352, 0.06120481, 0.66417377,
37+
0.63608897, 0.84959691, 0.43599069, 0.77867775, 0.88267754,
38+
0.83003623, 0.67016118, 0.67547638, 0.65220036, 0.68043427
39+
];
40+
// Computed using NumPy
41+
let expected_mean = 0.5475494059146699;
42+
abs_diff_eq!(a.mean().unwrap(), expected_mean, epsilon = f64::EPSILON);
43+
}

0 commit comments

Comments
 (0)