use crate::internal::int_bounds;
use crate::BoxGen;
use crate::*;
use core::panic;
use std::ops::{RangeBounds, RangeInclusive};
pub fn any() -> BoxGen<isize> {
ranged(..)
}
pub fn ranged<B>(bounds: B) -> BoxGen<isize>
where
B: RangeBounds<isize>,
{
assert_lib_supports_isize_bit_width();
let i64_bounds = map_to_i64_bounds(&bounds);
map_to_isize_gen(super::i64::ranged(i64_bounds))
}
pub fn completely_random<B>(bounds: B) -> BoxGen<isize>
where
B: RangeBounds<isize>,
{
assert_lib_supports_isize_bit_width();
let i64_bounds = map_to_i64_bounds(&bounds);
map_to_isize_gen(super::i64::completely_random(i64_bounds))
}
fn map_to_i64_bounds<B>(i_bounds: &B) -> RangeInclusive<i64>
where
B: RangeBounds<isize>,
{
let (start, end) = int_bounds::to_inclusive_range_tuple(i_bounds);
(start as i64)..=(end as i64)
}
fn map_to_isize_gen(gen: BoxGen<i64>) -> BoxGen<isize> {
gen.map(|i| i as isize, |j| j as i64)
}
fn assert_lib_supports_isize_bit_width() {
if isize::BITS > i64::BITS {
panic!(
"Generators for isize only support platforms where isize is at most \
64 bits wide. \
Please contact the library author for support for wider isize types.");
}
}
#[cfg(test)]
mod tests {
use crate::testing::distribution::assert_generator_has_distribution_within_percent;
use crate::testing::distribution::distribution_from_pairs;
#[test]
fn random_inclusive_has_uniform_distribution() {
assert_generator_has_distribution_within_percent(
super::completely_random(-10isize..=10isize),
distribution_from_pairs(&[
(1, -10isize),
(1, -9isize),
(1, -8isize),
(1, -7isize),
(1, -6isize),
(1, -5isize),
(1, -4isize),
(1, -3isize),
(1, -2isize),
(1, -1isize),
(1, 0isize),
(1, 1isize),
(1, 2isize),
(1, 3isize),
(1, 4isize),
(1, 5isize),
(1, 6isize),
(1, 7isize),
(1, 8isize),
(1, 9isize),
(1, 10isize),
]),
2.0, );
}
}