1// Licensed to the .NET Foundation under one or more agreements.
2// The .NET Foundation licenses this file to you under the MIT license.
3// See the LICENSE file in the project root for more information.
4
5/*=============================================================================
6**
7** Source: test1.c (modf)
8**
9** Purpose: Test to ensure that modf return the correct values
10**
11** Dependencies: PAL_Initialize
12** PAL_Terminate
13** Fail
14** fabs
15**
16**===========================================================================*/
17
18#include <palsuite.h>
19
20// binary32 (float) has a machine epsilon of 2^-23 (approx. 1.19e-07). However, this
21// is slightly too accurate when writing tests meant to run against libm implementations
22// for various platforms. 2^-21 (approx. 4.76e-07) seems to be as accurate as we can get.
23//
24// The tests themselves will take PAL_EPSILON and adjust it according to the expected result
25// so that the delta used for comparison will compare the most significant digits and ignore
26// any digits that are outside the double precision range (6-9 digits).
27
28// For example, a test with an expect result in the format of 0.xxxxxxxxx will use PAL_EPSILON
29// for the variance, while an expected result in the format of 0.0xxxxxxxxx will use
30// PAL_EPSILON / 10 and and expected result in the format of x.xxxxxx will use PAL_EPSILON * 10.
31#define PAL_EPSILON 4.76837158e-07
32
33#define PAL_NAN sqrt(-1.0)
34#define PAL_POSINF -log(0.0)
35#define PAL_NEGINF log(0.0)
36
37/**
38 * Helper test structure
39 */
40struct test
41{
42 float value; /* value to test the function with */
43 float expected; /* expected result */
44 float variance; /* maximum delta between the expected and actual result */
45 float expected_intpart; /* expected result */
46 float variance_intpart; /* maximum delta between the expected and actual result */
47};
48
49/**
50 * validate
51 *
52 * test validation function
53 */
54void __cdecl validate(float value, float expected, float variance, float expected_intpart, float variance_intpart)
55{
56 float result_intpart;
57 float result = modff(value, &result_intpart);
58
59 /*
60 * The test is valid when the difference between result
61 * and expected is less than or equal to variance
62 */
63 float delta = fabsf(result - expected);
64 float delta_intpart = fabsf(result_intpart - expected_intpart);
65
66 if ((delta > variance) || (delta_intpart > variance_intpart))
67 {
68 Fail("modff(%g) returned %10.9g with an intpart of %10.9g when it should have returned %10.9g with an intpart of %10.9g",
69 value, result, result_intpart, expected, expected_intpart);
70 }
71}
72
73/**
74 * validate
75 *
76 * test validation function for values returning NaN
77 */
78void __cdecl validate_isnan(float value)
79{
80 float result_intpart;
81 float result = modff(value, &result_intpart);
82
83 if (!_isnan(result) || !_isnan(result_intpart))
84 {
85 Fail("modff(%g) returned %10.9g with an intpart of %10.9g when it should have returned %10.9g with an intpart of %10.9g",
86 value, result, result_intpart, PAL_NAN, PAL_NAN);
87 }
88}
89
90/**
91 * main
92 *
93 * executable entry point
94 */
95int __cdecl main(int argc, char **argv)
96{
97 struct test tests[] =
98 {
99 /* value expected variance expected_intpart variance_intpart */
100 { 0, 0, PAL_EPSILON, 0, PAL_EPSILON },
101 { 0.318309886f, 0.318309886f, PAL_EPSILON, 0, PAL_EPSILON }, // value: 1 / pi
102 { 0.434294482f, 0.434294482f, PAL_EPSILON, 0, PAL_EPSILON }, // value: log10(e)
103 { 0.636619772f, 0.636619772f, PAL_EPSILON, 0, PAL_EPSILON }, // value: 2 / pi
104 { 0.693147181f, 0.693147181f, PAL_EPSILON, 0, PAL_EPSILON }, // value: ln(2)
105 { 0.707106781f, 0.707106781f, PAL_EPSILON, 0, PAL_EPSILON }, // value: 1 / sqrt(2)
106 { 0.785398163f, 0.785398163f, PAL_EPSILON, 0, PAL_EPSILON }, // value: pi / 4
107 { 1, 0, PAL_EPSILON, 1, PAL_EPSILON * 10 },
108 { 1.12837917f, 0.128379167f, PAL_EPSILON, 1, PAL_EPSILON * 10 }, // value: 2 / sqrt(pi)
109 { 1.41421356f, 0.414213562f, PAL_EPSILON, 1, PAL_EPSILON * 10 }, // value: sqrt(2)
110 { 1.44269504f, 0.442695041f, PAL_EPSILON, 1, PAL_EPSILON * 10 }, // value: log2(e)
111 { 1.57079633f, 0.570796327f, PAL_EPSILON, 1, PAL_EPSILON * 10 }, // value: pi / 2
112 { 2.30258509f, 0.302585093f, PAL_EPSILON, 2, PAL_EPSILON * 10 }, // value: ln(10)
113 { 2.71828183f, 0.718281828f, PAL_EPSILON, 2, PAL_EPSILON * 10 }, // value: e
114 { 3.14159265f, 0.141592654f, PAL_EPSILON, 3, PAL_EPSILON * 10 }, // value: pi
115 { PAL_POSINF, 0, PAL_EPSILON, PAL_POSINF, 0 }
116
117 };
118
119 /* PAL initialization */
120 if (PAL_Initialize(argc, argv) != 0)
121 {
122 return FAIL;
123 }
124
125 for (int i = 0; i < (sizeof(tests) / sizeof(struct test)); i++)
126 {
127 validate( tests[i].value, tests[i].expected, tests[i].variance, tests[i].expected_intpart, tests[i].variance_intpart);
128 validate(-tests[i].value, -tests[i].expected, tests[i].variance, -tests[i].expected_intpart, tests[i].variance_intpart);
129 }
130
131 validate_isnan(PAL_NAN);
132
133 PAL_Terminate();
134 return PASS;
135}
136