use str_edit_distance::{ damerau_levenshtein, dice_coefficient, hamming, jaro_winkler, levenshtein, }; #[test] fn test_levenshtein() { assert_eq!(levenshtein("kitten", "sitting").unwrap(), 3); assert_eq!(levenshtein("flaw", "lawn").unwrap(), 2); assert_eq!(levenshtein("", "").unwrap(), 0); assert_eq!(levenshtein("a", "a").unwrap(), 0); assert_eq!(levenshtein("a", "").unwrap(), 1); } #[test] fn test_damerau_levenshtein() { assert_eq!(damerau_levenshtein("kitten", "sitting").unwrap(), 3); assert_eq!(damerau_levenshtein("flaw", "lawn").unwrap(), 2); assert_eq!(damerau_levenshtein("", "").unwrap(), 0); assert_eq!(damerau_levenshtein("a", "a").unwrap(), 0); assert_eq!(damerau_levenshtein("a", "").unwrap(), 1); assert_eq!(damerau_levenshtein("ca", "abc").unwrap(), 3); } #[test] fn test_hamming() { assert_eq!(hamming("karolin", "kathrin").unwrap(), 3); assert_eq!(hamming("karolin", "kerstin").unwrap(), 3); assert_eq!(hamming("1011101", "1001001").unwrap(), 2); assert_eq!(hamming("2173896", "2233796").unwrap(), 3); } #[test] fn test_hamming_distance_unequal_length() { assert!(hamming("karolin", "kathrine").is_err()); } #[test] fn test_jaro_winkler() { assert!((jaro_winkler("martha", "marhta").unwrap() - 0.961).abs() < 0.001); assert!((jaro_winkler("dwayne", "duane").unwrap() - 0.84).abs() < 0.001); assert!((jaro_winkler("dixon", "dicksonx").unwrap() - 0.813).abs() < 0.001); assert!((jaro_winkler("", "").unwrap() - 1.0).abs() < 0.001); } #[test] fn test_dice_coefficient() { assert!((dice_coefficient("night", "nacht").unwrap() - 0.25).abs() < 0.001); assert!((dice_coefficient("context", "contact").unwrap() - 0.5).abs() < 0.001); assert!((dice_coefficient("night", "night").unwrap() - 1.0).abs() < 0.001); assert!((dice_coefficient("", "").unwrap() - 1.0).abs() < 0.001); assert!((dice_coefficient("night", "").unwrap() - 0.0).abs() < 0.001); assert!((dice_coefficient("", "nacht").unwrap() - 0.0).abs() < 0.001); }