-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add support for calling generators from anywhere
- Loading branch information
Showing
24 changed files
with
1,095 additions
and
323 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
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,33 @@ | ||
pub use bolero_generator::any::*; | ||
|
||
/// Runs a function that overrides the default driver for `bolero_generator::any` and | ||
/// returns the result | ||
#[cfg(not(kani))] | ||
pub fn run<D, F, R>(driver: Box<D>, test: F) -> (Box<D>, Result<bool, crate::panic::PanicError>) | ||
where | ||
D: 'static + bolero_generator::driver::object::DynDriver + core::any::Any + Sized, | ||
F: FnMut() -> R, | ||
R: super::IntoResult, | ||
{ | ||
let mut test = core::panic::AssertUnwindSafe(test); | ||
scope::with(driver, || { | ||
crate::panic::catch(|| test.0().into_result().map(|_| true)) | ||
}) | ||
} | ||
|
||
/// Runs a function that overrides the default driver for `bolero_generator::any` and | ||
/// returns the result | ||
#[cfg(kani)] | ||
pub fn run<F, R>( | ||
driver: bolero_generator::kani::Driver, | ||
mut test: F, | ||
) -> ( | ||
bolero_generator::kani::Driver, | ||
Result<bool, crate::panic::PanicError>, | ||
) | ||
where | ||
F: FnMut() -> R, | ||
R: super::IntoResult, | ||
{ | ||
scope::with(driver, || test().into_result().map(|_| true)) | ||
} |
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
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,113 @@ | ||
use crate::{gen, TypeGenerator, ValueGenerator}; | ||
|
||
#[cfg(not(kani))] | ||
mod default; | ||
|
||
#[cfg(any(kani, test))] | ||
#[cfg_attr(not(kani), allow(dead_code, unused_imports))] | ||
mod kani; | ||
|
||
pub mod scope { | ||
#[cfg(not(kani))] | ||
pub use super::default::*; | ||
#[cfg(kani)] | ||
pub use super::kani::*; | ||
} | ||
|
||
pub use scope::{assume, fill_bytes, Error}; | ||
|
||
pub trait Any: ValueGenerator { | ||
fn any(&self) -> Self::Output; | ||
} | ||
|
||
impl<G: 'static + ValueGenerator> Any for G { | ||
#[track_caller] | ||
fn any(&self) -> Self::Output { | ||
scope::any(self) | ||
} | ||
} | ||
|
||
#[inline] | ||
pub fn any<T: TypeGenerator>() -> T { | ||
gen().any() | ||
} | ||
|
||
pub trait AnySliceExt<T> { | ||
fn pick(&self) -> &T; | ||
} | ||
|
||
impl<T> AnySliceExt<T> for [T] { | ||
#[inline] | ||
fn pick(&self) -> &T { | ||
let index = (0..self.len()).any(); | ||
&self[index] | ||
} | ||
} | ||
|
||
pub trait AnySliceMutExt<T> { | ||
fn shuffle(&mut self); | ||
fn fill_any(&mut self) | ||
where | ||
T: TypeGenerator; | ||
} | ||
|
||
impl<T> AnySliceMutExt<T> for [T] { | ||
#[inline] | ||
fn shuffle(&mut self) { | ||
for src in (1..self.len()).rev() { | ||
let dst = (0..=src).any(); | ||
// invariant: elements with index > src have been locked in place. | ||
self.swap(src, dst); | ||
} | ||
} | ||
|
||
#[inline] | ||
fn fill_any(&mut self) | ||
where | ||
T: TypeGenerator, | ||
{ | ||
for value in self { | ||
*value = any(); | ||
} | ||
} | ||
} | ||
|
||
#[cfg(feature = "alloc")] | ||
impl<T> AnySliceMutExt<T> for alloc::collections::VecDeque<T> { | ||
#[inline] | ||
fn shuffle(&mut self) { | ||
for src in (1..self.len()).rev() { | ||
let dst = (0..=src).any(); | ||
// invariant: elements with index > src have been locked in place. | ||
self.swap(src, dst); | ||
} | ||
} | ||
|
||
#[inline] | ||
fn fill_any(&mut self) | ||
where | ||
T: TypeGenerator, | ||
{ | ||
for value in self { | ||
*value = any(); | ||
} | ||
} | ||
} | ||
|
||
#[inline] | ||
pub fn fill<T>(values: &mut [T]) | ||
where | ||
T: TypeGenerator, | ||
{ | ||
values.fill_any() | ||
} | ||
|
||
#[inline] | ||
pub fn shuffle<T>(items: &mut [T]) { | ||
items.shuffle() | ||
} | ||
|
||
#[inline] | ||
pub fn pick<T>(items: &[T]) -> &T { | ||
items.pick() | ||
} |
Oops, something went wrong.