-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from MathieuSoysal/3-add-fuzzing-tests
3 add fuzzing tests
- Loading branch information
Showing
9 changed files
with
190 additions
and
122 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,8 @@ | ||
[package] | ||
name = "template-exercisme" | ||
name = "template_exercisme" | ||
escription = "A template for exercism exercises in Rust" | ||
repository = "https://github.com/MathieuSoysal/Exercism-Rust-Template" | ||
readme = "README.md" | ||
version = "0.1.0" | ||
authors = ["MathieuSoysal <[email protected]>"] | ||
license = "Apache-2.0" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
target | ||
corpus | ||
artifacts | ||
coverage |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
[package] | ||
name = "template-exercisme-fuzz" | ||
version = "0.0.0" | ||
publish = false | ||
edition = "2021" | ||
|
||
[package.metadata] | ||
cargo-fuzz = true | ||
|
||
[dependencies] | ||
libfuzzer-sys = "0.4" | ||
|
||
[dependencies.template_exercisme] | ||
path = ".." | ||
|
||
[[bin]] | ||
name = "fuzz_play_game" | ||
path = "fuzz_targets/fuzz_play_game.rs" | ||
test = false | ||
doc = false | ||
bench = false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#![no_main] | ||
|
||
use libfuzzer_sys::fuzz_target; | ||
#[macro_use] | ||
extern crate libfuzzer_sys; | ||
|
||
use template_exercisme::fizz_buzz_fibonacci; | ||
|
||
fuzz_target!(|data: u32| { | ||
fizz_buzz_fibonacci(data); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
#![feature(test)] | ||
extern crate test; | ||
|
||
pub fn play_game(n: u32) { | ||
println!("{}", fizz_buzz_fibonacci(n)); | ||
} | ||
|
||
fn is_fibonacci_number(n: u32) -> bool { | ||
let (mut previous, mut current) = (0, 1); | ||
while current < n && n <= 2971215073 { | ||
let next = previous + current; | ||
previous = current; | ||
current = next; | ||
} | ||
current == n | ||
} | ||
|
||
pub fn fizz_buzz_fibonacci(n: u32) -> String { | ||
if is_fibonacci_number(n) { | ||
"Fibonacci".to_string() | ||
} else { | ||
match (n % 3, n % 5) { | ||
(0, 0) => "FizzBuzz".to_string(), | ||
(0, _) => "Fizz".to_string(), | ||
(_, 0) => "Buzz".to_string(), | ||
(_, _) => n.to_string(), | ||
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn test_is_fibonacci_number() { | ||
assert!(is_fibonacci_number(2_971_215_073)); | ||
assert_eq!(is_fibonacci_number(u32::MAX), false); | ||
} | ||
|
||
#[test] | ||
fn test_fizz_buzz_fibonacci() { | ||
assert_eq!(fizz_buzz_fibonacci(1), "Fibonacci"); | ||
assert_eq!(fizz_buzz_fibonacci(2), "Fibonacci"); | ||
assert_eq!(fizz_buzz_fibonacci(3), "Fibonacci"); | ||
assert_eq!(fizz_buzz_fibonacci(4), "4"); | ||
assert_eq!(fizz_buzz_fibonacci(5), "Fibonacci"); | ||
assert_eq!(fizz_buzz_fibonacci(6), "Fizz"); | ||
assert_eq!(fizz_buzz_fibonacci(7), "7"); | ||
assert_eq!(fizz_buzz_fibonacci(8), "Fibonacci"); | ||
assert_eq!(fizz_buzz_fibonacci(9), "Fizz"); | ||
assert_eq!(fizz_buzz_fibonacci(10), "Buzz"); | ||
assert_eq!(fizz_buzz_fibonacci(15), "FizzBuzz"); | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod benchmarks { | ||
use super::*; | ||
use std::hint::black_box; | ||
use test::Bencher; | ||
|
||
#[bench] | ||
fn bench_play_game(b: &mut Bencher) { | ||
b.iter(|| { | ||
black_box(for i in 1..=100 { | ||
play_game(i) | ||
}); | ||
}); | ||
} | ||
|
||
#[bench] | ||
fn bench_play_game_100(b: &mut Bencher) { | ||
b.iter(|| std::hint::black_box(play_game(100))); | ||
} | ||
|
||
#[bench] | ||
fn bench_play_game_1_000_000(b: &mut Bencher) { | ||
b.iter(|| std::hint::black_box(play_game(1_000_000))); | ||
} | ||
|
||
#[bench] | ||
fn bench_play_game_2_971_215_073(b: &mut Bencher) { | ||
b.iter(|| std::hint::black_box(play_game(2_971_215_073))); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters