Struct prop_check_rs::gen::Gen
source · pub struct Gen<A> { /* private fields */ }Expand description
Generator that generates values.
値を生成するジェネレータ。
Implementations§
source§impl<A: Clone + 'static> Gen<A>
impl<A: Clone + 'static> Gen<A>
sourcepub fn run(self, rng: RNG) -> (A, RNG)
pub fn run(self, rng: RNG) -> (A, RNG)
Evaluates expressions held by the Gen and generates values.
Genが保持する式を評価し値を生成します。
Examples found in repository?
src/prop.rs (line 105)
99 100 101 102 103 104 105 106 107 108 109 110
fn random_stream<A>(g: Gen<A>, rng: RNG) -> Unfold<RNG, Box<dyn FnMut(&mut RNG) -> Option<A>>>
where
A: Clone + 'static, {
itertools::unfold(
rng,
Box::new(move |rng| {
let (a, s) = g.clone().run(rng.clone());
*rng = s;
Some(a)
}),
)
}sourcepub fn new<B>(b: State<RNG, B>) -> Gen<B>
pub fn new<B>(b: State<RNG, B>) -> Gen<B>
Generate a Gen by specifying a State.
Stateを指定してGenを生成します。
Examples found in repository?
src/gen.rs (line 30)
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 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406
pub fn pure<B>(value: B) -> Gen<B>
where
B: Clone + 'static, {
Gen::<B>::new(State::value(value))
}
/// Generates a Gen that returns a value from a function.<br/>
/// 関数が返す値を返すGenを生成します。
pub fn pure_lazy<B, F>(f: F) -> Gen<B>
where
F: Fn() -> B + 'static,
B: Clone + 'static, {
Self::pure(()).map(move |_| f())
}
/// Generates a Gen that wraps the value of Gen into Option.<br/>
/// Genの値をOptionにラップするGenを生成します。
pub fn some<B>(gen: Gen<B>) -> Gen<Option<B>>
where
B: Clone + 'static, {
gen.map(Some)
}
/// Generates a Gen that returns Some or None based on the value of Gen.<br/>
/// Genの値を元にSomeもしくはNoneを返すGenを生成します。
pub fn option<B>(gen: Gen<B>) -> Gen<Option<B>>
where
B: Debug + Clone + 'static, {
Self::frequency([(1, Self::pure(None)), (9, Self::some(gen))])
}
/// Generates a Gen that returns Either based on two Gens.<br/>
/// 二つのGenを元にEitherを返すGenを生成します。
pub fn either<T, E>(gt: Gen<T>, ge: Gen<E>) -> Gen<Result<T, E>>
where
T: Choose + Clone + 'static,
E: Clone + 'static, {
Self::one_of([gt.map(Ok), ge.map(Err)])
}
/// Generates a Gen that produces values according to a specified ratio.<br/>
/// 指定の比率によって値を生成するGenを生成します。
pub fn frequency_values<B>(values: impl IntoIterator<Item = (u32, B)>) -> Gen<B>
where
B: Debug + Clone + 'static, {
Self::frequency(values.into_iter().map(|(n, value)| (n, Gens::pure(value))))
}
/// Generates a Gen that produces a value based on the specified ratio and Gen.<br/>
/// 指定された比率とGenに基づき値を生成するGenを生成します。
pub fn frequency<B>(values: impl IntoIterator<Item = (u32, Gen<B>)>) -> Gen<B>
where
B: Debug + Clone + 'static, {
let filtered = values.into_iter().filter(|kv| kv.0 > 0).collect::<Vec<_>>();
let (tree, total) = filtered
.into_iter()
.fold((BTreeMap::new(), 0), |(mut tree, total), (weight, value)| {
let t = total + weight;
tree.insert(t, value.clone());
(tree, t)
});
Self::choose_u32(1, total).flat_map(move |n| tree.range(n..).into_iter().next().unwrap().1.clone())
}
/// Generates a Gen whose elements are the values generated by the specified number of Gen.<br/>
/// 指定した個数のGenによって生成された値を要素とするGenを生成します。
pub fn list_of_n<B>(n: usize, gen: Gen<B>) -> Gen<Vec<B>>
where
B: Clone + 'static, {
let mut v: Vec<State<RNG, B>> = Vec::with_capacity(n);
v.resize_with(n, move || gen.clone().sample);
Gen {
sample: State::sequence(v),
}
}
/// Generates a Gen that returns a single value of a certain type.<br/>
/// ある型の値を一つ返すGenを生成します。
pub fn one<T: One>() -> Gen<T> {
One::one()
}
/// Generates a Gen that returns a single value of type i64.<br/>
/// i64型の値を一つ返すGenを生成します。
pub fn one_i64() -> Gen<i64> {
Gen {
sample: State::<RNG, i64>::new(move |rng: RNG| rng.next_i64()),
}
}
/// Generates a Gen that returns a single value of type u64.<br/>
/// u64型の値を一つ返すGenを生成します。
pub fn one_u64() -> Gen<u64> {
Gen {
sample: State::<RNG, u64>::new(move |rng: RNG| rng.next_u64()),
}
}
/// Generates a Gen that returns a single value of type i32.<br/>
/// i32型の値を一つ返すGenを生成します。
pub fn one_i32() -> Gen<i32> {
Gen {
sample: State::<RNG, i32>::new(move |rng: RNG| rng.next_i32()),
}
}
/// Generates a Gen that returns a single value of type u32.<br/>
/// u32型の値を一つ返すGenを生成します。
pub fn one_u32() -> Gen<u32> {
Gen {
sample: State::<RNG, u16>::new(move |rng: RNG| rng.next_u32()),
}
}
/// Generates a Gen that returns a single value of type i16.<br/>
/// i16型の値を一つ返すGenを生成します。
pub fn one_i16() -> Gen<i16> {
Gen {
sample: State::<RNG, i16>::new(move |rng: RNG| rng.next_i16()),
}
}
/// Generates a Gen that returns a single value of type u16.<br/>
/// u16型の値を一つ返すGenを生成します。
pub fn one_u16() -> Gen<u16> {
Gen {
sample: State::<RNG, u32>::new(move |rng: RNG| rng.next_u16()),
}
}
/// Generates a Gen that returns a single value of type i8.<br/>
/// i8型の値を一つ返すGenを生成します。
pub fn one_i8() -> Gen<i8> {
Gen {
sample: State::<RNG, i8>::new(move |rng: RNG| rng.next_i8()),
}
}
/// Generates a Gen that returns a single value of type u8.<br/>
/// u8型の値を一つ返すGenを生成します。
pub fn one_u8() -> Gen<u8> {
Gen {
sample: State::<RNG, u8>::new(move |rng: RNG| rng.next_u8()),
}
}
/// Generates a Gen that returns a single value of type char.<br/>
/// char型の値を一つ返すGenを生成します。
pub fn one_char() -> Gen<char> {
Self::one_u8().map(|v| v as char)
}
/// Generates a Gen that returns a single value of type bool.<br/>
/// bool型の値を一つ返すGenを生成します。
pub fn one_bool() -> Gen<bool> {
Gen {
sample: State::<RNG, bool>::new(|rng: RNG| rng.next_bool()),
}
}
/// Generates a Gen that returns a single value of type f64.<br/>
/// f64型の値を一つ返すGenを生成します。
pub fn one_f64() -> Gen<f64> {
Gen {
sample: State::<RNG, f64>::new(move |rng: RNG| rng.next_f64()),
}
}
/// Generates a Gen that returns a single value of type f32.<br/>
/// f32型の値を一つ返すGenを生成します。
pub fn one_f32() -> Gen<f32> {
Gen {
sample: State::<RNG, f32>::new(move |rng: RNG| rng.next_f32()),
}
}
/// Generates a Gen that returns a value selected at random from a specified set of Gen.<br/>
/// 指定されたGenの集合からランダムに一つ選択した値を返すGenを生成します。
pub fn one_of<T: Choose + Clone + 'static>(values: impl IntoIterator<Item = Gen<T>>) -> Gen<T> {
let mut vec = vec![];
vec.extend(values.into_iter());
Self::choose(0usize, vec.len() - 1).flat_map(move |idx| vec[idx as usize].clone())
}
/// Generates a Gen that returns one randomly selected value from the specified set of values.<br/>
/// 指定された値の集合からランダムに一つ選択した値を返すGenを生成します。
pub fn one_of_values<T: Choose + Clone + 'static>(values: impl IntoIterator<Item = T>) -> Gen<T> {
Self::one_of(values.into_iter().map(Gens::pure))
}
/// Generates a Gen that returns one randomly selected value from the specified maximum and minimum ranges of generic type.<br/>
/// 指定された最大・最小の範囲からランダムに一つ選択した値を返すGenを生成します。
pub fn choose<T: Choose>(min: T, max: T) -> Gen<T> {
Choose::choose(min, max)
}
/// Generates a Gen that returns one randomly selected value from a specified maximum and minimum range of type char.<br/>
/// char型の指定された最大・最小の範囲からランダムに一つ選択した値を返すGenを生成します。
pub fn choose_char(min: char, max: char) -> Gen<char> {
let chars = (min..=max).into_iter().map(|e| Self::pure(e)).collect::<Vec<_>>();
Self::one_of(chars)
}
/// Generates a Gen that returns one randomly selected value from a specified maximum and minimum range of type i64.<br/>
/// i64型の指定された最大・最小の範囲からランダムに一つ選択した値を返すGenを生成します。
pub fn choose_i64(min: i64, max: i64) -> Gen<i64> {
Gen {
sample: State::<RNG, i64>::new(move |rng: RNG| rng.next_i64()),
}
.map(move |n| min + n % (max - min + 1))
}
/// Generates a Gen that returns one randomly selected value from a specified maximum and minimum range of type u64.<br/>
/// u64型の指定された最大・最小の範囲からランダムに一つ選択した値を返すGenを生成します。
pub fn choose_u64(min: u64, max: u64) -> Gen<u64> {
Gen {
sample: State::<RNG, u64>::new(move |rng: RNG| rng.next_u64()),
}
.map(move |n| min + n % (max - min + 1))
}
/// Generates a Gen that returns one randomly selected value from a specified maximum and minimum range of type i32.<br/>
/// i32型の指定された最大・最小の範囲からランダムに一つ選択した値を返すGenを生成します。
pub fn choose_i32(min: i32, max: i32) -> Gen<i32> {
Gen {
sample: State::<RNG, i32>::new(move |rng: RNG| rng.next_i32()),
}
.map(move |n| min + n % (max - min + 1))
}
/// Generates a Gen that returns one randomly selected value from a specified maximum and minimum range of type u32.<br/>
/// u32型の指定された最大・最小の範囲からランダムに一つ選択した値を返すGenを生成します。
pub fn choose_u32(min: u32, max: u32) -> Gen<u32> {
Gen {
sample: State::<RNG, u32>::new(move |rng: RNG| rng.next_u32()),
}
.map(move |n| min + n % (max - min + 1))
}
/// Generates a Gen that returns one randomly selected value from a specified maximum and minimum range of type i16.<br/>
/// i16型の指定された最大・最小の範囲からランダムに一つ選択した値を返すGenを生成します。
pub fn choose_i16(min: i16, max: i16) -> Gen<i16> {
Gen {
sample: State::<RNG, i16>::new(move |rng: RNG| rng.next_i16()),
}
.map(move |n| min + n % (max - min + 1))
}
/// Generates a Gen that returns one randomly selected value from a specified maximum and minimum range of type u16.<br/>
/// u16型の指定された最大・最小の範囲からランダムに一つ選択した値を返すGenを生成します。
pub fn choose_u16(min: u16, max: u16) -> Gen<u16> {
Gen {
sample: State::<RNG, u16>::new(move |rng: RNG| rng.next_u16()),
}
.map(move |n| min + n % (max - min + 1))
}
/// Generates a Gen that returns one randomly selected value from a specified maximum and minimum range of type i8.<br/>
/// i8型の指定された最大・最小の範囲からランダムに一つ選択した値を返すGenを生成します。
pub fn choose_i8(min: i8, max: i8) -> Gen<i8> {
Gen {
sample: State::<RNG, i8>::new(move |rng: RNG| rng.next_i8()),
}
.map(move |n| min + n % (max - min + 1))
}
/// Generates a Gen that returns one randomly selected value from a specified maximum and minimum range of type u8.<br/>
/// u8型の指定された最大・最小の範囲からランダムに一つ選択した値を返すGenを生成します。
pub fn choose_u8(min: u8, max: u8) -> Gen<u8> {
Gen {
sample: State::<RNG, u8>::new(move |rng: RNG| rng.next_u8()),
}
.map(move |n| min + n % (max - min + 1))
}
/// Generates a Gen that returns one randomly selected value from a specified maximum and minimum range of type f64.<br/>
/// f64型の指定された最大・最小の範囲からランダムに一つ選択した値を返すGenを生成します。
pub fn choose_f64(min: f64, max: f64) -> Gen<f64> {
Gen {
sample: State::<RNG, f64>::new(move |rng: RNG| rng.next_f64()),
}
.map(move |d| min + d * (max - min))
}
/// Generates a Gen that returns one randomly selected value from a specified maximum and minimum range of type f32.<br/>
/// f32型の指定された最大・最小の範囲からランダムに一つ選択した値を返すGenを生成します。
pub fn choose_f32(min: f32, max: f32) -> Gen<f32> {
Gen {
sample: State::<RNG, f32>::new(move |rng: RNG| rng.next_f32()),
}
.map(move |d| min + d * (max - min))
}
/// Generates a Gen that returns one even number randomly selected from a specified range of values.<br/>
/// 指定された値の範囲から偶数をランダムに一つ選択した値を返すGenを生成します。
pub fn even<T: Choose + Num + Copy + 'static>(start: T, stop_exclusive: T) -> Gen<T> {
let two = T::one().add(T::one());
Self::choose(
start,
if stop_exclusive % two == T::zero() {
stop_exclusive - T::one()
} else {
stop_exclusive
},
)
.map(move |n| if n % two == T::zero() { n + T::one() } else { n })
}
/// Generates a Gen that returns one randomly selected odd number from a specified range of values.<br/>
/// 指定された値の範囲から奇数をランダムに一つ選択した値を返すGenを生成します。
pub fn odd<T: Choose + Num + Copy + 'static>(start: T, stop_exclusive: T) -> Gen<T> {
let two = T::one().add(T::one());
Self::choose(
start,
if stop_exclusive % two != T::zero() {
stop_exclusive - T::one()
} else {
stop_exclusive
},
)
.map(move |n| if n % two != T::zero() { n + T::one() } else { n })
}
}
/// Generator that generates values.<br/>
/// 値を生成するジェネレータ。
#[derive(Debug)]
pub struct Gen<A> {
sample: State<RNG, A>,
}
impl<A: Clone + 'static> Clone for Gen<A> {
fn clone(&self) -> Self {
Self {
sample: self.sample.clone(),
}
}
}
impl<A: Clone + 'static> Gen<A> {
/// Evaluates expressions held by the Gen and generates values.<br/>
/// Genが保持する式を評価し値を生成します。
pub fn run(self, rng: RNG) -> (A, RNG) {
self.sample.run(rng)
}
/// Generate a Gen by specifying a State.<br/>
/// Stateを指定してGenを生成します。
pub fn new<B>(b: State<RNG, B>) -> Gen<B> {
Gen { sample: b }
}
/// Applies a function to Gen.<br/>
/// Genに関数を適用します。
pub fn map<B, F>(self, f: F) -> Gen<B>
where
F: Fn(A) -> B + 'static,
B: Clone + 'static, {
Self::new(self.sample.map(f))
}
/// Applies a function that takes the result of two Gen's as arguments.<br/>
/// 二つのGenの結果を引数に取る関数を適用します。
pub fn and_then<B, C, F>(self, g: Gen<B>, f: F) -> Gen<C>
where
F: Fn(A, B) -> C + 'static,
A: Clone,
B: Clone + 'static,
C: Clone + 'static, {
Self::new(self.sample.and_then(g.sample).map(move |(a, b)| f(a, b)))
}
/// Applies a function to a Gen that takes the result of the Gen as an argument and returns the result.<br/>
/// Genに対してそのGenの結果を引数にとりGenを返す関数を適用しその結果を返します。
pub fn flat_map<B, F>(self, f: F) -> Gen<B>
where
F: Fn(A) -> Gen<B> + 'static,
B: Clone + 'static, {
Self::new(self.sample.flat_map(move |a| f(a).sample))
}sourcepub fn map<B, F>(self, f: F) -> Gen<B>where
F: Fn(A) -> B + 'static,
B: Clone + 'static,
pub fn map<B, F>(self, f: F) -> Gen<B>where
F: Fn(A) -> B + 'static,
B: Clone + 'static,
Applies a function to Gen.
Genに関数を適用します。
Examples found in repository?
src/gen.rs (line 39)
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
pub fn pure_lazy<B, F>(f: F) -> Gen<B>
where
F: Fn() -> B + 'static,
B: Clone + 'static, {
Self::pure(()).map(move |_| f())
}
/// Generates a Gen that wraps the value of Gen into Option.<br/>
/// Genの値をOptionにラップするGenを生成します。
pub fn some<B>(gen: Gen<B>) -> Gen<Option<B>>
where
B: Clone + 'static, {
gen.map(Some)
}
/// Generates a Gen that returns Some or None based on the value of Gen.<br/>
/// Genの値を元にSomeもしくはNoneを返すGenを生成します。
pub fn option<B>(gen: Gen<B>) -> Gen<Option<B>>
where
B: Debug + Clone + 'static, {
Self::frequency([(1, Self::pure(None)), (9, Self::some(gen))])
}
/// Generates a Gen that returns Either based on two Gens.<br/>
/// 二つのGenを元にEitherを返すGenを生成します。
pub fn either<T, E>(gt: Gen<T>, ge: Gen<E>) -> Gen<Result<T, E>>
where
T: Choose + Clone + 'static,
E: Clone + 'static, {
Self::one_of([gt.map(Ok), ge.map(Err)])
}
/// Generates a Gen that produces values according to a specified ratio.<br/>
/// 指定の比率によって値を生成するGenを生成します。
pub fn frequency_values<B>(values: impl IntoIterator<Item = (u32, B)>) -> Gen<B>
where
B: Debug + Clone + 'static, {
Self::frequency(values.into_iter().map(|(n, value)| (n, Gens::pure(value))))
}
/// Generates a Gen that produces a value based on the specified ratio and Gen.<br/>
/// 指定された比率とGenに基づき値を生成するGenを生成します。
pub fn frequency<B>(values: impl IntoIterator<Item = (u32, Gen<B>)>) -> Gen<B>
where
B: Debug + Clone + 'static, {
let filtered = values.into_iter().filter(|kv| kv.0 > 0).collect::<Vec<_>>();
let (tree, total) = filtered
.into_iter()
.fold((BTreeMap::new(), 0), |(mut tree, total), (weight, value)| {
let t = total + weight;
tree.insert(t, value.clone());
(tree, t)
});
Self::choose_u32(1, total).flat_map(move |n| tree.range(n..).into_iter().next().unwrap().1.clone())
}
/// Generates a Gen whose elements are the values generated by the specified number of Gen.<br/>
/// 指定した個数のGenによって生成された値を要素とするGenを生成します。
pub fn list_of_n<B>(n: usize, gen: Gen<B>) -> Gen<Vec<B>>
where
B: Clone + 'static, {
let mut v: Vec<State<RNG, B>> = Vec::with_capacity(n);
v.resize_with(n, move || gen.clone().sample);
Gen {
sample: State::sequence(v),
}
}
/// Generates a Gen that returns a single value of a certain type.<br/>
/// ある型の値を一つ返すGenを生成します。
pub fn one<T: One>() -> Gen<T> {
One::one()
}
/// Generates a Gen that returns a single value of type i64.<br/>
/// i64型の値を一つ返すGenを生成します。
pub fn one_i64() -> Gen<i64> {
Gen {
sample: State::<RNG, i64>::new(move |rng: RNG| rng.next_i64()),
}
}
/// Generates a Gen that returns a single value of type u64.<br/>
/// u64型の値を一つ返すGenを生成します。
pub fn one_u64() -> Gen<u64> {
Gen {
sample: State::<RNG, u64>::new(move |rng: RNG| rng.next_u64()),
}
}
/// Generates a Gen that returns a single value of type i32.<br/>
/// i32型の値を一つ返すGenを生成します。
pub fn one_i32() -> Gen<i32> {
Gen {
sample: State::<RNG, i32>::new(move |rng: RNG| rng.next_i32()),
}
}
/// Generates a Gen that returns a single value of type u32.<br/>
/// u32型の値を一つ返すGenを生成します。
pub fn one_u32() -> Gen<u32> {
Gen {
sample: State::<RNG, u16>::new(move |rng: RNG| rng.next_u32()),
}
}
/// Generates a Gen that returns a single value of type i16.<br/>
/// i16型の値を一つ返すGenを生成します。
pub fn one_i16() -> Gen<i16> {
Gen {
sample: State::<RNG, i16>::new(move |rng: RNG| rng.next_i16()),
}
}
/// Generates a Gen that returns a single value of type u16.<br/>
/// u16型の値を一つ返すGenを生成します。
pub fn one_u16() -> Gen<u16> {
Gen {
sample: State::<RNG, u32>::new(move |rng: RNG| rng.next_u16()),
}
}
/// Generates a Gen that returns a single value of type i8.<br/>
/// i8型の値を一つ返すGenを生成します。
pub fn one_i8() -> Gen<i8> {
Gen {
sample: State::<RNG, i8>::new(move |rng: RNG| rng.next_i8()),
}
}
/// Generates a Gen that returns a single value of type u8.<br/>
/// u8型の値を一つ返すGenを生成します。
pub fn one_u8() -> Gen<u8> {
Gen {
sample: State::<RNG, u8>::new(move |rng: RNG| rng.next_u8()),
}
}
/// Generates a Gen that returns a single value of type char.<br/>
/// char型の値を一つ返すGenを生成します。
pub fn one_char() -> Gen<char> {
Self::one_u8().map(|v| v as char)
}
/// Generates a Gen that returns a single value of type bool.<br/>
/// bool型の値を一つ返すGenを生成します。
pub fn one_bool() -> Gen<bool> {
Gen {
sample: State::<RNG, bool>::new(|rng: RNG| rng.next_bool()),
}
}
/// Generates a Gen that returns a single value of type f64.<br/>
/// f64型の値を一つ返すGenを生成します。
pub fn one_f64() -> Gen<f64> {
Gen {
sample: State::<RNG, f64>::new(move |rng: RNG| rng.next_f64()),
}
}
/// Generates a Gen that returns a single value of type f32.<br/>
/// f32型の値を一つ返すGenを生成します。
pub fn one_f32() -> Gen<f32> {
Gen {
sample: State::<RNG, f32>::new(move |rng: RNG| rng.next_f32()),
}
}
/// Generates a Gen that returns a value selected at random from a specified set of Gen.<br/>
/// 指定されたGenの集合からランダムに一つ選択した値を返すGenを生成します。
pub fn one_of<T: Choose + Clone + 'static>(values: impl IntoIterator<Item = Gen<T>>) -> Gen<T> {
let mut vec = vec![];
vec.extend(values.into_iter());
Self::choose(0usize, vec.len() - 1).flat_map(move |idx| vec[idx as usize].clone())
}
/// Generates a Gen that returns one randomly selected value from the specified set of values.<br/>
/// 指定された値の集合からランダムに一つ選択した値を返すGenを生成します。
pub fn one_of_values<T: Choose + Clone + 'static>(values: impl IntoIterator<Item = T>) -> Gen<T> {
Self::one_of(values.into_iter().map(Gens::pure))
}
/// Generates a Gen that returns one randomly selected value from the specified maximum and minimum ranges of generic type.<br/>
/// 指定された最大・最小の範囲からランダムに一つ選択した値を返すGenを生成します。
pub fn choose<T: Choose>(min: T, max: T) -> Gen<T> {
Choose::choose(min, max)
}
/// Generates a Gen that returns one randomly selected value from a specified maximum and minimum range of type char.<br/>
/// char型の指定された最大・最小の範囲からランダムに一つ選択した値を返すGenを生成します。
pub fn choose_char(min: char, max: char) -> Gen<char> {
let chars = (min..=max).into_iter().map(|e| Self::pure(e)).collect::<Vec<_>>();
Self::one_of(chars)
}
/// Generates a Gen that returns one randomly selected value from a specified maximum and minimum range of type i64.<br/>
/// i64型の指定された最大・最小の範囲からランダムに一つ選択した値を返すGenを生成します。
pub fn choose_i64(min: i64, max: i64) -> Gen<i64> {
Gen {
sample: State::<RNG, i64>::new(move |rng: RNG| rng.next_i64()),
}
.map(move |n| min + n % (max - min + 1))
}
/// Generates a Gen that returns one randomly selected value from a specified maximum and minimum range of type u64.<br/>
/// u64型の指定された最大・最小の範囲からランダムに一つ選択した値を返すGenを生成します。
pub fn choose_u64(min: u64, max: u64) -> Gen<u64> {
Gen {
sample: State::<RNG, u64>::new(move |rng: RNG| rng.next_u64()),
}
.map(move |n| min + n % (max - min + 1))
}
/// Generates a Gen that returns one randomly selected value from a specified maximum and minimum range of type i32.<br/>
/// i32型の指定された最大・最小の範囲からランダムに一つ選択した値を返すGenを生成します。
pub fn choose_i32(min: i32, max: i32) -> Gen<i32> {
Gen {
sample: State::<RNG, i32>::new(move |rng: RNG| rng.next_i32()),
}
.map(move |n| min + n % (max - min + 1))
}
/// Generates a Gen that returns one randomly selected value from a specified maximum and minimum range of type u32.<br/>
/// u32型の指定された最大・最小の範囲からランダムに一つ選択した値を返すGenを生成します。
pub fn choose_u32(min: u32, max: u32) -> Gen<u32> {
Gen {
sample: State::<RNG, u32>::new(move |rng: RNG| rng.next_u32()),
}
.map(move |n| min + n % (max - min + 1))
}
/// Generates a Gen that returns one randomly selected value from a specified maximum and minimum range of type i16.<br/>
/// i16型の指定された最大・最小の範囲からランダムに一つ選択した値を返すGenを生成します。
pub fn choose_i16(min: i16, max: i16) -> Gen<i16> {
Gen {
sample: State::<RNG, i16>::new(move |rng: RNG| rng.next_i16()),
}
.map(move |n| min + n % (max - min + 1))
}
/// Generates a Gen that returns one randomly selected value from a specified maximum and minimum range of type u16.<br/>
/// u16型の指定された最大・最小の範囲からランダムに一つ選択した値を返すGenを生成します。
pub fn choose_u16(min: u16, max: u16) -> Gen<u16> {
Gen {
sample: State::<RNG, u16>::new(move |rng: RNG| rng.next_u16()),
}
.map(move |n| min + n % (max - min + 1))
}
/// Generates a Gen that returns one randomly selected value from a specified maximum and minimum range of type i8.<br/>
/// i8型の指定された最大・最小の範囲からランダムに一つ選択した値を返すGenを生成します。
pub fn choose_i8(min: i8, max: i8) -> Gen<i8> {
Gen {
sample: State::<RNG, i8>::new(move |rng: RNG| rng.next_i8()),
}
.map(move |n| min + n % (max - min + 1))
}
/// Generates a Gen that returns one randomly selected value from a specified maximum and minimum range of type u8.<br/>
/// u8型の指定された最大・最小の範囲からランダムに一つ選択した値を返すGenを生成します。
pub fn choose_u8(min: u8, max: u8) -> Gen<u8> {
Gen {
sample: State::<RNG, u8>::new(move |rng: RNG| rng.next_u8()),
}
.map(move |n| min + n % (max - min + 1))
}
/// Generates a Gen that returns one randomly selected value from a specified maximum and minimum range of type f64.<br/>
/// f64型の指定された最大・最小の範囲からランダムに一つ選択した値を返すGenを生成します。
pub fn choose_f64(min: f64, max: f64) -> Gen<f64> {
Gen {
sample: State::<RNG, f64>::new(move |rng: RNG| rng.next_f64()),
}
.map(move |d| min + d * (max - min))
}
/// Generates a Gen that returns one randomly selected value from a specified maximum and minimum range of type f32.<br/>
/// f32型の指定された最大・最小の範囲からランダムに一つ選択した値を返すGenを生成します。
pub fn choose_f32(min: f32, max: f32) -> Gen<f32> {
Gen {
sample: State::<RNG, f32>::new(move |rng: RNG| rng.next_f32()),
}
.map(move |d| min + d * (max - min))
}
/// Generates a Gen that returns one even number randomly selected from a specified range of values.<br/>
/// 指定された値の範囲から偶数をランダムに一つ選択した値を返すGenを生成します。
pub fn even<T: Choose + Num + Copy + 'static>(start: T, stop_exclusive: T) -> Gen<T> {
let two = T::one().add(T::one());
Self::choose(
start,
if stop_exclusive % two == T::zero() {
stop_exclusive - T::one()
} else {
stop_exclusive
},
)
.map(move |n| if n % two == T::zero() { n + T::one() } else { n })
}
/// Generates a Gen that returns one randomly selected odd number from a specified range of values.<br/>
/// 指定された値の範囲から奇数をランダムに一つ選択した値を返すGenを生成します。
pub fn odd<T: Choose + Num + Copy + 'static>(start: T, stop_exclusive: T) -> Gen<T> {
let two = T::one().add(T::one());
Self::choose(
start,
if stop_exclusive % two != T::zero() {
stop_exclusive - T::one()
} else {
stop_exclusive
},
)
.map(move |n| if n % two != T::zero() { n + T::one() } else { n })
}More examples
src/gen/choose.rs (line 15)
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
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)
}sourcepub fn and_then<B, C, F>(self, g: Gen<B>, f: F) -> Gen<C>where
F: Fn(A, B) -> C + 'static,
A: Clone,
B: Clone + 'static,
C: Clone + 'static,
pub fn and_then<B, C, F>(self, g: Gen<B>, f: F) -> Gen<C>where
F: Fn(A, B) -> C + 'static,
A: Clone,
B: Clone + 'static,
C: Clone + 'static,
Applies a function that takes the result of two Gen’s as arguments.
二つのGenの結果を引数に取る関数を適用します。
sourcepub fn flat_map<B, F>(self, f: F) -> Gen<B>where
F: Fn(A) -> Gen<B> + 'static,
B: Clone + 'static,
pub fn flat_map<B, F>(self, f: F) -> Gen<B>where
F: Fn(A) -> Gen<B> + 'static,
B: Clone + 'static,
Applies a function to a Gen that takes the result of the Gen as an argument and returns the result.
Genに対してそのGenの結果を引数にとりGenを返す関数を適用しその結果を返します。
Examples found in repository?
src/gen.rs (line 88)
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
pub fn frequency<B>(values: impl IntoIterator<Item = (u32, Gen<B>)>) -> Gen<B>
where
B: Debug + Clone + 'static, {
let filtered = values.into_iter().filter(|kv| kv.0 > 0).collect::<Vec<_>>();
let (tree, total) = filtered
.into_iter()
.fold((BTreeMap::new(), 0), |(mut tree, total), (weight, value)| {
let t = total + weight;
tree.insert(t, value.clone());
(tree, t)
});
Self::choose_u32(1, total).flat_map(move |n| tree.range(n..).into_iter().next().unwrap().1.clone())
}
/// Generates a Gen whose elements are the values generated by the specified number of Gen.<br/>
/// 指定した個数のGenによって生成された値を要素とするGenを生成します。
pub fn list_of_n<B>(n: usize, gen: Gen<B>) -> Gen<Vec<B>>
where
B: Clone + 'static, {
let mut v: Vec<State<RNG, B>> = Vec::with_capacity(n);
v.resize_with(n, move || gen.clone().sample);
Gen {
sample: State::sequence(v),
}
}
/// Generates a Gen that returns a single value of a certain type.<br/>
/// ある型の値を一つ返すGenを生成します。
pub fn one<T: One>() -> Gen<T> {
One::one()
}
/// Generates a Gen that returns a single value of type i64.<br/>
/// i64型の値を一つ返すGenを生成します。
pub fn one_i64() -> Gen<i64> {
Gen {
sample: State::<RNG, i64>::new(move |rng: RNG| rng.next_i64()),
}
}
/// Generates a Gen that returns a single value of type u64.<br/>
/// u64型の値を一つ返すGenを生成します。
pub fn one_u64() -> Gen<u64> {
Gen {
sample: State::<RNG, u64>::new(move |rng: RNG| rng.next_u64()),
}
}
/// Generates a Gen that returns a single value of type i32.<br/>
/// i32型の値を一つ返すGenを生成します。
pub fn one_i32() -> Gen<i32> {
Gen {
sample: State::<RNG, i32>::new(move |rng: RNG| rng.next_i32()),
}
}
/// Generates a Gen that returns a single value of type u32.<br/>
/// u32型の値を一つ返すGenを生成します。
pub fn one_u32() -> Gen<u32> {
Gen {
sample: State::<RNG, u16>::new(move |rng: RNG| rng.next_u32()),
}
}
/// Generates a Gen that returns a single value of type i16.<br/>
/// i16型の値を一つ返すGenを生成します。
pub fn one_i16() -> Gen<i16> {
Gen {
sample: State::<RNG, i16>::new(move |rng: RNG| rng.next_i16()),
}
}
/// Generates a Gen that returns a single value of type u16.<br/>
/// u16型の値を一つ返すGenを生成します。
pub fn one_u16() -> Gen<u16> {
Gen {
sample: State::<RNG, u32>::new(move |rng: RNG| rng.next_u16()),
}
}
/// Generates a Gen that returns a single value of type i8.<br/>
/// i8型の値を一つ返すGenを生成します。
pub fn one_i8() -> Gen<i8> {
Gen {
sample: State::<RNG, i8>::new(move |rng: RNG| rng.next_i8()),
}
}
/// Generates a Gen that returns a single value of type u8.<br/>
/// u8型の値を一つ返すGenを生成します。
pub fn one_u8() -> Gen<u8> {
Gen {
sample: State::<RNG, u8>::new(move |rng: RNG| rng.next_u8()),
}
}
/// Generates a Gen that returns a single value of type char.<br/>
/// char型の値を一つ返すGenを生成します。
pub fn one_char() -> Gen<char> {
Self::one_u8().map(|v| v as char)
}
/// Generates a Gen that returns a single value of type bool.<br/>
/// bool型の値を一つ返すGenを生成します。
pub fn one_bool() -> Gen<bool> {
Gen {
sample: State::<RNG, bool>::new(|rng: RNG| rng.next_bool()),
}
}
/// Generates a Gen that returns a single value of type f64.<br/>
/// f64型の値を一つ返すGenを生成します。
pub fn one_f64() -> Gen<f64> {
Gen {
sample: State::<RNG, f64>::new(move |rng: RNG| rng.next_f64()),
}
}
/// Generates a Gen that returns a single value of type f32.<br/>
/// f32型の値を一つ返すGenを生成します。
pub fn one_f32() -> Gen<f32> {
Gen {
sample: State::<RNG, f32>::new(move |rng: RNG| rng.next_f32()),
}
}
/// Generates a Gen that returns a value selected at random from a specified set of Gen.<br/>
/// 指定されたGenの集合からランダムに一つ選択した値を返すGenを生成します。
pub fn one_of<T: Choose + Clone + 'static>(values: impl IntoIterator<Item = Gen<T>>) -> Gen<T> {
let mut vec = vec![];
vec.extend(values.into_iter());
Self::choose(0usize, vec.len() - 1).flat_map(move |idx| vec[idx as usize].clone())
}