summaryrefslogtreecommitdiff
path: root/src/pal/tests/palsuite/c_runtime/_gcvt/test2/test2.c
blob: 7ac9a4fcf099dd163dbd10b6bf45f606845d8d53 (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
// 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:  test2.c
**
** Purpose:  Call the _gcvt function on a number of cases.  Check that it
** handles negatives, exponents and hex digits properly.  Also check that 
** the 'digit' specification works. (And that it doesn't truncate negative
** signs or decimals)  
**
**
**===================================================================*/

#include <palsuite.h>

struct testCase
{
    double Value;
    int Digits;
    char WinCorrectResult[128];
    char BsdCorrectResult[128]; /* for the odd case where bsd sprintf 
                                    varies from windows sprintf */
};

int __cdecl main(int argc, char **argv)
{
    char result[128];
    int i=0;

    struct testCase testCases[] =
    {
        {1234567,  7, "1234567"},
        {1234.123, 7, "1234.123"},
        {1234.1234, 7, "1234.123"},
        {12.325678e+2, 7, "1232.568"},
        {-12.3233333, 8, "-12.323333"},
        {-12.32, 8, "-12.32"},
        {-12.32e+2, 8, "-1232.", "-1232" },
        {0x21DDFABC, 8, "5.6819577e+008", "5.6819577e+08" },
        {123456789012345.0, 15, "123456789012345" },
        {12340000.0, 8, "12340000"},
        {12340000000000000.0, 15, "1.234e+016", "1.234e+16" },
        {12340000000000000.0, 17, "12340000000000000"  },
        
    };

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

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

    for(i = 0; i < sizeof(testCases) / sizeof(struct testCase); i++)
    {
        _gcvt(testCases[i].Value, testCases[i].Digits, result);

        if (strcmp(testCases[i].WinCorrectResult, result) != 0 && 
            
            ( testCases[i].BsdCorrectResult && 
              strcmp(testCases[i].BsdCorrectResult, result) != 0 ) )
        {
            Fail("ERROR: _gcvt attempted to convert %f with %d digits "
                 "signifigant, which resulted in "
                 "the string '%s' instead of the correct(Win) string '%s' or the"
                 "correct(bsd) string '%s'.\n",
                 testCases[i].Value,
                 testCases[i].Digits,
                 result,
                 testCases[i].WinCorrectResult,
                 testCases[i].BsdCorrectResult);
        }

        memset(result, '\0', 128);
    }
    PAL_Terminate();
    return PASS;
}