1 // Any copyright to the test code below is dedicated to the Public Domain.
2 // http://creativecommons.org/publicdomain/zero/1.0/
3
4 use mapped_hyph::Hyphenator;
5
6 #[test]
basic_tests()7 fn basic_tests() {
8 let dic_path = "hyph_en_US.hyf";
9 let dic = match unsafe { mapped_hyph::load_file(dic_path) } {
10 Some(dic) => dic,
11 _ => panic!("failed to load dictionary {}", dic_path),
12 };
13 let hyph = Hyphenator::new(&*dic);
14 assert_eq!(hyph.hyphenate_word("haha", '-'), "haha");
15 assert_eq!(hyph.hyphenate_word("hahaha", '-'), "ha-haha");
16 assert_eq!(hyph.hyphenate_word("photo", '-'), "photo");
17 assert_eq!(hyph.hyphenate_word("photograph", '-'), "pho-to-graph");
18 assert_eq!(hyph.hyphenate_word("photographer", '-'), "pho-tog-ra-pher");
19 assert_eq!(hyph.hyphenate_word("photographic", '-'), "pho-to-graphic");
20 assert_eq!(hyph.hyphenate_word("photographical", '-'), "pho-to-graph-i-cal");
21 assert_eq!(hyph.hyphenate_word("photographically", '-'), "pho-to-graph-i-cally");
22 assert_eq!(hyph.hyphenate_word("supercalifragilisticexpialidocious", '-'), "su-per-cal-ifrag-ilis-tic-ex-pi-ali-do-cious");
23 }
24
25 // Testcases adapted from tests included with libhyphen.
26 // (Using only the UTF-8 dictionaries/tests, and omitting those that require
27 // the extended hyphenation algorithm.)
28
29 #[test]
base()30 fn base() {
31 let dic_path = "tests/base.hyf";
32 let dic = match unsafe { mapped_hyph::load_file(dic_path) } {
33 Some(dic) => dic,
34 _ => panic!("failed to load dictionary {}", dic_path),
35 };
36 let hyph = Hyphenator::new(&*dic);
37 use std::fs::File;
38 use std::io::{BufRead,BufReader};
39 let words: Vec<String> = {
40 let file = File::open("tests/base.word").unwrap();
41 BufReader::new(file).lines().map(|l| l.unwrap()).collect()
42 };
43 let hyphs: Vec<String> = {
44 let file = File::open("tests/base.hyph").unwrap();
45 BufReader::new(file).lines().map(|l| l.unwrap()).collect()
46 };
47 for i in 0 .. words.len() {
48 assert_eq!(hyph.hyphenate_word(&words[i], '='), hyphs[i]);
49 }
50 }
51
52 #[test]
compound()53 fn compound() {
54 let dic_path = "tests/compound.hyf";
55 let dic = match unsafe { mapped_hyph::load_file(dic_path) } {
56 Some(dic) => dic,
57 _ => panic!("failed to load dictionary {}", dic_path),
58 };
59 let hyph = Hyphenator::new(&*dic);
60 assert_eq!(hyph.hyphenate_word("motorcycle", '-'), "mo-tor-cy-cle");
61 }
62
63 #[test]
compound4()64 fn compound4() {
65 let dic_path = "tests/compound4.hyf";
66 let dic = match unsafe { mapped_hyph::load_file(dic_path) } {
67 Some(dic) => dic,
68 _ => panic!("failed to load dictionary {}", dic_path),
69 };
70 let hyph = Hyphenator::new(&*dic);
71 assert_eq!(hyph.hyphenate_word("motorcycle", '-'), "motor-cycle");
72 }
73
74 #[test]
compound5()75 fn compound5() {
76 let dic_path = "tests/compound5.hyf";
77 let dic = match unsafe { mapped_hyph::load_file(dic_path) } {
78 Some(dic) => dic,
79 _ => panic!("failed to load dictionary {}", dic_path),
80 };
81 let hyph = Hyphenator::new(&*dic);
82 assert_eq!(hyph.hyphenate_word("postea", '-'), "post-e-a");
83 }
84
85 #[test]
compound6()86 fn compound6() {
87 let dic_path = "tests/compound6.hyf";
88 let dic = match unsafe { mapped_hyph::load_file(dic_path) } {
89 Some(dic) => dic,
90 _ => panic!("failed to load dictionary {}", dic_path),
91 };
92 let hyph = Hyphenator::new(&*dic);
93 assert_eq!(hyph.hyphenate_word("meaque", '-'), "me-a-que");
94 }
95
96 #[test]
settings2()97 fn settings2() {
98 let dic_path = "tests/settings2.hyf";
99 let dic = match unsafe { mapped_hyph::load_file(dic_path) } {
100 Some(dic) => dic,
101 _ => panic!("failed to load dictionary {}", dic_path),
102 };
103 let hyph = Hyphenator::new(&*dic);
104 assert_eq!(hyph.hyphenate_word("őőőőőőő", '='), "ő=ő=ő=ő=ő=ő=ő");
105 }
106
107 #[test]
settings3()108 fn settings3() {
109 let dic_path = "tests/settings3.hyf";
110 let dic = match unsafe { mapped_hyph::load_file(dic_path) } {
111 Some(dic) => dic,
112 _ => panic!("failed to load dictionary {}", dic_path),
113 };
114 let hyph = Hyphenator::new(&*dic);
115 assert_eq!(hyph.hyphenate_word("őőőőőőő", '='), "őő=ő=ő=ő=őő");
116 }
117
118 #[test]
hyphen()119 fn hyphen() {
120 let dic_path = "tests/hyphen.hyf";
121 let dic = match unsafe { mapped_hyph::load_file(dic_path) } {
122 Some(dic) => dic,
123 _ => panic!("failed to load dictionary {}", dic_path),
124 };
125 let hyph = Hyphenator::new(&*dic);
126 assert_eq!(hyph.hyphenate_word("foobar'foobar-foobar’foobar", '='), "foobar'foobar-foobar’foobar");
127 }
128
129 #[test]
lhmin()130 fn lhmin() {
131 let dic_path = "tests/lhmin.hyf";
132 let dic = match unsafe { mapped_hyph::load_file(dic_path) } {
133 Some(dic) => dic,
134 _ => panic!("failed to load dictionary {}", dic_path),
135 };
136 let hyph = Hyphenator::new(&*dic);
137 assert_eq!(hyph.hyphenate_word("miért", '='), "mi=ért");
138 }
139
140 #[test]
rhmin()141 fn rhmin() {
142 let dic_path = "tests/rhmin.hyf";
143 let dic = match unsafe { mapped_hyph::load_file(dic_path) } {
144 Some(dic) => dic,
145 _ => panic!("failed to load dictionary {}", dic_path),
146 };
147 let hyph = Hyphenator::new(&*dic);
148 assert_eq!(hyph.hyphenate_word("övéit", '='), "övéit");
149 assert_eq!(hyph.hyphenate_word("అంగడిధర", '='), "అం=గ=డిధర");
150 }
151
152 #[test]
num()153 fn num() {
154 let dic_path = "tests/num.hyf";
155 let dic = match unsafe { mapped_hyph::load_file(dic_path) } {
156 Some(dic) => dic,
157 _ => panic!("failed to load dictionary {}", dic_path),
158 };
159 let hyph = Hyphenator::new(&*dic);
160 assert_eq!(hyph.hyphenate_word("foobar", '='), "foobar");
161 assert_eq!(hyph.hyphenate_word("foobarfoobar", '='), "foobar=foobar");
162 assert_eq!(hyph.hyphenate_word("barfoobarfoo", '='), "barfoo=barfoo");
163 assert_eq!(hyph.hyphenate_word("123foobarfoobar", '='), "123foobar=foobar");
164 assert_eq!(hyph.hyphenate_word("foobarfoobar123", '='), "foobar=foobar123");
165 assert_eq!(hyph.hyphenate_word("123foobarfoobar123", '='), "123foobar=foobar123");
166 assert_eq!(hyph.hyphenate_word("123barfoobarfoo", '='), "123barfoo=barfoo");
167 assert_eq!(hyph.hyphenate_word("barfoobarfoo123", '='), "barfoo=barfoo123");
168 assert_eq!(hyph.hyphenate_word("123barfoobarfoo123", '='), "123barfoo=barfoo123");
169 }
170