Debian Codegraph @

debian/rust-vendor/time/tests/integration/rand.rs

raw
1 use std::marker::PhantomData;
2
3 use rstest::rstest;
4 use time::{Date, Month, OffsetDateTime, PlainDateTime, SignedDuration, Time, UtcOffset, Weekday};
5
6 #[rstest]
7 #[case(7, PhantomData::<Weekday>)]
8 #[case(12, PhantomData::<Month>)]
9 #[case(1, PhantomData::<Time>)]
10 #[case(1, PhantomData::<Date>)]
11 #[case(1, PhantomData::<UtcOffset>)]
12 #[case(1, PhantomData::<PlainDateTime>)]
13 #[case(1, PhantomData::<OffsetDateTime>)]
14 #[case(1, PhantomData::<SignedDuration>)]
15 fn support08<T>(#[case] iterations: usize, #[case] _type: PhantomData<T>)
16 where
17 rand08::distributions::Standard: rand08::distributions::Distribution<T>,
18 {
19 use rand08::Rng as _;
20
21 // Work around rust-random/rand#1020.
22 let mut rng = rand08::rngs::mock::StepRng::new(0, 2_505_397_590);
23
24 for _ in 0..iterations {
25 drop(rng.r#gen::<T>());
26 }
27 }
28
29 #[rstest]
30 #[case(7, PhantomData::<Weekday>)]
31 #[case(12, PhantomData::<Month>)]
32 #[case(1, PhantomData::<Time>)]
33 #[case(1, PhantomData::<Date>)]
34 #[case(1, PhantomData::<UtcOffset>)]
35 #[case(1, PhantomData::<PlainDateTime>)]
36 #[case(1, PhantomData::<OffsetDateTime>)]
37 #[case(1, PhantomData::<SignedDuration>)]
38 fn support09<T>(#[case] iterations: usize, #[case] _type: PhantomData<T>)
39 where
40 rand09::distr::StandardUniform: rand09::distr::Distribution<T>,
41 {
42 use rand09::Rng as _;
43
44 // Work around rust-random/rand#1020.
45 let mut rng = StepRng::new(0, 2_505_397_590);
46
47 for _ in 0..iterations {
48 drop(rng.random::<T>());
49 }
50 }
51
52 #[rstest]
53 #[case(7, PhantomData::<Weekday>)]
54 #[case(12, PhantomData::<Month>)]
55 #[case(1, PhantomData::<Time>)]
56 #[case(1, PhantomData::<Date>)]
57 #[case(1, PhantomData::<UtcOffset>)]
58 #[case(1, PhantomData::<PlainDateTime>)]
59 #[case(1, PhantomData::<OffsetDateTime>)]
60 #[case(1, PhantomData::<SignedDuration>)]
61 fn support010<T>(#[case] iterations: usize, #[case] _type: PhantomData<T>)
62 where
63 rand010::distr::StandardUniform: rand010::distr::Distribution<T>,
64 {
65 use rand010::RngExt as _;
66
67 // Work around rust-random/rand#1020.
68 let mut rng = StepRng::new(0, 2_505_397_590);
69
70 for _ in 0..iterations {
71 drop(rng.random::<T>());
72 }
73 }
74
75 // copy of `StepRng` from rand 0.8 to avoid deprecation warnings
76 #[derive(Debug, Clone)]
77 struct StepRng {
78 v: u64,
79 a: u64,
80 }
81
82 impl StepRng {
83 fn new(initial: u64, increment: u64) -> Self {
84 Self {
85 v: initial,
86 a: increment,
87 }
88 }
89 }
90
91 impl rand09::RngCore for StepRng {
92 fn next_u32(&mut self) -> u32 {
93 self.next_u64() as u32
94 }
95
96 fn next_u64(&mut self) -> u64 {
97 let res = self.v;
98 self.v = self.v.wrapping_add(self.a);
99 res
100 }
101
102 fn fill_bytes(&mut self, dst: &mut [u8]) {
103 rand09::rand_core::impls::fill_bytes_via_next(self, dst)
104 }
105 }
106
107 impl rand010::TryRng for StepRng {
108 type Error = core::convert::Infallible;
109
110 fn try_next_u32(&mut self) -> Result<u32, Self::Error> {
111 self.try_next_u64().map(|v| v as u32)
112 }
113
114 fn try_next_u64(&mut self) -> Result<u64, Self::Error> {
115 let res = self.v;
116 self.v = self.v.wrapping_add(self.a);
117 Ok(res)
118 }
119
120 #[expect(clippy::unimplemented)]
121 fn try_fill_bytes(&mut self, _dst: &mut [u8]) -> Result<(), Self::Error> {
122 unimplemented!("not used in testing, so not implemented");
123 }
124 }