tests: add signature checks
[gnulib.git] / tests / test-wcrtomb.c
1 /* Test of conversion of wide character to multibyte character.
2    Copyright (C) 2008, 2009 Free Software Foundation, Inc.
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; either version 3 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
16
17 /* Written by Bruno Haible <bruno@clisp.org>, 2008.  */
18
19 #include <config.h>
20
21 #include <wchar.h>
22
23 #include "signature.h"
24 SIGNATURE_CHECK (wcrtomb, size_t, (char *, wchar_t, mbstate_t *));
25
26 #include <locale.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30
31 #define ASSERT(expr) \
32   do                                                                         \
33     {                                                                        \
34       if (!(expr))                                                           \
35         {                                                                    \
36           fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
37           fflush (stderr);                                                   \
38           abort ();                                                          \
39         }                                                                    \
40     }                                                                        \
41   while (0)
42
43 /* Check the multibyte character s[0..n-1].  */
44 static void
45 check_character (const char *s, size_t n)
46 {
47   wchar_t wc;
48   char buf[64];
49   int iret;
50   size_t ret;
51
52   wc = (wchar_t) 0xBADFACE;
53   iret = mbtowc (&wc, s, n);
54   ASSERT (iret == n);
55
56   ret = wcrtomb (buf, wc, NULL);
57   ASSERT (ret == n);
58   ASSERT (memcmp (buf, s, n) == 0);
59
60   /* Test special calling convention, passing a NULL pointer.  */
61   ret = wcrtomb (NULL, wc, NULL);
62   ASSERT (ret == 1);
63 }
64
65 int
66 main (int argc, char *argv[])
67 {
68   char buf[64];
69   size_t ret;
70
71   /* configure should already have checked that the locale is supported.  */
72   if (setlocale (LC_ALL, "") == NULL)
73     return 1;
74
75   /* Test NUL character.  */
76   {
77     buf[0] = 'x';
78     ret = wcrtomb (buf, 0, NULL);
79     ASSERT (ret == 1);
80     ASSERT (buf[0] == '\0');
81   }
82
83   /* Test single bytes.  */
84   {
85     int c;
86
87     for (c = 0; c < 0x100; c++)
88       switch (c)
89         {
90         case '\t': case '\v': case '\f':
91         case ' ': case '!': case '"': case '#': case '%':
92         case '&': case '\'': case '(': case ')': case '*':
93         case '+': case ',': case '-': case '.': case '/':
94         case '0': case '1': case '2': case '3': case '4':
95         case '5': case '6': case '7': case '8': case '9':
96         case ':': case ';': case '<': case '=': case '>':
97         case '?':
98         case 'A': case 'B': case 'C': case 'D': case 'E':
99         case 'F': case 'G': case 'H': case 'I': case 'J':
100         case 'K': case 'L': case 'M': case 'N': case 'O':
101         case 'P': case 'Q': case 'R': case 'S': case 'T':
102         case 'U': case 'V': case 'W': case 'X': case 'Y':
103         case 'Z':
104         case '[': case '\\': case ']': case '^': case '_':
105         case 'a': case 'b': case 'c': case 'd': case 'e':
106         case 'f': case 'g': case 'h': case 'i': case 'j':
107         case 'k': case 'l': case 'm': case 'n': case 'o':
108         case 'p': case 'q': case 'r': case 's': case 't':
109         case 'u': case 'v': case 'w': case 'x': case 'y':
110         case 'z': case '{': case '|': case '}': case '~':
111           /* c is in the ISO C "basic character set".  */
112           ret = wcrtomb (buf, btowc (c), NULL);
113           ASSERT (ret == 1);
114           ASSERT (buf[0] == (char) c);
115           break;
116         }
117   }
118
119   if (argc > 1)
120     switch (argv[1][0])
121       {
122       case '1':
123         /* Locale encoding is ISO-8859-1 or ISO-8859-15.  */
124         {
125           const char input[] = "B\374\337er"; /* "Büßer" */
126
127           check_character (input + 1, 1);
128           check_character (input + 2, 1);
129         }
130         return 0;
131
132       case '2':
133         /* Locale encoding is UTF-8.  */
134         {
135           const char input[] = "B\303\274\303\237er"; /* "Büßer" */
136
137           check_character (input + 1, 2);
138           check_character (input + 3, 2);
139         }
140         return 0;
141
142       case '3':
143         /* Locale encoding is EUC-JP.  */
144         {
145           const char input[] = "<\306\374\313\334\270\354>"; /* "<日本語>" */
146
147           check_character (input + 1, 2);
148           check_character (input + 3, 2);
149           check_character (input + 5, 2);
150         }
151         return 0;
152
153       case '4':
154         /* Locale encoding is GB18030.  */
155         {
156           const char input[] = "B\250\271\201\060\211\070er"; /* "Büßer" */
157
158           check_character (input + 1, 2);
159           check_character (input + 3, 4);
160         }
161         return 0;
162       }
163
164   return 1;
165 }