From 62c9560fe6c6e4a17660cda0a9d1e093c197755f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D1=80=D1=82=D1=91=D0=BC=20=D0=9F=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=BE=D0=B2=20=5BArtyom=20Pavlov=5D?= Date: Wed, 8 May 2024 17:27:15 +0300 Subject: [PATCH] Fix test_sample_counts_match_probabilities --- rand_distr/src/weighted_tree.rs | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/rand_distr/src/weighted_tree.rs b/rand_distr/src/weighted_tree.rs index 189fa5ef5a..b508f86d3d 100644 --- a/rand_distr/src/weighted_tree.rs +++ b/rand_distr/src/weighted_tree.rs @@ -362,6 +362,7 @@ mod test { } #[test] + #[allow(clippy::needless_range_loop)] fn test_sample_counts_match_probabilities() { let start = 1; let end = 3; @@ -371,14 +372,14 @@ mod test { let mut tree = WeightedTreeIndex::new(weights).unwrap(); let mut total_weight = 0.0; let mut weights = alloc::vec![0.0; end]; - for (i, w) in weights.iter_mut().enumerate() { + for i in 0..end { tree.update(i, i as f64).unwrap(); - *w = i as f64; + weights[i] = i as f64; total_weight += i as f64; } - for (i, w) in weights.iter_mut().enumerate() { + for i in 0..start { tree.update(i, 0.0).unwrap(); - *w = 0.0; + weights[i] = 0.0; total_weight -= i as f64; } let mut counts = alloc::vec![0_usize; end]; @@ -386,7 +387,9 @@ mod test { let i = tree.sample(&mut rng); counts[i] += 1; } - counts.iter().for_each(|&c| assert_eq!(c, 0)); + for i in 0..start { + assert_eq!(counts[i], 0); + } for i in start..end { let diff = counts[i] as f64 / samples as f64 - weights[i] / total_weight; assert!(diff.abs() < 0.05);