Back to index

glibc  2.9
tst-strtod5.c
Go to the documentation of this file.
00001 #include <locale.h>
00002 #include <stdio.h>
00003 #include <stdlib.h>
00004 #include <string.h>
00005 #include <math.h>
00006 
00007 #define NBSP "\xc2\xa0"
00008 
00009 static const struct
00010 {
00011   const char *in;
00012   int group;
00013   double expected;
00014 } tests[] =
00015   {
00016     { "0", 0, 0.0 },
00017     { "000", 0, 0.0 },
00018     { "-0", 0, -0.0 },
00019     { "-000", 0, -0.0 },
00020     { "0,", 0, 0.0 },
00021     { "-0,", 0, -0.0 },
00022     { "0,0", 0, 0.0 },
00023     { "-0,0", 0, -0.0 },
00024     { "0e-10", 0, 0.0 },
00025     { "-0e-10", 0, -0.0 },
00026     { "0,e-10", 0, 0.0 },
00027     { "-0,e-10", 0, -0.0 },
00028     { "0,0e-10", 0, 0.0 },
00029     { "-0,0e-10", 0, -0.0 },
00030     { "0e-1000000", 0, 0.0 },
00031     { "-0e-1000000", 0, -0.0 },
00032     { "0,0e-1000000", 0, 0.0 },
00033     { "-0,0e-1000000", 0, -0.0 },
00034     { "0", 1, 0.0 },
00035     { "000", 1, 0.0 },
00036     { "-0", 1, -0.0 },
00037     { "-000", 1, -0.0 },
00038     { "0e-10", 1, 0.0 },
00039     { "-0e-10", 1, -0.0 },
00040     { "0e-1000000", 1, 0.0 },
00041     { "-0e-1000000", 1, -0.0 },
00042     { "000"NBSP"000"NBSP"000", 1, 0.0 },
00043     { "-000"NBSP"000"NBSP"000", 1, -0.0 }
00044   };
00045 #define NTESTS (sizeof (tests) / sizeof (tests[0]))
00046 
00047 
00048 static int
00049 do_test (void)
00050 {
00051   if (setlocale (LC_ALL, "cs_CZ.UTF-8") == NULL)
00052     {
00053       puts ("could not set locale");
00054       return 1;
00055     }
00056 
00057   int status = 0;
00058 
00059   for (int i = 0; i < NTESTS; ++i)
00060     {
00061       char *ep;
00062       double r;
00063 
00064       if (tests[i].group)
00065        r = __strtod_internal (tests[i].in, &ep, 1);
00066       else
00067        r = strtod (tests[i].in, &ep);
00068 
00069       if (*ep != '\0')
00070        {
00071          printf ("%d: got rest string \"%s\", expected \"\"\n", i, ep);
00072          status = 1;
00073        }
00074 
00075       if (r != tests[i].expected
00076          || copysign (10.0, r) != copysign (10.0, tests[i].expected))
00077        {
00078          printf ("%d: got wrong results %g, expected %g\n",
00079                 i, r, tests[i].expected);
00080          status = 1;
00081        }
00082     }
00083 
00084   return status;
00085 }
00086 
00087 #define TEST_FUNCTION do_test ()
00088 #include "../test-skeleton.c"