Skip to content

Commit

Permalink
Add proptests for mergeable_heap.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
indy256 committed Sep 1, 2024
1 parent fe15b99 commit b5f77f4
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 15 deletions.
1 change: 1 addition & 0 deletions rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ rand = "0.8.5"

[dev-dependencies]
rstest="0.22.0"
proptest = "1.5.0"

[lib]
path = "lib.rs"
47 changes: 32 additions & 15 deletions rust/structures/mergeable_heap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,22 @@ impl<V: PartialOrd> Heap<V> {
}))
}

fn merge(mut a: Option<Box<Heap<V>>>, mut b: Option<Box<Heap<V>>>) -> Option<Box<Heap<V>>> {
fn merge(a: Option<Box<Heap<V>>>, b: Option<Box<Heap<V>>>) -> Option<Box<Heap<V>>> {
if a.is_none() {
return b;
}
if b.is_none() {
return a;
}
if a.as_ref()?.value > b.as_ref()?.value {
swap(&mut a, &mut b);
let mut ha = a.unwrap();
let mut hb = b.unwrap();
if ha.value > hb.value {
swap(&mut ha, &mut hb);
}
let mut ha = a?;
if rand::random() {
swap(&mut ha.left, &mut ha.right);
}
ha.left = Self::merge(ha.left, b);
ha.left = Self::merge(ha.left, Some(hb));
Some(ha)
}

Expand All @@ -46,6 +47,8 @@ impl<V: PartialOrd> Heap<V> {
#[cfg(test)]
mod tests {
use crate::structures::mergeable_heap::Heap;
use proptest::collection::vec;
use proptest::{prop_assert_eq, proptest};
use rand::seq::SliceRandom;
use rand::thread_rng;
use rstest::rstest;
Expand All @@ -56,32 +59,46 @@ mod tests {
#[case(&mut [1, 1])]
#[case(&mut [3, 1, 2])]
fn basic_test(#[case] seq: &mut [u32]) {
test(seq);
let heap_sorted_values = sort_with_heap(&seq);
seq.sort();
assert_eq!(heap_sorted_values, seq);
}

#[test]
fn big_test1() {
let mut values = (0..10_000).collect::<Vec<u32>>();
values.shuffle(&mut thread_rng());
test(&mut values);
let mut seq = (0..10_000).collect::<Vec<u32>>();
seq.shuffle(&mut thread_rng());
let heap_sorted_values = sort_with_heap(&seq);
seq.sort();
assert_eq!(heap_sorted_values, seq);
}

#[test]
fn big_test2() {
let mut values = vec![0; 10_000];
values.shuffle(&mut thread_rng());
test(&mut values);
let mut seq = vec![0; 10_000];
seq.shuffle(&mut thread_rng());
let heap_sorted_values = sort_with_heap(&seq);
seq.sort();
assert_eq!(heap_sorted_values, seq);
}

fn test(seq: &mut [u32]) {
proptest! {
#[test]
fn test_random(mut seq in vec(0u32..3, 0..10)) {
let heap_sorted_values = sort_with_heap(&seq);
seq.sort();
prop_assert_eq!(heap_sorted_values, seq);
}
}

fn sort_with_heap(seq: &[u32]) -> Vec<u32> {
let mut heap = seq.iter().fold(None, |h, v| Heap::add(h, v));
let mut heap_sorted_values = Vec::new();
while heap.is_some() {
let (updated_heap, min_value) = Heap::remove_min(heap);
heap = updated_heap;
heap_sorted_values.push(*min_value);
}
seq.sort();
assert_eq!(heap_sorted_values, seq);
heap_sorted_values
}
}

0 comments on commit b5f77f4

Please sign in to comment.