Skip to content

Commit 3c25727

Browse files
committed
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.
1 parent 803274f commit 3c25727

File tree

1 file changed

+109
-0
lines changed

1 file changed

+109
-0
lines changed

std/src/net/ip/tests.rs

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -809,3 +809,112 @@ fn is_v6() {
809809
assert!(!ip.is_ipv4());
810810
assert!(ip.is_ipv6());
811811
}
812+
813+
#[test]
814+
fn ipv4_const() {
815+
// test that the methods of `Ipv4Addr` are usable in a const context
816+
817+
const IP_ADDRESS: Ipv4Addr = Ipv4Addr::new(127, 0, 0, 1);
818+
assert_eq!(IP_ADDRESS, Ipv4Addr::LOCALHOST);
819+
820+
const OCTETS: [u8; 4] = IP_ADDRESS.octets();
821+
assert_eq!(OCTETS, [127, 0, 0, 1]);
822+
823+
const IS_UNSPECIFIED: bool = IP_ADDRESS.is_unspecified();
824+
assert!(!IS_UNSPECIFIED);
825+
826+
const IS_LOOPBACK: bool = IP_ADDRESS.is_loopback();
827+
assert!(IS_LOOPBACK);
828+
829+
const IS_PRIVATE: bool = IP_ADDRESS.is_private();
830+
assert!(!IS_PRIVATE);
831+
832+
const IS_LINK_LOCAL: bool = IP_ADDRESS.is_link_local();
833+
assert!(!IS_LINK_LOCAL);
834+
835+
const IS_GLOBAL: bool = IP_ADDRESS.is_global();
836+
assert!(!IS_GLOBAL);
837+
838+
const IS_SHARED: bool = IP_ADDRESS.is_shared();
839+
assert!(!IS_SHARED);
840+
841+
const IS_IETF_PROTOCOL_ASSIGNMENT: bool = IP_ADDRESS.is_ietf_protocol_assignment();
842+
assert!(!IS_IETF_PROTOCOL_ASSIGNMENT);
843+
844+
const IS_BENCHMARKING: bool = IP_ADDRESS.is_benchmarking();
845+
assert!(!IS_BENCHMARKING);
846+
847+
const IS_RESERVED: bool = IP_ADDRESS.is_reserved();
848+
assert!(!IS_RESERVED);
849+
850+
const IS_MULTICAST: bool = IP_ADDRESS.is_multicast();
851+
assert!(!IS_MULTICAST);
852+
853+
const IS_BROADCAST: bool = IP_ADDRESS.is_broadcast();
854+
assert!(!IS_BROADCAST);
855+
856+
const IS_DOCUMENTATION: bool = IP_ADDRESS.is_documentation();
857+
assert!(!IS_DOCUMENTATION);
858+
859+
const IP_V6_COMPATIBLE: Ipv6Addr = IP_ADDRESS.to_ipv6_compatible();
860+
assert_eq!(
861+
IP_V6_COMPATIBLE,
862+
Ipv6Addr::from([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 127, 0, 0, 1])
863+
);
864+
865+
const IP_V6_MAPPED: Ipv6Addr = IP_ADDRESS.to_ipv6_mapped();
866+
assert_eq!(
867+
IP_V6_MAPPED,
868+
Ipv6Addr::from([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 255, 127, 0, 0, 1])
869+
);
870+
}
871+
872+
#[test]
873+
fn ipv6_const() {
874+
// test that the methods of `Ipv6Addr` are usable in a const context
875+
876+
const IP_ADDRESS: Ipv6Addr = Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1);
877+
assert_eq!(IP_ADDRESS, Ipv6Addr::LOCALHOST);
878+
879+
const SEGMENTS: [u16; 8] = IP_ADDRESS.segments();
880+
assert_eq!(SEGMENTS, [0, 0, 0, 0, 0, 0, 0, 1]);
881+
882+
const OCTETS: [u8; 16] = IP_ADDRESS.octets();
883+
assert_eq!(OCTETS, [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]);
884+
885+
const IS_UNSPECIFIED: bool = IP_ADDRESS.is_unspecified();
886+
assert!(!IS_UNSPECIFIED);
887+
888+
const IS_LOOPBACK: bool = IP_ADDRESS.is_loopback();
889+
assert!(IS_LOOPBACK);
890+
891+
const IS_GLOBAL: bool = IP_ADDRESS.is_global();
892+
assert!(!IS_GLOBAL);
893+
894+
const IS_UNIQUE_LOCAL: bool = IP_ADDRESS.is_unique_local();
895+
assert!(!IS_UNIQUE_LOCAL);
896+
897+
const IS_UNICAST_LINK_LOCAL_STRICT: bool = IP_ADDRESS.is_unicast_link_local_strict();
898+
assert!(!IS_UNICAST_LINK_LOCAL_STRICT);
899+
900+
const IS_UNICAST_LINK_LOCAL: bool = IP_ADDRESS.is_unicast_link_local();
901+
assert!(!IS_UNICAST_LINK_LOCAL);
902+
903+
const IS_UNICAST_SITE_LOCAL: bool = IP_ADDRESS.is_unicast_site_local();
904+
assert!(!IS_UNICAST_SITE_LOCAL);
905+
906+
const IS_DOCUMENTATION: bool = IP_ADDRESS.is_documentation();
907+
assert!(!IS_DOCUMENTATION);
908+
909+
const IS_UNICAST_GLOBAL: bool = IP_ADDRESS.is_unicast_global();
910+
assert!(!IS_UNICAST_GLOBAL);
911+
912+
const MULTICAST_SCOPE: Option<Ipv6MulticastScope> = IP_ADDRESS.multicast_scope();
913+
assert_eq!(MULTICAST_SCOPE, None);
914+
915+
const IS_MULTICAST: bool = IP_ADDRESS.is_multicast();
916+
assert!(!IS_MULTICAST);
917+
918+
const IP_V4: Option<Ipv4Addr> = IP_ADDRESS.to_ipv4();
919+
assert_eq!(IP_V4.unwrap(), Ipv4Addr::new(0, 0, 0, 1));
920+
}

0 commit comments

Comments
 (0)