summaryrefslogtreecommitdiff
path: root/gen_random/src/lib.rs
blob: b75d8cce464d0d66a8eb1a41a39e856fe663cca9 (plain)
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
extern crate rand;
use rand::Rng;
use std::rc::Rc;
use std::sync::Arc;
use std::cell::{Cell, RefCell};

/// parameters passed to [GenRandom] implementation
pub trait GenRandomParams: Sized + Default + Copy {
	/// If `params` is used to generate `object`, then `params.inc_depth()` will
	/// be used to generate `object`'s members.
	fn inc_depth(self) -> Self;
}

impl GenRandomParams for () {
	fn inc_depth(self) -> Self {
		()
	}
}


impl GenRandomParams for i64 {
	fn inc_depth(self) -> Self {
		self - 1
	}
}

/// Generate random structs and enums!
///
/// You don't need to implement this trait yourself — instead, use the `derive` macro:
/// ```
/// use gen_random_proc_macro::GenRandom;
/// use gen_random::{GenRandom, GenRandomParams};
///
/// #[derive(GenRandom, Debug)]
/// enum MyType {
///     // this variant will be chosen 7 / 10.5 = 2/3 of the time
///     #[prob(7)]
///     Variant1(f64),
///     // this variant will be chosen 3.5 / 10.5 = 1/3 of the time
///     #[prob(3.5)]
///     Variant2 {
///         // bias & scale attributes can be used for fields of type f32/f64.
///         // this makes `a` range from 2 to 6 (as opposed to the default 0 to 1).
///         #[bias(2.0)]
///         #[scale(4.0)]
///         a: f64,
///         // we can even include a randomly-generated MyType inside this MyType!
///         // be careful when doing this or else you might try to generate an infinite struct!
///         b: Box<MyType>
///     }
/// }
///
/// fn main() {
///     let my_value = MyType::gen_thread_random();
///     println!("{my_value:?}");
/// }
/// ```

pub trait GenRandom<Params: GenRandomParams>: Sized {
	/// Generate a random value with parameters.
	fn gen_random_params(rng: &mut impl Rng, params: Params) -> Self;
	
	/// Generate a random value
	fn gen_random(rng: &mut impl Rng) -> Self {
		Self::gen_random_params(rng, Params::default())
	}
	
	/// Generate a random instance of this struct using `rand::thread_rng()` with parameters.
	fn gen_thread_random_params(params: Params) -> Self {
		let mut thread_rng = rand::thread_rng();
		Self::gen_random_params(&mut thread_rng, params)
	}
	
	/// Generate a random instance of this struct using `rand::thread_rng()`.
	fn gen_thread_random() -> Self {
		Self::gen_thread_random_params(Params::default())
	}
}

/// generate a [Vec] of `len` random items.
pub fn gen_random_vec<P: GenRandomParams, T: GenRandom<P>>(rng: &mut impl Rng, len: usize) -> Vec<T> {
	(0..len).map(|_| T::gen_random(rng)).collect()
}

/// generate a [Vec] of `len` random items using `rand::thread_rng()`.
pub fn gen_thread_random_vec<P: GenRandomParams, T: GenRandom<P>>(len: usize) -> Vec<T> {
	gen_random_vec(&mut rand::thread_rng(), len)
}

impl<Params: GenRandomParams> GenRandom<Params> for f32 {
	fn gen_random_params(rng: &mut impl Rng, _params: Params) -> Self {
		rng.gen_range(0.0..1.0)
	}
}

impl<Params: GenRandomParams> GenRandom<Params> for f64 {
	fn gen_random_params(rng: &mut impl Rng, _params: Params) -> Self {
		rng.gen_range(0.0..1.0)
	}
}

impl<Params: GenRandomParams, T: GenRandom<Params>> GenRandom<Params> for Box<T> {
	fn gen_random_params(rng: &mut impl Rng, params: Params) -> Self {
		Box::new(T::gen_random_params(rng, params))
	}
}

impl<Params: GenRandomParams, T: GenRandom<Params>> GenRandom<Params> for [T; 1] {
	fn gen_random_params(rng: &mut impl Rng, params: Params) -> Self {
		[T::gen_random_params(rng, params)]
	}
}

impl<Params: GenRandomParams, T: GenRandom<Params>> GenRandom<Params> for [T; 2] {
	fn gen_random_params(rng: &mut impl Rng, params: Params) -> Self {
		[T::gen_random_params(rng, params), T::gen_random_params(rng, params)]
	}
}

impl<Params: GenRandomParams, T: GenRandom<Params>> GenRandom<Params> for [T; 3] {
	fn gen_random_params(rng: &mut impl Rng, params: Params) -> Self {
		[T::gen_random_params(rng, params), T::gen_random_params(rng, params), T::gen_random_params(rng, params)]
	}
}

impl<Params: GenRandomParams, T: GenRandom<Params>> GenRandom<Params> for [T; 4] {
	fn gen_random_params(rng: &mut impl Rng, params: Params) -> Self {
		[T::gen_random_params(rng, params), T::gen_random_params(rng, params), T::gen_random_params(rng, params), T::gen_random_params(rng, params)]
	}
}

impl<Params: GenRandomParams, T: GenRandom<Params>> GenRandom<Params> for (T, T) {
	fn gen_random_params(rng: &mut impl Rng, params: Params) -> Self {
		(T::gen_random_params(rng, params), T::gen_random_params(rng, params))
	}
}

impl<Params: GenRandomParams, T: GenRandom<Params>> GenRandom<Params> for (T, T, T) {
	fn gen_random_params(rng: &mut impl Rng, params: Params) -> Self {
		(T::gen_random_params(rng, params), T::gen_random_params(rng, params), T::gen_random_params(rng, params))
	}
}

impl<Params: GenRandomParams, T: GenRandom<Params>> GenRandom<Params> for (T, T, T, T) {
	fn gen_random_params(rng: &mut impl Rng, params: Params) -> Self {
		(T::gen_random_params(rng, params), T::gen_random_params(rng, params), T::gen_random_params(rng, params), T::gen_random_params(rng, params))
	}
}

impl<Params: GenRandomParams, T: GenRandom<Params>> GenRandom<Params> for Rc<T> {
	fn gen_random_params(rng: &mut impl Rng, params: Params) -> Self {
		Self::new(T::gen_random_params(rng, params))
	}
}

impl<Params: GenRandomParams, T: GenRandom<Params>> GenRandom<Params> for Arc<T> {
	fn gen_random_params(rng: &mut impl Rng, params: Params) -> Self {
		Self::new(T::gen_random_params(rng, params))
	}
}

impl<Params: GenRandomParams, T: GenRandom<Params>> GenRandom<Params> for Cell<T> {
	fn gen_random_params(rng: &mut impl Rng, params: Params) -> Self {
		Self::new(T::gen_random_params(rng, params))
	}
}

impl<Params: GenRandomParams, T: GenRandom<Params>> GenRandom<Params> for RefCell<T> {
	fn gen_random_params(rng: &mut impl Rng, params: Params) -> Self {
		Self::new(T::gen_random_params(rng, params))
	}
}

impl<Params: GenRandomParams, T: GenRandom<Params>> GenRandom<Params> for Option<T> {
	fn gen_random_params(rng: &mut impl Rng, params: Params) -> Self {
		if rng.gen_range(0..2) == 0 {
			None
		} else {
			Some(T::gen_random_params(rng, params))
		}
	}
}


#[cfg(test)]
mod tests {
	extern crate gen_random_proc_macro;
	extern crate rand;
	use super::{gen_thread_random_vec, GenRandom, GenRandomParams};
	use gen_random_proc_macro::GenRandom;

	#[derive(GenRandom, Debug)]
	enum Test1 {
		#[prob(0.2)]
		A(f32),
		#[prob(0.8)]
		B(Option<f32>),
	}

	#[derive(GenRandom, Debug)]
	#[allow(dead_code)]
	enum Test2 {
		#[prob(0.1)]
		Variant1,
		#[prob(0.7)]
		Variant2 { x: f32, y: f64, z: Test1 },
		#[prob(0.2)]
		Variant3(f32, Box<Test2>),
	}

	#[derive(GenRandom, Debug)]
	enum LinkedList {
		#[prob(10)]
		Empty,
		#[prob(90)]
		Cons(f32, Box<LinkedList>),
	}

	#[derive(GenRandom, Debug)]
	#[params(i64)]
	enum BinaryTree {
		#[prob(1)]
		Empty,
		#[prob(99)]
		#[only_if(params >= 0)]
		Node(f64, Box<BinaryTree>, Box<BinaryTree>)
	}

	#[derive(GenRandom, Debug)]
	struct ScaleBias {
		#[bias(1.0)]
		#[scale(10.0)]
		a: f32,
		#[bias(2.0)]
		#[scale(0.0)]
		b: f32,
	}

	#[test]
	fn basic() {
		let tests1: Vec<Test1> = gen_thread_random_vec(10);
		println!("{tests1:?}");
	}

	#[test]
	fn many_types_of_variants() {
		let tests2: Vec<Test2> = gen_thread_random_vec(10);
		println!("{tests2:?}");
	}

	#[test]
	fn linked_list() {
		let ll = LinkedList::gen_thread_random();
		println!("{ll:?}");
	}

	#[test]
	fn scale_bias() {
		let sb: Vec<ScaleBias> = gen_thread_random_vec(10);
		println!("{sb:?}");
		for x in sb.iter() {
			if x.a < 1.0 || x.a > 11.0 {
				panic!("a field should be between 1 and 11; got {}", x.a);
			}
			if x.b != 2.0 {
				panic!("b field should be exactly 2; got {}", x.b);
			}
		}
	}
	
	#[test]
	fn binary_tree_params() {
		let bintree = BinaryTree::gen_thread_random_params(5);
		println!("{bintree:?}");
	}
}