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