Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

372: Implement Eq and Hash for socket2::SockAddr #374

Merged
merged 5 commits into from
Jan 8, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,7 @@ features = [
[features]
# Enable all API, even ones not available on all OSs.
all = []

[dev-dependencies]
Thomasdezeeuw marked this conversation as resolved.
Show resolved Hide resolved
quickcheck = "1.0.3"
quickcheck_macros = "1.0.0"
6 changes: 6 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@
// Disallow warnings in examples.
#![doc(test(attr(deny(warnings))))]

#[cfg(test)]
extern crate quickcheck;
#[cfg(test)]
#[macro_use(quickcheck)]
extern crate quickcheck_macros;
Thomasdezeeuw marked this conversation as resolved.
Show resolved Hide resolved

use std::fmt;
use std::mem::MaybeUninit;
use std::net::SocketAddr;
Expand Down
41 changes: 41 additions & 0 deletions src/sockaddr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,13 @@ unsafe fn any_as_u8_slice<T: Sized>(p: &T, size: usize) -> &[u8] {
#[cfg(test)]
mod tests {
use super::*;
use quickcheck::{self, Arbitrary};

impl Arbitrary for SockAddr {
fn arbitrary(g: &mut quickcheck::Gen) -> Self {
SockAddr::from(std::net::SocketAddr::arbitrary(g))
}
}

#[test]
fn ipv4() {
Expand Down Expand Up @@ -491,6 +498,40 @@ mod tests {
assert!(b != a0);
}

#[allow(clippy::eq_op)] // allow x == x
#[quickcheck]
fn reflexive_eq(x: SockAddr) -> bool {
x == x
}

#[quickcheck]
fn symmetric_eq(x: SockAddr, y: SockAddr) -> bool {
(x == y) == (y == x)
}

#[quickcheck]
fn transitive_eq(x: SockAddr, y: SockAddr, z: SockAddr) -> bool {
if x == y && y == z {
x == z
} else {
true
}
}

#[quickcheck]
fn consistent_eq(x: SockAddr, y: SockAddr) -> bool {
(x == y) == (x == y)
}

#[quickcheck]
fn hash(x: SockAddr, y: SockAddr) -> bool {
if x == y {
calculate_hash(&x) == calculate_hash(&y)
} else {
true
}
}

fn test_hash(a0: SockAddr, a1: SockAddr, b: SockAddr) {
assert!(calculate_hash(&a0) == calculate_hash(&a0));
assert!(calculate_hash(&a0) == calculate_hash(&a1));
Expand Down