1 #![feature(test)]
2 #![feature(non_ascii_idents)]
3
4 #[cfg(test)]
5 extern crate test;
6 extern crate wana_kana;
7
8 #[cfg(test)]
9 extern crate speculate;
10
11 #[cfg(test)]
12 use speculate::speculate;
13
14 use wana_kana::to_hiragana::*;
15 use wana_kana::to_kana;
16 use wana_kana::to_kana::*;
17 use wana_kana::to_katakana::*;
18
19 use wana_kana::to_romaji;
20 use wana_kana::to_romaji::*;
21 use wana_kana::Options;
22
23 mod conversion_tables;
24 use conversion_tables::*;
25
26 speculate! {
27
28 describe "character_conversion" {
29
30 describe "test every conversion table char" {
31 it "to_kana()" {
32 for &[romaji, hiragana, katakana] in ROMA_TO_HIRA_KATA.iter() {
33 let lower = to_kana(romaji);
34 let upper = to_kana(&romaji.to_uppercase());
35 assert_eq!(lower, hiragana);
36 assert_eq!(upper, katakana);
37 }
38 }
39
40 it "to_hiragana()" {
41 for &[romaji, hiragana, _katakana] in ROMA_TO_HIRA_KATA.iter() {
42 let lower = to_hiragana(romaji);
43 let upper = to_hiragana(&romaji.to_uppercase());
44 assert_eq!(lower, hiragana);
45 assert_eq!(upper, hiragana);
46 }
47 }
48
49
50 it "Hiragana input to_romaji()" {
51 for &[hiragana, _, romaji] in HIRA_KATA_TO_ROMA.iter() {
52
53 if hiragana != ""{
54 assert_eq!(to_romaji(hiragana), romaji);
55 }
56 }
57 }
58
59 it "Katakana input to_romaji()" {
60 for &[_, katakana, romaji] in HIRA_KATA_TO_ROMA.iter() {
61
62 if katakana != ""{
63 assert_eq!(to_romaji(katakana), romaji);
64 }
65 }
66 }
67 }
68
69 describe "converting_kana_to_kana" {
70 it "k -> h" {
71 assert_eq!(to_hiragana("バケル"), "ばける");
72 }
73 it "h -> k" {
74 assert_eq!(to_katakana("ばける"), "バケル");
75 }
76
77 it "It survives only katakana to_katakana" {
78
79 assert_eq!(to_katakana("スタイル"), "スタイル");
80
81 }
82 it "It survives only hiragana to_hiragana" {
83 assert_eq!(to_hiragana("すたーいる"), "すたーいる");
84 }
85 it "Mixed kana converts every char k -> h" {
86 assert_eq!(to_katakana("アメリカじん"), "アメリカジン");
87 }
88 it "Mixed kana converts every char h -> k" {
89 assert_eq!(to_hiragana("アメリカじん"), "あめりかじん");
90 }
91 }
92 describe "long_vowels" {
93 it "Converts long vowels correctly from k -> h" {
94 assert_eq!(to_hiragana("バツゴー"), "ばつごう");
95 }
96 it "Preserves long dash from h -> k" {
97 assert_eq!(to_katakana("ばつゲーム"), "バツゲーム");
98 }
99 it "Preserves long dash from h -> h" {
100 assert_eq!(to_hiragana("ばつげーむ"), "ばつげーむ");
101 }
102 it "Preserves long dash from k -> k" {
103 assert_eq!(to_katakana("バツゲーム"), "バツゲーム");
104 }
105 it "Preserves long dash from mixed -> k 1" {
106 assert_eq!(to_katakana("バツゲーム"), "バツゲーム");
107 }
108 it "Preserves long dash from mixed -> k 2" {
109 assert_eq!(to_katakana("テスーと"), "テスート");
110 }
111 it "Preserves long dash from mixed -> h 1" {
112 assert_eq!(to_hiragana("てすート"), "てすーと");
113 }
114 it "Preserves long dash from mixed -> h 2" {
115 assert_eq!(to_hiragana("てすー戸"), "てすー戸");
116 }
117 it "Preserves long dash from mixed -> h 3" {
118 assert_eq!(to_hiragana("手巣ート"), "手巣ーと");
119 }
120 it "Preserves long dash from mixed -> h 4" {
121 assert_eq!(to_hiragana("tesート"), "てsーと");
122 }
123 it "Preserves long dash from mixed -> h 5" {
124 assert_eq!(to_hiragana("ートtesu"), "ーとてす");
125 }
126 }
127
128 describe "mixed_syllabaries" {
129 it "It passes non-katakana through when pass_romaji is true k -> h" {
130 assert_eq!(to_hiragana_with_opt("座禅‘zazen’スタイル", Options{ pass_romaji: true, .. Default::default() }), "座禅‘zazen’すたいる");
131 }
132 it "It passes non-hiragana through when pass_romaji is true h -> k" {
133 assert_eq!(to_katakana_with_opt("座禅‘zazen’すたいる", Options{ pass_romaji: true, .. Default::default() }), "座禅‘zazen’スタイル");
134 }
135 it "It converts non-katakana when pass_romaji is false k -> h" {
136 assert_eq!(to_hiragana("座禅‘zazen’スタイル"), "座禅「ざぜん」すたいる");
137 }
138 it "It converts non-hiragana when pass_romaji is false h -> k" {
139 assert_eq!(to_katakana("座禅‘zazen’すたいる"), "座禅「ザゼン」スタイル");
140 }
141 }
142 }
143
144 describe "case_sensitivity" {
145 it "cAse DoEsnT MatTER for to_hiragana()" {
146 assert_eq!(to_hiragana("aiueo"), to_hiragana("AIUEO"));
147 }
148 it "cAse DoEsnT MatTER for to_katakana()" {
149 assert_eq!(to_katakana("aiueo"), to_katakana("AIUEO"));
150 }
151 it "Case DOES matter for to_kana()" {
152 assert_ne!(to_kana("aiueo"), to_kana("AIUEO"));
153 }
154 }
155
156
157
158 describe "n_edge_cases" {
159 it "Solo N" {
160 assert_eq!(to_kana("n"), "ん");
161 }
162 it "double N" {
163 assert_eq!(to_kana("onn"), "おんん");
164 }
165 it "N followed by N* syllable" {
166 assert_eq!(to_kana("onna"), "おんな");
167 }
168 it "Triple N" {
169 assert_eq!(to_kana("nnn"), "んんん");
170 }
171 it "Triple N followed by N* syllable" {
172 assert_eq!(to_kana("onnna"), "おんんな");
173 }
174 it "Quadruple N" {
175 assert_eq!(to_kana("nnnn"), "んんんん");
176 }
177 it "nya -> にゃ" {
178 assert_eq!(to_kana("nyan"), "にゃん");
179 }
180 it "nnya -> んにゃ" {
181 assert_eq!(to_kana("nnyann"), "んにゃんん");
182 }
183 it "nnnya -> んにゃ" {
184 assert_eq!(to_kana("nnnyannn"), "んんにゃんんん");
185 }
186 it "n'ya -> んや" {
187 assert_eq!(to_kana("n'ya"), "んや");
188 }
189 it "kin'ya -> きんや" {
190 assert_eq!(to_kana("kin'ya"), "きんや");
191 }
192 it "shin'ya -> しんや" {
193 assert_eq!(to_kana("shin'ya"), "しんや");
194 }
195 it "kinyou -> きにょう" {
196 assert_eq!(to_kana("kinyou"), "きにょう");
197 }
198 it "kin'you -> きんよう" {
199 assert_eq!(to_kana("kin'you"), "きんよう");
200 }
201 it "kin'yu -> きんゆ" {
202 assert_eq!(to_kana("kin'yu"), "きんゆ");
203 }
204 it "Properly add space after n[space]" {
205 assert_eq!(to_kana("ichiban warui"), "いちばん わるい");
206 }
207 }
208
209 describe "bogus_4_character_sequences" {
210 it "Non bogus sequences work" {
211 assert_eq!(to_kana("chya"), "ちゃ");
212 }
213 it "Bogus sequences do not work" {
214 assert_eq!(to_kana("chyx"), "chyx");
215 }
216 it "Bogus sequences do not work 2" {
217 assert_eq!(to_kana("shyp"), "shyp");
218 }
219 it "Bogus sequences do not work 3" {
220 assert_eq!(to_kana("ltsb"), "ltsb");
221 }
222 }
223
224
225 }
226
227 #[bench]
bench_kana_1(b: &mut test::Bencher)228 fn bench_kana_1(b: &mut test::Bencher) {
229 b.iter(|| to_kana::to_kana("aiueosashisusesonaninunenokakikukeko"))
230 }
231
232 #[bench]
bench_kana_2(b: &mut test::Bencher)233 fn bench_kana_2(b: &mut test::Bencher) {
234 b.iter(|| to_kana::to_kana("AIUEOSASHISUSESONANINUNENOKAKIKUKEKO"))
235 }
236
237 #[bench]
bench_romaji_to_hiragana(b: &mut test::Bencher)238 fn bench_romaji_to_hiragana(b: &mut test::Bencher) {
239 b.iter(|| to_hiragana("aiueosashisusesonaninunenokakikukeko"))
240 }
241
242 #[bench]
bench_katakana_to_hiragana(b: &mut test::Bencher)243 fn bench_katakana_to_hiragana(b: &mut test::Bencher) {
244 b.iter(|| to_hiragana("アイウエオサシスセソナニヌネノカキクケコ"))
245 }
246
247 #[bench]
bench_romaji_to_katakana(b: &mut test::Bencher)248 fn bench_romaji_to_katakana(b: &mut test::Bencher) {
249 b.iter(|| to_katakana("aiueosashisusesonaninunenokakikukeko"))
250 }
251
252 #[bench]
bench_katakana_to_katakana(b: &mut test::Bencher)253 fn bench_katakana_to_katakana(b: &mut test::Bencher) {
254 b.iter(|| to_katakana("あいうえおさしすせそなにぬねのかきくけこ"))
255 }
256
257 #[bench]
bench_hiragana_to_romaji(b: &mut test::Bencher)258 fn bench_hiragana_to_romaji(b: &mut test::Bencher) {
259 b.iter(|| to_romaji::to_romaji("あいうえおさしすせそなにぬねのかきくけこ"))
260 }
261
262 #[bench]
bench_katakana_to_romaji(b: &mut test::Bencher)263 fn bench_katakana_to_romaji(b: &mut test::Bencher) {
264 b.iter(|| to_romaji::to_romaji("アイウエオサシスセソナニヌネノカキクケコ"))
265 }
266