Skip to content

Commit

Permalink
fix(waveform): fix waveform sample_at bugs (#442)
Browse files Browse the repository at this point in the history
* fix(waveform): fix waveform `sample_at` bugs

* remove print statements

* use relative eq for all comparisons
  • Loading branch information
rahulk29 authored Aug 5, 2024
1 parent 3eae4ad commit dac7b53
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion substrate/src/simulation/waveform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -764,7 +764,7 @@ where
let mut lo = 0usize;
let mut hi = data.len() - 1;
let mut x;
while lo < hi {
while lo <= hi {
let mid = (lo + hi) / 2;
x = data.get(mid).unwrap().t();
match target.partial_cmp(&x)? {
Expand Down Expand Up @@ -934,6 +934,25 @@ mod tests {
);
}

#[test]
fn waveform_sample_at() {
let wav =
Waveform::from_iter([(0., 0.), (1., 1.), (2., 0.9), (3., 0.1), (4., 0.), (5., 1.)]);
assert_relative_eq!(wav.sample_at(0.0), 0.0, epsilon = 1e-15);
assert_relative_eq!(wav.sample_at(0.5), 0.5, epsilon = 1e-15);
assert_relative_eq!(wav.sample_at(1.2), 0.98, epsilon = 1e-15);
assert_relative_eq!(wav.sample_at(1.8), 0.92, epsilon = 1e-15);
assert_relative_eq!(wav.sample_at(2.0), 0.9, epsilon = 1e-15);
assert_relative_eq!(wav.sample_at(4.0), 0.0, epsilon = 1e-15);
assert_relative_eq!(wav.sample_at(4.3), 0.3, epsilon = 1e-15);
assert_relative_eq!(wav.sample_at(4.5), 0.5, epsilon = 1e-15);
assert_relative_eq!(wav.sample_at(4.9), 0.9, epsilon = 1e-15);
assert_relative_ne!(wav.sample_at(4.9) + 1e-12, 0.9, epsilon = 1e-15);
assert_relative_eq!(wav.sample_at(4.99), 0.99, epsilon = 1e-15);
assert_relative_ne!(wav.sample_at(4.99) + 1e-12, 0.99, epsilon = 1e-15);
assert_relative_ne!(wav.sample_at(4.99) - 1e-12, 0.99, epsilon = 1e-15);
}

#[test]
fn waveform_transitions() {
let wav =
Expand Down

0 comments on commit dac7b53

Please sign in to comment.