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