use super::*; /// The same id always gives the same ref, and two ids may differ by one /// character yet get unrelated refs. #[test] fn the_same_id_gives_the_same_ref() { let id = "0193f2a4-7c1e-7b2d-9f00-3a4b5c6d7e8f"; assert_eq!(short_ref(id), short_ref(id)); assert_eq!(short_ref(id).as_str().len(), LENGTH); assert_ne!(short_ref(id), short_ref("0193f2a4-7c1e-7b2d-9f00-3a4b5c6d7e8e")); } /// The alphabet is Crockford's: 32 distinct characters, digits and /// capitals, no I, L, O or U, and every ref is drawn from it alone. #[test] fn the_alphabet_is_pure_crockford() { let alphabet: std::collections::BTreeSet = ALPHABET.iter().copied().collect(); assert_eq!(alphabet.len(), 32); assert!(ALPHABET.iter().all(|c| c.is_ascii_digit() || c.is_ascii_uppercase())); assert!(!ALPHABET.iter().any(|c| b"ILOU".contains(c))); for n in 0..2_000 { let said = short_ref(&format!("synthetic-{n}")); assert!(said.as_str().bytes().all(|c| alphabet.contains(&c)), "{said:?}"); } } /// Across 10,000 synthetic ids, collisions stay near what twenty bits /// promise (about 48 pairs); four decimal digits read off the id would give /// thousands. Run with --nocapture to read the count. #[test] fn ten_thousand_ids_rarely_share_a_ref() { let mut seen = std::collections::HashMap::new(); let mut collisions = 0; for n in 0..10_000_u32 { let id = format!("{:08x}-{:04x}-7{:03x}-8{:03x}-{:012x}", n.wrapping_mul(2_654_435_761), n % 65_536, n % 4_096, (n * 7) % 4_096, u64::from(n) * 1_000_003); let count = seen.entry(short_ref(&id)).or_insert(0); collisions += *count; *count += 1; } println!("short_ref collisions across 10k synthetic ids: {collisions}"); assert!(collisions < 120, "{collisions} collisions in 10k ids"); }