diff --git a/benches/parse_str.rs b/benches/parse_str.rs index 0a87d0e02..9bd7bcb0b 100644 --- a/benches/parse_str.rs +++ b/benches/parse_str.rs @@ -4,10 +4,10 @@ extern crate slog; extern crate test; extern crate uuid; -use test::Bencher; -use uuid::Uuid; #[cfg(feature = "slog")] use slog::Drain; +use test::Bencher; +use uuid::Uuid; #[bench] fn bench_parse(b: &mut Bencher) { diff --git a/src/lib.rs b/src/lib.rs index 06ccd3c08..0b1faf292 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -180,6 +180,12 @@ cfg_if! { } } +cfg_if! { + if #[cfg(test)] { + mod test_util; + } +} + /// A 128-bit (16 byte) buffer containing the ID. pub type UuidBytes = [u8; 16]; @@ -1104,32 +1110,29 @@ impl<'a> fmt::Display for Hyphenated<'a> { } macro_rules! hyphenated_write { - ($f:expr, $format:expr, $bytes:expr) => {{ - - let data1 = u32::from($bytes[0]) << 24 | - u32::from($bytes[1]) << 16 | - u32::from($bytes[2]) << 8 | - u32::from($bytes[3]); - - let data2 = u16::from($bytes[4]) << 8 | - u16::from($bytes[5]); - - let data3 = u16::from($bytes[6]) << 8 | - u16::from($bytes[7]); - - write!($f, - $format, - data1, - data2, - data3, - $bytes[8], - $bytes[9], - $bytes[10], - $bytes[11], - $bytes[12], - $bytes[13], - $bytes[14], - $bytes[15]) + ($f: expr, $format: expr, $bytes: expr) => {{ + let data1 = u32::from($bytes[0]) << 24 | u32::from($bytes[1]) << 16 + | u32::from($bytes[2]) << 8 | u32::from($bytes[3]); + + let data2 = u16::from($bytes[4]) << 8 | u16::from($bytes[5]); + + let data3 = u16::from($bytes[6]) << 8 | u16::from($bytes[7]); + + write!( + $f, + $format, + data1, + data2, + data3, + $bytes[8], + $bytes[9], + $bytes[10], + $bytes[11], + $bytes[12], + $bytes[13], + $bytes[14], + $bytes[15] + ) }}; } @@ -1173,20 +1176,14 @@ mod tests { use self::std::prelude::v1::*; + use super::test_util; + use super::{NAMESPACE_X500, NAMESPACE_DNS, NAMESPACE_OID, NAMESPACE_URL}; use super::{Uuid, UuidVariant, UuidVersion}; #[cfg(feature = "slog")] use slog::{self, Drain}; - fn new() -> Uuid { - Uuid::parse_str("F9168C5E-CEB2-4FAA-B6BF-329BF39FA1E4").unwrap() - } - - fn new2() -> Uuid { - Uuid::parse_str("F9168C5E-CEB2-4FAB-B6BF-329BF39FA1E4").unwrap() - } - #[cfg(feature = "v3")] static FIXTURE_V3: &'static [(&'static Uuid, &'static str, &'static str)] = &[ ( @@ -1334,7 +1331,7 @@ mod tests { #[test] fn test_nil() { let nil = Uuid::nil(); - let not_nil = new(); + let not_nil = test_util::new(); assert!(nil.is_nil()); assert!(!not_nil.is_nil()); @@ -1464,7 +1461,7 @@ mod tests { #[test] fn test_get_variant() { - let uuid1 = new(); + let uuid1 = test_util::new(); let uuid2 = Uuid::parse_str("550e8400-e29b-41d4-a716-446655440000").unwrap(); let uuid3 = Uuid::parse_str("67e55044-10b1-426f-9247-bb680e5fe0c8").unwrap(); let uuid4 = Uuid::parse_str("936DA01F9ABD4d9dC0C702AF85C822A8").unwrap(); @@ -1567,7 +1564,7 @@ mod tests { ); // Round-trip - let uuid_orig = new(); + let uuid_orig = test_util::new(); let orig_str = uuid_orig.to_string(); let uuid_out = Uuid::parse_str(&orig_str).unwrap(); assert_eq!(uuid_orig, uuid_out); @@ -1593,7 +1590,7 @@ mod tests { #[test] fn test_to_simple_string() { - let uuid1 = new(); + let uuid1 = test_util::new(); let s = uuid1.simple().to_string(); assert_eq!(s.len(), 32); @@ -1602,7 +1599,7 @@ mod tests { #[test] fn test_to_string() { - let uuid1 = new(); + let uuid1 = test_util::new(); let s = uuid1.to_string(); assert_eq!(s.len(), 36); @@ -1611,7 +1608,7 @@ mod tests { #[test] fn test_display() { - let uuid1 = new(); + let uuid1 = test_util::new(); let s = uuid1.to_string(); assert_eq!(s, uuid1.hyphenated().to_string()); @@ -1619,7 +1616,7 @@ mod tests { #[test] fn test_to_hyphenated_string() { - let uuid1 = new(); + let uuid1 = test_util::new(); let s = uuid1.hyphenated().to_string(); assert!(s.len() == 36); @@ -1631,10 +1628,10 @@ mod tests { use super::fmt::Write; let mut buf = String::new(); - let u = new(); + let u = test_util::new(); macro_rules! check { - ($buf:ident, $format:expr, $target:expr, $len:expr, $cond:expr) => { + ($buf: ident, $format: expr, $target: expr, $len: expr, $cond: expr) => { $buf.clear(); write!($buf, $format, $target).unwrap(); assert!(buf.len() == $len); @@ -1687,7 +1684,7 @@ mod tests { #[test] fn test_to_urn_string() { - let uuid1 = new(); + let uuid1 = test_util::new(); let ss = uuid1.urn().to_string(); let s = &ss[9..]; @@ -1698,7 +1695,7 @@ mod tests { #[test] fn test_to_simple_string_matching() { - let uuid1 = new(); + let uuid1 = test_util::new(); let hs = uuid1.hyphenated().to_string(); let ss = uuid1.simple().to_string(); @@ -1710,7 +1707,7 @@ mod tests { #[test] fn test_string_roundtrip() { - let uuid = new(); + let uuid = test_util::new(); let hs = uuid.hyphenated().to_string(); let uuid_hs = Uuid::parse_str(&hs).unwrap(); @@ -1723,8 +1720,8 @@ mod tests { #[test] fn test_compare() { - let uuid1 = new(); - let uuid2 = new2(); + let uuid1 = test_util::new(); + let uuid2 = test_util::new2(); assert_eq!(uuid1, uuid1); assert_eq!(uuid2, uuid2); @@ -1749,7 +1746,7 @@ mod tests { #[test] fn test_as_fields() { - let u = new(); + let u = test_util::new(); let (d1, d2, d3, d4) = u.as_fields(); assert_ne!(d1, 0); @@ -1803,7 +1800,7 @@ mod tests { #[test] fn test_as_bytes() { - let u = new(); + let u = test_util::new(); let ub = u.as_bytes(); assert_eq!(ub.len(), 16); @@ -1839,9 +1836,9 @@ mod tests { #[test] fn test_operator_eq() { - let u1 = new(); + let u1 = test_util::new(); let u2 = u1.clone(); - let u3 = new2(); + let u3 = test_util::new2(); assert_eq!(u1, u1); assert_eq!(u1, u2); @@ -1856,8 +1853,8 @@ mod tests { #[test] fn test_iterbytes_impl_for_uuid() { let mut set = std::collections::HashSet::new(); - let id1 = new(); - let id2 = new2(); + let id1 = test_util::new(); + let id2 = test_util::new2(); set.insert(id1.clone()); assert!(set.contains(&id1)); @@ -1868,7 +1865,7 @@ mod tests { #[test] fn test_slog_kv() { let root = slog::Logger::root(slog::Discard.fuse(), o!()); - let u1 = new(); + let u1 = test_util::new(); crit!(root, "test"; "u1" => u1); } } diff --git a/src/test_util.rs b/src/test_util.rs new file mode 100644 index 000000000..b103a6925 --- /dev/null +++ b/src/test_util.rs @@ -0,0 +1,19 @@ +use Uuid; + +pub fn new() -> Uuid { + Uuid { + bytes: [ + 0xF9, 0x16, 0x8C, 0x5E, 0xCE, 0xB2, 0x4F, 0xAA, 0xB6, 0xBF, 0x32, 0x9B, 0xF3, 0x9F, + 0xA1, 0xE4, + ], + } +} + +pub fn new2() -> Uuid { + Uuid { + bytes: [ + 0xF9, 0x16, 0x8C, 0x5E, 0xCE, 0xB2, 0x4F, 0xAB, 0xB6, 0xBF, 0x32, 0x9B, 0xF3, 0x9F, + 0xA1, 0xE4, + ], + } +} \ No newline at end of file