tag | ad10ddb0d957d8833213053cd2391234e0a8d2bd | |
---|---|---|
tagger | The Android Open Source Project <initial-contribution@android.com> | Fri May 26 06:24:18 2023 -0700 |
object | 5187f941e4737e1d721ac600acefe66a8f4d1a46 |
t_frc_odp_330442040
commit | 5187f941e4737e1d721ac600acefe66a8f4d1a46 | [log] [tgz] |
---|---|---|
author | ziyiw <ziyiw@google.com> | Thu Apr 07 03:53:57 2022 +0000 |
committer | Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com> | Thu Apr 07 03:53:57 2022 +0000 |
tree | 252a378bfaad1976f5f9ecf366574d7e62e75985 | |
parent | 490dab6fb72a9ea200f312e2859275e7ac1d1757 [diff] | |
parent | ec3a7b2a50277643e4a8e720f8a35ca631c5f0ac [diff] |
Add apex_available and min_sdk_version to arbitrary crate. am: 5ca7816403 am: 135e34b109 am: ec3a7b2a50 Original change: https://android-review.googlesource.com/c/platform/external/rust/crates/arbitrary/+/2057767 Change-Id: Ibdbba868a0d1bb69873e32f09a5cd4f0dcd6ee1e Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
The Arbitrary
crate lets you construct arbitrary instances of a type.
This crate is primarily intended to be combined with a fuzzer like libFuzzer and cargo-fuzz
or AFL, and to help you turn the raw, untyped byte buffers that they produce into well-typed, valid, structured values. This allows you to combine structure-aware test case generation with coverage-guided, mutation-based fuzzers.
Read the API documentation on docs.rs
!
Say you're writing a color conversion library, and you have an Rgb
struct to represent RGB colors. You might want to implement Arbitrary
for Rgb
so that you could take arbitrary Rgb
instances in a test function that asserts some property (for example, asserting that RGB converted to HSL and converted back to RGB always ends up exactly where we started).
Arbitrary
Automatically deriving the Arbitrary
trait is the recommended way to implement Arbitrary
for your types.
Automatically deriving Arbitrary
requires you to enable the "derive"
cargo feature:
# Cargo.toml [dependencies] arbitrary = { version = "1", features = ["derive"] }
And then you can simply add #[derive(Arbitrary)]
annotations to your types:
// rgb.rs use arbitrary::Arbitrary; #[derive(Arbitrary)] pub struct Rgb { pub r: u8, pub g: u8, pub b: u8, }
Arbitrary
By HandAlternatively, you can write an Arbitrary
implementation by hand:
// rgb.rs use arbitrary::{Arbitrary, Result, Unstructured}; #[derive(Copy, Clone, Debug)] pub struct Rgb { pub r: u8, pub g: u8, pub b: u8, } impl<'a> Arbitrary<'a> for Rgb { fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> { let r = u8::arbitrary(u)?; let g = u8::arbitrary(u)?; let b = u8::arbitrary(u)?; Ok(Rgb { r, g, b }) } }
Licensed under dual MIT or Apache-2.0 at your choice.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this project by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.