| 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 sqrtf function on a positive value, a positive value |
| 10 | ** with a decimal and on the maxium possible float value. |
| 11 | ** |
| 12 | ** |
| 13 | **===================================================================*/ |
| 14 | |
| 15 | #include <palsuite.h> |
| 16 | |
| 17 | // binary32 (float) has a machine epsilon of 2^-23 (approx. 1.19e-07). However, this |
| 18 | // is slightly too accurate when writing tests meant to run against libm implementations |
| 19 | // for various platforms. 2^-21 (approx. 4.76e-07) 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 (6-9 digits). |
| 24 | |
| 25 | // For example, a test with an expect result in the format of 0.xxxxxxxxx will use PAL_EPSILON |
| 26 | // for the variance, while an expected result in the format of 0.0xxxxxxxxx will use |
| 27 | // PAL_EPSILON / 10 and and expected result in the format of x.xxxxxx will use PAL_EPSILON * 10. |
| 28 | #define PAL_EPSILON 4.76837158e-07 |
| 29 | |
| 30 | #define PAL_NAN sqrtf(-1.0f) |
| 31 | #define PAL_POSINF -logf(0.0f) |
| 32 | #define PAL_NEGINF logf(0.0f) |
| 33 | |
| 34 | /** |
| 35 | * Helper test structure |
| 36 | */ |
| 37 | struct test |
| 38 | { |
| 39 | float value; /* value to test the function with */ |
| 40 | float expected; /* expected result */ |
| 41 | float variance; /* maximum delta between the expected and actual result */ |
| 42 | }; |
| 43 | |
| 44 | /** |
| 45 | * validate |
| 46 | * |
| 47 | * test validation function |
| 48 | */ |
| 49 | void __cdecl validate(float value, float expected, float variance) |
| 50 | { |
| 51 | float result = sqrtf(value); |
| 52 | |
| 53 | /* |
| 54 | * The test is valid when the difference between result |
| 55 | * and expected is less than or equal to variance |
| 56 | */ |
| 57 | float delta = fabsf(result - expected); |
| 58 | |
| 59 | if (delta > variance) |
| 60 | { |
| 61 | Fail("sqrtf(%g) returned %10.9g when it should have returned %10.9g" , |
| 62 | value, result, expected); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * validate |
| 68 | * |
| 69 | * test validation function for values returning NaN |
| 70 | */ |
| 71 | void __cdecl validate_isnan(float value) |
| 72 | { |
| 73 | float result = sqrtf(value); |
| 74 | |
| 75 | if (!_isnanf(result)) |
| 76 | { |
| 77 | Fail("sqrtf(%g) returned %10.9g when it should have returned %10.9g" , |
| 78 | value, result, PAL_NAN); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | int __cdecl main(int argc, char **argv) |
| 83 | { |
| 84 | struct test tests[] = |
| 85 | { |
| 86 | /* value expected variance */ |
| 87 | { 0.318309886f, 0.564189584f, PAL_EPSILON }, // value: 1 / pi |
| 88 | { 0.434294482f, 0.659010229f, PAL_EPSILON }, // value: log10f(e) |
| 89 | { 0.636619772f, 0.797884561f, PAL_EPSILON }, // value: 2 / pi |
| 90 | { 0.693147181f, 0.832554611f, PAL_EPSILON }, // value: ln(2) |
| 91 | { 0.707106781f, 0.840896415f, PAL_EPSILON }, // value: 1 / sqrtf(2) |
| 92 | { 0.785398163f, 0.886226925f, PAL_EPSILON }, // value: pi / 4 |
| 93 | { 1, 1, PAL_EPSILON * 10 }, |
| 94 | { 1.12837917f, 1.06225193f, PAL_EPSILON * 10 }, // value: 2 / sqrtf(pi) |
| 95 | { 1.41421356f, 1.18920712f, PAL_EPSILON * 10 }, // value: sqrtf(2) |
| 96 | { 1.44269504f, 1.20112241f, PAL_EPSILON * 10 }, // value: logf2(e) |
| 97 | { 1.57079633f, 1.25331414f, PAL_EPSILON * 10 }, // value: pi / 2 |
| 98 | { 2.30258509f, 1.51742713f, PAL_EPSILON * 10 }, // value: ln(10) |
| 99 | { 2.71828183f, 1.64872127f, PAL_EPSILON * 10 }, // value: e |
| 100 | { 3.14159265f, 1.77245385F, PAL_EPSILON * 10 }, // value: pi |
| 101 | }; |
| 102 | |
| 103 | /* PAL initialization */ |
| 104 | if (PAL_Initialize(argc, argv) != 0) |
| 105 | { |
| 106 | return FAIL; |
| 107 | } |
| 108 | |
| 109 | validate(-0.0f, -0.0f, PAL_EPSILON); |
| 110 | validate( 0.0f, 0.0f, PAL_EPSILON); |
| 111 | |
| 112 | for (int i = 0; i < (sizeof(tests) / sizeof(struct test)); i++) |
| 113 | { |
| 114 | validate(tests[i].value, tests[i].expected, tests[i].variance); |
| 115 | validate_isnan(-tests[i].value); |
| 116 | } |
| 117 | |
| 118 | validate_isnan(PAL_NAN); |
| 119 | |
| 120 | PAL_Terminate(); |
| 121 | return PASS; |
| 122 | } |
| 123 | |