| 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 fabsf return the correct values |
| 10 | ** |
| 11 | ** Dependencies: PAL_Initialize |
| 12 | ** PAL_Terminate |
| 13 | ** Fail |
| 14 | ** |
| 15 | **===========================================================================*/ |
| 16 | |
| 17 | #include <palsuite.h> |
| 18 | |
| 19 | // binary32 (float) has a machine epsilon of 2^-23 (approx. 1.19e-07). However, this |
| 20 | // is slightly too accurate when writing tests meant to run against libm implementations |
| 21 | // for various platforms. 2^-21 (approx. 4.76e-07) 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 (6-9 digits). |
| 26 | |
| 27 | // For example, a test with an expect result in the format of 0.xxxxxxxxx will use PAL_EPSILON |
| 28 | // for the variance, while an expected result in the format of 0.0xxxxxxxxx will use |
| 29 | // PAL_EPSILON / 10 and and expected result in the format of x.xxxxxx will use PAL_EPSILON * 10. |
| 30 | #define PAL_EPSILON 4.76837158e-07 |
| 31 | |
| 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 | */ |
| 40 | struct 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 | }; |
| 46 | |
| 47 | /** |
| 48 | * validate |
| 49 | * |
| 50 | * test validation function |
| 51 | */ |
| 52 | void __cdecl validate(float value, float expected, float variance) |
| 53 | { |
| 54 | float result = fabsf(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 | float delta = fabsf(result - expected); |
| 61 | |
| 62 | if (delta > variance) |
| 63 | { |
| 64 | Fail("fabsf(%g) returned %10.9g when it should have returned %10.9g" , |
| 65 | value, result, expected); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * validate |
| 71 | * |
| 72 | * test validation function for values returning NaN |
| 73 | */ |
| 74 | void __cdecl validate_isnan(float value) |
| 75 | { |
| 76 | float result = fabsf(value); |
| 77 | |
| 78 | if (!_isnan(result)) |
| 79 | { |
| 80 | Fail("fabsf(%g) returned %10.9g when it should have returned %10.9g" , |
| 81 | value, result, PAL_NAN); |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * main |
| 87 | * |
| 88 | * executable entry point |
| 89 | */ |
| 90 | INT __cdecl main(INT argc, CHAR **argv) |
| 91 | { |
| 92 | struct test tests[] = |
| 93 | { |
| 94 | /* value expected variance */ |
| 95 | { PAL_NEGINF, PAL_POSINF, 0 }, |
| 96 | { -3.14159265f, 3.14159265f, PAL_EPSILON * 10 }, // value: -(pi) expected: pi |
| 97 | { -2.71828183f, 2.71828183f, PAL_EPSILON * 10 }, // value: -(e) expected: e |
| 98 | { -2.30258509f, 2.30258509f, PAL_EPSILON * 10 }, // value: -(ln(10)) expected: ln(10) |
| 99 | { -1.57079633f, 1.57079633f, PAL_EPSILON * 10 }, // value: -(pi / 2) expected: pi / 2 |
| 100 | { -1.44269504f, 1.44269504f, PAL_EPSILON * 10 }, // value: -(log2(e)) expected: log2(e) |
| 101 | { -1.41421356f, 1.41421356f, PAL_EPSILON * 10 }, // value: -(sqrt(2)) expected: sqrt(2) |
| 102 | { -1.12837917f, 1.12837917f, PAL_EPSILON * 10 }, // value: -(2 / sqrt(pi)) expected: 2 / sqrt(pi) |
| 103 | { -1, 1, PAL_EPSILON * 10 }, |
| 104 | { -0.785398163f, 0.785398163f, PAL_EPSILON }, // value: -(pi / 4) expected: pi / 4 |
| 105 | { -0.707106781f, 0.707106781f, PAL_EPSILON }, // value: -(1 / sqrt(2)) expected: 1 / sqrt(2) |
| 106 | { -0.693147181f, 0.693147181f, PAL_EPSILON }, // value: -(ln(2)) expected: ln(2) |
| 107 | { -0.636619772f, 0.636619772f, PAL_EPSILON }, // value: -(2 / pi) expected: 2 / pi |
| 108 | { -0.434294482f, 0.434294482f, PAL_EPSILON }, // value: -(log10(e)) expected: log10(e) |
| 109 | { -0.318309886f, 0.318309886f, PAL_EPSILON }, // value: -(1 / pi) expected: 1 / pi |
| 110 | { -0.0f, 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 | |