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
28static int
29do_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

source code of glibc/localedata/tst-iconv-emojis-trans.c