Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update fuzz tests and have run them in CI #75

Merged
merged 1 commit into from
Apr 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,16 @@ jobs:
with:
token: ${{ secrets.GITHUB_TOKEN }}
args: --all-features

fuzz-tests:
name: Fuzz tests
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: nightly
override: true
- run: cargo install cargo-fuzz
- run: for fuzz_test in `cargo fuzz list`; do cargo fuzz run $fuzz_test -- -max_total_time=10 || exit 1; done
10 changes: 7 additions & 3 deletions fuzz/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,18 @@ name = "im-fuzz"
version = "0.0.0"
authors = ["Automatically generated"]
publish = false
edition = "2018"
edition = "2021"

[package.metadata]
cargo-fuzz = true

[features]
default = ["small-chunks"]
small-chunks = ["imbl/small-chunks"]

[dependencies]
libfuzzer-sys = "0.2"
arbitrary = { version = "0.3", features = ["derive"] }
libfuzzer-sys = "0.4"
arbitrary = { version = "1", features = ["derive"] }

[dependencies.imbl]
path = ".."
Expand Down
11 changes: 6 additions & 5 deletions fuzz/fuzz_targets/hashset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use std::collections::HashSet as NatSet;
use std::fmt::Debug;
use std::iter::FromIterator;

use arbitrary::Arbitrary;
use libfuzzer_sys::fuzz_target;
Expand All @@ -14,10 +15,7 @@ enum Action<A> {
Remove(A),
}

#[derive(Arbitrary)]
struct Actions<A>(Vec<Action<A>>);

fuzz_target!(|actions: Vec<Action<u64>>| {
fuzz_target!(|actions: Vec<Action<u32>>| {
let mut set = HashSet::new();
let mut nat = NatSet::new();
for action in actions {
Expand All @@ -36,6 +34,9 @@ fuzz_target!(|actions: Vec<Action<u64>>| {
}
}
assert_eq!(nat.len(), set.len());
assert_eq!(HashSet::from(nat.clone()), set);
}
assert_eq!(HashSet::from(nat.clone()), set);
assert_eq!(NatSet::from_iter(set.iter().cloned()), nat);
assert_eq!(set.iter().count(), nat.len());
assert_eq!(set.into_iter().count(), nat.len());
});
52 changes: 45 additions & 7 deletions fuzz/fuzz_targets/ordset.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
#![no_main]

use std::collections::HashSet as NatSet;
use std::collections::BTreeSet as NatSet;
use std::fmt::Debug;
use std::ops::Range;

use arbitrary::Arbitrary;
use libfuzzer_sys::fuzz_target;

use imbl::OrdSet;

#[derive(Arbitrary, Debug)]
enum Action<A> {
enum Action<A: Clone + PartialOrd> {
Insert(A),
Remove(A),
Range(Range<A>),
}

#[derive(Arbitrary)]
struct Actions<A>(Vec<Action<A>>);

fuzz_target!(|actions: Vec<Action<u64>>| {
fuzz_target!(|actions: Vec<Action<u32>>| {
let mut set = OrdSet::new();
let mut nat = NatSet::new();
for action in actions {
Expand All @@ -34,8 +33,47 @@ fuzz_target!(|actions: Vec<Action<u64>>| {
set.remove(&value);
assert_eq!(len, set.len());
}
Action::Range(range) => {
assert_eq!(set.get_min(), nat.first());
assert_eq!(set.get_max(), nat.last());
assert_eq!(set.get_next(&range.start), nat.range(range.start..).next());
assert_eq!(set.get_prev(&range.start), nat.range(..=range.start).last());

let mut set_it = set.range(range.clone());
let mut nat_it = nat.range(range.clone());
loop {
let (a, b) = (set_it.next(), nat_it.next());
assert_eq!(a, b);
if a.is_none() {
break;
}
}
let range = range.start..=range.end;
let mut set_it = set.range(range.clone());
let mut nat_it = nat.range(range);
loop {
let (a, b) = (set_it.next(), nat_it.next());
assert_eq!(a, b);
if a.is_none() {
break;
}
}
}
}
assert_eq!(nat.len(), set.len());
assert_eq!(OrdSet::from(nat.clone()), set);
}
assert_eq!(OrdSet::from(nat.clone()), set);
assert_eq!(OrdSet::from_iter(nat.iter().cloned()), set);
for (a, b) in set.range(..).zip(nat.range(..)) {
assert_eq!(a, b);
}
for (a, b) in set.iter().zip(&nat) {
assert_eq!(a, b);
}
for (a, b) in set.iter().rev().zip(nat.iter().rev()) {
assert_eq!(a, b);
}
for (a, b) in set.into_iter().zip(nat) {
assert_eq!(a, b);
}
});
9 changes: 6 additions & 3 deletions fuzz/fuzz_targets/vector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ fn cap_index(len: usize, index: usize) -> usize {
}
}

fuzz_target!(|actions: Vec<Action<u64>>| {
fuzz_target!(|actions: Vec<Action<u32>>| {
let mut vec = Vector::new();
let mut nat = Vec::new();
vec.assert_invariants();
Expand Down Expand Up @@ -121,8 +121,11 @@ fuzz_target!(|actions: Vec<Action<u64>>| {
nat = nat_right;
}
}
vec.assert_invariants();
assert_eq!(nat.len(), vec.len());
assert_eq!(Vector::from_iter(nat.iter().cloned()), vec);
}
vec.assert_invariants();
assert_eq!(Vector::from_iter(nat.iter().cloned()), vec);
assert_eq!(Vec::from_iter(vec.iter().cloned()), nat);
assert_eq!(Vec::from_iter(vec.clone().into_iter()), nat);
assert_eq!(Vec::from_iter(vec.iter().rev()), Vec::from_iter(nat.iter().rev()));
});
Loading