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
// devela::phys::wave::tests
#[cfg(feature = "alloc")]
mod alloc {
use super::super::*;
use crate::vec_ as vec;
/// Checks that the forward transform produces the correct coefficients for a basic input,
/// testing the correct averaging and differencing.
#[test]
fn test_haar_forward_transform() {
let haar = WaveletHaar;
let input = vec![9.0, 7.0, 3.0, 5.0];
let result = haar.forward(&input);
// Expected: Overall average, followed by detail coefficients
assert_eq!(result, vec![6.0, 2.0, 1.0, -1.0]);
}
/// Ensures the inverse transform accurately reconstructs the original values from the wavelet
/// coefficients.
#[test]
fn test_haar_inverse_transform() {
let haar = WaveletHaar;
let coeffs = vec![6.0, 2.0, 1.0, -1.0];
let result = haar.inverse(&coeffs);
// Reconstructed values should match the original input
assert_eq!(result, vec![9.0, 7.0, 3.0, 5.0]);
}
/// Verifies that the WaveletScalingFunction struct initializes with the correct level,
/// position, and values.
#[test]
fn test_scaling_function_initialization() {
let scaling_func = WaveletUnitVec::new(WaveletUnitRole::Scaling, 1, 0, vec![0.5, 0.5]);
assert_eq!(scaling_func.level, 1);
assert_eq!(scaling_func.position, 0);
assert_eq!(scaling_func.values, vec![0.5, 0.5]);
}
/// Confirms that the WaveletFunction struct initializes with the expected properties.
#[test]
fn test_wavelet_function_initialization() {
let wavelet_func = WaveletUnitVec::new(WaveletUnitRole::Wavelet, 1, 1, vec![1.0, -1.0]);
assert_eq!(wavelet_func.level, 1);
assert_eq!(wavelet_func.position, 1);
assert_eq!(wavelet_func.values, vec![1.0, -1.0]);
}
/// Validates that the compression method zeroes out coefficients below the specified
/// tolerance, while leaving significant values untouched.
#[test]
fn test_haar_compression() {
let haar = WaveletHaar;
let coeffs = vec![6.0, 0.1, 1.0, -0.05];
let result = haar.compress(&coeffs, 0.5);
// Expect values below 0.5 to be zeroed out
assert_eq!(result, vec![6.0, 0.0, 1.0, 0.0]);
}
/// Verifies that a forward and inverse transform round-trip reproduces the original input
/// within a small floating-point tolerance, ensuring transform consistency.
#[test]
fn test_forward_inverse_roundtrip() {
let haar = WaveletHaar;
let input = vec![10.0, 2.0, 8.0, 4.0];
let forward_result = haar.forward(&input);
let inverse_result = haar.inverse(&forward_result);
// Verify round-trip accuracy within floating-point tolerance
for (a, b) in input.iter().zip(inverse_result.iter()) {
assert!((a - b).abs() < 1e-10);
}
}
}