Debian Codegraph @

.cargo/vendor/rustdct/tests/common/mod.rs

raw
1 use rand::{distributions::Uniform, prelude::Distribution, rngs::StdRng, SeedableRng};
2 use rustdct::num_traits::{Float, FromPrimitive};
3
4 pub mod known_data;
5 pub mod reference_impls;
6
7 #[macro_use]
8 pub mod macros;
9
10 pub fn fuzzy_cmp<T: Float>(a: T, b: T, tolerance: T) -> bool {
11 a >= b - tolerance && a <= b + tolerance
12 }
13
14 pub fn compare_float_vectors<T: Float + FromPrimitive>(expected: &[T], observed: &[T]) -> bool {
15 assert_eq!(expected.len(), observed.len());
16
17 let tolerance = T::from_f64(0.001).unwrap();
18
19 for i in 0..expected.len() {
20 if !fuzzy_cmp(observed[i], expected[i], tolerance) {
21 return false;
22 }
23 }
24 true
25 }
26
27 pub fn random_signal<T: Float + FromPrimitive>(length: usize) -> Vec<T> {
28 let mut sig = Vec::with_capacity(length);
29 let distribution = Uniform::new(0.0, 10.0);
30
31 let seed: [u8; 32] = [
32 1, 5, 6, 7, 1, 5, 3, 7, 4, 2, 6, 2, 6, 1, 5, 6, 7, 1, 5, 3, 7, 4, 2, 6, 2, 6, 1, 5, 1, 0,
33 1, 7,
34 ];
35 let mut rng: StdRng = SeedableRng::from_seed(seed);
36
37 for _ in 0..length {
38 sig.push(T::from_f64(distribution.sample(&mut rng)).unwrap());
39 }
40 return sig;
41 }