// Copyright 2020-2021 IOTA Stiftung // SPDX-License-Identifier: Apache-2.0 // Workaround for cargo/rustc unused warning bug #![allow(dead_code)] use std::ops::Range; use bee_ternary::*; use rand::{thread_rng, Rng}; /// Generates a random balanced i8 trit. pub fn gen_trit_balanced() -> i8 { (thread_rng().gen::() % 3) as i8 - 1 } /// Generates a random unbalanced i8 trit. pub fn gen_trit_unbalanced() -> i8 { (thread_rng().gen::() % 3) as i8 } /// Generates a buffer of balanced trits. pub fn gen_buf_balanced(len: Range) -> (TritBuf, Vec) { let len = thread_rng().gen_range(len.start..len.end); let trits = (0..len).map(|_| gen_trit_balanced()).collect::>(); ( trits .iter() .map(|t| ::Trit::try_from(*t).ok().unwrap()) .collect(), trits, ) } /// Generates a buffer of unbalanced trits. pub fn gen_buf_unbalanced(len: Range) -> (TritBuf, Vec) { let len = thread_rng().gen_range(len.start..len.end); let trits = (0..len).map(|_| gen_trit_unbalanced()).collect::>(); ( trits .iter() .map(|t| ::Trit::try_from(*t).ok().unwrap()) .collect(), trits, ) } /// Not exactly fuzzing, just doing something a lot pub fn fuzz(n: usize, mut f: impl FnMut()) { (0..n).for_each(|_| f()); }