Skip to content

Commit

Permalink
Rollup merge of rust-lang#76299 - CDirkx:ip-tests, r=matklad
Browse files Browse the repository at this point in the history
Make `Ipv4Addr` and `Ipv6Addr` const tests unit tests under `library`

These tests are about the standard library, not the compiler itself, thus should live in `library`, see rust-lang#76268.
  • Loading branch information
matklad committed Sep 4, 2020
2 parents ca0210b + a2e077e commit 0ca512e
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 111 deletions.
109 changes: 109 additions & 0 deletions library/std/src/net/ip/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -809,3 +809,112 @@ fn is_v6() {
assert!(!ip.is_ipv4());
assert!(ip.is_ipv6());
}

#[test]
fn ipv4_const() {
// test that the methods of `Ipv4Addr` are usable in a const context

const IP_ADDRESS: Ipv4Addr = Ipv4Addr::new(127, 0, 0, 1);
assert_eq!(IP_ADDRESS, Ipv4Addr::LOCALHOST);

const OCTETS: [u8; 4] = IP_ADDRESS.octets();
assert_eq!(OCTETS, [127, 0, 0, 1]);

const IS_UNSPECIFIED: bool = IP_ADDRESS.is_unspecified();
assert!(!IS_UNSPECIFIED);

const IS_LOOPBACK: bool = IP_ADDRESS.is_loopback();
assert!(IS_LOOPBACK);

const IS_PRIVATE: bool = IP_ADDRESS.is_private();
assert!(!IS_PRIVATE);

const IS_LINK_LOCAL: bool = IP_ADDRESS.is_link_local();
assert!(!IS_LINK_LOCAL);

const IS_GLOBAL: bool = IP_ADDRESS.is_global();
assert!(!IS_GLOBAL);

const IS_SHARED: bool = IP_ADDRESS.is_shared();
assert!(!IS_SHARED);

const IS_IETF_PROTOCOL_ASSIGNMENT: bool = IP_ADDRESS.is_ietf_protocol_assignment();
assert!(!IS_IETF_PROTOCOL_ASSIGNMENT);

const IS_BENCHMARKING: bool = IP_ADDRESS.is_benchmarking();
assert!(!IS_BENCHMARKING);

const IS_RESERVED: bool = IP_ADDRESS.is_reserved();
assert!(!IS_RESERVED);

const IS_MULTICAST: bool = IP_ADDRESS.is_multicast();
assert!(!IS_MULTICAST);

const IS_BROADCAST: bool = IP_ADDRESS.is_broadcast();
assert!(!IS_BROADCAST);

const IS_DOCUMENTATION: bool = IP_ADDRESS.is_documentation();
assert!(!IS_DOCUMENTATION);

const IP_V6_COMPATIBLE: Ipv6Addr = IP_ADDRESS.to_ipv6_compatible();
assert_eq!(
IP_V6_COMPATIBLE,
Ipv6Addr::from([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 127, 0, 0, 1])
);

const IP_V6_MAPPED: Ipv6Addr = IP_ADDRESS.to_ipv6_mapped();
assert_eq!(
IP_V6_MAPPED,
Ipv6Addr::from([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 255, 127, 0, 0, 1])
);
}

#[test]
fn ipv6_const() {
// test that the methods of `Ipv6Addr` are usable in a const context

const IP_ADDRESS: Ipv6Addr = Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1);
assert_eq!(IP_ADDRESS, Ipv6Addr::LOCALHOST);

const SEGMENTS: [u16; 8] = IP_ADDRESS.segments();
assert_eq!(SEGMENTS, [0, 0, 0, 0, 0, 0, 0, 1]);

const OCTETS: [u8; 16] = IP_ADDRESS.octets();
assert_eq!(OCTETS, [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]);

const IS_UNSPECIFIED: bool = IP_ADDRESS.is_unspecified();
assert!(!IS_UNSPECIFIED);

const IS_LOOPBACK: bool = IP_ADDRESS.is_loopback();
assert!(IS_LOOPBACK);

const IS_GLOBAL: bool = IP_ADDRESS.is_global();
assert!(!IS_GLOBAL);

const IS_UNIQUE_LOCAL: bool = IP_ADDRESS.is_unique_local();
assert!(!IS_UNIQUE_LOCAL);

const IS_UNICAST_LINK_LOCAL_STRICT: bool = IP_ADDRESS.is_unicast_link_local_strict();
assert!(!IS_UNICAST_LINK_LOCAL_STRICT);

const IS_UNICAST_LINK_LOCAL: bool = IP_ADDRESS.is_unicast_link_local();
assert!(!IS_UNICAST_LINK_LOCAL);

const IS_UNICAST_SITE_LOCAL: bool = IP_ADDRESS.is_unicast_site_local();
assert!(!IS_UNICAST_SITE_LOCAL);

const IS_DOCUMENTATION: bool = IP_ADDRESS.is_documentation();
assert!(!IS_DOCUMENTATION);

const IS_UNICAST_GLOBAL: bool = IP_ADDRESS.is_unicast_global();
assert!(!IS_UNICAST_GLOBAL);

const MULTICAST_SCOPE: Option<Ipv6MulticastScope> = IP_ADDRESS.multicast_scope();
assert_eq!(MULTICAST_SCOPE, None);

const IS_MULTICAST: bool = IP_ADDRESS.is_multicast();
assert!(!IS_MULTICAST);

const IP_V4: Option<Ipv4Addr> = IP_ADDRESS.to_ipv4();
assert_eq!(IP_V4.unwrap(), Ipv4Addr::new(0, 0, 0, 1));
}
58 changes: 0 additions & 58 deletions src/test/ui/consts/std/net/ipv4.rs

This file was deleted.

53 changes: 0 additions & 53 deletions src/test/ui/consts/std/net/ipv6.rs

This file was deleted.

0 comments on commit 0ca512e

Please sign in to comment.