Back to index

glibc  2.9
tst-digits.c
Go to the documentation of this file.
00001 /* Copyright (C) 2000 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003    Contributed by Ulrich Drepper <drepper@gnu.org>, 2000.
00004 
00005    The GNU C Library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public
00007    License as published by the Free Software Foundation; either
00008    version 2.1 of the License, or (at your option) any later version.
00009 
00010    The GNU C Library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Lesser General Public License for more details.
00014 
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with the GNU C Library; if not, write to the Free
00017    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00018    02111-1307 USA.  */
00019 
00020 #include <ctype.h>
00021 #include <langinfo.h>
00022 #include <locale.h>
00023 #include <stdio.h>
00024 #include <stdlib.h>
00025 #include <string.h>
00026 #include <unistd.h>
00027 #include <wchar.h>
00028 #include <wctype.h>
00029 #include <sys/types.h>
00030 
00031 
00032 #define ZERO  "\xe2\x82\x80"
00033 #define ONE   "\xe2\x82\x81"
00034 #define TWO   "\xe2\x82\x82"
00035 #define THREE "\xe2\x82\x83"
00036 #define FOUR  "\xe2\x82\x84"
00037 #define FIVE  "\xe2\x82\x85"
00038 #define SIX   "\xe2\x82\x86"
00039 #define SEVEN "\xe2\x82\x87"
00040 #define EIGHT "\xe2\x82\x88"
00041 #define NINE  "\xe2\x82\x89"
00042 
00043 static struct printf_int_test
00044 {
00045   int n;
00046   const char *format;
00047   const char *expected;
00048 } printf_int_tests[] =
00049 {
00050   {       0, "%I'10d", "       " ZERO },
00051   {       1, "%I'10d", "       " ONE },
00052   {       2, "%I'10d", "       " TWO },
00053   {       3, "%I'10d", "       " THREE },
00054   {       4, "%I'10d", "       " FOUR },
00055   {       5, "%I'10d", "       " FIVE },
00056   {       6, "%I'10d", "       " SIX },
00057   {       7, "%I'10d", "       " SEVEN },
00058   {       8, "%I'10d", "       " EIGHT },
00059   {       9, "%I'10d", "       " NINE },
00060   {      11, "%I'10d", "    " ONE ONE },
00061   {      12, "%I'10d", "    " ONE TWO },
00062   {     123, "%I10d",  " " ONE TWO THREE },
00063   {     123, "%I'10d", " " ONE TWO THREE },
00064   {    1234, "%I10d",  ONE TWO THREE FOUR },
00065   {    1234, "%I'10d", ONE "," TWO THREE FOUR },
00066   {   12345, "%I'10d", ONE TWO "," THREE FOUR FIVE },
00067   {  123456, "%I'10d", ONE TWO THREE "," FOUR FIVE SIX },
00068   { 1234567, "%I'10d", ONE "," TWO THREE FOUR "," FIVE SIX SEVEN }
00069 };
00070 #define nprintf_int_tests \
00071   (sizeof (printf_int_tests) / sizeof (printf_int_tests[0]))
00072 
00073 #define WZERO  L"\x2080"
00074 #define WONE   L"\x2081"
00075 #define WTWO   L"\x2082"
00076 #define WTHREE L"\x2083"
00077 #define WFOUR  L"\x2084"
00078 #define WFIVE  L"\x2085"
00079 #define WSIX   L"\x2086"
00080 #define WSEVEN L"\x2087"
00081 #define WEIGHT L"\x2088"
00082 #define WNINE  L"\x2089"
00083 
00084 static struct wprintf_int_test
00085 {
00086   int n;
00087   const wchar_t *format;
00088   const wchar_t *expected;
00089 } wprintf_int_tests[] =
00090 {
00091   {       0, L"%I'10d", L"         " WZERO },
00092   {       1, L"%I'10d", L"         " WONE },
00093   {       2, L"%I'10d", L"         " WTWO },
00094   {       3, L"%I'10d", L"         " WTHREE },
00095   {       4, L"%I'10d", L"         " WFOUR },
00096   {       5, L"%I'10d", L"         " WFIVE },
00097   {       6, L"%I'10d", L"         " WSIX },
00098   {       7, L"%I'10d", L"         " WSEVEN },
00099   {       8, L"%I'10d", L"         " WEIGHT },
00100   {       9, L"%I'10d", L"         " WNINE },
00101   {      11, L"%I'10d", L"        " WONE WONE },
00102   {      12, L"%I'10d", L"        " WONE WTWO },
00103   {     123, L"%I10d",  L"       " WONE WTWO WTHREE },
00104   {     123, L"%I'10d", L"       " WONE WTWO WTHREE },
00105   {    1234, L"%I10d",  L"      " WONE WTWO WTHREE WFOUR },
00106   {    1234, L"%I'10d", L"     " WONE L"," WTWO WTHREE WFOUR },
00107   {   12345, L"%I'10d", L"    " WONE WTWO L"," WTHREE WFOUR WFIVE },
00108   {  123456, L"%I'10d", L"   " WONE WTWO WTHREE L"," WFOUR WFIVE WSIX },
00109   { 1234567, L"%I'10d", L" " WONE L"," WTWO WTHREE WFOUR L"," WFIVE WSIX WSEVEN }
00110 };
00111 #define nwprintf_int_tests \
00112   (sizeof (wprintf_int_tests) / sizeof (wprintf_int_tests[0]))
00113 
00114 
00115 int
00116 main (void)
00117 {
00118   int cnt;
00119   int failures;
00120   int status;
00121 
00122   if (setlocale (LC_ALL, "test7") == NULL)
00123     {
00124       puts ("cannot set locale `test7'");
00125       exit (1);
00126     }
00127   printf ("CODESET = \"%s\"\n", nl_langinfo (CODESET));
00128 
00129   /* First: printf tests.  */
00130   failures = 0;
00131   for (cnt = 0; cnt < (int) nprintf_int_tests; ++cnt)
00132     {
00133       char buf[100];
00134       ssize_t n;
00135 
00136       n = snprintf (buf, sizeof buf, printf_int_tests[cnt].format,
00137                   printf_int_tests[cnt].n);
00138 
00139       printf ("%3d: got \"%s\", expected \"%s\"",
00140              cnt, buf, printf_int_tests[cnt].expected);
00141 
00142       if (n != (ssize_t) strlen (printf_int_tests[cnt].expected)
00143          || strcmp (buf, printf_int_tests[cnt].expected) != 0)
00144        {
00145          puts ("  -> FAILED");
00146          ++failures;
00147        }
00148       else
00149        puts ("  -> OK");
00150     }
00151 
00152   printf ("%d failures in printf tests\n", failures);
00153   status = failures != 0;
00154 
00155   /* wprintf tests.  */
00156   failures = 0;
00157   for (cnt = 0; cnt < (int) nwprintf_int_tests; ++cnt)
00158     {
00159       wchar_t buf[100];
00160       ssize_t n;
00161 
00162       n = swprintf (buf, sizeof buf / sizeof (buf[0]),
00163                   wprintf_int_tests[cnt].format,
00164                   wprintf_int_tests[cnt].n);
00165 
00166       printf ("%3d: got \"%ls\", expected \"%ls\"",
00167              cnt, buf, wprintf_int_tests[cnt].expected);
00168 
00169       if (n != (ssize_t) wcslen (wprintf_int_tests[cnt].expected)
00170          || wcscmp (buf, wprintf_int_tests[cnt].expected) != 0)
00171        {
00172          puts ("  -> FAILED");
00173          ++failures;
00174        }
00175       else
00176        puts ("  -> OK");
00177     }
00178 
00179   printf ("%d failures in wprintf tests\n", failures);
00180   status = failures != 0;
00181 
00182   /* ctype tests.  This makes sure that the multibyte chracter digit
00183      representations are not handle in this table.  */
00184   failures = 0;
00185   for (cnt = 0; cnt < 256; ++cnt)
00186     if (cnt >= '0' && cnt <= '9')
00187       {
00188        if (! isdigit (cnt))
00189          {
00190            printf ("isdigit ('%c') == 0\n", cnt);
00191            ++failures;
00192          }
00193       }
00194     else
00195       {
00196        if (isdigit (cnt))
00197          {
00198            printf ("isdigit (%d) != 0\n", cnt);
00199            ++failures;
00200          }
00201       }
00202 
00203   printf ("%d failures in ctype tests\n", failures);
00204   status = failures != 0;
00205 
00206   /* wctype tests.  This makes sure the second set of digits is also
00207      recorded.  */
00208   failures = 0;
00209   for (cnt = 0; cnt < 256; ++cnt)
00210     if (cnt >= '0' && cnt <= '9')
00211       {
00212        if (! iswdigit (cnt))
00213          {
00214            printf ("iswdigit (L'%c') == 0\n", cnt);
00215            ++failures;
00216          }
00217       }
00218     else
00219       {
00220        if (iswdigit (cnt))
00221          {
00222            printf ("iswdigit (%d) != 0\n", cnt);
00223            ++failures;
00224          }
00225       }
00226 
00227   for (cnt = 0x2070; cnt < 0x2090; ++cnt)
00228     if (cnt >= 0x2080 && cnt <= 0x2089)
00229       {
00230        if (! iswdigit (cnt))
00231          {
00232            printf ("iswdigit (U%04X) == 0\n", cnt);
00233            ++failures;
00234          }
00235       }
00236     else
00237       {
00238        if (iswdigit (cnt))
00239          {
00240            printf ("iswdigit (U%04X) != 0\n", cnt);
00241            ++failures;
00242          }
00243       }
00244 
00245   printf ("%d failures in wctype tests\n", failures);
00246   status = failures != 0;
00247 
00248   return status;
00249 }