1 | /* Test some emoji transliterations |
2 | |
3 | Copyright (C) 2019-2024 Free Software Foundation, Inc. |
4 | Copyright The GNU Toolchain Authors. |
5 | |
6 | This file is part of the GNU C Library. |
7 | |
8 | The GNU C Library is free software; you can redistribute it and/or |
9 | modify it under the terms of the GNU Lesser General Public |
10 | License as published by the Free Software Foundation; either |
11 | version 2.1 of the License, or (at your option) any later version. |
12 | |
13 | The GNU C Library is distributed in the hope that it will be useful, |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
16 | Lesser General Public License for more details. |
17 | |
18 | You should have received a copy of the GNU Lesser General Public |
19 | License along with the GNU C Library; if not, see |
20 | <https://www.gnu.org/licenses/>. */ |
21 | |
22 | #include <iconv.h> |
23 | #include <locale.h> |
24 | #include <stdio.h> |
25 | #include <string.h> |
26 | #include <support/check.h> |
27 | |
28 | static int |
29 | do_test (void) |
30 | { |
31 | iconv_t cd; |
32 | |
33 | const int num_emojis = 70; |
34 | |
35 | const char str[] = "โก โฅ โค ๐ ๐ " |
36 | "๐ ๐ ๐ ๐ ๐ " |
37 | "๐ ๐ค ๐งก ๐ค ๐ค " |
38 | "๐ ๐ ๐ ๐ ๐ " |
39 | "๐
๐ ๐ ๐ ๐ " |
40 | "๐ ๐ ๐ ๐ ๐ " |
41 | "๐ ๐ ๐ ๐ ๐ " |
42 | "๐ ๐ ๐ ๐ ๐ " |
43 | "๐ ๐ ๐ ๐ ๐ " |
44 | "๐ ๐ ๐ ๐ก ๐ข " |
45 | "๐ฃ ๐ฆ ๐ง ๐จ ๐ฉ " |
46 | "๐ญ ๐ฎ ๐ฏ ๐ฐ ๐ฑ " |
47 | "๐ฒ ๐ธ ๐น ๐บ ๐ป " |
48 | "๐ผ ๐ฝ ๐ ๐ ๐" ; |
49 | |
50 | const char expected[] = "<3 <3 <3 <3 <3 " |
51 | "</3 <3 <3 <3 <3 " |
52 | "<3 <3 <3 <3 <3 " |
53 | ":-D :-D :'D :-D :-D " |
54 | ":-D :-D O:-) >:) ;-) " |
55 | ":-) :-P :-) :-* B-) " |
56 | ";-) :-| :-| :-| :'-| " |
57 | ":-| :-/ :-S :-* :-* " |
58 | ":-* :-* :-P ;-P X-P " |
59 | ":-( :-( >:-( :-( :'-( " |
60 | "X-( :-O :-O :-O :-O " |
61 | ":\"-( :-O :-O :'-O :-O " |
62 | ":-O :-3 :'-3 :-3 :-3 " |
63 | ";-3 :-3 :-( :-) (-:" ; |
64 | |
65 | char *inptr = (char *) str; |
66 | size_t inlen = strlen (str) + 1; |
67 | char outbuf[500]; |
68 | char *outptr = outbuf; |
69 | size_t outlen = sizeof (outbuf); |
70 | int result = 0; |
71 | size_t n; |
72 | |
73 | if (setlocale (LC_ALL, "en_US.UTF-8" ) == NULL) |
74 | FAIL_EXIT1 ("setlocale failed" ); |
75 | |
76 | cd = iconv_open (tocode: "ASCII//TRANSLIT" , fromcode: "UTF-8" ); |
77 | if (cd == (iconv_t) -1) |
78 | FAIL_EXIT1 ("iconv_open failed" ); |
79 | |
80 | n = iconv (cd: cd, inbuf: &inptr, inbytesleft: &inlen, outbuf: &outptr, outbytesleft: &outlen); |
81 | if (n != num_emojis) |
82 | { |
83 | if (n == (size_t) -1) |
84 | printf (format: "iconv() returned error: %m\n" ); |
85 | else |
86 | printf (format: "iconv() returned %zd, expected %d\n" , n, num_emojis); |
87 | result = 1; |
88 | } |
89 | if (inlen != 0) |
90 | { |
91 | puts (s: "not all input consumed" ); |
92 | result = 1; |
93 | } |
94 | else if (inptr - str != strlen (str) + 1) |
95 | { |
96 | printf (format: "inptr wrong, advanced by %td\n" , inptr - str); |
97 | result = 1; |
98 | } |
99 | if (memcmp (outbuf, expected, sizeof (expected)) != 0) |
100 | { |
101 | printf (format: "result wrong: \"%.*s\", expected: \"%s\"\n" , |
102 | (int) (sizeof (outbuf) - outlen), outbuf, expected); |
103 | result = 1; |
104 | } |
105 | else if (outlen != sizeof (outbuf) - sizeof (expected)) |
106 | { |
107 | printf (format: "outlen wrong: %zd, expected %zd\n" , outlen, |
108 | sizeof (outbuf) - sizeof (expected)); |
109 | result = 1; |
110 | } |
111 | else |
112 | printf (format: "output is \"%s\" which is OK\n" , outbuf); |
113 | |
114 | return result; |
115 | } |
116 | |
117 | #include <support/test-driver.c> |
118 | |