summaryrefslogtreecommitdiffstats
path: root/compiler/rustc_span/src/lev_distance/tests.rs
blob: b17d6588c9f06dfe998a6b654749d6d7e8de8ca5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
use super::*;

#[test]
fn test_lev_distance() {
    use std::char::{from_u32, MAX};
    // Test bytelength agnosticity
    for c in (0..MAX as u32).filter_map(from_u32).map(|i| i.to_string()) {
        assert_eq!(lev_distance(&c[..], &c[..], usize::MAX), Some(0));
    }

    let a = "\nMäry häd ä little lämb\n\nLittle lämb\n";
    let b = "\nMary häd ä little lämb\n\nLittle lämb\n";
    let c = "Mary häd ä little lämb\n\nLittle lämb\n";
    assert_eq!(lev_distance(a, b, usize::MAX), Some(1));
    assert_eq!(lev_distance(b, a, usize::MAX), Some(1));
    assert_eq!(lev_distance(a, c, usize::MAX), Some(2));
    assert_eq!(lev_distance(c, a, usize::MAX), Some(2));
    assert_eq!(lev_distance(b, c, usize::MAX), Some(1));
    assert_eq!(lev_distance(c, b, usize::MAX), Some(1));
}

#[test]
fn test_lev_distance_limit() {
    assert_eq!(lev_distance("abc", "abcd", 1), Some(1));
    assert_eq!(lev_distance("abc", "abcd", 0), None);
    assert_eq!(lev_distance("abc", "xyz", 3), Some(3));
    assert_eq!(lev_distance("abc", "xyz", 2), None);
}

#[test]
fn test_method_name_similarity_score() {
    assert_eq!(lev_distance_with_substrings("empty", "is_empty", 1), Some(1));
    assert_eq!(lev_distance_with_substrings("shrunk", "rchunks", 2), None);
    assert_eq!(lev_distance_with_substrings("abc", "abcd", 1), Some(1));
    assert_eq!(lev_distance_with_substrings("a", "abcd", 1), None);
    assert_eq!(lev_distance_with_substrings("edf", "eq", 1), None);
    assert_eq!(lev_distance_with_substrings("abc", "xyz", 3), Some(3));
    assert_eq!(lev_distance_with_substrings("abcdef", "abcdef", 2), Some(0));
}

#[test]
fn test_find_best_match_for_name() {
    use crate::create_default_session_globals_then;
    create_default_session_globals_then(|| {
        let input = vec![Symbol::intern("aaab"), Symbol::intern("aaabc")];
        assert_eq!(
            find_best_match_for_name(&input, Symbol::intern("aaaa"), None),
            Some(Symbol::intern("aaab"))
        );

        assert_eq!(find_best_match_for_name(&input, Symbol::intern("1111111111"), None), None);

        let input = vec![Symbol::intern("AAAA")];
        assert_eq!(
            find_best_match_for_name(&input, Symbol::intern("aaaa"), None),
            Some(Symbol::intern("AAAA"))
        );

        let input = vec![Symbol::intern("AAAA")];
        assert_eq!(
            find_best_match_for_name(&input, Symbol::intern("aaaa"), Some(4)),
            Some(Symbol::intern("AAAA"))
        );

        let input = vec![Symbol::intern("a_longer_variable_name")];
        assert_eq!(
            find_best_match_for_name(&input, Symbol::intern("a_variable_longer_name"), None),
            Some(Symbol::intern("a_longer_variable_name"))
        );
    })
}