1 /*
2  * PROJECT:         ReactOS CRT regression tests
3  * LICENSE:         GPL - See COPYING in the top level directory
4  * FILE:            rostests/regtests/crt/iofuncs.c
5  * PURPOSE:         Tests for input/output functions of the CRT
6  * PROGRAMMERS:     Gregor Schneider
7  */
8 
9 #include <stdio.h>
10 #include <wine/test.h>
11 
12 #define _CRT_NON_CONFORMING_SWPRINTFS
13 
14 struct _testData
15 {
16     double val;
17     int prec;
18     char* exp;
19     char* exp2;
20 } ECVTTESTDATA[] =
21 {
22     {   45.0,           2,      "+4.50E+001",           "+4.5000E+001"          },  /*0*/
23     {   0.0001,         1,      "+1.0E-004",            "+1.000E-004"           },
24     {   0.0001,         -10,    "+1.000000E-004",       "+1.000000E-004"        },
25     {   0.0001,         10,     "+1.0000000000E-004",   "+1.000000000000E-004"  },
26     {   -111.0001,      5,      "-1.11000E+002",        "-1.1100010E+002"       },
27     {   111.0001,       5,      "+1.11000E+002",        "+1.1100010E+002"       },  /*5*/
28     {   3333.3,         2,      "+3.33E+003",           "+3.3333E+003"          },
29     {   999999999999.9, 3,      "+1.000E+012",          "+1.00000E+012"         },
30     {   0.0,            5,      "+0.00000E+000",        "+0.0000000E+000"       },
31     {   0.0,            0,      "+0E+000",              "+0.00E+000"            },
32     {   0.0,            -1,     "+0.000000E+000",       "+0.0E+000"             },  /*10*/
33     {   -123.0001,      0,      "-1E+002",              "-1.23E+002"            },
34     {   -123.0001,      -1,     "-1.230001E+002",       "-1.2E+002"             },
35     {   -123.0001,      -2,     "-1.230001E+002",       "-1E+002"               },
36     {   -123.0001,      -3,     "-1.230001E+002",       "-1.230001E+002"        },
37     {   99.99,          1,      "+1.0E+002",            "+9.999E+001"           },  /*15*/
38     {   0.0063,         2,      "+6.30E-003",           "+6.3000E-003"          },
39     {   0.0063,         3,      "+6.300E-003",          "+6.30000E-003"         },
40     {   0.09999999996,  2,      "+1.00E-001",           "+1.0000E-001"          },
41     {   0.6,            1,      "+6.0E-001",            "+6.000E-001"           },
42     {   0.6,            0,      "+6E-001",              "+6.00E-001"            },  /*20*/
43     {   0.4,            0,      "+4E-001",              "+4.00E-001"            },
44     {   0.49,           0,      "+5E-001",              "+4.90E-001"            },
45     {   0.51,           0,      "+5E-001",              "+5.10E-001"            }
46 };
47 
48 void Test_ofuncs()
49 {
50     int i;
51     char* buf = NULL;
52 
53     /* Test exponential format */
54     buf = malloc(30 * sizeof(char));
55     if (buf == NULL)
56     {
57         printf("Memory full, exiting\n");
58         return;
59     }
60     for (i = 0; i < sizeof(ECVTTESTDATA)/sizeof(ECVTTESTDATA[0]); i++)
61     {
62         sprintf(buf, "%-+.*E", ECVTTESTDATA[i].prec, ECVTTESTDATA[i].val);
63         ok(!strcmp(buf, ECVTTESTDATA[i].exp),
64             "sprintf exp test %d failed: got %s, expected %s\n",
65             i, buf, ECVTTESTDATA[i].exp);
66     }
67     for (i = 0; i < sizeof(ECVTTESTDATA)/sizeof(ECVTTESTDATA[0]); i++)
68     {
69         sprintf(buf, "%-+.*E", ECVTTESTDATA[i].prec + 2, ECVTTESTDATA[i].val);
70         ok(!strcmp(buf, ECVTTESTDATA[i].exp2),
71             "sprintf exp +2 prec test %d failed: got %s, expected %s\n",
72             i, buf, ECVTTESTDATA[i].exp2);
73     }
74 
75     /* Test with negative number to be rounded */
76     sprintf(buf, "%-+.*E", ECVTTESTDATA[18].prec + 2, -ECVTTESTDATA[18].val);
77     ok(!strcmp(buf, "-1.0000E-001"), "Negative number sprintf rounding failed: got %s, expected %s\n",
78         buf, "-1.0000E-001");
79     free(buf);
80 }
81 
82 void Test_ifuncs()
83 {
84     double var;
85     char cnum[] = "12.3";
86     wchar_t wnum[] = L"12.3";
87 
88     /* Test sscanf behaviour */
89     sscanf(cnum, "%lf", &var);
90     ok(var == 12.3, "sscanf double conversion failed: got %f\n", var);
91     swscanf(wnum, L"%lf", &var);
92     ok(var == 12.3, "swscanf double conversion failed: got %f\n", var);
93 }
94 
95 START_TEST(iofuncs)
96 {
97     Test_ofuncs();
98     Test_ifuncs();
99 }
100 
101