summaryrefslogtreecommitdiff
path: root/src/pal/tests/palsuite/c_runtime/_ecvt/test1/test1.c
blob: fbcf11ecfcccf37270948216bc67fa44a08ad76c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

/*=====================================================================
**
** Source:  c_runtime/_ecvt/test1/test1.c
**
** Purpose:  Call the _ecvt function on a number of cases.  Check that it
** handles negatives, positives and double bounds correctly.  Also check that 
** the 'digit' specification works.
**
**
**===================================================================*/

#include <palsuite.h>

#define INT64_TO_DOUBLE(a) (*(double*)&a)

INT64 NaN = 0x7ff8000000000000;
INT64 NegativeInfinity = 0xfff0000000000000;
INT64 NegativeSmall = 0x8000000000000001;
INT64 PositiveInfinity = 0x7ff0000000000000;
INT64 PositiveSmall = 0x0000000000000001;

struct testCase
{
    double value;        /* number to be converted */ 
    int precision;       /* number of digits to be stored */
    int decimal;         /* (expected) decimal point position for stored 
                          * number */
    int sign;            /* (expected) return value */
    char expResult[256]; /* (expected) character array to be returned
                          * NOTE: this necessarily limits precision 
                          * to a value between 0 and 255 */
    char bsdExpResult[256]; /* (expected) character array to be returned
                          * NOTE: this necessarily limits precision 
                          * to a value between 0 and 255 */
};

int __cdecl main(int argc, char **argv)
{
    char *result;
    int testDecimal;
    int testSign;
    int i=0;

    struct testCase testCases[] =
        {
            /* odd ball values */
            {INT64_TO_DOUBLE(NaN), 7, 1, 0, "1#QNAN0" },
            /* positive values */
            {0, 0, 0, 0, ""},
            {INT64_TO_DOUBLE(PositiveSmall), 17, -323, 0, 
             "49406564584124654"},
            {.00123, 3, -2, 0, "123"},
            {.123, 3, 0, 0, "123"},
            {123, 3, 3, 0, "123"},
            {3.1415926535, 9, 1, 0, "314159265"},
            {3.1415926535, 10, 1, 0, "3141592654"},
            {3.1415926535, 11, 1, 0, "31415926535"},
            {3.1415926535, 12, 1, 0, "314159265350"},
            {184467444073709570000.0, 21, 21, 0, "184467444073709570000", 
                "184467444073709568000" },  
            {184467444073709570000.0, 22, 21, 0, "1844674440737095700000", 
                "1844674440737095680000" }, 
            {INT64_TO_DOUBLE(PositiveInfinity), 7, 1, 0, "1#INF00" },
            /* negative values */
            {-0, 0, 0, 0, ""},
            {INT64_TO_DOUBLE(NegativeSmall), 17, -323, 1, 
            "49406564584124654"},
            {-.00123, 3, -2, 1, "123"},
            {-.123, 3, 0, 1, "123"},
            {-123, 3, 3, 1, "123"},
            {-3.1415926535, 9, 1, 1, "314159265"},
            {-3.1415926535, 10, 1, 1, "3141592654"},
            {-3.1415926535, 11, 1, 1, "31415926535"},
            {-3.1415926535, 12, 1, 1, "314159265350"},
            {-184467444073709570000.0, 21, 21, 1, "184467444073709570000", 
                "184467444073709568000" },  
            {-184467444073709570000.0, 22, 21, 1, "1844674440737095700000", 
                "1844674440737095680000" },
            {INT64_TO_DOUBLE(NegativeInfinity), 7, 1, 1, "1#INF00"}

        };

    if (0 != (PAL_Initialize(argc, argv)))
    {
        return FAIL;
    }

    /* Loop through each case. Call _ecvt on each test case and check the
       result.
    */

    for(i = 0; i < sizeof(testCases) / sizeof(struct testCase); i++)
    {
        result = _ecvt(testCases[i].value, 
                       testCases[i].precision,
                       &testDecimal,
                       &testSign);
        
        if (( strcmp(testCases[i].expResult, result) != 0 && 
              strcmp(testCases[i].bsdExpResult, result) != 0 ) || 

            ( testCases[i].sign != testSign ) || 
            ( testCases[i].decimal != testDecimal ))
        
        {
            Fail("PALSUITE ERROR: Test %d\n"
                 "-----------------------\n"
                 "testCases[i].value = '%f'\n"
                 "testCases[i].precision = '%d'\n"
                 "testCases[i].decimal = '%d'\n"
                 "testCases[i].sign = '%d'\n"
                 "testCases[i].expResult = '%s'\n"
                 "result = '%s'\n"
                 "testDecimal = '%d'\n"
                 "testSign = '%d'\n\n",
                 i,
                 testCases[i].value,
                 testCases[i].precision,
                 testCases[i].decimal,
                 testCases[i].sign,
                 testCases[i].expResult,
                 result,
                 testDecimal,
                 testSign);
        }

    }

    PAL_Terminate();
    return PASS;
}