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
8**
9** Purpose: Call the sqrt function on a positive value, a positive value
10** with a decimal and on the maxium possible double value.
11**
12**
13**===================================================================*/
14
15#include <palsuite.h>
16
17// binary64 (double) has a machine epsilon of 2^-52 (approx. 2.22e-16). However, this
18// is slightly too accurate when writing tests meant to run against libm implementations
19// for various platforms. 2^-50 (approx. 8.88e-16) seems to be as accurate as we can get.
20//
21// The tests themselves will take PAL_EPSILON and adjust it according to the expected result
22// so that the delta used for comparison will compare the most significant digits and ignore
23// any digits that are outside the double precision range (15-17 digits).
24
25// For example, a test with an expect result in the format of 0.xxxxxxxxxxxxxxxxx will use
26// PAL_EPSILON for the variance, while an expected result in the format of 0.0xxxxxxxxxxxxxxxxx
27// will use PAL_EPSILON / 10 and and expected result in the format of x.xxxxxxxxxxxxxxxx will
28// use PAL_EPSILON * 10.
29#define PAL_EPSILON 8.8817841970012523e-16
30
31#define PAL_NAN sqrt(-1.0)
32#define PAL_POSINF -log(0.0)
33#define PAL_NEGINF log(0.0)
34
35/**
36 * Helper test structure
37 */
38struct test
39{
40 double value; /* value to test the function with */
41 double expected; /* expected result */
42 double variance; /* maximum delta between the expected and actual result */
43};
44
45/**
46 * validate
47 *
48 * test validation function
49 */
50void __cdecl validate(double value, double expected, double variance)
51{
52 double result = sqrt(value);
53
54 /*
55 * The test is valid when the difference between result
56 * and expected is less than or equal to variance
57 */
58 double delta = fabs(result - expected);
59
60 if (delta > variance)
61 {
62 Fail("sqrt(%g) returned %20.17g when it should have returned %20.17g",
63 value, result, expected);
64 }
65}
66
67/**
68 * validate
69 *
70 * test validation function for values returning NaN
71 */
72void __cdecl validate_isnan(double value)
73{
74 double result = sqrt(value);
75
76 if (!_isnan(result))
77 {
78 Fail("sqrt(%g) returned %20.17g when it should have returned %20.17g",
79 value, result, PAL_NAN);
80 }
81}
82
83int __cdecl main(int argc, char **argv)
84{
85 struct test tests[] =
86 {
87 /* value expected variance */
88 { 0.31830988618379067, 0.56418958354775629, PAL_EPSILON }, // value: 1 / pi
89 { 0.43429448190325183, 0.65901022898226081, PAL_EPSILON }, // value: log10(e)
90 { 0.63661977236758134, 0.79788456080286536, PAL_EPSILON }, // value: 2 / pi
91 { 0.69314718055994531, 0.83255461115769776, PAL_EPSILON }, // value: ln(2)
92 { 0.70710678118654752, 0.84089641525371454, PAL_EPSILON }, // value: 1 / sqrt(2)
93 { 0.78539816339744831, 0.88622692545275801, PAL_EPSILON }, // value: pi / 4
94 { 1, 1, PAL_EPSILON * 10 },
95 { 1.1283791670955126, 1.0622519320271969, PAL_EPSILON * 10 }, // value: 2 / sqrt(pi)
96 { 1.4142135623730950, 1.1892071150027211, PAL_EPSILON * 10 }, // value: sqrt(2)
97 { 1.4426950408889634, 1.2011224087864498, PAL_EPSILON * 10 }, // value: log2(e)
98 { 1.5707963267948966, 1.2533141373155003, PAL_EPSILON * 10 }, // value: pi / 2
99 { 2.3025850929940457, 1.5174271293851464, PAL_EPSILON * 10 }, // value: ln(10)
100 { 2.7182818284590452, 1.6487212707001281, PAL_EPSILON * 10 }, // value: e
101 { 3.1415926535897932, 1.7724538509055160, PAL_EPSILON * 10 }, // value: pi
102 };
103
104 /* PAL initialization */
105 if (PAL_Initialize(argc, argv) != 0)
106 {
107 return FAIL;
108 }
109
110 validate(-0.0, -0.0, PAL_EPSILON);
111 validate( 0.0, 0.0, PAL_EPSILON);
112
113 for (int i = 0; i < (sizeof(tests) / sizeof(struct test)); i++)
114 {
115 validate(tests[i].value, tests[i].expected, tests[i].variance);
116 validate_isnan(-tests[i].value);
117 }
118
119 validate_isnan(PAL_NAN);
120
121 PAL_Terminate();
122 return PASS;
123}
124