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
/*
* Copyright (C) 2025 Pierre Fouilloux, Hibiscus Collective
*
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License along with this program.
* If not, see https://www.gnu.org/licenses/.
*/
use Cycle;
use crateRandom;
/// Macro for asserting that a value is approximately equal to an expected value.
///
/// This macro is useful for testing random number generators, where exact equality
/// cannot be guaranteed but approximate equality within a tolerance is sufficient.
///
/// # Arguments
///
/// * `$expect` - The expected value
/// * `$actual` - The actual value
/// * `$tolerance` - The maximum allowed difference between the expected and actual values
///
/// # Examples
///
/// ```
/// # #[macro_use] extern crate dfrng;
/// # fn main() {
/// // Assert that 100 is approximately equal to 102 with a tolerance of 5
/// assert_approx!(100, 102, 5);
///
/// // This would fail because 100 is not approximately equal to 110 with a tolerance of 5
/// // assert_approx!(100, 110, 5);
/// # }
/// ```
/// Implementation of the `Random` trait for `Cycle<I>` iterators.
///
/// This implementation is useful for testing, as it allows for predictable
/// "random" values by cycling through a fixed sequence.
///
/// # Type Parameters
///
/// * `T` - The type of values generated
/// * `I` - The type of iterator being cycled
///
/// # Examples
///
/// ```
/// use dfrng::rng::Random;
/// use std::iter::Cycle;
///
/// #[cfg(test)]
/// fn test_with_cycle() {
/// // Create a cycle of values 1, 2, 3
/// let mut cycle = [1, 2, 3].iter().cycle();
///
/// // Use the cycle as a random number generator
/// let value = Random::next(&mut cycle);
/// assert_eq!(*value, 1);
///
/// let values = Random::take(&mut cycle, 5);
/// assert_eq!(values, vec![&2, &3, &1, &2, &3]);
/// }
/// ```
/// A random number generator that always returns the same value.
///
/// This struct is useful for testing, as it allows for completely predictable
/// "random" values by always returning the same value.
///
/// # Type Parameters
///
/// * `T` - The type of values generated
///
/// # Examples
///
/// ```
/// #![cfg(test)]
/// use dfrng::rng::{Random, test::Repeat};
///
/// fn test_with_repeat() {
/// // Create a random number generator that always returns 42
/// let mut repeat = Repeat(42);
///
/// // Generate a "random" number
/// let value = repeat.next();
/// assert_eq!(value, 42);
///
/// // Generate multiple "random" numbers
/// let values = repeat.take(3);
/// assert_eq!(values, vec![42, 42, 42]);
/// }
/// ```
;