Back to index

glibc  2.9
tstdiomisc.c
Go to the documentation of this file.
00001 #include <float.h>
00002 #include <math.h>
00003 #include <stdio.h>
00004 #include <string.h>
00005 #include <wchar.h>
00006 
00007 static int
00008 t1 (void)
00009 {
00010   int n = -1;
00011   sscanf ("abc  ", "abc %n", &n);
00012   printf ("t1: count=%d\n", n);
00013 
00014   return n != 5;
00015 }
00016 
00017 static int
00018 t2 (void)
00019 {
00020   int result = 0;
00021   int n;
00022   long N;
00023   int retval;
00024 #define SCAN(INPUT, FORMAT, VAR, EXP_RES, EXP_VAL) \
00025   VAR = -1; \
00026   retval = sscanf (INPUT, FORMAT, &VAR); \
00027   printf ("sscanf (\"%s\", \"%s\", &x) => %d, x = %ld\n", \
00028          INPUT, FORMAT, retval, (long int) VAR); \
00029   result |= retval != EXP_RES || VAR != EXP_VAL
00030 
00031   SCAN ("12345", "%ld", N, 1, 12345);
00032   SCAN ("12345", "%llllld", N, 0, -1);
00033   SCAN ("12345", "%LLLLLd", N, 0, -1);
00034   SCAN ("test ", "%*s%n",  n, 0, 4);
00035   SCAN ("test ", "%2*s%n",  n, 0, -1);
00036   SCAN ("12 ",   "%l2d",  n, 0, -1);
00037   SCAN ("12 ",   "%2ld",  N, 1, 12);
00038 
00039   n = -1;
00040   N = -1;
00041   retval = sscanf ("1 1", "%d %Z", &n, &N);
00042   printf ("sscanf (\"1 1\", \"%%d %%Z\", &n, &N) => %d, n = %d, N = %ld\n", \
00043          retval, n, N); \
00044   result |= retval != 1 || n != 1 || N != -1;
00045 
00046   return result;
00047 }
00048 
00049 volatile double nanval;
00050 
00051 
00052 static int
00053 F (void)
00054 {
00055   char buf[20];
00056   wchar_t wbuf[10];
00057   int result;
00058 
00059   nanval = NAN;
00060 
00061   snprintf (buf, sizeof buf, "%f %F", nanval, nanval);
00062   result = strcmp (buf, "nan NAN") != 0;
00063   printf ("expected \"nan NAN\", got \"%s\"\n", buf);
00064 
00065   snprintf (buf, sizeof buf, "%f %F", DBL_MAX * DBL_MAX, DBL_MAX * DBL_MAX);
00066   result |= strcmp (buf, "inf INF") != 0;
00067   printf ("expected \"inf INF\", got \"%s\"\n", buf);
00068 
00069   swprintf (wbuf, sizeof wbuf / sizeof (wbuf[0]), L"%f %F", nanval, nanval);
00070   result |= wcscmp (wbuf, L"nan NAN") != 0;
00071   printf ("expected L\"nan NAN\", got L\"%S\"\n", wbuf);
00072 
00073   swprintf (wbuf, sizeof wbuf / sizeof (wbuf[0]), L"%f %F",
00074            DBL_MAX * DBL_MAX, DBL_MAX * DBL_MAX);
00075   result |= wcscmp (wbuf, L"inf INF") != 0;
00076   printf ("expected L\"inf INF\", got L\"%S\"\n", wbuf);
00077 
00078   return result;
00079 }
00080 
00081 int
00082 main (int argc, char *argv[])
00083 {
00084   int result = 0;
00085 
00086   result |= t1 ();
00087   result |= t2 ();
00088   result |= F ();
00089 
00090   result |= fflush (stdout) == EOF;
00091 
00092   return result;
00093 }