-
Notifications
You must be signed in to change notification settings - Fork 206
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update fuzzer documentation and changelog
- Loading branch information
1 parent
39b6739
commit 46d0afe
Showing
6 changed files
with
76 additions
and
9 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 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 |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# `generate_arg` | ||
|
||
> `fn generate_arg<T, +Serde<T>, +Drop<T>, +Into<T, felt252>>(min_value: T, max_value: T) -> T` | ||
Returns a random number from a range `[min_value, max_value]`. | ||
It is used mostly in the context of fuzz testing to implement [Fuzzable](../snforge-library/fuzzable.md) trait. |
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,50 @@ | ||
# `fuzzable` Module | ||
|
||
Module containing `Fuzzable` trait needed for fuzz testing and its implementations for basic types. | ||
|
||
## `Fuzzable` | ||
|
||
```rust | ||
pub trait Fuzzable<T, +Debug<T>> { | ||
fn blank() -> T; | ||
fn generate() -> T; | ||
} | ||
``` | ||
|
||
This trait is used by `snforge` to generate random data for fuzz testing. | ||
Any type that is used as a parameter in a test function with the [`#[fuzzer]`](../../testing/test-attributes.md#fuzzer) attribute must implement this trait. | ||
|
||
- `blank()` function returns an empty or default value that is used only for configuration runs. For instance, it returns `0` for numeric types. | ||
- `generate()` function is used to return a random value of the given type. To implement this function, it is necessary to either use the [generate_arg](../cheatcodes/generate_arg.md) cheatcode, | ||
which can uniformly generate a random number within a specified range, or use a `Fuzzable` implementation for a different type. | ||
|
||
## Example | ||
|
||
Implementation for a custom type `Message`: | ||
|
||
```rust | ||
use core::num::traits::Bounded; | ||
use snforge_std::fuzzable::{Fuzzable, generate_arg}; | ||
|
||
#[derive(Debug, Drop)] | ||
struct Message { | ||
id: u64, | ||
text: ByteArray | ||
} | ||
|
||
impl FuzzableMessage of Fuzzable<Message> { | ||
fn blank() -> Message { | ||
Message { | ||
id: 0, | ||
text: "" | ||
} | ||
} | ||
|
||
fn generate() -> Message { | ||
Message { | ||
id: generate_arg(0, Bounded::<u64>::MAX), | ||
text: Fuzzable::<ByteArray>::generate() | ||
} | ||
} | ||
} | ||
``` |
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