Skip to content

Commit 5bd2f93

Browse files
committed
Use proptest instead of quickcheck
1 parent 95b1af3 commit 5bd2f93

File tree

2 files changed

+52
-61
lines changed

2 files changed

+52
-61
lines changed

Cargo.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,7 @@ thiserror = "1.0"
4141

4242
[dev-dependencies]
4343
hex-literal = "0.4.1"
44-
quickcheck = "1"
45-
quickcheck_macros = "1"
44+
proptest = "1.3.1"
4645

4746
[package.metadata.docs.rs]
4847
all-features = true

src/dht/node.rs

Lines changed: 51 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -530,85 +530,77 @@ impl IdAllocator for Ipv6Addr {
530530
}
531531
}
532532

533-
#[cfg(test)]
534-
impl quickcheck::Arbitrary for Id {
535-
fn arbitrary(g: &mut quickcheck::Gen) -> Id {
536-
Id::from([
537-
u8::arbitrary(g),
538-
u8::arbitrary(g),
539-
u8::arbitrary(g),
540-
u8::arbitrary(g),
541-
u8::arbitrary(g),
542-
u8::arbitrary(g),
543-
u8::arbitrary(g),
544-
u8::arbitrary(g),
545-
u8::arbitrary(g),
546-
u8::arbitrary(g),
547-
u8::arbitrary(g),
548-
u8::arbitrary(g),
549-
u8::arbitrary(g),
550-
u8::arbitrary(g),
551-
u8::arbitrary(g),
552-
u8::arbitrary(g),
553-
u8::arbitrary(g),
554-
u8::arbitrary(g),
555-
u8::arbitrary(g),
556-
u8::arbitrary(g),
557-
])
558-
}
559-
}
560-
561533
#[cfg(test)]
562534
mod test {
563535
use super::*;
564-
use quickcheck_macros::quickcheck;
565536

566537
#[cfg(all(feature = "alloc", not(feature = "std")))]
567538
use alloc::format;
568-
569539
#[cfg(feature = "std")]
570540
use std::format;
571541

542+
#[cfg(feature = "std")]
543+
use proptest::prelude::*;
544+
545+
#[cfg(feature = "std")]
546+
fn arb_id() -> impl Strategy<Value = Id> {
547+
proptest::array::uniform20(any::<u8>()).prop_map(Id)
548+
}
549+
572550
#[test]
573551
fn test_debug() {
574552
let node_id = Id::max();
575553
let debug_str = format!("{node_id:?}");
576554
assert_eq!(debug_str, "Id(FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF)");
577555
}
578556

579-
#[quickcheck]
580-
fn id_distance_commutative(id1: Id, id2: Id) -> bool {
581-
id1.distance(id2) == id2.distance(id1)
582-
}
583-
584557
#[cfg(feature = "std")]
585-
#[quickcheck]
586-
fn make_only_valid_node_ids_for_ipv4(ip: Ipv4Addr, rand: Option<u8>) -> bool {
587-
ip.is_valid(ip.rand_id(rand, &mut rand::thread_rng()).unwrap())
588-
}
558+
proptest! {
559+
#[allow(clippy::ignored_unit_patterns)]
560+
#[test]
561+
fn id_distance_commutative(
562+
id1 in arb_id(),
563+
id2 in arb_id()
564+
) {
565+
assert_eq!(id1.distance(id2), id2.distance(id1));
566+
}
589567

590-
#[cfg(feature = "std")]
591-
#[quickcheck]
592-
fn make_only_valid_node_ids_for_ipv6(ip: Ipv6Addr, rand: Option<u8>) -> bool {
593-
ip.is_valid(ip.rand_id(rand, &mut rand::thread_rng()).unwrap())
594-
}
595-
596-
#[quickcheck]
597-
#[allow(clippy::needless_pass_by_value)]
598-
fn id_try_from_slice(values: Vec<u8>) -> bool {
599-
if values.len() == 20 {
600-
Id::try_from(values.as_slice()).is_ok()
601-
} else {
602-
Id::try_from(values.as_slice()).is_err()
568+
#[allow(clippy::ignored_unit_patterns)]
569+
#[test]
570+
fn make_only_valid_node_ids_for_ipv4(ip in any::<Ipv4Addr>(), rand in any::<Option<u8>>()) {
571+
assert!(ip.is_valid(ip.rand_id(rand, &mut rand::thread_rng()).unwrap()));
603572
}
604-
}
605573

606-
#[quickcheck]
607-
fn id_cmp(id1: Id, id2: Id) -> bool {
608-
match id1.cmp(&id2) {
609-
Ordering::Equal => id2.cmp(&id1) == Ordering::Equal,
610-
Ordering::Less => id2.cmp(&id1) == Ordering::Greater,
611-
Ordering::Greater => id2.cmp(&id1) == Ordering::Less,
574+
#[allow(clippy::ignored_unit_patterns)]
575+
#[test]
576+
fn make_only_valid_node_ids_for_ipv6(ip in any::<Ipv6Addr>(), rand in any::<Option<u8>>()) {
577+
assert!(ip.is_valid(ip.rand_id(rand, &mut rand::thread_rng()).unwrap()));
578+
}
579+
580+
#[allow(clippy::ignored_unit_patterns)]
581+
#[test]
582+
fn id_try_from_slice(values in any::<Vec<u8>>()) {
583+
if values.len() == 20 {
584+
assert!(Id::try_from(values.as_slice()).is_ok());
585+
} else {
586+
assert!(Id::try_from(values.as_slice()).is_err());
587+
}
588+
}
589+
590+
#[allow(clippy::ignored_unit_patterns)]
591+
#[test]
592+
fn id_cmp(id1 in arb_id(), id2 in arb_id()) {
593+
match id1.cmp(&id2) {
594+
Ordering::Equal => {
595+
assert_eq!(id2.cmp(&id1), Ordering::Equal);
596+
}
597+
Ordering::Less => {
598+
assert_eq!(id2.cmp(&id1) , Ordering::Greater);
599+
}
600+
Ordering::Greater => {
601+
assert_eq!(id2.cmp(&id1) , Ordering::Less);
602+
}
603+
}
612604
}
613605
}
614606

0 commit comments

Comments
 (0)