Back to index

glibc  2.9
tst-swprintf.c
Go to the documentation of this file.
00001 #include <locale.h>
00002 #include <stdio.h>
00003 #include <stdlib.h>
00004 #include <wchar.h>
00005 
00006 /* This is the relevant piece from the charmap:
00007 <UFF61>     /x8e/xa1     HALFWIDTH IDEOGRAPHIC FULL STOP
00008 <UFF62>     /x8e/xa2     HALFWIDTH LEFT CORNER BRACKET
00009 <UFF63>     /x8e/xa3     HALFWIDTH RIGHT CORNER BRACKET
00010 <UFF64>     /x8e/xa4     HALFWIDTH IDEOGRAPHIC COMMA
00011  */
00012 
00013 const char input[] = "\x8e\xa1g\x8e\xa2h\x8e\xa3i\x8e\xa4j";
00014 
00015 int
00016 main (void)
00017 {
00018   wchar_t buf[1000];
00019 #define nbuf (sizeof (buf) / sizeof (buf[0]))
00020   int result = 0;
00021   ssize_t n;
00022 
00023   if (setlocale (LC_ALL, "ja_JP.EUC-JP") == NULL)
00024     {
00025       puts ("cannot set locale");
00026       exit (1);
00027     }
00028 
00029 #define CHECK(fmt, nexp, exp) \
00030   n = swprintf (buf, nbuf, fmt, input);                                     \
00031   if (n != nexp)                                                     \
00032     {                                                                \
00033       printf ("swprintf (.., .., L\"%ls\", \"%ls\") return %d, not %d\n",     \
00034              fmt, (wchar_t*) input, (int) n, (int) nexp);                   \
00035       result = 1;                                                    \
00036     }                                                                \
00037   else if (wcscmp (buf, exp) != 0)                                   \
00038     {                                                                \
00039       printf ("\
00040 swprintf (.., .., L\"%ls\", \"%ls\") produced \"%ls\", not \"%ls\"\n",             \
00041             fmt, (wchar_t *) input, buf, exp );                      \
00042       result = 1;                                                    \
00043     }
00044 
00045   CHECK (L"[%-6.0s]", 8, L"[      ]");
00046   CHECK (L"[%-6.1s]", 8, L"[\xff61     ]");
00047   CHECK (L"[%-6.2s]", 8, L"[\xff61g    ]");
00048   CHECK (L"[%-6.3s]", 8, L"[\xff61g\xff62   ]");
00049   CHECK (L"[%-6.4s]", 8, L"[\xff61g\xff62h  ]");
00050   CHECK (L"[%-6.5s]", 8, L"[\xff61g\xff62h\xff63 ]");
00051   CHECK (L"[%-6.6s]", 8, L"[\xff61g\xff62h\xff63i]");
00052   CHECK (L"[%-6.7s]", 9, L"[\xff61g\xff62h\xff63i\xff64]");
00053   CHECK (L"[%-6.8s]", 10, L"[\xff61g\xff62h\xff63i\xff64j]");
00054 
00055   return result;
00056 }