Skip to content

Commit ebef2f0

Browse files
committed
tests/lib: Only use Arbitrary trait for randomness
Pre quickcheck v1.0 rand and quickcheck had to be updated in lock-step, given that the latter makes use of traits of the former. This commit decouples the two, only depending on quickcheck directly for randomness. This will ease the transisition to quickcheck 1.0, see BurntSushi/quickcheck#241.
1 parent 9ea8ac6 commit ebef2f0

File tree

1 file changed

+21
-15
lines changed

1 file changed

+21
-15
lines changed

tests/lib.rs

+21-15
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,10 @@ use data_encoding::HEXUPPER;
22
use multihash::Multihash;
33
use multiaddr::*;
44
use quickcheck::{Arbitrary, Gen, QuickCheck};
5-
use rand::Rng;
65
use std::{
76
borrow::Cow,
8-
convert::TryFrom,
9-
iter::FromIterator,
7+
convert::{TryFrom, TryInto},
8+
iter::{FromIterator, self},
109
net::{Ipv4Addr, Ipv6Addr},
1110
str::FromStr
1211
};
@@ -87,8 +86,8 @@ struct Proto(Protocol<'static>);
8786
impl Arbitrary for Proto {
8887
fn arbitrary<G: Gen>(g: &mut G) -> Self {
8988
use Protocol::*;
90-
match g.gen_range(0, 25) { // TODO: Add Protocol::Quic
91-
0 => Proto(Dccp(g.gen())),
89+
match u8::arbitrary(g) % 25 { // TODO: Add Protocol::Quic
90+
0 => Proto(Dccp(Arbitrary::arbitrary(g))),
9291
1 => Proto(Dns(Cow::Owned(SubString::arbitrary(g).0))),
9392
2 => Proto(Dns4(Cow::Owned(SubString::arbitrary(g).0))),
9493
3 => Proto(Dns6(Cow::Owned(SubString::arbitrary(g).0))),
@@ -99,28 +98,35 @@ impl Arbitrary for Proto {
9998
8 => Proto(P2pWebRtcDirect),
10099
9 => Proto(P2pWebRtcStar),
101100
10 => Proto(P2pWebSocketStar),
102-
11 => Proto(Memory(g.gen())),
101+
11 => Proto(Memory(Arbitrary::arbitrary(g))),
103102
// TODO: impl Arbitrary for Multihash:
104103
12 => Proto(P2p(multihash("QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC"))),
105104
13 => Proto(P2pCircuit),
106105
14 => Proto(Quic),
107-
15 => Proto(Sctp(g.gen())),
108-
16 => Proto(Tcp(g.gen())),
109-
17 => Proto(Udp(g.gen())),
106+
15 => Proto(Sctp(Arbitrary::arbitrary(g))),
107+
16 => Proto(Tcp(Arbitrary::arbitrary(g))),
108+
17 => Proto(Udp(Arbitrary::arbitrary(g))),
110109
18 => Proto(Udt),
111110
19 => Proto(Unix(Cow::Owned(SubString::arbitrary(g).0))),
112111
20 => Proto(Utp),
113112
21 => Proto(Ws("/".into())),
114113
22 => Proto(Wss("/".into())),
115114
23 => {
116-
let mut a = [0; 10];
117-
g.fill(&mut a);
118-
Proto(Onion(Cow::Owned(a), g.gen_range(1, std::u16::MAX)))
115+
116+
let a = iter::repeat_with(|| u8::arbitrary(g))
117+
.take(10)
118+
.collect::<Vec<_>>()
119+
.try_into()
120+
.unwrap();
121+
Proto(Onion(Cow::Owned(a), std::cmp::max(1, u16::arbitrary(g))))
119122
},
120123
24 => {
121-
let mut a = [0; 35];
122-
g.fill_bytes(&mut a);
123-
Proto(Onion3((a, g.gen_range(1, std::u16::MAX)).into()))
124+
let a: [u8;35] = iter::repeat_with(|| u8::arbitrary(g))
125+
.take(35)
126+
.collect::<Vec<_>>()
127+
.try_into()
128+
.unwrap();
129+
Proto(Onion3((a, std::cmp::max(1, u16::arbitrary(g))).into()))
124130
},
125131
_ => panic!("outside range")
126132
}

0 commit comments

Comments
 (0)