chiark / gitweb /
eglibc (2.11.3-4+deb6u3) squeeze-lts; urgency=medium
[eglibc.git] / string / tst-strxfrm.c
1 /* Based on a test case by Paul Eggert.  */
2 #include <locale.h>
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <string.h>
6 #include <gnu/option-groups.h>
7
8
9 char const string[] = "";
10
11
12 static int
13 test (const char *locale)
14 {
15   size_t bufsize;
16   size_t r;
17   size_t l;
18   char *buf;
19   locale_t loc;
20   int result = 0;
21
22   if (setlocale (LC_COLLATE, locale) == NULL)
23     {
24       printf ("cannot set locale \"%s\"\n", locale);
25       return 1;
26     }
27   bufsize = strxfrm (NULL, string, 0) + 1;
28   buf = malloc (bufsize);
29   if (buf == NULL)
30     {
31       printf ("cannot allocate %zd bytes\n", bufsize);
32       return 1;
33     }
34   r = strxfrm (buf, string, bufsize);
35   l = strlen (buf);
36   if (r != l)
37     {
38        printf ("locale \"%s\": strxfrm returned %zu, strlen returned %zu\n",
39                locale, r, l);
40        result = 1;
41     }
42
43   loc = newlocale (1 << LC_ALL, locale, NULL);
44
45   r = strxfrm_l (buf, string, bufsize, loc);
46   l = strlen (buf);
47   if (r != l)
48     {
49        printf ("locale \"%s\": strxfrm_l returned %zu, strlen returned %zu\n",
50                locale, r, l);
51        result = 1;
52     }
53
54   freelocale (loc);
55
56   free (buf);
57
58   return result;
59 }
60
61
62 int
63 main (void)
64 {
65   int result = 0;
66
67   result |= test ("C");
68 #if __OPTION_EGLIBC_LOCALE_CODE
69   result |= test ("en_US.ISO-8859-1");
70   result |= test ("de_DE.UTF-8");
71 #endif
72
73   return result;
74 }