Skip to content

Feature | Integration with the arbitrary crate #84

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
9 changes: 9 additions & 0 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,12 @@ jobs:
- name: Run tests
run: cargo test --verbose --features std

build_arbitrary:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- name: Build
run: cargo build --verbose --features arbitrary
- name: Run tests
run: cargo test --verbose --features arbitrary
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@ categories = ["embedded", "no-std", "data-structures"]


[dependencies]
arbitrary = { version = "1.4.1", optional = true }

[features]
default = []
# The std feature only toggles whether the Error trait is implemented for error
# types. Apart from that, this crate works without explicit indication both on
# std and no_std systems.
std = []
arbitrary = ["dep:arbitrary"]
27 changes: 27 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,13 @@ macro_rules! implement_common {
self.wrapping_sub(other)
}
}

#[cfg(feature = "arbitrary")]
impl<'a> arbitrary::Arbitrary<'a> for $name {
fn arbitrary(u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result<Self> {
Ok($name($type::arbitrary(u)?).mask())
}
}
};
}

Expand Down Expand Up @@ -967,3 +974,23 @@ mod tests {
}
}
}

#[cfg(all(test, feature = "arbitrary"))]
mod tests_arbitrary {
use super::*;
use arbitrary::Arbitrary;

#[test]
fn generate_from_unstructured_data() {
let data = [0x0, 0x0, 0x0, 0x0];
let mut unstructured = arbitrary::Unstructured::new(&data);
assert_eq!(u31::arbitrary(&mut unstructured), Ok(u31(0x0)));
}

#[test]
fn data_is_masked_appropriately_on_generation() {
let data = [0xFF, 0xFF, 0xFF, 0xFF];
let mut unstructured = arbitrary::Unstructured::new(&data);
assert_eq!(u31::arbitrary(&mut unstructured), Ok(u31(0x7FFFFFFF)));
}
}