summaryrefslogtreecommitdiffstats
path: root/third_party/rust/mapped_hyph/tests/test.rs
blob: 95eae86f67e23b0b1c5f421faa484cc84bf5be6d (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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// Any copyright to the test code below is dedicated to the Public Domain.
// http://creativecommons.org/publicdomain/zero/1.0/

use mapped_hyph::Hyphenator;

#[test]
fn basic_tests() {
    let dic_path = "hyph_en_US.hyf";
    let dic = match unsafe { mapped_hyph::load_file(dic_path) } {
        Some(dic) => dic,
        _ => panic!("failed to load dictionary {}", dic_path),
    };
    let hyph = Hyphenator::new(&*dic);
    assert_eq!(hyph.hyphenate_word("haha", '-'), "haha");
    assert_eq!(hyph.hyphenate_word("hahaha", '-'), "ha-haha");
    assert_eq!(hyph.hyphenate_word("photo", '-'), "photo");
    assert_eq!(hyph.hyphenate_word("photograph", '-'), "pho-to-graph");
    assert_eq!(hyph.hyphenate_word("photographer", '-'), "pho-tog-ra-pher");
    assert_eq!(hyph.hyphenate_word("photographic", '-'), "pho-to-graphic");
    assert_eq!(hyph.hyphenate_word("photographical", '-'), "pho-to-graph-i-cal");
    assert_eq!(hyph.hyphenate_word("photographically", '-'), "pho-to-graph-i-cally");
    assert_eq!(hyph.hyphenate_word("supercalifragilisticexpialidocious", '-'), "su-per-cal-ifrag-ilis-tic-ex-pi-ali-do-cious");
}

// Testcases adapted from tests included with libhyphen.
// (Using only the UTF-8 dictionaries/tests, and omitting those that require
// the extended hyphenation algorithm.)

#[test]
fn base() {
    let dic_path = "tests/base.hyf";
    let dic = match unsafe { mapped_hyph::load_file(dic_path) } {
        Some(dic) => dic,
        _ => panic!("failed to load dictionary {}", dic_path),
    };
    let hyph = Hyphenator::new(&*dic);
    use std::fs::File;
    use std::io::{BufRead,BufReader};
    let words: Vec<String> = {
        let file = File::open("tests/base.word").unwrap();
        BufReader::new(file).lines().map(|l| l.unwrap()).collect()
    };
    let hyphs: Vec<String> = {
        let file = File::open("tests/base.hyph").unwrap();
        BufReader::new(file).lines().map(|l| l.unwrap()).collect()
    };
    for i in 0 .. words.len() {
        assert_eq!(hyph.hyphenate_word(&words[i], '='), hyphs[i]);
    }
}

#[test]
fn compound() {
    let dic_path = "tests/compound.hyf";
    let dic = match unsafe { mapped_hyph::load_file(dic_path) } {
        Some(dic) => dic,
        _ => panic!("failed to load dictionary {}", dic_path),
    };
    let hyph = Hyphenator::new(&*dic);
    assert_eq!(hyph.hyphenate_word("motorcycle", '-'), "mo-tor-cy-cle");
}

#[test]
fn compound4() {
    let dic_path = "tests/compound4.hyf";
    let dic = match unsafe { mapped_hyph::load_file(dic_path) } {
        Some(dic) => dic,
        _ => panic!("failed to load dictionary {}", dic_path),
    };
    let hyph = Hyphenator::new(&*dic);
    assert_eq!(hyph.hyphenate_word("motorcycle", '-'), "motor-cycle");
}

#[test]
fn compound5() {
    let dic_path = "tests/compound5.hyf";
    let dic = match unsafe { mapped_hyph::load_file(dic_path) } {
        Some(dic) => dic,
        _ => panic!("failed to load dictionary {}", dic_path),
    };
    let hyph = Hyphenator::new(&*dic);
    assert_eq!(hyph.hyphenate_word("postea", '-'), "post-e-a");
}

#[test]
fn compound6() {
    let dic_path = "tests/compound6.hyf";
    let dic = match unsafe { mapped_hyph::load_file(dic_path) } {
        Some(dic) => dic,
        _ => panic!("failed to load dictionary {}", dic_path),
    };
    let hyph = Hyphenator::new(&*dic);
    assert_eq!(hyph.hyphenate_word("meaque", '-'), "me-a-que");
}

#[test]
fn settings2() {
    let dic_path = "tests/settings2.hyf";
    let dic = match unsafe { mapped_hyph::load_file(dic_path) } {
        Some(dic) => dic,
        _ => panic!("failed to load dictionary {}", dic_path),
    };
    let hyph = Hyphenator::new(&*dic);
    assert_eq!(hyph.hyphenate_word("őőőőőőő", '='), "ő=ő=ő=ő=ő=ő=ő");
}

#[test]
fn settings3() {
    let dic_path = "tests/settings3.hyf";
    let dic = match unsafe { mapped_hyph::load_file(dic_path) } {
        Some(dic) => dic,
        _ => panic!("failed to load dictionary {}", dic_path),
    };
    let hyph = Hyphenator::new(&*dic);
    assert_eq!(hyph.hyphenate_word("őőőőőőő", '='), "őő=ő=ő=ő=őő");
}

#[test]
fn hyphen() {
    let dic_path = "tests/hyphen.hyf";
    let dic = match unsafe { mapped_hyph::load_file(dic_path) } {
        Some(dic) => dic,
        _ => panic!("failed to load dictionary {}", dic_path),
    };
    let hyph = Hyphenator::new(&*dic);
    assert_eq!(hyph.hyphenate_word("foobar'foobar-foobar’foobar", '='), "foobar'foobar-foobar’foobar");
}

#[test]
fn lhmin() {
    let dic_path = "tests/lhmin.hyf";
    let dic = match unsafe { mapped_hyph::load_file(dic_path) } {
        Some(dic) => dic,
        _ => panic!("failed to load dictionary {}", dic_path),
    };
    let hyph = Hyphenator::new(&*dic);
    assert_eq!(hyph.hyphenate_word("miért", '='), "mi=ért");
}

#[test]
fn rhmin() {
    let dic_path = "tests/rhmin.hyf";
    let dic = match unsafe { mapped_hyph::load_file(dic_path) } {
        Some(dic) => dic,
        _ => panic!("failed to load dictionary {}", dic_path),
    };
    let hyph = Hyphenator::new(&*dic);
    assert_eq!(hyph.hyphenate_word("övéit", '='), "övéit");
    assert_eq!(hyph.hyphenate_word("అంగడిధర", '='), "అం=గ=డిధర");
}

#[test]
fn num() {
    let dic_path = "tests/num.hyf";
    let dic = match unsafe { mapped_hyph::load_file(dic_path) } {
        Some(dic) => dic,
        _ => panic!("failed to load dictionary {}", dic_path),
    };
    let hyph = Hyphenator::new(&*dic);
    assert_eq!(hyph.hyphenate_word("foobar", '='), "foobar");
    assert_eq!(hyph.hyphenate_word("foobarfoobar", '='), "foobar=foobar");
    assert_eq!(hyph.hyphenate_word("barfoobarfoo", '='), "barfoo=barfoo");
    assert_eq!(hyph.hyphenate_word("123foobarfoobar", '='), "123foobar=foobar");
    assert_eq!(hyph.hyphenate_word("foobarfoobar123", '='), "foobar=foobar123");
    assert_eq!(hyph.hyphenate_word("123foobarfoobar123", '='), "123foobar=foobar123");
    assert_eq!(hyph.hyphenate_word("123barfoobarfoo", '='), "123barfoo=barfoo");
    assert_eq!(hyph.hyphenate_word("barfoobarfoo123", '='), "barfoo=barfoo123");
    assert_eq!(hyph.hyphenate_word("123barfoobarfoo123", '='), "123barfoo=barfoo123");
}