summaryrefslogtreecommitdiff
path: root/src/pal/tests/palsuite/c_runtime/_isnanf/test1/test1.c
blob: 9b75a7236dc17a39f01797b293a378d7e73ba49f (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
// 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: 
** Test _isnanf with a number of trivial values, to ensure they indicated that
** they are numbers.  Then try with Positive/Negative Infinite, which should
** also be numbers.  Finally set the least and most significant bits of 
** the fraction to positive and negative, at which point it should return
** the true value. 
**
**==========================================================================*/

#include <palsuite.h>

#define TO_FLOAT(x)    (*((float*)((void*)&x)))
#define TO_I32(x)      (*((INT32*)((void*)&x)))

/*
 * NaN: any float with maximum exponent (0x7f8) and non-zero fraction
 */
int __cdecl main(int argc, char *argv[])
{
    /*
     * Initialize the PAL and return FAIL if this fails
     */
    if (PAL_Initialize(argc, argv) != 0)
    {
        return FAIL;
    }

    /*
     * Try some trivial values
     */
    if (_isnanf(0.0f))
    {
        Fail("_isnanf() incorrectly identified %f as NaN!\n", 0.0f);
    }

    if (_isnanf(1.234567f))
    {
        Fail("_isnanf() incorrectly identified %f as NaN!\n", 1.234567f);
    }

    if (_isnanf(42.0f))
    {
        Fail("_isnanf() incorrectly identified %f as NaN!\n", 42.0f);
    }

    UINT32 lneginf =           0xff800000u;
    UINT32 lposinf =           0x7f800000u;
    
    float neginf =             TO_FLOAT(lneginf);
    float posinf =             TO_FLOAT(lposinf);

    /*
     * Try positive and negative infinity
     */
    if (_isnanf(neginf))
    {
        Fail("_isnanf() incorrectly identified negative infinity as NaN!\n");
    }

    if (_isnanf(posinf))
    {
        Fail("_isnanf() incorrectly identified infinity as NaN!\n");
    }

    /*
     * Try setting the least significant bit of the fraction,
     * positive and negative
     */
    UINT32 lsnan =             0xff800001u;
    float snan =               TO_FLOAT(lsnan);
    
    if (!_isnanf(snan))
    {
        Fail("_isnanf() failed to identify %I32x as NaN!\n", lsnan);
    }

    UINT32 lqnan =             0x7f800001u;
    float qnan =               TO_FLOAT(lqnan);
    
    if (!_isnanf(qnan))
    {
        Fail("_isnanf() failed to identify %I32x as NaN!\n", lqnan);
    }

    /*
     * Try setting the most significant bit of the fraction,
     * positive and negative
     */
    lsnan =                     0xffc00000u;
    snan =                      TO_FLOAT(lsnan);

    if (!_isnanf(snan))
    {
        Fail ("_isnanf() failed to identify %I32x as NaN!\n", lsnan);
    }

    lqnan =                     0x7fc00000u;
    qnan =                      TO_FLOAT(lqnan);

    if (!_isnanf(qnan))
    {
        Fail ("_isnanf() failed to identify %I32x as NaN!\n", lqnan);
    }

    PAL_Terminate();
    return PASS;
}