-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
98 additions
and
1 deletion.
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
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,45 @@ | ||
use anyhow::Result; | ||
use rand::Rng; | ||
use rand_chacha::rand_core::SeedableRng; | ||
|
||
pub fn bool() -> Result<bool> { | ||
let mut rng = rand_chacha::ChaCha20Rng::from_entropy(); | ||
Ok(rng.gen::<bool>()) | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
const NUM_ITERATION: i32 = 1000; | ||
|
||
#[test] | ||
fn test_bool() -> anyhow::Result<()> { | ||
bool()?; | ||
Ok(()) | ||
} | ||
|
||
#[test] | ||
fn test_bool_uniform() -> anyhow::Result<()> { | ||
let mut num_true = 0; | ||
for _ in 0..NUM_ITERATION { | ||
let b = bool()?; | ||
if b { | ||
num_true += 1; | ||
} | ||
} | ||
|
||
let lower_bound = 0.40 * NUM_ITERATION as f64; | ||
let upper_bound = 0.60 * NUM_ITERATION as f64; | ||
let high_enough = lower_bound < num_true as f64; | ||
let low_enough = upper_bound > num_true as f64; | ||
assert!( | ||
high_enough && low_enough, | ||
"{} was not between the acceptable bounds of ({},{})", | ||
num_true, | ||
lower_bound, | ||
upper_bound | ||
); | ||
Ok(()) | ||
} | ||
} |
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,22 @@ | ||
mod bool_impl; | ||
|
||
use starlark::{environment::MethodsBuilder, starlark_module, values::starlark_value}; | ||
|
||
/* | ||
* Define our library for this module. | ||
*/ | ||
crate::eldritch_lib!(RandomLibrary, "random_library"); | ||
|
||
/* | ||
* Below, we define starlark wrappers for all of our library methods. | ||
* The functions must be defined here to be present on our library. | ||
*/ | ||
#[starlark_module] | ||
#[rustfmt::skip] | ||
#[allow(clippy::needless_lifetimes, clippy::type_complexity, clippy::too_many_arguments)] | ||
fn methods(builder: &mut MethodsBuilder) { | ||
#[allow(unused_variables)] | ||
fn bool<'v>(this: &RandomLibrary) -> anyhow::Result<bool> { | ||
bool_impl::bool() | ||
} | ||
} |
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