Back to index

glibc  2.9
tst-strxfrm.c
Go to the documentation of this file.
00001 /* Based on a test case by Paul Eggert.  */
00002 #include <locale.h>
00003 #include <stdio.h>
00004 #include <stdlib.h>
00005 #include <string.h>
00006 
00007 
00008 char const string[] = "";
00009 
00010 
00011 static int
00012 test (const char *locale)
00013 {
00014   size_t bufsize;
00015   size_t r;
00016   size_t l;
00017   char *buf;
00018   locale_t loc;
00019   int result = 0;
00020 
00021   if (setlocale (LC_COLLATE, locale) == NULL)
00022     {
00023       printf ("cannot set locale \"%s\"\n", locale);
00024       return 1;
00025     }
00026   bufsize = strxfrm (NULL, string, 0) + 1;
00027   buf = malloc (bufsize);
00028   if (buf == NULL)
00029     {
00030       printf ("cannot allocate %zd bytes\n", bufsize);
00031       return 1;
00032     }
00033   r = strxfrm (buf, string, bufsize);
00034   l = strlen (buf);
00035   if (r != l)
00036     {
00037        printf ("locale \"%s\": strxfrm returned %zu, strlen returned %zu\n",
00038               locale, r, l);
00039        result = 1;
00040     }
00041 
00042   loc = newlocale (1 << LC_ALL, locale, NULL);
00043 
00044   r = strxfrm_l (buf, string, bufsize, loc);
00045   l = strlen (buf);
00046   if (r != l)
00047     {
00048        printf ("locale \"%s\": strxfrm_l returned %zu, strlen returned %zu\n",
00049               locale, r, l);
00050        result = 1;
00051     }
00052 
00053   freelocale (loc);
00054 
00055   free (buf);
00056 
00057   return result;
00058 }
00059 
00060 
00061 int
00062 main (void)
00063 {
00064   int result = 0;
00065 
00066   result |= test ("C");
00067   result |= test ("en_US.ISO-8859-1");
00068   result |= test ("de_DE.UTF-8");
00069 
00070   return result;
00071 }