plutus_ledger_api/generators/correct/
primitive.rs1use num_bigint::{BigInt, BigUint, Sign};
5use num_traits::identities::Zero;
6use proptest::arbitrary::{any, StrategyFor};
7use proptest::char::CharStrategy;
8use proptest::collection::vec;
9use proptest::collection::{btree_map, btree_set};
10use proptest::option;
11use proptest::prelude::{prop_oneof, Just};
12use proptest::result::maybe_err;
13use proptest::strategy::Strategy;
14use std::collections::{BTreeMap, BTreeSet};
15
16pub fn arb_bool() -> StrategyFor<bool> {
18 any::<bool>()
19}
20
21fn arb_sign() -> impl Strategy<Value = Sign> {
24 prop_oneof![Just(Sign::Minus), Just(Sign::Plus)]
26}
27
28pub fn arb_integer() -> impl Strategy<Value = BigInt> {
30 (arb_sign(), arb_biguint(2)).prop_map(|(sign, nat)| {
32 BigInt::from_biguint(if nat.is_zero() { Sign::NoSign } else { sign }, nat)
34 })
35}
36
37pub fn arb_natural(n: usize) -> impl Strategy<Value = BigInt> {
39 arb_biguint(n).prop_map(|x| {
40 BigInt::from_biguint(
41 if x.is_zero() {
42 Sign::NoSign
43 } else {
44 Sign::Plus
45 },
46 x,
47 )
48 })
49}
50
51fn arb_biguint(n: usize) -> impl Strategy<Value = BigUint> {
54 vec(any::<u32>(), n).prop_map(BigUint::new)
55}
56
57pub fn arb_char<'a>() -> CharStrategy<'a> {
59 any::<char>()
60}
61
62pub fn arb_bytes() -> StrategyFor<Vec<u8>> {
64 any::<Vec<u8>>()
65}
66
67pub fn arb_text() -> StrategyFor<String> {
69 any::<String>()
70}
71
72pub fn arb_complicated(
74) -> impl Strategy<Value = BTreeMap<String, Result<BTreeSet<char>, Option<Result<Vec<u8>, bool>>>>>
75{
76 btree_map(
77 arb_text(),
78 maybe_err(
79 btree_set(arb_char(), 20),
80 option::of(maybe_err(arb_bytes(), arb_bool())),
81 ),
82 20,
83 )
84}