Skip to content

Commit

Permalink
tests/lib: Only use Arbitrary trait for randomness
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
mxinden committed Jun 16, 2021
1 parent 9ea8ac6 commit ebef2f0
Showing 1 changed file with 21 additions and 15 deletions.
36 changes: 21 additions & 15 deletions tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@ use data_encoding::HEXUPPER;
use multihash::Multihash;
use multiaddr::*;
use quickcheck::{Arbitrary, Gen, QuickCheck};
use rand::Rng;
use std::{
borrow::Cow,
convert::TryFrom,
iter::FromIterator,
convert::{TryFrom, TryInto},
iter::{FromIterator, self},
net::{Ipv4Addr, Ipv6Addr},
str::FromStr
};
Expand Down Expand Up @@ -87,8 +86,8 @@ struct Proto(Protocol<'static>);
impl Arbitrary for Proto {
fn arbitrary<G: Gen>(g: &mut G) -> Self {
use Protocol::*;
match g.gen_range(0, 25) { // TODO: Add Protocol::Quic
0 => Proto(Dccp(g.gen())),
match u8::arbitrary(g) % 25 { // TODO: Add Protocol::Quic
0 => Proto(Dccp(Arbitrary::arbitrary(g))),
1 => Proto(Dns(Cow::Owned(SubString::arbitrary(g).0))),
2 => Proto(Dns4(Cow::Owned(SubString::arbitrary(g).0))),
3 => Proto(Dns6(Cow::Owned(SubString::arbitrary(g).0))),
Expand All @@ -99,28 +98,35 @@ impl Arbitrary for Proto {
8 => Proto(P2pWebRtcDirect),
9 => Proto(P2pWebRtcStar),
10 => Proto(P2pWebSocketStar),
11 => Proto(Memory(g.gen())),
11 => Proto(Memory(Arbitrary::arbitrary(g))),
// TODO: impl Arbitrary for Multihash:
12 => Proto(P2p(multihash("QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC"))),
13 => Proto(P2pCircuit),
14 => Proto(Quic),
15 => Proto(Sctp(g.gen())),
16 => Proto(Tcp(g.gen())),
17 => Proto(Udp(g.gen())),
15 => Proto(Sctp(Arbitrary::arbitrary(g))),
16 => Proto(Tcp(Arbitrary::arbitrary(g))),
17 => Proto(Udp(Arbitrary::arbitrary(g))),
18 => Proto(Udt),
19 => Proto(Unix(Cow::Owned(SubString::arbitrary(g).0))),
20 => Proto(Utp),
21 => Proto(Ws("/".into())),
22 => Proto(Wss("/".into())),
23 => {
let mut a = [0; 10];
g.fill(&mut a);
Proto(Onion(Cow::Owned(a), g.gen_range(1, std::u16::MAX)))

let a = iter::repeat_with(|| u8::arbitrary(g))
.take(10)
.collect::<Vec<_>>()
.try_into()
.unwrap();
Proto(Onion(Cow::Owned(a), std::cmp::max(1, u16::arbitrary(g))))
},
24 => {
let mut a = [0; 35];
g.fill_bytes(&mut a);
Proto(Onion3((a, g.gen_range(1, std::u16::MAX)).into()))
let a: [u8;35] = iter::repeat_with(|| u8::arbitrary(g))
.take(35)
.collect::<Vec<_>>()
.try_into()
.unwrap();
Proto(Onion3((a, std::cmp::max(1, u16::arbitrary(g))).into()))
},
_ => panic!("outside range")
}
Expand Down

0 comments on commit ebef2f0

Please sign in to comment.