Mercurial > libunicode
comparison test/main.cpp @ 0:f94206b2e05e
Initial import
author | David Demelier <markand@malikania.fr> |
---|---|
date | Tue, 27 Dec 2016 13:35:57 +0100 |
parents | |
children | 0d9603b420c2 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:f94206b2e05e |
---|---|
1 /* | |
2 * main.cpp -- main test file for unicode | |
3 * | |
4 * Copyright (c) 2013-2016 David Demelier <markand@malikania.fr> | |
5 * | |
6 * Permission to use, copy, modify, and/or distribute this software for any | |
7 * purpose with or without fee is hereby granted, provided that the above | |
8 * copyright notice and this permission notice appear in all copies. | |
9 * | |
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | |
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | |
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | |
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | |
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | |
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | |
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | |
17 */ | |
18 | |
19 /* | |
20 * /!\ Be sure to keep this file with UTF-8 encoding /!\ | |
21 */ | |
22 | |
23 #include <gtest/gtest.h> | |
24 | |
25 #include <unicode.hpp> | |
26 | |
27 using namespace testing; | |
28 | |
29 /* | |
30 * Conversion UTF32 -> UTF8 | |
31 * ------------------------------------------------------------------ | |
32 */ | |
33 | |
34 TEST(Conversion32to8, ascii) | |
35 { | |
36 try { | |
37 std::u32string u32{'a', 'b', 'c'}; | |
38 std::string s = unicode::toUtf8(u32); | |
39 | |
40 ASSERT_EQ("abc", s); | |
41 } catch (const std::exception &ex) { | |
42 FAIL() << ex.what(); | |
43 } | |
44 } | |
45 | |
46 TEST(Conversion32to8, valid) | |
47 { | |
48 try { | |
49 std::u32string u32{'a', U'é', 'c', U'𠀀'}; | |
50 std::string s = unicode::toUtf8(u32); | |
51 std::string expected = u8"aéc𠀀"; | |
52 | |
53 ASSERT_EQ(expected, s); | |
54 } catch (const std::exception &ex) { | |
55 FAIL() << ex.what(); | |
56 } | |
57 } | |
58 | |
59 TEST(Conversion32to8, invalid) | |
60 { | |
61 std::u32string u32{'a', 0xFFFFFFFF, 'c'}; | |
62 | |
63 ASSERT_ANY_THROW(unicode::toUtf8(u32)); | |
64 } | |
65 | |
66 /* | |
67 * Conversion UTF8 -> UTF32 | |
68 * ------------------------------------------------------------------ | |
69 */ | |
70 | |
71 TEST(Conversion8to32, ascii) | |
72 { | |
73 try { | |
74 std::string s{"abc"}; | |
75 std::u32string expected{'a', 'b', 'c'}; | |
76 std::u32string result = unicode::toUtf32(s); | |
77 | |
78 ASSERT_EQ(expected, result); | |
79 } catch (const std::exception &ex) { | |
80 FAIL() << ex.what(); | |
81 } | |
82 } | |
83 | |
84 TEST(Conversion8to32, valid) | |
85 { | |
86 try { | |
87 std::string s{u8"aéc𠀀"}; | |
88 std::u32string expected{'a', U'é', 'c', U'𠀀'}; | |
89 std::u32string result = unicode::toUtf32(s); | |
90 | |
91 ASSERT_EQ(expected, result); | |
92 } catch (const std::exception &ex) { | |
93 FAIL() << ex.what(); | |
94 } | |
95 } | |
96 | |
97 /* | |
98 * UTF32 to upper | |
99 * ------------------------------------------------------------------ | |
100 */ | |
101 | |
102 TEST(Toupper32, ascii) | |
103 { | |
104 try { | |
105 std::u32string u32{'a', 'b', 'c'}; | |
106 std::u32string expected{'A', 'B', 'C'}; | |
107 std::u32string result = unicode::toupper(u32); | |
108 | |
109 ASSERT_EQ(expected, result); | |
110 } catch (const std::exception &ex) { | |
111 FAIL() << ex.what(); | |
112 } | |
113 } | |
114 | |
115 TEST(Toupper32, valid) | |
116 { | |
117 try { | |
118 std::u32string u32{U'ä', U'ç', U'ë'}; | |
119 std::u32string expected{U'Ä', U'Ç', U'Ë'}; | |
120 std::u32string result = unicode::toupper(u32); | |
121 | |
122 ASSERT_EQ(expected, result); | |
123 } catch (const std::exception &ex) { | |
124 FAIL() << ex.what(); | |
125 } | |
126 } | |
127 | |
128 TEST(Toupper32, invalid) | |
129 { | |
130 try { | |
131 std::u32string u32{'a', 0xFFFFFFFF, 'b'}; | |
132 std::u32string expected{'A', 0xFFFFFFFF, 'B'}; | |
133 std::u32string result = unicode::toupper(u32); | |
134 | |
135 ASSERT_EQ(expected, result); | |
136 } catch (const std::exception &ex) { | |
137 FAIL() << ex.what(); | |
138 } | |
139 } | |
140 | |
141 /* | |
142 * UTF32 to lower | |
143 * ------------------------------------------------------------------ | |
144 */ | |
145 | |
146 TEST(Tolower32, ascii) | |
147 { | |
148 try { | |
149 std::u32string u32{'A', 'B', 'C'}; | |
150 std::u32string expected{'a', 'b', 'c'}; | |
151 std::u32string result = unicode::tolower(u32); | |
152 | |
153 ASSERT_EQ(expected, result); | |
154 } catch (const std::exception &ex) { | |
155 FAIL() << ex.what(); | |
156 } | |
157 } | |
158 | |
159 TEST(Tolower32, valid) | |
160 { | |
161 try { | |
162 std::u32string u32{U'Ä', U'Ç', U'Ë'}; | |
163 std::u32string expected{U'ä', U'ç', U'ë'}; | |
164 std::u32string result = unicode::tolower(u32); | |
165 | |
166 ASSERT_EQ(expected, result); | |
167 } catch (const std::exception &ex) { | |
168 FAIL() << ex.what(); | |
169 } | |
170 } | |
171 | |
172 TEST(Tolower32, invalid) | |
173 { | |
174 try { | |
175 std::u32string u32{'A', 0xFFFFFFFF, 'B'}; | |
176 std::u32string expected{'a', 0xFFFFFFFF, 'b'}; | |
177 std::u32string result = unicode::tolower(u32); | |
178 | |
179 ASSERT_EQ(expected, result); | |
180 } catch (const std::exception &ex) { | |
181 FAIL() << ex.what(); | |
182 } | |
183 } | |
184 | |
185 /* | |
186 * UTF8 to upper | |
187 * ------------------------------------------------------------------ | |
188 */ | |
189 | |
190 TEST(Toupper8, ascii) | |
191 { | |
192 try { | |
193 std::string s{"abc"}; | |
194 std::string r = unicode::toupper(s); | |
195 | |
196 ASSERT_EQ("ABC", r); | |
197 } catch (const std::exception &ex) { | |
198 FAIL() << ex.what(); | |
199 } | |
200 } | |
201 | |
202 TEST(Toupper8, valid) | |
203 { | |
204 try { | |
205 std::string s{u8"aéc"}; | |
206 std::string r = unicode::toupper(s); | |
207 | |
208 ASSERT_EQ(u8"AÉC", r); | |
209 } catch (const std::exception &ex) { | |
210 FAIL() << ex.what(); | |
211 } | |
212 } | |
213 | |
214 TEST(Toupper8, invalid) | |
215 { | |
216 std::string s{"a" "\xFF""b"}; | |
217 | |
218 ASSERT_ANY_THROW(unicode::toupper(s)); | |
219 } | |
220 | |
221 /* | |
222 * UTF8 to lower | |
223 * ------------------------------------------------------------------ | |
224 */ | |
225 | |
226 TEST(Tolower8, ascii) | |
227 { | |
228 try { | |
229 std::string s{"ABC"}; | |
230 std::string r = unicode::tolower(s); | |
231 | |
232 ASSERT_EQ("abc", r); | |
233 } catch (const std::exception &ex) { | |
234 FAIL() << ex.what(); | |
235 } | |
236 } | |
237 | |
238 TEST(Tolower8, valid) | |
239 { | |
240 try { | |
241 std::string s{u8"AÉC"}; | |
242 std::string r = unicode::tolower(s); | |
243 | |
244 ASSERT_EQ(u8"aéc", r); | |
245 } catch (const std::exception &ex) { | |
246 FAIL() << ex.what(); | |
247 } | |
248 } | |
249 | |
250 TEST(Tolower8, invalid) | |
251 { | |
252 std::string s{"A" "\xFF""B"}; | |
253 | |
254 ASSERT_ANY_THROW(unicode::tolower(s)); | |
255 } | |
256 | |
257 /* | |
258 * Check functions | |
259 * ------------------------------------------------------------------ | |
260 */ | |
261 | |
262 TEST(Check, isspace) | |
263 { | |
264 ASSERT_TRUE(unicode::isspace(' ')); | |
265 ASSERT_FALSE(unicode::isspace(/* é */ 233)); | |
266 } | |
267 | |
268 TEST(Check, isalpha) | |
269 { | |
270 ASSERT_TRUE(unicode::isalpha(U'é')); | |
271 ASSERT_FALSE(unicode::isalpha(U'€')); | |
272 } | |
273 | |
274 TEST(Check, isupper) | |
275 { | |
276 ASSERT_FALSE(unicode::isupper('a')); | |
277 ASSERT_FALSE(unicode::isupper(U'é')); | |
278 ASSERT_TRUE(unicode::isupper('A')); | |
279 ASSERT_TRUE(unicode::isupper(U'É')); | |
280 } | |
281 | |
282 TEST(Check, islower) | |
283 { | |
284 ASSERT_TRUE(unicode::islower('a')); | |
285 ASSERT_TRUE(unicode::islower(U'é')); | |
286 ASSERT_FALSE(unicode::islower('A')); | |
287 ASSERT_FALSE(unicode::islower(U'É')); | |
288 } | |
289 | |
290 /* | |
291 * Miscellaneous | |
292 * ------------------------------------------------------------------ | |
293 */ | |
294 | |
295 TEST(Misc, nbytesPoint) | |
296 { | |
297 ASSERT_EQ(1, unicode::nbytesPoint('a')); | |
298 ASSERT_EQ(2, unicode::nbytesPoint(U'é')); | |
299 ASSERT_EQ(3, unicode::nbytesPoint(U'€')); | |
300 ASSERT_EQ(4, unicode::nbytesPoint(U'𠀀')); | |
301 } | |
302 | |
303 TEST(Misc, nbytesUtf8) | |
304 { | |
305 std::string s1{u8"a"}; | |
306 std::string s2{u8"é"}; | |
307 std::string s3{u8"€"}; | |
308 std::string s4{u8"𠀀"}; | |
309 | |
310 ASSERT_EQ(1, unicode::nbytesUtf8(s1[0])); | |
311 ASSERT_EQ(2, unicode::nbytesUtf8(s2[0])); | |
312 ASSERT_EQ(3, unicode::nbytesUtf8(s3[0])); | |
313 ASSERT_EQ(4, unicode::nbytesUtf8(s4[0])); | |
314 } | |
315 | |
316 TEST(Misc, forEach) | |
317 { | |
318 std::string s{u8"aé€𠀀"}; | |
319 int current = 0; | |
320 | |
321 unicode::forEach(s, [&] (char32_t code) { | |
322 if (current == 0) | |
323 ASSERT_EQ(U'a', code); | |
324 else if (current == 1) | |
325 ASSERT_EQ(U'é', code); | |
326 else if (current == 2) | |
327 ASSERT_EQ(U'€', code); | |
328 else if (current == 3) | |
329 ASSERT_EQ(U'𠀀', code); | |
330 | |
331 current++; | |
332 }); | |
333 | |
334 ASSERT_EQ(4, current); | |
335 } | |
336 | |
337 TEST(Misc, forEachInvalid) | |
338 { | |
339 std::string s{"a" "\xFF" "b"}; | |
340 | |
341 ASSERT_ANY_THROW(unicode::forEach(s, [&] (char32_t) { })); | |
342 } | |
343 | |
344 int main(int argc, char **argv) | |
345 { | |
346 InitGoogleTest(&argc, argv); | |
347 | |
348 return RUN_ALL_TESTS(); | |
349 } |