use super::*;
use std::ops::{Add, Range, RangeTo};
use proptest::num::f64;
default!(Probability, 0.5);
pub fn prob<X: Into<Probability>>(from: X) -> Probability {
from.into()
}
impl From<f64> for Probability {
fn from(prob: f64) -> Self {
Probability::new(prob)
}
}
impl Probability {
pub fn new(prob: f64) -> Self {
assert!(prob >= 0.0 && prob <= 1.0);
Probability(prob)
}
pub fn with<X>(self, and: X) -> product_type![Self, X] {
product_pack![self, and]
}
pub fn lift<X: Default>(self) -> product_type![Self, X] {
self.with(default())
}
}
arbitrary!(Probability, FromMapStrategy<Range<f64>, Self>;
from_map_strategy(0.0..1.0)
);
#[cfg(feature = "frunk")]
use frunk_core::generic::Generic;
#[cfg(feature = "frunk")]
impl Generic for Probability {
type Repr = f64;
fn into(self) -> Self::Repr { self.0 }
fn from(r: Self::Repr) -> Self { prob(r) }
}
#[derive(Clone, Copy, PartialEq, Debug, Into)]
pub struct Probability(f64);
default!(SizeBounds, 0..100);
pub fn size_bounds<X: Into<SizeBounds>>(from: X) -> SizeBounds {
from.into()
}
impl SizeBounds {
pub fn new(range: Range<usize>) -> Self {
SizeBounds(range)
}
pub fn with<X>(self, and: X) -> product_type![Self, X] {
product_pack![self, and]
}
pub fn lift<X: Default>(self) -> product_type![Self, X] {
self.with(default())
}
}
impl From<(usize, usize)> for SizeBounds {
fn from(x: (usize, usize)) -> Self {
(x.0..x.1).into()
}
}
impl From<usize> for SizeBounds {
fn from(exact: usize) -> Self {
size_bounds(exact..exact + 1)
}
}
impl From<RangeTo<usize>> for SizeBounds {
fn from(high: RangeTo<usize>) -> Self {
size_bounds(0..high.end)
}
}
impl Add<usize> for SizeBounds {
type Output = SizeBounds;
fn add(self, rhs: usize) -> Self::Output {
let Range { start, end } = self.0;
size_bounds((start + rhs)..(end + rhs))
}
}
arbitrary!(SizeBounds, FMapped<'a, Range<usize>, Self>;
any_sinto::<Range<usize>, _>()
);
#[derive(Clone, PartialEq, Eq, Hash, Debug, From, Into)]
#[cfg_attr(feature = "frunk", derive(Generic))]
pub struct SizeBounds(Range<usize>);
#[cfg(test)]
mod test {
no_panic_test!(
probability => Probability,
size_bounds => SizeBounds
);
}