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

Test cleanup #242

Merged
merged 2 commits into from
May 23, 2018
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
19 changes: 10 additions & 9 deletions src/bank.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ pub const MAX_ENTRY_IDS: usize = 1024 * 4;

#[derive(Debug, PartialEq, Eq)]
pub enum BankError {
AccountNotFound,
InsufficientFunds,
InvalidTransferSignature,
AccountNotFound(PublicKey),
InsufficientFunds(PublicKey),
InvalidTransferSignature(Signature),
}

pub type Result<T> = result::Result<T, BankError>;
Expand Down Expand Up @@ -167,11 +167,11 @@ impl Bank {
let option = bals.get(&tr.from);

if option.is_none() {
return Err(BankError::AccountNotFound);
return Err(BankError::AccountNotFound(tr.from));
}

if !self.reserve_signature_with_last_id(&tr.sig, &tr.last_id) {
return Err(BankError::InvalidTransferSignature);
return Err(BankError::InvalidTransferSignature(tr.sig));
}

loop {
Expand All @@ -180,7 +180,7 @@ impl Bank {

if current < tr.contract.tokens {
self.forget_signature_with_last_id(&tr.sig, &tr.last_id);
return Err(BankError::InsufficientFunds);
return Err(BankError::InsufficientFunds(tr.from));
}

let result = bal.compare_exchange(
Expand Down Expand Up @@ -427,9 +427,10 @@ mod tests {
fn test_account_not_found() {
let mint = Mint::new(1);
let bank = Bank::new(&mint);
let keypair = KeyPair::new();
assert_eq!(
bank.transfer(1, &KeyPair::new(), mint.pubkey(), mint.last_id()),
Err(BankError::AccountNotFound)
bank.transfer(1, &keypair, mint.pubkey(), mint.last_id()),
Err(BankError::AccountNotFound(keypair.pubkey()))
);
assert_eq!(bank.transaction_count(), 0);
}
Expand All @@ -444,7 +445,7 @@ mod tests {
assert_eq!(bank.transaction_count(), 1);
assert_eq!(
bank.transfer(10_001, &mint.keypair(), pubkey, mint.last_id()),
Err(BankError::InsufficientFunds)
Err(BankError::InsufficientFunds(mint.pubkey()))
);
assert_eq!(bank.transaction_count(), 1);

Expand Down
2 changes: 1 addition & 1 deletion src/crdt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -502,7 +502,7 @@ impl Crdt {
}

#[cfg(test)]
mod test {
mod tests {
use crdt::{Crdt, ReplicatedData};
use logger;
use packet::Blob;
Expand Down
47 changes: 25 additions & 22 deletions src/signature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,27 +72,23 @@ impl GenKeys {
pub fn gen_n_seeds(&self, n_seeds: i64) -> Vec<[u8; 16]> {
let mut rng = self.generator.borrow_mut();

let seeds = (0..n_seeds)
(0..n_seeds)
.into_iter()
.map(|_| {
let seed: [u8; 16] = rng.gen();
seed
})
.collect();
seeds
.map(|_| rng.gen::<[u8; 16]>())
.collect()
}

pub fn gen_n_keys(&self, n_keys: i64, tokens_per_user: i64) -> Vec<(Vec<u8>, i64)> {
let keys = self.gen_n_seeds(n_keys);
let seeds = self.gen_n_seeds(n_keys);

let users: Vec<_> = keys.into_par_iter()
seeds
.into_par_iter()
.map(|seed| {
let new: GenKeys = GenKeys::new(&seed[..]);
let pkcs8 = KeyPair::generate_pkcs8(&new).unwrap().to_vec();
(pkcs8, tokens_per_user)
})
.collect();
users
.collect()
}
}

Expand All @@ -104,22 +100,12 @@ impl SecureRandom for GenKeys {
}
}

#[cfg(all(feature = "unstable", test))]
#[cfg(test)]
mod tests {
extern crate test;

use self::test::Bencher;
use super::*;
use std::collections::HashSet;
use std::iter::FromIterator;

#[bench]
fn bench_gen_keys(b: &mut Bencher) {
let seed: &[_] = &[1, 2, 3, 4];
let rnd = GenKeys::new(seed);
b.iter(|| rnd.gen_n_keys(1000, 1));
}

#[test]
fn test_new_key_is_redundant() {
let seed: &[_] = &[1, 2, 3, 4];
Expand All @@ -145,3 +131,20 @@ mod tests {
assert_eq!(users1_set, users2_set);
}
}

#[cfg(all(feature = "unstable", test))]
mod bench {
extern crate test;

use self::test::Bencher;
use super::*;
use std::collections::HashSet;
use std::iter::FromIterator;

#[bench]
fn bench_gen_keys(b: &mut Bencher) {
let seed: &[_] = &[1, 2, 3, 4];
let rnd = GenKeys::new(seed);
b.iter(|| rnd.gen_n_keys(1000, 1));
}
}