summaryrefslogtreecommitdiff
path: root/src/pal/tests/palsuite/c_runtime/atof/test1/test1.cpp
blob: a973133f9ec19a7bafe18ef0dc8f36d09d26e573 (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
// 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:  test1.c
**
** Purpose: Passes to atof() a series of strings containing floats,
**          checking that each one is correctly extracted.
**
**
**==========================================================================*/

#include <palsuite.h>
#include <float.h>

struct testCase
{
    float fvalue;
    char avalue[20];
};

int __cdecl main(int argc, char **argv)
{
    int i = 0;
    double f = 0;
    struct testCase testCases[] =
    {
        {1234, "1234"},
        {-1234, "-1234"},
        {1234e-5, "1234e-5"},
        {1234e+5, "1234e+5"},
        {1234e5, "1234E5"},
        {1234.567e-8, "1234.567e-8"},
        {1234.567e-8, "   1234.567e-8 foo"},
        {0,"a12"}
    };

    /*
     *  Initialize the PAL and return FAIL if this fails
     */
    if (0 != (PAL_Initialize(argc, argv)))
    {
        return FAIL;
    }

    for(i = 0; i < sizeof(testCases) / sizeof(struct testCase); i++)
    {
        /*Convert the string to a float.*/
        f = atof(testCases[i].avalue);
        double result = f - testCases[i].fvalue;

        if (fabs(result) > FLT_EPSILON)
        {
            Fail ("atof misinterpreted \"%s\" as %g instead of %g. result %g fabs %g\n",
                testCases[i].avalue, f, testCases[i].fvalue, result, fabs(result));
        }
    }
    PAL_Terminate();
    return PASS;
}