prop_check_rs/
rng.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
use rand::prelude::*;

/// The trait to generate random values.<br/>
/// ランダムな値を生成するためのトレイトです。
pub trait NextRandValue
where
  Self: Sized, {
  /// `next_i64` generates a tuple of `i64` and `Self`.<br/>
  /// `next_i64`は`i64`と`Self`のタプルを生成します。
  fn next_i64(&self) -> (i64, Self);

  /// `next_u64` generates a tuple of `u64` and `Self`.<br/>
  /// `next_u64`は`u64`と`Self`のタプルを生成します。
  fn next_u64(&self) -> (u64, Self) {
    let (i, r) = self.next_i64();
    (if i < 0 { -(i + 1) as u64 } else { i as u64 }, r)
  }

  /// `next_i32` generates a tuple of `i32` and `Self`.<br/>
  /// `next_i32`は`i32`と`Self`のタプルを生成します。
  fn next_i32(&self) -> (i32, Self);

  /// `next_u32` generates a tuple of `u32` and `Self`.<br/>
  /// `next_u32`は`u32`と`Self`のタプルを生成します。
  fn next_u32(&self) -> (u32, Self) {
    let (i, r) = self.next_i32();
    (if i < 0 { -(i + 1) as u32 } else { i as u32 }, r)
  }

  /// `next_i16` generates a tuple of `i16` and `Self`.<br/>
  /// `next_i16`は`i16`と`Self`のタプルを生成します。
  fn next_i16(&self) -> (i16, Self);

  /// `next_u16` generates a tuple of `u16` and `Self`.<br/>
  /// `next_u16`は`u16`と`Self`のタプルを生成します。
  fn next_u16(&self) -> (u16, Self) {
    let (i, r) = self.next_i16();
    (if i < 0 { -(i + 1) as u16 } else { i as u16 }, r)
  }

  /// `next_i8` generates a tuple of `i8` and `Self`.<br/>
  /// `next_i8`は`i8`と`Self`のタプルを生成します。
  fn next_i8(&self) -> (i8, Self);

  /// `next_u8` generates a tuple of `u8` and `Self`.<br/>
  /// `next_u8`は`u8`と`Self`のタプルを生成します。
  fn next_u8(&self) -> (u8, Self) {
    let (i, r) = self.next_i8();
    (if i < 0 { -(i + 1) as u8 } else { i as u8 }, r)
  }

  /// `next_f64` generates a tuple of `f64` and `Self`.<br/>
  /// `next_f64`は`f64`と`Self`のタプルを生成します。
  fn next_f64(&self) -> (f64, Self) {
    let (i, r) = self.next_i64();
    (i as f64 / (i64::MAX as f64 + 1.0f64), r)
  }

  /// `next_f32` generates a tuple of `f32` and `Self`.<br/>
  /// `next_f32`は`f32`と`Self`のタプルを生成します。
  fn next_f32(&self) -> (f32, Self) {
    let (i, r) = self.next_i32();
    (i as f32 / (i32::MAX as f32 + 1.0f32), r)
  }

  /// `next_bool` generates a tuple of `bool` and `Self`.<br/>
  /// `next_bool`は`bool`と`Self`のタプルを生成します。
  fn next_bool(&self) -> (bool, Self) {
    let (i, r) = self.next_i32();
    ((i % 2) != 0, r)
  }
}

/// `RandGen` is a trait to generate random values.<br/>
/// `RandGen`はランダムな値を生成するためのトレイトです。
pub trait RandGen<T: NextRandValue>
where
  Self: Sized, {
  /// `rnd_gen` generates a tuple of `Self` and `T`.<br/>
  /// `rnd_gen`は`Self`と`T`のタプルを生成します。
  fn rnd_gen(rng: T) -> (Self, T);
}

impl<T: NextRandValue> RandGen<T> for i64 {
  fn rnd_gen(rng: T) -> (Self, T) {
    rng.next_i64()
  }
}

impl<T: NextRandValue> RandGen<T> for u32 {
  fn rnd_gen(rng: T) -> (Self, T) {
    rng.next_u32()
  }
}

impl<T: NextRandValue> RandGen<T> for i32 {
  fn rnd_gen(rng: T) -> (Self, T) {
    rng.next_i32()
  }
}

impl<T: NextRandValue> RandGen<T> for i16 {
  fn rnd_gen(rng: T) -> (Self, T) {
    rng.next_i16()
  }
}

impl<T: NextRandValue> RandGen<T> for f32 {
  fn rnd_gen(rng: T) -> (Self, T) {
    rng.next_f32()
  }
}

impl<T: NextRandValue> RandGen<T> for bool {
  fn rnd_gen(rng: T) -> (Self, T) {
    rng.next_bool()
  }
}

/// `RNG` is a random number generator.
/// `RNG`は乱数生成器です。
#[derive(Clone, Debug, PartialEq)]
pub struct RNG {
  rng: StdRng,
}

type DynRand<A> = dyn FnMut(RNG) -> (A, RNG);
type BoxRand<A> = Box<DynRand<A>>;

impl Default for RNG {
  fn default() -> Self {
    Self::new()
  }
}

impl NextRandValue for RNG {
  fn next_i64(&self) -> (i64, Self) {
    let mut mr = self.rng.clone();
    let n = mr.gen();
    (n, Self { rng: mr })
  }

  fn next_u64(&self) -> (u64, Self) {
    let mut mr = self.rng.clone();
    let n = mr.gen();
    (n, Self { rng: mr })
  }

  fn next_i32(&self) -> (i32, Self) {
    let mut mr = self.rng.clone();
    let n = mr.gen();
    (n, Self { rng: mr })
  }

  fn next_u32(&self) -> (u32, Self) {
    let mut mr = self.rng.clone();
    let n = mr.gen();
    (n, Self { rng: mr })
  }

  fn next_i16(&self) -> (i16, Self) {
    let mut mr = self.rng.clone();
    let n = mr.gen();
    (n, Self { rng: mr })
  }

  fn next_u16(&self) -> (u16, Self) {
    let mut mr = self.rng.clone();
    let n = mr.gen();
    (n, Self { rng: mr })
  }

  fn next_i8(&self) -> (i8, Self) {
    let mut mr = self.rng.clone();
    let n = mr.gen();
    (n, Self { rng: mr })
  }

  fn next_u8(&self) -> (u8, Self) {
    let mut mr = self.rng.clone();
    let n = mr.gen();
    (n, Self { rng: mr })
  }

  fn next_f64(&self) -> (f64, Self) {
    let mut mr = self.rng.clone();
    let n = mr.gen();
    (n, Self { rng: mr })
  }

  fn next_f32(&self) -> (f32, Self) {
    let mut mr = self.rng.clone();
    let n = mr.gen();
    (n, Self { rng: mr })
  }

  fn next_bool(&self) -> (bool, Self) {
    let mut mr = self.rng.clone();
    let n = mr.gen();
    (n, Self { rng: mr })
  }
}

impl RNG {
  /// `new` is a constructor.<br/>
  /// `new`はファクトリです。
  pub fn new() -> Self {
    Self {
      rng: StdRng::from_rng(thread_rng()).unwrap(),
    }
  }

  /// `new_with_seed` is a constructor with seed.<br/>
  /// `new_with_seed`はシード値を指定するファクトリです。
  pub fn with_seed(mut self, seed: u64) -> Self {
    self.rng = StdRng::seed_from_u64(seed);
    self
  }

  /// `i32_f32` generates a tuple of `i32` and `f32`.<br/>
  /// `i32_f32`は`i32`と`f32`のタプルを生成します。
  pub fn i32_f32(&self) -> ((i32, f32), Self) {
    let (i, r1) = self.next_i32();
    let (d, r2) = r1.next_f32();
    ((i, d), r2)
  }

  /// `f32_i32` generates a tuple of `f32` and `i32`.<br/>
  /// `f32_i32`は`f32`と`i32`のタプルを生成します。
  pub fn f32_i32(&self) -> ((f32, i32), Self) {
    let ((i, d), r) = self.i32_f32();
    ((d, i), r)
  }

  /// `f32_3` generates a tuple of `f32`, `f32` and `f32`.<br/>
  /// `f32_3`は`f32`と`f32`と`f32`のタプルを生成します。
  pub fn f32_3(&self) -> ((f32, f32, f32), Self) {
    let (d1, r1) = self.next_f32();
    let (d2, r2) = r1.next_f32();
    let (d3, r3) = r2.next_f32();
    ((d1, d2, d3), r3)
  }

  /// `f32s` generates a vector of `f32`.<br/>
  /// `f32s`は`f32`のベクタを生成します。
  pub fn i32s(self, count: u32) -> (Vec<i32>, Self) {
    let mut index = count;
    let mut acc = vec![];
    let mut current_rng = self;
    while index > 0 {
      let (x, new_rng) = current_rng.next_i32();
      acc.push(x);
      index -= 1;
      current_rng = new_rng;
    }
    (acc, current_rng)
  }

  /// `unit` generates a function that returns a tuple of `A` and `RNG`.<br/>
  /// `unit`は`A`と`RNG`のタプルを返す関数を生成します。
  pub fn unit<A>(a: A) -> BoxRand<A>
  where
    A: Clone + 'static, {
    Box::new(move |rng: RNG| (a.clone(), rng))
  }

  /// `sequence` generates a function that returns a tuple of `Vec<A>` and `RNG`.<br/>
  /// `sequence`は`Vec<A>`と`RNG`のタプルを返す関数を生成します。
  pub fn sequence<A, F>(fs: Vec<F>) -> BoxRand<Vec<A>>
  where
    A: Clone + 'static,
    F: FnMut(RNG) -> (A, RNG) + 'static, {
    let unit = Self::unit(Vec::<A>::new());
    let result = fs.into_iter().fold(unit, |acc, e| {
      Self::map2(acc, e, |mut a, b| {
        a.push(b);
        a
      })
    });
    result
  }

  /// `int_value` generates a function that returns a tuple of `i32` and `RNG`.<br/>
  /// `int_value`は`i32`と`RNG`のタプルを返す関数を生成します。
  pub fn int_value() -> BoxRand<i32> {
    Box::new(move |rng| rng.next_i32())
  }

  /// `double_value` generates a function that returns a tuple of `f32` and `RNG`.<br/>
  /// `double_value`は`f32`と`RNG`のタプルを返す関数を生成します。
  pub fn double_value() -> BoxRand<f32> {
    Box::new(move |rng| rng.next_f32())
  }

  /// `map` generates a function that returns a tuple of `B` and `RNG`.<br/>
  /// `map`は`B`と`RNG`のタプルを返す関数を生成します。
  pub fn map<A, B, F1, F2>(mut s: F1, mut f: F2) -> BoxRand<B>
  where
    F1: FnMut(RNG) -> (A, RNG) + 'static,
    F2: FnMut(A) -> B + 'static, {
    Box::new(move |rng| {
      let (a, rng2) = s(rng);
      (f(a), rng2)
    })
  }

  /// `map2` generates a function that returns a tuple of `C` and `RNG`.<br/>
  /// `map2`は`C`と`RNG`のタプルを返す関数を生成します。
  pub fn map2<F1, F2, F3, A, B, C>(mut ra: F1, mut rb: F2, mut f: F3) -> BoxRand<C>
  where
    F1: FnMut(RNG) -> (A, RNG) + 'static,
    F2: FnMut(RNG) -> (B, RNG) + 'static,
    F3: FnMut(A, B) -> C + 'static, {
    Box::new(move |rng| {
      let (a, r1) = ra(rng);
      let (b, r2) = rb(r1);
      (f(a, b), r2)
    })
  }

  /// `both` generates a function that returns a tuple of `(A, B)` and `RNG`.<br/>
  /// `both`は`(A, B)`と`RNG`のタプルを返す関数を生成します。
  pub fn both<F1, F2, A, B>(ra: F1, rb: F2) -> BoxRand<(A, B)>
  where
    F1: FnMut(RNG) -> (A, RNG) + 'static,
    F2: FnMut(RNG) -> (B, RNG) + 'static, {
    Self::map2(ra, rb, |a, b| (a, b))
  }

  /// `rand_int_double` generates a function that returns a tuple of `(i32, f32)` and `RNG`.<br/>
  /// `rand_int_double`は`(i32, f32)`と`RNG`のタプルを返す関数を生成します。
  pub fn rand_int_double() -> BoxRand<(i32, f32)> {
    Self::both(Self::int_value(), Self::double_value())
  }

  /// `rand_double_int` generates a function that returns a tuple of `(f32, i32)` and `RNG`.<br/>
  /// `rand_double_int`は`(f32, i32)`と`RNG`のタプルを返す関数を生成します。
  pub fn rand_double_int<'a>() -> BoxRand<(f32, i32)> {
    Self::both(Self::double_value(), Self::int_value())
  }

  /// `flat_map` generates a function that returns a tuple of `B` and `RNG`.<br/>
  /// `flat_map`は`B`と`RNG`のタプルを返す関数を生成します。
  pub fn flat_map<A, B, F, GF, BF>(mut f: F, mut g: GF) -> BoxRand<B>
  where
    F: FnMut(RNG) -> (A, RNG) + 'static,
    BF: FnMut(RNG) -> (B, RNG) + 'static,
    GF: FnMut(A) -> BF + 'static, {
    Box::new(move |rng| {
      let (a, r1) = f(rng);
      (g(a))(r1)
    })
  }
}

#[cfg(test)]
mod tests {
  use crate::rng::{RandGen, RNG};
  use std::env;

  fn init() {
    env::set_var("RUST_LOG", "info");
    let _ = env_logger::builder().is_test(true).try_init();
  }

  #[test]
  fn next_i32() {
    init();
    let rng = RNG::new();
    let (v1, r1) = i32::rnd_gen(rng);
    log::info!("{:?}", v1);
    let (v2, _) = u32::rnd_gen(r1);
    log::info!("{:?}", v2);
  }
}