forked from rust-ndarray/ndarray
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtests.rs
More file actions
150 lines (134 loc) · 4.59 KB
/
tests.rs
File metadata and controls
150 lines (134 loc) · 4.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
use ndarray::{Array, Array2, ArrayView1, Axis};
#[cfg(feature = "quickcheck")]
use ndarray_rand::rand::{distr::Distribution, rng};
use ndarray::ShapeBuilder;
use ndarray_rand::rand_distr::Uniform;
use ndarray_rand::{RandomExt, SamplingStrategy};
use quickcheck::{quickcheck, TestResult};
#[test]
fn test_dim()
{
let (mm, nn) = (5, 5);
for m in 0..mm {
for n in 0..nn {
let a = Array::random((m, n), Uniform::new(0., 2.).unwrap());
assert_eq!(a.shape(), &[m, n]);
assert!(a.iter().all(|x| *x < 2.));
assert!(a.iter().all(|x| *x >= 0.));
assert!(a.is_standard_layout());
}
}
}
#[test]
fn test_dim_f()
{
let (mm, nn) = (5, 5);
for m in 0..mm {
for n in 0..nn {
let a = Array::random((m, n).f(), Uniform::new(0., 2.).unwrap());
assert_eq!(a.shape(), &[m, n]);
assert!(a.iter().all(|x| *x < 2.));
assert!(a.iter().all(|x| *x >= 0.));
assert!(a.t().is_standard_layout());
}
}
}
#[test]
fn sample_axis_on_view()
{
let m = 5;
let a = Array::random((m, 4), Uniform::new(0., 2.).unwrap());
let _samples = a
.view()
.sample_axis(Axis(0), m, SamplingStrategy::WithoutReplacement);
}
#[test]
#[should_panic]
fn oversampling_without_replacement_should_panic()
{
let m = 5;
let a = Array::random((m, 4), Uniform::new(0., 2.).unwrap());
let _samples = a.sample_axis(Axis(0), m + 1, SamplingStrategy::WithoutReplacement);
}
quickcheck! {
#[cfg_attr(miri, ignore)] // Takes an insufferably long time
fn oversampling_with_replacement_is_fine(m: u8, n: u8) -> TestResult {
let (m, n) = (m as usize, n as usize);
let a = Array::random((m, n), Uniform::new(0., 2.).unwrap());
// Higher than the length of both axes
let n_samples = m + n + 1;
// We don't want to deal with sampling from 0-length axes in this test
if m != 0 {
if !sampling_works(&a, SamplingStrategy::WithReplacement, Axis(0), n_samples) {
return TestResult::failed();
}
} else {
return TestResult::discard();
}
// We don't want to deal with sampling from 0-length axes in this test
if n != 0 {
if !sampling_works(&a, SamplingStrategy::WithReplacement, Axis(1), n_samples) {
return TestResult::failed();
}
} else {
return TestResult::discard();
}
TestResult::passed()
}
}
#[cfg(feature = "quickcheck")]
quickcheck! {
#[cfg_attr(miri, ignore)] // This takes *forever* with Miri
fn sampling_behaves_as_expected(m: u8, n: u8, strategy: SamplingStrategy) -> TestResult {
let (m, n) = (m as usize, n as usize);
let a = Array::random((m, n), Uniform::new(0., 2.).unwrap());
let mut rng = &mut rng();
// We don't want to deal with sampling from 0-length axes in this test
if m != 0 {
let n_row_samples = Uniform::new(1, m+1).unwrap().sample(&mut rng);
if !sampling_works(&a, strategy.clone(), Axis(0), n_row_samples) {
return TestResult::failed();
}
} else {
return TestResult::discard();
}
// We don't want to deal with sampling from 0-length axes in this test
if n != 0 {
let n_col_samples = Uniform::new(1, n+1).unwrap().sample(&mut rng);
if !sampling_works(&a, strategy, Axis(1), n_col_samples) {
return TestResult::failed();
}
} else {
return TestResult::discard();
}
TestResult::passed()
}
}
fn sampling_works(a: &Array2<f64>, strategy: SamplingStrategy, axis: Axis, n_samples: usize) -> bool
{
let samples = a.sample_axis(axis, n_samples, strategy);
samples
.axis_iter(axis)
.all(|lane| is_subset(a, &lane, axis))
}
// Check if, when sliced along `axis`, there is at least one lane in `a` equal to `b`
fn is_subset(a: &Array2<f64>, b: &ArrayView1<f64>, axis: Axis) -> bool
{
a.axis_iter(axis).any(|lane| &lane == b)
}
#[test]
#[should_panic]
fn sampling_without_replacement_from_a_zero_length_axis_should_panic()
{
let n = 5;
let a = Array::random((0, n), Uniform::new(0., 2.).unwrap());
let _samples = a.sample_axis(Axis(0), 1, SamplingStrategy::WithoutReplacement);
}
#[test]
#[should_panic]
fn sampling_with_replacement_from_a_zero_length_axis_should_panic()
{
let n = 5;
let a = Array::random((0, n), Uniform::new(0., 2.).unwrap());
let _samples = a.sample_axis(Axis(0), 1, SamplingStrategy::WithReplacement);
}