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: Tests logf with a normal set of values.
10**
11**===================================================================*/
12
13#include <palsuite.h>
14
15// binary32 (float) has a machine epsilon of 2^-23 (approx. 1.19e-07). However, this
16// is slightly too accurate when writing tests meant to run against libm implementations
17// for various platforms. 2^-21 (approx. 4.76e-07) seems to be as accurate as we can get.
18//
19// The tests themselves will take PAL_EPSILON and adjust it according to the expected result
20// so that the delta used for comparison will compare the most significant digits and ignore
21// any digits that are outside the double precision range (6-9 digits).
22
23// For example, a test with an expect result in the format of 0.xxxxxxxxx will use PAL_EPSILON
24// for the variance, while an expected result in the format of 0.0xxxxxxxxx will use
25// PAL_EPSILON / 10 and and expected result in the format of x.xxxxxx will use PAL_EPSILON * 10.
26#define PAL_EPSILON 4.76837158e-07
27
28#define PAL_NAN sqrtf(-1.0f)
29#define PAL_POSINF -logf(0.0f)
30#define PAL_NEGINF logf(0.0f)
31
32/**
33 * Helper test structure
34 */
35struct test
36{
37 float value; /* value to test the function with */
38 float expected; /* expected result */
39 float variance; /* maximum delta between the expected and actual result */
40};
41
42/**
43 * validate
44 *
45 * test validation function
46 */
47void __cdecl validate(float value, float expected, float variance)
48{
49 float result = logf(value);
50
51 /*
52 * The test is valid when the difference between result
53 * and expected is less than or equal to variance
54 */
55 float delta = fabsf(result - expected);
56
57 if (delta > variance)
58 {
59 Fail("logf(%g) returned %10.9g when it should have returned %10.9g",
60 value, result, expected);
61 }
62}
63
64/**
65 * validate
66 *
67 * test validation function for values returning NaN
68 */
69void __cdecl validate_isnan(float value)
70{
71 float result = logf(value);
72
73 if (!_isnanf(result))
74 {
75 Fail("logf(%g) returned %10.9g when it should have returned %10.9g",
76 value, result, PAL_NAN);
77 }
78}
79
80/**
81 * main
82 *
83 * executable entry point
84 */
85int __cdecl main(int argc, char **argv)
86{
87 struct test tests[] =
88 {
89 /* value expected variance */
90 { 0, PAL_NEGINF, 0 },
91 { 0.0432139183f, -3.14159265f, PAL_EPSILON * 10 }, // expected: -(pi)
92 { 0.0659880358f, -2.71828183f, PAL_EPSILON * 10 }, // expected: -(e)
93 { 0.1f, -2.30258509f, PAL_EPSILON * 10 }, // expected: -(ln(10))
94 { 0.207879576f, -1.57079633f, PAL_EPSILON * 10 }, // expected: -(pi / 2)
95 { 0.236290088f, -1.44269504f, PAL_EPSILON * 10 }, // expected: -(logf2(e))
96 { 0.243116734f, -1.41421356f, PAL_EPSILON * 10 }, // expected: -(sqrtf(2))
97 { 0.323557264f, -1.12837917f, PAL_EPSILON * 10 }, // expected: -(2 / sqrtf(pi))
98 { 0.367879441f, -1, PAL_EPSILON * 10 }, // expected: -(1)
99 { 0.455938128f, -0.785398163f, PAL_EPSILON }, // expected: -(pi / 4)
100 { 0.493068691f, -0.707106781f, PAL_EPSILON }, // expected: -(1 / sqrtf(2))
101 { 0.5f, -0.693147181f, PAL_EPSILON }, // expected: -(ln(2))
102 { 0.529077808f, -0.636619772f, PAL_EPSILON }, // expected: -(2 / pi)
103 { 0.647721485f, -0.434294482f, PAL_EPSILON }, // expected: -(log10f(e))
104 { 0.727377349f, -0.318309886f, PAL_EPSILON }, // expected: -(1 / pi)
105 { 1, 0, PAL_EPSILON },
106 { 1.37480223f, 0.318309886f, PAL_EPSILON }, // expected: 1 / pi
107 { 1.54387344f, 0.434294482f, PAL_EPSILON }, // expected: log10f(e)
108 { 1.89008116f, 0.636619772f, PAL_EPSILON }, // expected: 2 / pi
109 { 2, 0.693147181f, PAL_EPSILON }, // expected: ln(2)
110 { 2.02811498f, 0.707106781f, PAL_EPSILON }, // expected: 1 / sqrtf(2)
111 { 2.19328005f, 0.785398163f, PAL_EPSILON }, // expected: pi / 4
112 { 2.71828183f, 1, PAL_EPSILON * 10 }, // value: e
113 { 3.09064302f, 1.12837917f, PAL_EPSILON * 10 }, // expected: 2 / sqrtf(pi)
114 { 4.11325038f, 1.41421356f, PAL_EPSILON * 10 }, // expected: sqrtf(2)
115 { 4.23208611f, 1.44269504f, PAL_EPSILON * 10 }, // expected: logf2(e)
116 { 4.81047738f, 1.57079633f, PAL_EPSILON * 10 }, // expected: pi / 2
117 { 10, 2.30258509f, PAL_EPSILON * 10 }, // expected: ln(10)
118 { 15.1542622f, 2.71828183f, PAL_EPSILON * 10 }, // expected: e
119 { 23.1406926f, 3.14159265f, PAL_EPSILON * 10 }, // expected: pi
120 { PAL_POSINF, PAL_POSINF, 0 },
121 };
122
123
124 if (PAL_Initialize(argc, argv) != 0)
125 {
126 return FAIL;
127 }
128
129 for (int i = 0; i < (sizeof(tests) / sizeof(struct test)); i++)
130 {
131 validate(tests[i].value, tests[i].expected, tests[i].variance);
132 }
133
134 validate_isnan(PAL_NEGINF);
135 validate_isnan(PAL_NAN);
136
137 PAL_Terminate();
138 return PASS;
139}
140