Sample Project
# - ./sample_project/Cargo.toml -
[package]
name = "hello_add"
version = "0.1.0"
edition = "2021"
[dependencies]
[dev-dependencies]
criterion = "0.5"
[workspace]
exclude = ["fuzz"]
// - sample_project/src/lib.rs -
/// Простенька функція для прикладу
pub fn add(a: i32, b: i32) -> i32 {
a + b
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_add() {
assert_eq!(add(2, 2), 4);
}
#[test]
#[should_panic]
fn test_fail() {
assert_eq!(add(2, 2), 5);
}
}
// - sample_project/tests/integration_test.rs -
use hello_add::add;
#[test]
fn integration_add() {
assert_eq!(add(10, 5), 15);
}
// - sample_project/benches/test_bench.rs -
use criterion::{black_box, criterion_group, criterion_main, Criterion};
use hello_add::add;
fn bench_addition(c: &mut Criterion) {
c.bench_function("add two numbers", |b| {
b.iter(|| add(black_box(2), black_box(2)))
});
}
criterion_group!(benches, bench_addition);
criterion_main!(benches);
# - sample_project/fuzz/Cargo.toml -
[package]
name = "hello_add-fuzz"
version = "0.0.1"
edition = "2021"
publish = false
[dependencies]
libfuzzer-sys = { version = "0.4", features = [
"arbitrary-derive",
], default-features = false }
[dependencies.hello_add]
path = ".."
[[bin]]
name = "test_target"
path = "fuzz_targets/test_target.rs"
test = false
doc = false
[package.metadata]
cargo-fuzz = true
// - sample_project/fuzz/fuzz_targets/test_target.rs -
#![no_main]
use libfuzzer_sys::fuzz_target;
use hello_add::add;
fuzz_target!(|data: (i32, i32)| {
let (a, b) = data;
let _ = add(a, b);
});