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 ceilf with simple positive and negative values. Also tests |
10 | ** extreme cases like extremely small values and positive and |
11 | ** negative infinity. Makes sure that calling ceilf on NaN returns |
12 | ** NaN |
13 | ** |
14 | **==========================================================================*/ |
15 | |
16 | #include <palsuite.h> |
17 | |
18 | // binary32 (float) has a machine epsilon of 2^-23 (approx. 1.19e-07). However, this |
19 | // is slightly too accurate when writing tests meant to run against libm implementations |
20 | // for various platforms. 2^-21 (approx. 4.76e-07) seems to be as accurate as we can get. |
21 | // |
22 | // The tests themselves will take PAL_EPSILON and adjust it according to the expected result |
23 | // so that the delta used for comparison will compare the most significant digits and ignore |
24 | // any digits that are outside the double precision range (6-9 digits). |
25 | |
26 | // For example, a test with an expect result in the format of 0.xxxxxxxxx will use PAL_EPSILON |
27 | // for the variance, while an expected result in the format of 0.0xxxxxxxxx will use |
28 | // PAL_EPSILON / 10 and and expected result in the format of x.xxxxxx will use PAL_EPSILON * 10. |
29 | #define PAL_EPSILON 4.76837158e-07 |
30 | |
31 | #define PAL_NAN sqrtf(-1.0f) |
32 | #define PAL_POSINF -logf(0.0f) |
33 | #define PAL_NEGINF logf(0.0f) |
34 | |
35 | /** |
36 | * Helper test structure |
37 | */ |
38 | struct test |
39 | { |
40 | float value; /* value to test the function with */ |
41 | float expected; /* expected result */ |
42 | float variance; /* maximum delta between the expected and actual result */ |
43 | }; |
44 | |
45 | /** |
46 | * validate |
47 | * |
48 | * test validation function |
49 | */ |
50 | void __cdecl validate(float value, float expected, float variance) |
51 | { |
52 | float result = ceilf(value); |
53 | |
54 | /* |
55 | * The test is valid when the difference between result |
56 | * and expected is less than or equal to variance |
57 | */ |
58 | float delta = fabsf(result - expected); |
59 | |
60 | if (delta > variance) |
61 | { |
62 | Fail("ceilf(%g) returned %10.9g when it should have returned %10.9g" , |
63 | value, result, expected); |
64 | } |
65 | } |
66 | |
67 | /** |
68 | * validate |
69 | * |
70 | * test validation function for values returning NaN |
71 | */ |
72 | void __cdecl validate_isnan(float value) |
73 | { |
74 | float result = ceilf(value); |
75 | |
76 | if (!_isnanf(result)) |
77 | { |
78 | Fail("ceilf(%g) returned %10.9g when it should have returned %10.9g" , |
79 | value, result, PAL_NAN); |
80 | } |
81 | } |
82 | |
83 | /** |
84 | * main |
85 | * |
86 | * executable entry point |
87 | */ |
88 | int __cdecl main(int argc, char *argv[]) |
89 | { |
90 | struct test tests[] = |
91 | { |
92 | /* value expected variance */ |
93 | { 0.318309886f, 1, PAL_EPSILON * 10 }, // value: 1 / pi |
94 | { 0.434294482f, 1, PAL_EPSILON * 10 }, // value: log10f(e) |
95 | { 0.636619772f, 1, PAL_EPSILON * 10 }, // value: 2 / pi |
96 | { 0.693147181f, 1, PAL_EPSILON * 10 }, // value: ln(2) |
97 | { 0.707106781f, 1, PAL_EPSILON * 10 }, // value: 1 / sqrtf(2) |
98 | { 0.785398163f, 1, PAL_EPSILON * 10 }, // value: pi / 4 |
99 | { 1.12837917f, 2, PAL_EPSILON * 10 }, // value: 2 / sqrtf(pi) |
100 | { 1.41421356f, 2, PAL_EPSILON * 10 }, // value: sqrtf(2) |
101 | { 1.44269504f, 2, PAL_EPSILON * 10 }, // value: logf2(e) |
102 | { 1.57079633f, 2, PAL_EPSILON * 10 }, // value: pi / 2 |
103 | { 2.30258509f, 3, PAL_EPSILON * 10 }, // value: ln(10) |
104 | { 2.71828183f, 3, PAL_EPSILON * 10 }, // value: e |
105 | { 3.14159265f, 4, PAL_EPSILON * 10 }, // value: pi |
106 | { PAL_POSINF, PAL_POSINF, 0 } |
107 | }; |
108 | |
109 | /* PAL initialization */ |
110 | if (PAL_Initialize(argc, argv) != 0) |
111 | { |
112 | return FAIL; |
113 | } |
114 | |
115 | validate( 0, 0, PAL_EPSILON); |
116 | validate(-0.0f, 0, PAL_EPSILON); |
117 | |
118 | validate( 1, 1, PAL_EPSILON * 10); |
119 | validate(-1.0f, -1, PAL_EPSILON * 10); |
120 | |
121 | for (int i = 0; i < (sizeof(tests) / sizeof(struct test)); i++) |
122 | { |
123 | validate( tests[i].value, tests[i].expected, tests[i].variance); |
124 | validate(-tests[i].value, 1 - tests[i].expected, tests[i].variance); |
125 | } |
126 | |
127 | validate_isnan(PAL_NAN); |
128 | |
129 | PAL_Terminate(); |
130 | return PASS; |
131 | } |
132 | |