1use frame_election_provider_support::{SortedListProvider, VoteWeight};
31use honggfuzz::fuzz;
32use pallet_bags_list::mock::{AccountId, BagsList, ExtBuilder};
33
34const ID_RANGE: AccountId = 25_000;
35
36enum Action {
38 Insert,
39 Update,
40 Remove,
41}
42
43impl From<u32> for Action {
44 fn from(v: u32) -> Self {
45 let num_variants = Self::Remove as u32 + 1;
46 match v % num_variants {
47 _x if _x == Action::Insert as u32 => Action::Insert,
48 _x if _x == Action::Update as u32 => Action::Update,
49 _x if _x == Action::Remove as u32 => Action::Remove,
50 _ => unreachable!(),
51 }
52 }
53}
54
55fn main() {
56 ExtBuilder::default().build_and_execute(|| loop {
57 fuzz!(|data: (AccountId, VoteWeight, u32)| {
58 let (account_id_seed, vote_weight, action_seed) = data;
59
60 let id = account_id_seed % ID_RANGE;
61 let action = Action::from(action_seed);
62
63 match action {
64 Action::Insert => {
65 if BagsList::on_insert(id, vote_weight).is_err() {
66 BagsList::on_update(&id, vote_weight).unwrap();
68 }
69 assert!(BagsList::contains(&id));
70 },
71 Action::Update => {
72 let already_contains = BagsList::contains(&id);
73 if already_contains {
74 BagsList::on_update(&id, vote_weight).unwrap();
75 assert!(BagsList::contains(&id));
76 } else {
77 BagsList::on_update(&id, vote_weight).unwrap_err();
78 }
79 },
80 Action::Remove => {
81 let already_contains = BagsList::contains(&id);
82 if already_contains {
83 BagsList::on_remove(&id).unwrap();
84 } else {
85 BagsList::on_remove(&id).unwrap_err();
86 }
87 assert!(!BagsList::contains(&id));
88 },
89 }
90
91 assert!(BagsList::do_try_state().is_ok());
92 })
93 });
94}