| 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 that log2 returns correct values. |
| 10 | ** |
| 11 | **===================================================================*/ |
| 12 | |
| 13 | #include <palsuite.h> |
| 14 | |
| 15 | // binary64 (double) has a machine epsilon of 2^-52 (approx. 2.22e-16). However, this |
| 16 | // is slightly too accurate when writing tests meant to run against libm implementations |
| 17 | // for various platforms. 2^-50 (approx. 8.88e-16) 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 (15-17 digits). |
| 22 | |
| 23 | // For example, a test with an expect result in the format of 0.xxxxxxxxxxxxxxxxx will use |
| 24 | // PAL_EPSILON for the variance, while an expected result in the format of 0.0xxxxxxxxxxxxxxxxx |
| 25 | // will use PAL_EPSILON / 10 and and expected result in the format of x.xxxxxxxxxxxxxxxx will |
| 26 | // use PAL_EPSILON * 10. |
| 27 | #define PAL_EPSILON 8.8817841970012523e-16 |
| 28 | |
| 29 | #define PAL_NAN sqrt(-1.0) |
| 30 | #define PAL_POSINF -log(0.0) |
| 31 | #define PAL_NEGINF log(0.0) |
| 32 | |
| 33 | /** |
| 34 | * Helper test structure |
| 35 | */ |
| 36 | struct test |
| 37 | { |
| 38 | double value; /* value to test the function with */ |
| 39 | double expected; /* expected result */ |
| 40 | double variance; /* maximum delta between the expected and actual result */ |
| 41 | }; |
| 42 | |
| 43 | /** |
| 44 | * validate |
| 45 | * |
| 46 | * test validation function |
| 47 | */ |
| 48 | void __cdecl validate(double value, double expected, double variance) |
| 49 | { |
| 50 | double result = log2(value); |
| 51 | |
| 52 | /* |
| 53 | * The test is valid when the difference between result |
| 54 | * and expected is less than or equal to variance |
| 55 | */ |
| 56 | double delta = fabs(result - expected); |
| 57 | |
| 58 | if (delta > variance) |
| 59 | { |
| 60 | Fail("log2(%g) returned %20.17g when it should have returned %20.17g" , |
| 61 | value, result, expected); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * validate |
| 67 | * |
| 68 | * test validation function for values returning NaN |
| 69 | */ |
| 70 | void __cdecl validate_isnan(double value) |
| 71 | { |
| 72 | double result = log2(value); |
| 73 | |
| 74 | if (!_isnan(result)) |
| 75 | { |
| 76 | Fail("log2(%g) returned %20.17g when it should have returned %20.17g" , |
| 77 | value, result, PAL_NAN); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * main |
| 83 | * |
| 84 | * executable entry point |
| 85 | */ |
| 86 | int __cdecl main(int argc, char **argv) |
| 87 | { |
| 88 | struct test tests[] = |
| 89 | { |
| 90 | /* value expected variance */ |
| 91 | { 0, PAL_NEGINF, 0 }, |
| 92 | { 0.11331473229676087, -3.1415926535897932, PAL_EPSILON * 10 }, // expected: -(pi) |
| 93 | { 0.15195522325791297, -2.7182818284590452, PAL_EPSILON * 10 }, // expected: -(e) |
| 94 | { 0.20269956628651730, -2.3025850929940457, PAL_EPSILON * 10 }, // expected: -(ln(10)) |
| 95 | { 0.33662253682241906, -1.5707963267948966, PAL_EPSILON * 10 }, // expected: -(pi / 2) |
| 96 | { 0.36787944117144232, -1.4426950408889634, PAL_EPSILON * 10 }, // expected: -(log2(e)) |
| 97 | { 0.37521422724648177, -1.4142135623730950, PAL_EPSILON * 10 }, // expected: -(sqrt(2)) |
| 98 | { 0.45742934732229695, -1.1283791670955126, PAL_EPSILON * 10 }, // expected: -(2 / sqrt(pi)) |
| 99 | { 0.5, -1, PAL_EPSILON * 10 }, // expected: -(1) |
| 100 | { 0.58019181037172444, -0.78539816339744831, PAL_EPSILON }, // expected: -(pi / 4) |
| 101 | { 0.61254732653606592, -0.70710678118654752, PAL_EPSILON }, // expected: -(1 / sqrt(2)) |
| 102 | { 0.61850313780157598, -0.69314718055994531, PAL_EPSILON }, // expected: -(ln(2)) |
| 103 | { 0.64321824193300488, -0.63661977236758134, PAL_EPSILON }, // expected: -(2 / pi) |
| 104 | { 0.74005557395545179, -0.43429448190325183, PAL_EPSILON }, // expected: -(log10(e)) |
| 105 | { 0.80200887896145195, -0.31830988618379067, PAL_EPSILON }, // expected: -(1 / pi) |
| 106 | { 1, 0, PAL_EPSILON }, |
| 107 | { 1.2468689889006383, 0.31830988618379067, PAL_EPSILON }, // expected: 1 / pi |
| 108 | { 1.3512498725672678, 0.43429448190325183, PAL_EPSILON }, // expected: log10(e) |
| 109 | { 1.5546822754821001, 0.63661977236758134, PAL_EPSILON }, // expected: 2 / pi |
| 110 | { 1.6168066722416747, 0.69314718055994531, PAL_EPSILON }, // expected: ln(2) |
| 111 | { 1.6325269194381528, 0.70710678118654752, PAL_EPSILON }, // expected: 1 / sqrt(2) |
| 112 | { 1.7235679341273495, 0.78539816339744831, PAL_EPSILON }, // expected: pi / 4 |
| 113 | { 2, 1, PAL_EPSILON * 10 }, |
| 114 | { 2.1861299583286618, 1.1283791670955126, PAL_EPSILON * 10 }, // expected: 2 / sqrt(pi) |
| 115 | { 2.6651441426902252, 1.4142135623730950, PAL_EPSILON * 10 }, // expected: sqrt(2) |
| 116 | { 2.7182818284590452, 1.4426950408889634, PAL_EPSILON * 10 }, // expected: log2(e) value: e |
| 117 | { 2.9706864235520193, 1.5707963267948966, PAL_EPSILON * 10 }, // expected: pi / 2 |
| 118 | { 4.9334096679145963, 2.3025850929940457, PAL_EPSILON * 10 }, // expected: ln(10) |
| 119 | { 6.5808859910179210, 2.7182818284590452, PAL_EPSILON * 10 }, // expected: e |
| 120 | { 8.8249778270762876, 3.1415926535897932, PAL_EPSILON * 10 }, // expected: pi |
| 121 | { PAL_POSINF, PAL_POSINF, 0 }, |
| 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 | |