prop_check_rs/gen/
choose.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
use crate::gen::{Gen, Gens};

pub trait Choose
where
  Self: Sized, {
  fn choose(min: Self, max: Self) -> Gen<Self>;
}

impl<A> Choose for Option<A>
where
  A: Choose + Clone + 'static,
{
  fn choose(min: Self, max: Self) -> Gen<Self> {
    match (min, max) {
      (Some(mn), Some(mx)) => Gens::choose(mn, mx).map(Some),
      (none, _) if none.is_none() => Gens::pure(none),
      (_, none) if none.is_none() => Gens::pure(none),
      _ => panic!("occurred error"),
    }
  }
}

impl<A, B> Choose for Result<A, B>
where
  A: Choose + Clone + 'static,
  B: Clone + 'static,
{
  fn choose(min: Self, max: Self) -> Gen<Self> {
    match (min, max) {
      (Ok(mn), Ok(mx)) => Gens::choose(mn, mx).map(Ok),
      (err, _) if err.is_err() => Gens::pure(err),
      (_, err) if err.is_err() => Gens::pure(err),
      _ => panic!("occurred error"),
    }
  }
}

impl Choose for usize {
  fn choose(min: Self, max: Self) -> Gen<Self> {
    Gens::choose_u64(min as u64, max as u64).map(|v| v as usize)
  }
}

impl Choose for i64 {
  fn choose(min: Self, max: Self) -> Gen<Self> {
    Gens::choose_i64(min, max)
  }
}

impl Choose for u64 {
  fn choose(min: Self, max: Self) -> Gen<Self> {
    Gens::choose_u64(min, max)
  }
}

impl Choose for i32 {
  fn choose(min: Self, max: Self) -> Gen<Self> {
    Gens::choose_i32(min, max)
  }
}

impl Choose for u32 {
  fn choose(min: Self, max: Self) -> Gen<Self> {
    Gens::choose_u32(min, max)
  }
}

impl Choose for i16 {
  fn choose(min: Self, max: Self) -> Gen<Self> {
    Gens::choose_i16(min, max)
  }
}

impl Choose for u16 {
  fn choose(min: Self, max: Self) -> Gen<Self> {
    Gens::choose_u16(min, max)
  }
}

impl Choose for i8 {
  fn choose(min: Self, max: Self) -> Gen<Self> {
    Gens::choose_i8(min, max)
  }
}

impl Choose for u8 {
  fn choose(min: Self, max: Self) -> Gen<Self> {
    Gens::choose_u8(min, max)
  }
}

impl Choose for char {
  fn choose(min: Self, max: Self) -> Gen<Self> {
    Gens::choose_char(min, max)
  }
}

impl Choose for f64 {
  fn choose(min: Self, max: Self) -> Gen<Self> {
    Gens::choose_f64(min, max)
  }
}

impl Choose for f32 {
  fn choose(min: Self, max: Self) -> Gen<Self> {
    Gens::choose_f32(min, max)
  }
}