| 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 fmodf return the correct values |
| 10 | ** |
| 11 | ** Dependencies: PAL_Initialize |
| 12 | ** PAL_Terminate |
| 13 | ** Fail |
| 14 | ** fabsf |
| 15 | ** |
| 16 | **===========================================================================*/ |
| 17 | |
| 18 | #include <palsuite.h> |
| 19 | |
| 20 | // binary32 (float) has a machine epsilon of 2^-23 (approx. 1.19e-07). However, this |
| 21 | // is slightly too accurate when writing tests meant to run against libm implementations |
| 22 | // for various platforms. 2^-21 (approx. 4.76e-07) 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 (6-9 digits). |
| 27 | |
| 28 | // For example, a test with an expect result in the format of 0.xxxxxxxxx will use PAL_EPSILON |
| 29 | // for the variance, while an expected result in the format of 0.0xxxxxxxxx will use |
| 30 | // PAL_EPSILON / 10 and and expected result in the format of x.xxxxxx will use PAL_EPSILON * 10. |
| 31 | #define PAL_EPSILON 4.76837158e-07 |
| 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 numerator; /* second component of the value to test the function with */ |
| 43 | float denominator; /* first component of the value to test the function with */ |
| 44 | float expected; /* expected result */ |
| 45 | float variance; /* maximum delta between the expected and actual result */ |
| 46 | }; |
| 47 | |
| 48 | /** |
| 49 | * validate |
| 50 | * |
| 51 | * test validation function |
| 52 | */ |
| 53 | void __cdecl validate(float numerator, float denominator, float expected, float variance) |
| 54 | { |
| 55 | float result = fmodf(numerator, denominator); |
| 56 | |
| 57 | /* |
| 58 | * The test is valid when the difference between result |
| 59 | * and expected is less than or equal to variance |
| 60 | */ |
| 61 | float delta = fabsf(result - expected); |
| 62 | |
| 63 | if (delta > variance) |
| 64 | { |
| 65 | Fail("fmodf(%g, %g) returned %10.9g when it should have returned %10.9g" , |
| 66 | numerator, denominator, result, expected); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * validate |
| 72 | * |
| 73 | * test validation function for values returning NaN |
| 74 | */ |
| 75 | void __cdecl validate_isnan(float numerator, float denominator) |
| 76 | { |
| 77 | float result = fmodf(numerator, denominator); |
| 78 | |
| 79 | if (!_isnan(result)) |
| 80 | { |
| 81 | Fail("fmodf(%g, %g) returned %10.9g when it should have returned %10.9g" , |
| 82 | numerator, denominator, result, PAL_NAN); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * main |
| 88 | * |
| 89 | * executable entry point |
| 90 | */ |
| 91 | INT __cdecl main(INT argc, CHAR **argv) |
| 92 | { |
| 93 | struct test tests[] = |
| 94 | { |
| 95 | /* numerator denominator expected variance */ |
| 96 | { 0, PAL_POSINF, 0, PAL_EPSILON }, |
| 97 | { 0.312961796f, 0.949765715f, 0.312961796f, PAL_EPSILON }, |
| 98 | { 0.420770483f, 0.907167129f, 0.420770483f, PAL_EPSILON }, |
| 99 | { 0.594480769f, 0.804109828f, 0.594480769f, PAL_EPSILON }, |
| 100 | { 0.638961276f, 0.769238901f, 0.638961276f, PAL_EPSILON }, |
| 101 | { 0.649636939f, 0.760244597f, 0.649636939f, PAL_EPSILON }, |
| 102 | { 0.707106781f, 0.707106781f, 0, PAL_EPSILON }, |
| 103 | { 1, 1, 0, PAL_EPSILON }, |
| 104 | { 0.841470985f, 0.540302306f, 0.301168679f, PAL_EPSILON }, |
| 105 | { 0.903719457f, 0.428125148f, 0.0474691617f, PAL_EPSILON / 10 }, |
| 106 | { 0.987765946f, 0.155943695f, 0.0521037774f, PAL_EPSILON / 10 }, |
| 107 | { 0.991806244f, 0.127751218f, 0.0975477216f, PAL_EPSILON / 10 }, |
| 108 | { 0.743980337f, -0.668201510f, 0.0757788268f, PAL_EPSILON / 10 }, |
| 109 | { 0.410781291f, -0.911733915f, 0.410781291f, PAL_EPSILON }, |
| 110 | { 0, -1, 0, PAL_EPSILON }, |
| 111 | { 1, PAL_POSINF, 1, PAL_EPSILON * 10 }, |
| 112 | }; |
| 113 | |
| 114 | |
| 115 | // PAL initialization |
| 116 | if (PAL_Initialize(argc, argv) != 0) |
| 117 | { |
| 118 | return FAIL; |
| 119 | } |
| 120 | |
| 121 | for (int i = 0; i < (sizeof(tests) / sizeof(struct test)); i++) |
| 122 | { |
| 123 | validate( tests[i].numerator, tests[i].denominator, tests[i].expected, tests[i].variance); |
| 124 | validate(-tests[i].numerator, tests[i].denominator, -tests[i].expected, tests[i].variance); |
| 125 | validate( tests[i].numerator, -tests[i].denominator, tests[i].expected, tests[i].variance); |
| 126 | validate(-tests[i].numerator, -tests[i].denominator, -tests[i].expected, tests[i].variance); |
| 127 | } |
| 128 | |
| 129 | validate_isnan( 0, 0); |
| 130 | validate_isnan(-0.0f, 0); |
| 131 | validate_isnan( 0, -0.0f); |
| 132 | validate_isnan(-0.0f, -0.0f); |
| 133 | |
| 134 | validate_isnan( 1, 0); |
| 135 | validate_isnan(-1, 0); |
| 136 | validate_isnan( 1, -0.0f); |
| 137 | validate_isnan(-1, -0.0f); |
| 138 | |
| 139 | validate_isnan(PAL_POSINF, PAL_POSINF); |
| 140 | validate_isnan(PAL_NEGINF, PAL_POSINF); |
| 141 | validate_isnan(PAL_POSINF, PAL_NEGINF); |
| 142 | validate_isnan(PAL_NEGINF, PAL_NEGINF); |
| 143 | |
| 144 | validate_isnan(PAL_POSINF, 0); |
| 145 | validate_isnan(PAL_NEGINF, 0); |
| 146 | validate_isnan(PAL_POSINF, -0.0f); |
| 147 | validate_isnan(PAL_NEGINF, -0.0f); |
| 148 | |
| 149 | validate_isnan(PAL_POSINF, 1); |
| 150 | validate_isnan(PAL_NEGINF, 1); |
| 151 | validate_isnan(PAL_POSINF, -1); |
| 152 | validate_isnan(PAL_NEGINF, -1); |
| 153 | |
| 154 | PAL_Terminate(); |
| 155 | return PASS; |
| 156 | } |
| 157 | |