diff --git a/src/atomic64.rs b/src/atomic64.rs index 0c653c51..6a20c031 100644 --- a/src/atomic64.rs +++ b/src/atomic64.rs @@ -241,8 +241,8 @@ impl AtomicU64 { #[cfg(test)] mod test { + use std::f64; use std::f64::consts::PI; - use std::f64::{self, EPSILON}; use super::*; @@ -251,7 +251,7 @@ mod test { let table: Vec = vec![0.0, 1.0, PI, f64::MIN, f64::MAX]; for f in table { - assert!((f - AtomicF64::new(f).get()).abs() < EPSILON); + assert!((f - AtomicF64::new(f).get()).abs() < f64::EPSILON); } } diff --git a/src/counter.rs b/src/counter.rs index 239e2982..a64d87c4 100644 --- a/src/counter.rs +++ b/src/counter.rs @@ -323,7 +323,7 @@ impl Clone for GenericLocalCounterVec

{ #[cfg(test)] mod tests { use std::collections::HashMap; - use std::f64::EPSILON; + use std::f64; use super::*; use crate::metrics::{Collector, Opts}; @@ -343,7 +343,7 @@ mod tests { assert_eq!(mfs.len(), 1); let mf = mfs.pop().unwrap(); - let m = mf.get_metric().get(0).unwrap(); + let m = mf.get_metric().first().unwrap(); assert_eq!(m.get_label().len(), 2); assert_eq!(m.get_counter().get_value() as u64, 43); @@ -363,12 +363,12 @@ mod tests { assert_eq!(mfs.len(), 1); let mf = mfs.pop().unwrap(); - let m = mf.get_metric().get(0).unwrap(); + let m = mf.get_metric().first().unwrap(); assert_eq!(m.get_label().len(), 0); assert_eq!(m.get_counter().get_value() as u64, 12); counter.reset(); - assert_eq!(counter.get() as u64, 0); + assert_eq!(counter.get(), 0); } #[test] @@ -414,9 +414,9 @@ mod tests { local_counter.reset(); counter.reset(); - assert_eq!(counter.get() as u64, 0); + assert_eq!(counter.get(), 0); local_counter.flush(); - assert_eq!(counter.get() as u64, 0); + assert_eq!(counter.get(), 0); } #[test] @@ -502,48 +502,48 @@ mod tests { assert!(local_vec_1.remove_label_values(&["v1", "v2"]).is_err()); local_vec_1.with_label_values(&["v1", "v2"]).inc_by(23.0); - assert!((local_vec_1.with_label_values(&["v1", "v2"]).get() - 23.0) <= EPSILON); - assert!((vec.with_label_values(&["v1", "v2"]).get() - 0.0) <= EPSILON); + assert!((local_vec_1.with_label_values(&["v1", "v2"]).get() - 23.0) <= f64::EPSILON); + assert!((vec.with_label_values(&["v1", "v2"]).get() - 0.0) <= f64::EPSILON); local_vec_1.flush(); - assert!((local_vec_1.with_label_values(&["v1", "v2"]).get() - 0.0) <= EPSILON); - assert!((vec.with_label_values(&["v1", "v2"]).get() - 23.0) <= EPSILON); + assert!((local_vec_1.with_label_values(&["v1", "v2"]).get() - 0.0) <= f64::EPSILON); + assert!((vec.with_label_values(&["v1", "v2"]).get() - 23.0) <= f64::EPSILON); local_vec_1.flush(); - assert!((local_vec_1.with_label_values(&["v1", "v2"]).get() - 0.0) <= EPSILON); - assert!((vec.with_label_values(&["v1", "v2"]).get() - 23.0) <= EPSILON); + assert!((local_vec_1.with_label_values(&["v1", "v2"]).get() - 0.0) <= f64::EPSILON); + assert!((vec.with_label_values(&["v1", "v2"]).get() - 23.0) <= f64::EPSILON); local_vec_1.with_label_values(&["v1", "v2"]).inc_by(11.0); - assert!((local_vec_1.with_label_values(&["v1", "v2"]).get() - 11.0) <= EPSILON); - assert!((vec.with_label_values(&["v1", "v2"]).get() - 23.0) <= EPSILON); + assert!((local_vec_1.with_label_values(&["v1", "v2"]).get() - 11.0) <= f64::EPSILON); + assert!((vec.with_label_values(&["v1", "v2"]).get() - 23.0) <= f64::EPSILON); local_vec_1.flush(); - assert!((local_vec_1.with_label_values(&["v1", "v2"]).get() - 0.0) <= EPSILON); - assert!((vec.with_label_values(&["v1", "v2"]).get() - 34.0) <= EPSILON); + assert!((local_vec_1.with_label_values(&["v1", "v2"]).get() - 0.0) <= f64::EPSILON); + assert!((vec.with_label_values(&["v1", "v2"]).get() - 34.0) <= f64::EPSILON); // When calling `remove_label_values`, it is "flushed" immediately. assert!(local_vec_1.remove_label_values(&["v1", "v2"]).is_ok()); - assert!((local_vec_1.with_label_values(&["v1", "v2"]).get() - 0.0) <= EPSILON); - assert!((vec.with_label_values(&["v1", "v2"]).get() - 0.0) <= EPSILON); + assert!((local_vec_1.with_label_values(&["v1", "v2"]).get() - 0.0) <= f64::EPSILON); + assert!((vec.with_label_values(&["v1", "v2"]).get() - 0.0) <= f64::EPSILON); local_vec_1.with_label_values(&["v1", "v2"]).inc(); assert!(local_vec_1.remove_label_values(&["v1"]).is_err()); assert!(local_vec_1.remove_label_values(&["v1", "v3"]).is_err()); local_vec_1.with_label_values(&["v1", "v2"]).inc_by(13.0); - assert!((local_vec_1.with_label_values(&["v1", "v2"]).get() - 14.0) <= EPSILON); - assert!((vec.with_label_values(&["v1", "v2"]).get() - 0.0) <= EPSILON); + assert!((local_vec_1.with_label_values(&["v1", "v2"]).get() - 14.0) <= f64::EPSILON); + assert!((vec.with_label_values(&["v1", "v2"]).get() - 0.0) <= f64::EPSILON); local_vec_2.with_label_values(&["v1", "v2"]).inc_by(7.0); - assert!((local_vec_2.with_label_values(&["v1", "v2"]).get() - 7.0) <= EPSILON); + assert!((local_vec_2.with_label_values(&["v1", "v2"]).get() - 7.0) <= f64::EPSILON); local_vec_1.flush(); local_vec_2.flush(); - assert!((vec.with_label_values(&["v1", "v2"]).get() - 21.0) <= EPSILON); + assert!((vec.with_label_values(&["v1", "v2"]).get() - 21.0) <= f64::EPSILON); local_vec_1.flush(); local_vec_2.flush(); - assert!((vec.with_label_values(&["v1", "v2"]).get() - 21.0) <= EPSILON); + assert!((vec.with_label_values(&["v1", "v2"]).get() - 21.0) <= f64::EPSILON); } #[test] diff --git a/src/desc.rs b/src/desc.rs index f97b123d..23367df2 100644 --- a/src/desc.rs +++ b/src/desc.rs @@ -253,8 +253,7 @@ mod tests { vec![name.into()], HashMap::new(), ) - .err() - .expect(format!("expected error for {}", name).as_ref()); + .expect_err(format!("expected error for {}", name).as_ref()); match res { Error::Msg(msg) => assert_eq!(msg, format!("'{}' is not a valid label name", name)), other => panic!("{}", other), @@ -268,8 +267,7 @@ mod tests { let mut labels = HashMap::new(); labels.insert(name.into(), "value".into()); let res = Desc::new("name".into(), "help".into(), vec![], labels) - .err() - .expect(format!("expected error for {}", name).as_ref()); + .expect_err(format!("expected error for {}", name).as_ref()); match res { Error::Msg(msg) => assert_eq!(msg, format!("'{}' is not a valid label name", name)), other => panic!("{}", other), @@ -281,8 +279,7 @@ mod tests { fn test_invalid_metric_name() { for &name in &["-dash", "9gag", "has space"] { let res = Desc::new(name.into(), "help".into(), vec![], HashMap::new()) - .err() - .expect(format!("expected error for {}", name).as_ref()); + .expect_err(format!("expected error for {}", name).as_ref()); match res { Error::Msg(msg) => { assert_eq!(msg, format!("'{}' is not a valid metric name", name)) diff --git a/src/gauge.rs b/src/gauge.rs index 83eba2b1..a854d26a 100644 --- a/src/gauge.rs +++ b/src/gauge.rs @@ -188,7 +188,7 @@ mod tests { assert_eq!(mfs.len(), 1); let mf = mfs.pop().unwrap(); - let m = mf.get_metric().get(0).unwrap(); + let m = mf.get_metric().first().unwrap(); assert_eq!(m.get_label().len(), 2); assert_eq!(m.get_gauge().get_value() as u64, 42); } diff --git a/src/histogram.rs b/src/histogram.rs index 78480541..b51cec38 100644 --- a/src/histogram.rs +++ b/src/histogram.rs @@ -284,11 +284,11 @@ impl ShardAndCount { /// A histogram supports two main execution paths: /// /// 1. `observe` which increases the overall observation counter, updates the -/// observation sum and increases a single bucket counter. +/// observation sum and increases a single bucket counter. /// /// 2. `proto` (aka. collecting the metric, from now on referred to as the -/// collect operation) which snapshots the state of the histogram and exposes it -/// as a Protobuf struct. +/// collect operation) which snapshots the state of the histogram and exposes +/// it as a Protobuf struct. /// /// If an observe and a collect operation interleave, the latter could be /// exposing a snapshot of the histogram that does not uphold all histogram @@ -1207,7 +1207,7 @@ impl Clone for LocalHistogramVec { #[cfg(test)] mod tests { - use std::f64::{EPSILON, INFINITY}; + use std::f64; use std::thread; use std::time::Duration; @@ -1237,7 +1237,7 @@ mod tests { assert_eq!(mfs.len(), 1); let mf = mfs.pop().unwrap(); - let m = mf.get_metric().get(0).unwrap(); + let m = mf.get_metric().first().unwrap(); assert_eq!(m.get_label().len(), 2); let proto_histogram = m.get_histogram(); assert_eq!(proto_histogram.get_sample_count(), 3); @@ -1251,11 +1251,11 @@ mod tests { assert_eq!(mfs.len(), 1); let mf = mfs.pop().unwrap(); - let m = mf.get_metric().get(0).unwrap(); + let m = mf.get_metric().first().unwrap(); assert_eq!(m.get_label().len(), 0); let proto_histogram = m.get_histogram(); assert_eq!(proto_histogram.get_sample_count(), 0); - assert!((proto_histogram.get_sample_sum() - 0.0) < EPSILON); + assert!((proto_histogram.get_sample_sum() - 0.0) < f64::EPSILON); assert_eq!(proto_histogram.get_bucket().len(), buckets.len()) } @@ -1287,7 +1287,7 @@ mod tests { let m = mf.get_metric().get(0).unwrap(); let proto_histogram = m.get_histogram(); assert_eq!(proto_histogram.get_sample_count(), 3); - assert!((proto_histogram.get_sample_sum() - 0.0) > EPSILON); + assert!((proto_histogram.get_sample_sum() - 0.0) > f64::EPSILON); } #[test] @@ -1311,7 +1311,11 @@ mod tests { (vec![], true, DEFAULT_BUCKETS.len()), (vec![-2.0, -1.0, -0.5, 0.0, 0.5, 1.0, 2.0], true, 7), (vec![-2.0, -1.0, -0.5, 10.0, 0.5, 1.0, 2.0], false, 7), - (vec![-2.0, -1.0, -0.5, 0.0, 0.5, 1.0, INFINITY], true, 6), + ( + vec![-2.0, -1.0, -0.5, 0.0, 0.5, 1.0, f64::INFINITY], + true, + 6, + ), ]; for (buckets, is_ok, length) in table { @@ -1366,7 +1370,7 @@ mod tests { for (millis, seconds) in tbls { let d = Duration::from_millis(millis); let v = duration_to_seconds(d); - assert!((v - seconds).abs() < EPSILON); + assert!((v - seconds).abs() < f64::EPSILON); } } @@ -1405,7 +1409,7 @@ mod tests { let proto_histogram = m.get_histogram(); assert_eq!(proto_histogram.get_sample_count(), 1); - assert!((proto_histogram.get_sample_sum() - 1.0) < EPSILON); + assert!((proto_histogram.get_sample_sum() - 1.0) < f64::EPSILON); assert_eq!(proto_histogram.get_bucket().len(), buckets.len()) } @@ -1421,7 +1425,7 @@ mod tests { let m = histogram.metric(); let proto_histogram = m.get_histogram(); assert_eq!(proto_histogram.get_sample_count(), count); - assert!((proto_histogram.get_sample_sum() - sum) < EPSILON); + assert!((proto_histogram.get_sample_sum() - sum) < f64::EPSILON); }; local.observe(1.0); @@ -1456,7 +1460,7 @@ mod tests { let ms = vec.collect()[0].take_metric(); let proto_histogram = ms[0].get_histogram(); assert_eq!(proto_histogram.get_sample_count(), count); - assert!((proto_histogram.get_sample_sum() - sum) < EPSILON); + assert!((proto_histogram.get_sample_sum() - sum) < f64::EPSILON); }; { diff --git a/src/macros.rs b/src/macros.rs index 8095b2e9..a0da59ba 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -43,7 +43,7 @@ fn test_labels_without_trailing_comma() { "foo" => "bar" }; assert_eq!(labels.len(), 2); - assert!(labels.get("test").is_some()); + assert!(labels.contains_key("test")); assert_eq!(*(labels.get("test").unwrap()), "hello"); } @@ -84,6 +84,7 @@ macro_rules! opts { let opts = $crate::Opts::new($NAME, $HELP); let lbs = HashMap::::new(); $( + #[allow(clippy::redundant_locals)] let mut lbs = lbs; lbs.extend($CONST_LABELS.iter().map(|(k, v)| ((*k).into(), (*v).into()))); )* @@ -104,7 +105,7 @@ fn test_opts_trailing_comma() { let opts = opts!(name, help, labels! {"test" => "hello", "foo" => "bar",},); assert_eq!(opts.const_labels.len(), 2); - assert!(opts.const_labels.get("foo").is_some()); + assert!(opts.const_labels.contains_key("foo")); assert_eq!(opts.const_labels.get("foo").unwrap(), "bar"); let opts = opts!( @@ -114,7 +115,7 @@ fn test_opts_trailing_comma() { labels! {"ans" => "42",}, ); assert_eq!(opts.const_labels.len(), 3); - assert!(opts.const_labels.get("ans").is_some()); + assert!(opts.const_labels.contains_key("ans")); assert_eq!(opts.const_labels.get("ans").unwrap(), "42"); } @@ -191,7 +192,7 @@ fn test_histogram_opts_trailing_comma() { assert_eq!(opts.common_opts.name, name); assert_eq!(opts.common_opts.help, help); assert_eq!(opts.buckets.len(), 2); - assert!(opts.common_opts.const_labels.get("key").is_some()); + assert!(opts.common_opts.const_labels.contains_key("key")); assert_eq!(opts.common_opts.const_labels.get("key").unwrap(), "value"); }