Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

input generator #83

Merged
merged 1 commit into from
Aug 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion examples/examples/subxt_schedule.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// waits until event X is triggered then submits a subxt transaction


use libuptest::subxt_helper::tx_schedule;
use libuptest::types::event_summary;
use subxt::{OnlineClient, PolkadotConfig};
Expand Down
25 changes: 25 additions & 0 deletions examples/examples/test_input_gen.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use libuptest::error::Error;
use libuptest::test_helper::InputHelper;

fn main() -> Result<(), Error> {
let rand_u128: u128 = InputHelper::get_u128();
println!("u128: {}", rand_u128);
let rand_u64: u64 = InputHelper::get_u64();
println!("u64: {}", rand_u64);
let rand_u32: u32 = InputHelper::get_u32();
println!("u32: {}", rand_u32);
let rand_u8: u8 = InputHelper::get_u8();
println!("u8: {}", rand_u8);

let rand_f64: f64 = InputHelper::get_f64();
println!("f64: {}", rand_f64);
let rand_f32: f32 = InputHelper::get_f32();
println!("f32: {}", rand_f32);

let rand_boolean: bool = InputHelper::get_boolean();
println!("boolean: {}", rand_boolean);

let rand_address = InputHelper::get_address();
println!("Address: {}", rand_address.to_string());
Ok(())
}
2 changes: 2 additions & 0 deletions libuptest/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ pub mod pallet_storage_parse;
#[cfg(feature = "subxthelper")]
pub mod subxt_helper;

pub mod test_helper;

/*
pub struct PalletTest {
pallet_name: String,
Expand Down
64 changes: 64 additions & 0 deletions libuptest/src/test_helper.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/// automatically generate
use rand::{Rng, SeedableRng}; // https://rust-random.github.io/book/guide-rngs.html

#[cfg(feature = "subxthelper")]
use subxt_signer::sr25519::Keypair;
use subxt::utils::AccountId32;


/// auto generate inputs for tests
pub struct InputHelper {}

impl InputHelper {
pub fn get_boolean() -> bool {
rand::random()
}
/// get a random i32
pub fn get_i32() -> i32 {
let mut rng = rand::thread_rng();
rng.gen::<i32>()
}
/// get a random u8
pub fn get_u8() -> u8 {
let mut rng = rand::thread_rng();
rng.gen::<u8>()
}
/// get a random u32
pub fn get_u32() -> u32 {
let mut rng = rand::thread_rng();
rng.gen::<u32>()
}
/// get a random u64
pub fn get_u64() -> u64 {
let mut rng = rand::thread_rng();
rng.gen::<u64>()
}
/// get a random u128
pub fn get_u128() -> u128 {
let mut rng = rand::thread_rng();
rng.gen::<u128>()
}

/// get a random f32
pub fn get_f32() -> f32 {
let mut rng = rand::thread_rng();
let x: f32 = rng.gen();
x
}

/// get a random f64
pub fn get_f64() -> f64 {
let mut rng = rand::thread_rng();
let x: f64 = rng.gen();
x
}
/// generate a random accountid32 with subxt_signer
#[cfg(feature = "subxthelper")]
pub fn get_address() -> AccountId32 {
let mut rng = rand::thread_rng();
let random_bytes: [u8; 32] = rng.gen();
let seckey = Keypair::from_seed(random_bytes).expect("Could not generate key");
let key2: AccountId32 = seckey.public_key().to_account_id();
key2
}
}