diff --git a/src/encoding/type_support/common.rs b/src/encoding/type_support/common.rs index 527b240..7c72693 100644 --- a/src/encoding/type_support/common.rs +++ b/src/encoding/type_support/common.rs @@ -84,15 +84,13 @@ pub(crate) mod time_proxies { /// This is where we show that we have equivalent encodings for the time and chrono crate types. #[cfg(all(test, feature = "chrono", feature = "time"))] mod chrono_time_value_compat { - use crate::encoding::type_support::time::RANDOM_SAMPLES; + use crate::encoding::type_support::time::with_random_values; use crate::encoding::type_support::{chrono as impl_chrono, time as impl_time}; use crate::encoding::{EmptyState, General, Proxiable, ValueEncoder}; use alloc::fmt::Debug; use alloc::vec::Vec; use chrono::{Datelike, FixedOffset, Timelike}; - use core::iter::repeat_with; use itertools::iproduct; - use rand::{thread_rng, Rng}; fn assert_same_encoding(t: &T, u: &U) where @@ -118,16 +116,13 @@ mod chrono_time_value_compat { #[test] fn date() { - let mut rng = thread_rng(); for chrono_date in impl_chrono::test_dates() { let Some(time_date) = date_c_to_t(chrono_date) else { continue; }; assert_same_encoding(&chrono_date, &time_date); } - for time_date in - impl_time::test_dates().chain(repeat_with(|| rng.gen()).take(RANDOM_SAMPLES)) - { + for time_date in with_random_values(impl_time::test_dates()) { let Some(chrono_date) = date_t_to_c(time_date) else { continue; }; @@ -156,16 +151,13 @@ mod chrono_time_value_compat { #[test] fn time() { - let mut rng = thread_rng(); for chrono_time in impl_chrono::test_times() { let Some(time_time) = time_c_to_t(chrono_time) else { continue; }; assert_same_encoding(&chrono_time, &time_time); } - for time_time in - impl_time::test_times().chain(repeat_with(|| rng.gen()).take(RANDOM_SAMPLES)) - { + for time_time in with_random_values(impl_time::test_times()) { let Some(chrono_time) = time_t_to_c(time_time) else { continue; }; @@ -189,17 +181,13 @@ mod chrono_time_value_compat { #[test] fn datetime() { - let mut rng = thread_rng(); for chrono_datetime in impl_chrono::test_datetimes() { let Some(time_datetime) = datetime_c_to_t(chrono_datetime) else { continue; }; assert_same_encoding(&chrono_datetime, &time_datetime); } - for time_datetime in impl_time::test_datetimes() - .into_iter() - .chain(repeat_with(|| rng.gen()).take(RANDOM_SAMPLES)) - { + for time_datetime in with_random_values(impl_time::test_datetimes()) { let Some(chrono_datetime) = datetime_t_to_c(time_datetime) else { continue; }; @@ -217,16 +205,13 @@ mod chrono_time_value_compat { #[test] fn zone() { - let mut rng = thread_rng(); for chrono_offset in impl_chrono::test_zones() { let Some(time_offset) = offset_c_to_t(chrono_offset) else { continue; }; assert_same_encoding(&chrono_offset, &time_offset); } - for time_offset in - impl_time::test_zones().chain(repeat_with(|| rng.gen()).take(RANDOM_SAMPLES)) - { + for time_offset in with_random_values(impl_time::test_zones()) { let Some(chrono_offset) = offset_t_to_c(time_offset) else { continue; }; @@ -262,7 +247,6 @@ mod chrono_time_value_compat { #[test] fn aware_date() { - let mut rng = thread_rng(); for chrono_pair in iproduct!(impl_chrono::test_datetimes(), impl_chrono::test_zones()) { let chrono_aware = aware_compose_chrono(chrono_pair).unwrap(); let Some(time_aware) = aware_c_to_t(chrono_aware) else { @@ -270,9 +254,10 @@ mod chrono_time_value_compat { }; assert_same_encoding(&chrono_aware, &time_aware); } - for time_pair in iproduct!(impl_time::test_datetimes(), impl_time::test_zones()) - .chain(repeat_with(|| rng.gen()).take(RANDOM_SAMPLES)) - { + for time_pair in with_random_values(iproduct!( + impl_time::test_datetimes(), + impl_time::test_zones() + )) { let time_aware = aware_compose_time(time_pair).unwrap(); let Some(chrono_aware) = aware_t_to_c(time_aware) else { continue; @@ -298,19 +283,19 @@ mod chrono_time_value_compat { #[test] fn timedelta() { - let mut rng = thread_rng(); + let mut rng = rand::thread_rng(); for chrono_delta in impl_chrono::test_timedeltas() - .chain(repeat_with(|| impl_chrono::random_timedelta(&mut rng))) - .take(RANDOM_SAMPLES) + .chain(core::iter::repeat_with(|| { + impl_chrono::random_timedelta(&mut rng) + })) + .take(crate::encoding::type_support::time::RANDOM_SAMPLES) { let Some(time_delta) = delta_c_to_t(chrono_delta) else { continue; }; assert_same_encoding(&chrono_delta, &time_delta); } - for time_delta in - impl_time::test_durations().chain(repeat_with(|| rng.gen()).take(RANDOM_SAMPLES)) - { + for time_delta in with_random_values(impl_time::test_durations()) { let Some(chrono_delta) = delta_t_to_c(time_delta) else { continue; }; diff --git a/src/encoding/type_support/time.rs b/src/encoding/type_support/time.rs index 43b06f0..e929857 100644 --- a/src/encoding/type_support/time.rs +++ b/src/encoding/type_support/time.rs @@ -11,12 +11,31 @@ use time::{Date, Duration, OffsetDateTime, PrimitiveDateTime, Time, UtcOffset}; #[cfg(all(test, feature = "chrono"))] pub(super) use { - date::test_dates, duration::test_durations, primitivedatetime::test_datetimes, - time_ty::test_times, utcoffset::test_zones, + date::test_dates, + duration::test_durations, + helpers::{with_random_values, RANDOM_SAMPLES}, + primitivedatetime::test_datetimes, + time_ty::test_times, + utcoffset::test_zones, }; #[cfg(test)] -pub(super) const RANDOM_SAMPLES: usize = 100; +mod helpers { + use core::iter::repeat_with; + use rand::{thread_rng, Rng}; + + pub(in super::super) const RANDOM_SAMPLES: usize = 100; + + pub(in super::super) fn with_random_values(it: It) -> impl Iterator + where + It: IntoIterator, + rand::distributions::Standard: rand::distributions::Distribution, + { + let mut rng = thread_rng(); + it.into_iter() + .chain(repeat_with(move || rng.gen()).take(RANDOM_SAMPLES)) + } +} impl ForOverwrite for Date { fn for_overwrite() -> Self { @@ -76,11 +95,9 @@ delegate_value_encoding!(delegate from (General) to (Proxied>) #[cfg(test)] mod date { - use super::RANDOM_SAMPLES; + use super::with_random_values; use crate::encoding::test::{check_type_empty, distinguished, expedient}; use crate::encoding::{EmptyState, WireType}; - use core::iter::repeat_with; - use rand::{thread_rng, Rng}; use time::Date; use time::Month::{January, June}; @@ -97,12 +114,12 @@ mod date { #[test] fn check_type() { - let mut rng = thread_rng(); - for date in test_dates().chain(repeat_with(|| rng.gen()).take(RANDOM_SAMPLES)) { + for date in with_random_values(test_dates()) { expedient::check_type(date, 123, WireType::LengthDelimited).unwrap(); distinguished::check_type(date, 123, WireType::LengthDelimited).unwrap(); } } + check_type_empty!(Date, via proxy); check_type_empty!(Date, via distinguished proxy); } @@ -176,11 +193,9 @@ delegate_value_encoding!(delegate from (General) to (Proxied>) #[cfg(test)] mod time_ty { - use super::RANDOM_SAMPLES; + use super::with_random_values; use crate::encoding::test::{check_type_empty, distinguished, expedient}; use crate::encoding::{EmptyState, WireType}; - use core::iter::repeat_with; - use rand::{thread_rng, Rng}; use time::Time; pub(in super::super) fn test_times() -> impl Iterator + Clone { @@ -196,12 +211,12 @@ mod time_ty { #[test] fn check_type() { - let mut rng = thread_rng(); - for date in test_times().chain(repeat_with(|| rng.gen()).take(RANDOM_SAMPLES)) { + for date in with_random_values(test_times()) { expedient::check_type(date, 123, WireType::LengthDelimited).unwrap(); distinguished::check_type(date, 123, WireType::LengthDelimited).unwrap(); } } + check_type_empty!(Time, via proxy); check_type_empty!(Time, via distinguished proxy); } @@ -271,12 +286,10 @@ delegate_value_encoding!(delegate from (General) to (Proxied>) mod primitivedatetime { use super::date::test_dates; use super::time_ty::test_times; - use super::RANDOM_SAMPLES; + use super::with_random_values; use crate::encoding::test::{check_type_empty, distinguished, expedient}; use crate::encoding::{EmptyState, WireType}; - use core::iter::repeat_with; use itertools::iproduct; - use rand::{thread_rng, Rng}; use time::Month::{August, March}; use time::{Date, PrimitiveDateTime, Time}; @@ -301,15 +314,12 @@ mod primitivedatetime { #[test] fn check_type() { - let mut rng = thread_rng(); - for datetime in test_datetimes() - .into_iter() - .chain(repeat_with(|| rng.gen()).take(RANDOM_SAMPLES)) - { + for datetime in with_random_values(test_datetimes()) { expedient::check_type(datetime, 123, WireType::LengthDelimited).unwrap(); distinguished::check_type(datetime, 123, WireType::LengthDelimited).unwrap(); } } + check_type_empty!(PrimitiveDateTime, via proxy); check_type_empty!(PrimitiveDateTime, via distinguished proxy); } @@ -377,7 +387,7 @@ delegate_value_encoding!(delegate from (General) to (Proxied<(Varint, Varint, Va #[cfg(test)] mod utcoffset { - use super::RANDOM_SAMPLES; + use super::with_random_values; use crate::encoding::test::{check_type_empty, distinguished, expedient}; use crate::encoding::{ Capped, DecodeContext, DistinguishedValueEncoder, EmptyState, ForOverwrite, General, @@ -386,8 +396,6 @@ mod utcoffset { use crate::DecodeError; use crate::DecodeErrorKind::InvalidValue; use alloc::vec::Vec; - use core::iter::repeat_with; - use rand::{thread_rng, Rng}; use time::UtcOffset; pub(in super::super) fn test_zones() -> impl Iterator + Clone { @@ -402,12 +410,12 @@ mod utcoffset { #[test] fn check_type() { - let mut rng = thread_rng(); - for zone in test_zones().chain(repeat_with(|| rng.gen()).take(RANDOM_SAMPLES)) { + for zone in with_random_values(test_zones()) { expedient::check_type(zone, 123, WireType::LengthDelimited).unwrap(); distinguished::check_type(zone, 123, WireType::LengthDelimited).unwrap(); } } + check_type_empty!(UtcOffset, via proxy); check_type_empty!(UtcOffset, via distinguished proxy); @@ -499,25 +507,21 @@ delegate_value_encoding!(delegate from (General) to (Proxied) mod offsetdatetime { use super::primitivedatetime::test_datetimes; use super::utcoffset::test_zones; - use super::{odt_compose, RANDOM_SAMPLES}; + use super::{odt_compose, with_random_values}; use crate::encoding::test::{check_type_empty, distinguished, expedient}; use crate::encoding::WireType; - use core::iter::repeat_with; use itertools::iproduct; - use rand::{thread_rng, Rng}; use time::OffsetDateTime; #[test] fn check_type() { - let mut rng = thread_rng(); - for (datetime, zone) in iproduct!(test_datetimes(), test_zones()) - .chain(repeat_with(|| rng.gen()).take(RANDOM_SAMPLES)) - { + for (datetime, zone) in with_random_values(iproduct!(test_datetimes(), test_zones())) { let odt = odt_compose(datetime, zone); expedient::check_type(odt, 123, WireType::LengthDelimited).unwrap(); distinguished::check_type(odt, 123, WireType::LengthDelimited).unwrap(); } } + check_type_empty!(OffsetDateTime, via proxy); check_type_empty!(OffsetDateTime, via distinguished proxy); } @@ -565,11 +569,9 @@ delegate_value_encoding!(delegate from (General) to (Proxied) #[cfg(test)] mod duration { - use super::RANDOM_SAMPLES; use crate::encoding::test::{check_type_empty, distinguished, expedient}; + use crate::encoding::type_support::time::with_random_values; use crate::encoding::{EmptyState, WireType}; - use core::iter::repeat_with; - use rand::{thread_rng, Rng}; use time::Duration; pub(in super::super) fn test_durations() -> impl Iterator + Clone { @@ -586,12 +588,12 @@ mod duration { #[test] fn check_type() { - let mut rng = thread_rng(); - for duration in test_durations().chain(repeat_with(|| rng.gen()).take(RANDOM_SAMPLES)) { + for duration in with_random_values(test_durations()) { expedient::check_type(duration, 123, WireType::LengthDelimited).unwrap(); distinguished::check_type(duration, 123, WireType::LengthDelimited).unwrap(); } } + check_type_empty!(Duration, via proxy); check_type_empty!(Duration, via distinguished proxy); }