Back to index

glibc  2.9
tst-sprintf2.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 
00006 int
00007 main (void)
00008 {
00009 #if LDBL_MANT_DIG >= 106
00010   volatile union { long double l; long long x[2]; } u, v;
00011   char buf[64];
00012 #endif
00013   int result = 0;
00014 
00015 #if LDBL_MANT_DIG == 106 || LDBL_MANT_DIG == 113
00016 # define COMPARE_LDBL(u, v) \
00017   ((u).l == (v).l && (u).x[0] == (v).x[0] && (u).x[1] == (v).x[1])
00018 #else
00019 # define COMPARE_LDBL(u, v) ((u).l == (v).l)
00020 #endif
00021 
00022 #define TEST(val) \
00023   do                                                              \
00024     {                                                             \
00025       u.l = (val);                                                \
00026       snprintf (buf, sizeof buf, "%LaL", u.l);                           \
00027       if (strcmp (buf, #val) != 0)                                \
00028        {                                                          \
00029          printf ("Error on line %d: %s != %s\n", __LINE__, buf, #val);          \
00030          result = 1;                                              \
00031        }                                                          \
00032       if (sscanf (#val, "%La", &v.l) != 1 || !COMPARE_LDBL (u, v))       \
00033        {                                                          \
00034          printf ("Error sscanf on line %d: %La != %La\n", __LINE__,      \
00035                 u.l, v.l);                                        \
00036          result = 1;                                              \
00037        }                                                          \
00038       /* printf ("%s %La %016Lx %016Lx\n", #val, u.l, u.x[0], u.x[1]); */  \
00039     }                                                             \
00040   while (0)
00041 
00042 #if LDBL_MANT_DIG >= 106
00043 # if LDBL_MANT_DIG == 106
00044   TEST (0x0.ffffffffffffp-1022L);
00045   TEST (0x0.ffffffffffff1p-1022L);
00046   TEST (0x0.fffffffffffffp-1022L);
00047 # endif
00048   TEST (0x1p-1022L);
00049   TEST (0x1.0000000000001p-1022L);
00050   TEST (0x1.00000000001e7p-1022L);
00051   TEST (0x1.fffffffffffffp-1022L);
00052   TEST (0x1p-1021L);
00053   TEST (0x1.00000000000008p-1021L);
00054   TEST (0x1.0000000000001p-1021L);
00055   TEST (0x1.00000000000018p-1021L);
00056   TEST (0x1.0000000000000f8p-1017L);
00057   TEST (0x1.0000000000001p-1017L);
00058   TEST (0x1.000000000000108p-1017L);
00059   TEST (0x1.000000000000dcf8p-1013L);
00060   TEST (0x1.000000000000ddp-1013L);
00061   TEST (0x1.000000000000dd08p-1013L);
00062   TEST (0x1.ffffffffffffffffffffffffffp-1L);
00063   TEST (0x1.ffffffffffffffffffffffffff8p-1L);
00064   TEST (0x1p+0L);
00065   TEST (0x1.000000000000000000000000008p+0L);
00066   TEST (0x1.00000000000000000000000001p+0L);
00067   TEST (0x1.000000000000000000000000018p+0L);
00068   TEST (0x1.23456789abcdef123456789abc8p+0L);
00069   TEST (0x1.23456789abcde7123456789abc8p+0L);
00070   TEST (0x1.23456789abcdef123456789abc8p+64L);
00071   TEST (0x1.23456789abcde7123456789abc8p+64L);
00072   TEST (0x1.123456789abcdef123456789p-969L);
00073 # if LDBL_MANT_DIG == 106
00074   TEST (-0x1.2d71957cc1263bbbeb1d365f1e8p-969L);
00075   TEST (0x1.23456789abcdef0123456789abp-970L);
00076   TEST (0x1.579bde02468acp-1001L);
00077   TEST (0x0.abcdef0123456p-1022L);
00078   TEST (0x1.abcdef0123456p-1022L);
00079   TEST (0x1.abcdef012345678p-1014L);
00080   TEST (0x1.abcdef0123456f8p-1014L);
00081 # endif
00082 #endif
00083   return result;
00084 }