Skip to content
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)));
}
}