Skip to content

Commit

Permalink
fix compress function and add better testing
Browse files Browse the repository at this point in the history
  • Loading branch information
ekiwi committed May 7, 2024
1 parent 95ae142 commit 873a7b3
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 10 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

[package]
name = "wellen"
version = "0.9.5"
version = "0.9.6"
edition = "2021"
authors = ["Kevin Laeufer <[email protected]>"]
description = "Fast VCD and FST library for waveform viewers written in Rust."
Expand Down Expand Up @@ -33,6 +33,7 @@ vcd = "0.7.0"
clap = { version = "4.4.6", features = ["derive"] }
criterion = "0.5"
indicatif = "0.17.8"
proptest = "1.4.0"

[profile.release]
debug = true
Expand Down
6 changes: 0 additions & 6 deletions src/ghw/hierarchy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1378,9 +1378,3 @@ impl FloatRange {
self_range.start() >= other_range.start() && self_range.end() <= other_range.end()
}
}

#[derive(Debug, Copy, Clone, PartialEq)]
struct GhwUnit {
name: StringId,
value: i64,
}
3 changes: 1 addition & 2 deletions src/ghw/signals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -362,8 +362,7 @@ impl VecBuffer {
// note, we keep the signal on the change list

// return reference to value
let data = &self.data[info.data_range()];
data
&self.data[info.data_range()]
}

#[inline]
Expand Down
36 changes: 35 additions & 1 deletion src/wavemem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1005,8 +1005,9 @@ fn compress_template(
) {
debug_assert!(in_states.bits_in_a_byte() < out_states.bits_in_a_byte());
let mut working_byte = 0u8;
let max_bits = value.len() * in_states.bits_in_a_byte();
for bit in (0..bits).rev() {
let rev_bit = bits - bit - 1;
let rev_bit = max_bits - bit - 1;
let in_byte = value[rev_bit / in_states.bits_in_a_byte()];
let in_value =
(in_byte >> ((bit % in_states.bits_in_a_byte()) * in_states.bits())) & in_states.mask();
Expand Down Expand Up @@ -1180,4 +1181,37 @@ mod tests {
}
}
}

use proptest::prelude::*;

fn convert_to_bits(states: States, chars: &str) -> Vec<u8> {
let mut out = Vec::new();
write_n_state(states, chars.as_bytes(), &mut out, None);
out
}

fn do_test_compress(value: String, max_states: States) {
let min_states = check_states(value.as_bytes()).unwrap();
let bits = value.len();
// convert string to bit vector
let max_value = convert_to_bits(max_states, &value);
// compress
let mut out = Vec::new();
compress(&max_value, max_states, min_states, bits, &mut out);
// check
let direct_conversion = convert_to_bits(min_states, &value);
assert_eq!(direct_conversion, out, "{value} - write_n_states -> {max_value:?} - compress -> {out:?} != {direct_conversion:?}");
}

proptest! {
#[test]
fn compress_from_nine_state(value in "[01xz]{0,4}") {
do_test_compress(value, States::Nine);
}

#[test]
fn compress_from_four_state(value in "[01]{0,4}") {
do_test_compress(value, States::Four);
}
}
}

0 comments on commit 873a7b3

Please sign in to comment.