Fix clippy errors on tests (#2160)

## Issue Addressed

There are some clippy error on tests.


## Proposed Changes

Enable clippy check on tests and fix the errors. 💪
This commit is contained in:
Akihito Nakano
2021-01-28 23:31:06 +00:00
parent e4b62139d7
commit 1a22a096c6
36 changed files with 513 additions and 516 deletions

View File

@@ -78,7 +78,6 @@ pub fn int_to_bytes96(int: u64) -> Vec<u8> {
#[cfg(test)]
mod tests {
use super::*;
use hex;
use std::{fs::File, io::prelude::*, path::PathBuf};
use yaml_rust::yaml;

View File

@@ -386,7 +386,7 @@ mod test_compute_deltas {
state_root,
target_root: finalized_root,
current_epoch_shuffling_id: junk_shuffling_id.clone(),
next_epoch_shuffling_id: junk_shuffling_id.clone(),
next_epoch_shuffling_id: junk_shuffling_id,
justified_epoch: genesis_epoch,
finalized_epoch: genesis_epoch,
})

View File

@@ -83,6 +83,7 @@ mod round_trip {
}
#[test]
#[allow(clippy::zero_prefixed_literal)]
fn fixed_len_struct_encoding() {
let items: Vec<FixedLen> = vec![
FixedLen { a: 0, b: 0, c: 0 },
@@ -142,6 +143,7 @@ mod round_trip {
}
#[test]
#[allow(clippy::zero_prefixed_literal)]
fn offset_into_fixed_bytes() {
let bytes = vec![
// 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
@@ -172,6 +174,7 @@ mod round_trip {
}
#[test]
#[allow(clippy::zero_prefixed_literal)]
fn first_offset_skips_byte() {
let bytes = vec![
// 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
@@ -186,6 +189,7 @@ mod round_trip {
}
#[test]
#[allow(clippy::zero_prefixed_literal)]
fn variable_len_struct_encoding() {
let items: Vec<VariableLen> = vec![
VariableLen {
@@ -274,6 +278,7 @@ mod round_trip {
}
#[test]
#[allow(clippy::zero_prefixed_literal)]
fn offsets_decreasing() {
let bytes = vec![
// 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
@@ -296,6 +301,7 @@ mod round_trip {
}
#[test]
#[allow(clippy::zero_prefixed_literal)]
fn two_variable_len_options_encoding() {
let s = TwoVariableLenOptions {
a: 42,

View File

@@ -777,17 +777,17 @@ mod bitlist {
fn ssz_encode() {
assert_eq!(
BitList0::with_capacity(0).unwrap().as_ssz_bytes(),
vec![0b0000_00001],
vec![0b0000_0001],
);
assert_eq!(
BitList1::with_capacity(0).unwrap().as_ssz_bytes(),
vec![0b0000_00001],
vec![0b0000_0001],
);
assert_eq!(
BitList1::with_capacity(1).unwrap().as_ssz_bytes(),
vec![0b0000_00010],
vec![0b0000_0010],
);
assert_eq!(

View File

@@ -387,7 +387,7 @@ fn invalid_attestation_wrong_justified_checkpoint() {
index: 0,
reason: AttestationInvalid::WrongJustifiedCheckpoint {
state: Checkpoint {
epoch: Epoch::from(2 as u64),
epoch: Epoch::from(2_u64),
root: Hash256::zero(),
},
attestation: Checkpoint {
@@ -878,7 +878,7 @@ fn invalid_proposer_slashing_proposal_epoch_mismatch() {
index: 0,
reason: ProposerSlashingInvalid::ProposalSlotMismatch(
Slot::from(0_u64),
Slot::from(128 as u64)
Slot::from(128_u64)
)
})
);

View File

@@ -181,6 +181,7 @@ mod tests {
}
#[test]
#[allow(clippy::assertions_on_constants)]
fn sanity_check_constants() {
assert!(TOTAL_SIZE > SEED_SIZE);
assert!(TOTAL_SIZE > PIVOT_VIEW_SIZE);

View File

@@ -397,7 +397,7 @@ mod test {
let merklizer_root_individual_3_bytes = {
let mut m = MerkleHasher::with_depth(depth);
for bytes in reference_bytes.clone().chunks(3) {
for bytes in reference_bytes.chunks(3) {
m.write(bytes).expect("should process byte");
}
m.finish().expect("should finish")
@@ -426,7 +426,7 @@ mod test {
/// of leaves and a depth.
fn compare_reference_with_len(leaves: u64, depth: usize) {
let leaves = (0..leaves)
.map(|i| Hash256::from_low_u64_be(i))
.map(Hash256::from_low_u64_be)
.collect::<Vec<_>>();
compare_with_reference(&leaves, depth)
}
@@ -435,7 +435,7 @@ mod test {
/// results.
fn compare_new_with_leaf_count(num_leaves: u64, depth: usize) {
let leaves = (0..num_leaves)
.map(|i| Hash256::from_low_u64_be(i))
.map(Hash256::from_low_u64_be)
.collect::<Vec<_>>();
let from_depth = {

View File

@@ -1,6 +1,7 @@
#![cfg(test)]
use super::*;
use crate::test_utils::*;
use std::ops::Mul;
ssz_and_tree_hash_tests!(FoundationBeaconState);
@@ -49,13 +50,13 @@ fn test_beacon_proposer_index<T: EthSpec>() {
// Test where we have two validators per slot.
// 0th candidate should be chosen every time.
let state = build_state(T::slots_per_epoch() as usize * 2);
let state = build_state((T::slots_per_epoch() as usize).mul(2));
for i in 0..T::slots_per_epoch() {
test(&state, Slot::from(i), 0);
}
// Test with two validators per slot, first validator has zero balance.
let mut state = build_state(T::slots_per_epoch() as usize * 2);
let mut state = build_state((T::slots_per_epoch() as usize).mul(2));
let slot0_candidate0 = ith_candidate(&state, Slot::new(0), 0, &spec);
state.validators[slot0_candidate0].effective_balance = 0;
test(&state, Slot::new(0), 1);
@@ -74,8 +75,8 @@ fn beacon_proposer_index() {
/// 1. Using the cache before it's built fails.
/// 2. Using the cache after it's build passes.
/// 3. Using the cache after it's dropped fails.
fn test_cache_initialization<'a, T: EthSpec>(
state: &'a mut BeaconState<T>,
fn test_cache_initialization<T: EthSpec>(
state: &mut BeaconState<T>,
relative_epoch: RelativeEpoch,
spec: &ChainSpec,
) {
@@ -126,7 +127,7 @@ fn cache_initialization() {
}
fn test_clone_config<E: EthSpec>(base_state: &BeaconState<E>, clone_config: CloneConfig) {
let state = base_state.clone_with(clone_config.clone());
let state = base_state.clone_with(clone_config);
if clone_config.committee_caches {
state
.committee_cache(RelativeEpoch::Previous)
@@ -260,6 +261,7 @@ fn tree_hash_cache() {
mod committees {
use super::*;
use crate::beacon_state::MinimalEthSpec;
use std::ops::{Add, Div};
use swap_or_not_shuffle::shuffle_list;
fn execute_committee_consistency_test<T: EthSpec>(
@@ -295,7 +297,10 @@ mod committees {
// of committees in an epoch.
assert_eq!(
beacon_committees.len() as u64,
state.get_epoch_committee_count(relative_epoch).unwrap() / T::slots_per_epoch()
state
.get_epoch_committee_count(relative_epoch)
.unwrap()
.div(T::slots_per_epoch())
);
for (committee_index, bc) in beacon_committees.iter().enumerate() {
@@ -372,10 +377,11 @@ mod committees {
fn committee_consistency_test_suite<T: EthSpec>(cached_epoch: RelativeEpoch) {
let spec = T::default_spec();
let validator_count = spec.max_committees_per_slot
* T::slots_per_epoch() as usize
* spec.target_committee_size
+ 1;
let validator_count = spec
.max_committees_per_slot
.mul(T::slots_per_epoch() as usize)
.mul(spec.target_committee_size)
.add(1);
committee_consistency_test::<T>(validator_count as usize, Epoch::new(0), cached_epoch);
@@ -387,7 +393,10 @@ mod committees {
committee_consistency_test::<T>(
validator_count as usize,
T::genesis_epoch() + T::slots_per_historical_root() as u64 * T::slots_per_epoch() * 4,
T::genesis_epoch()
+ (T::slots_per_historical_root() as u64)
.mul(T::slots_per_epoch())
.mul(4),
cached_epoch,
);
}

View File

@@ -405,6 +405,7 @@ mod tests {
let _ = ChainSpec::mainnet();
}
#[allow(clippy::useless_vec)]
fn test_domain(domain_type: Domain, raw_domain: u32, spec: &ChainSpec) {
let previous_version = [0, 0, 0, 1];
let current_version = [0, 0, 0, 2];

View File

@@ -137,9 +137,11 @@ mod test {
#[test]
fn zeroed_validator() {
let mut v = Validator::default();
v.activation_eligibility_epoch = Epoch::from(0u64);
v.activation_epoch = Epoch::from(0u64);
let v = Validator {
activation_eligibility_epoch: Epoch::from(0u64),
activation_epoch: Epoch::from(0u64),
..Default::default()
};
test_validator_tree_hash(&v);
}
@@ -153,6 +155,7 @@ mod test {
}
#[test]
#[allow(clippy::assertions_on_constants)]
pub fn smallvec_size_check() {
// If this test fails we need to go and reassess the length of the `SmallVec` in
// `cached_tree_hash::TreeHashCache`. If the size of the `SmallVec` is too slow we're going