| 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 ilogbf returns correct values. |
| 10 | ** |
| 11 | **===================================================================*/ |
| 12 | |
| 13 | #include <palsuite.h> |
| 14 | |
| 15 | #define PAL_NAN sqrtf(-1.0f) |
| 16 | #define PAL_POSINF -logf(0.0f) |
| 17 | #define PAL_NEGINF logf(0.0f) |
| 18 | |
| 19 | /** |
| 20 | * Helper test structure |
| 21 | */ |
| 22 | struct test |
| 23 | { |
| 24 | float value; /* value to test the function with */ |
| 25 | int expected; /* expected result */ |
| 26 | }; |
| 27 | |
| 28 | /** |
| 29 | * validate |
| 30 | * |
| 31 | * test validation function |
| 32 | */ |
| 33 | void __cdecl validate(float value, int expected) |
| 34 | { |
| 35 | int result = ilogbf(value); |
| 36 | |
| 37 | if (result != expected) |
| 38 | { |
| 39 | Fail("ilogbf(%g) returned %d when it should have returned %d" , |
| 40 | value, result, expected); |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * main |
| 46 | * |
| 47 | * executable entry point |
| 48 | */ |
| 49 | int __cdecl main(int argc, char **argv) |
| 50 | { |
| 51 | struct test tests[] = |
| 52 | { |
| 53 | /* value expected */ |
| 54 | { PAL_NEGINF, 2147483647 }, |
| 55 | { 0, -2147483648 }, |
| 56 | { PAL_POSINF, 2147483647 }, |
| 57 | { 0.113314732f, -4 }, // expected: -(pi) |
| 58 | { 0.151955223f, -3 }, // expected: -(e) |
| 59 | { 0.202699566f, -3 }, // expected: -(ln(10)) |
| 60 | { 0.336622537f, -2 }, // expected: -(pi / 2) |
| 61 | { 0.367879441f, -2 }, // expected: -(log2(e)) |
| 62 | { 0.375214227f, -2 }, // expected: -(sqrt(2)) |
| 63 | { 0.457429347f, -2 }, // expected: -(2 / sqrt(pi)) |
| 64 | { 0.5f, -1 }, // expected: -(1) |
| 65 | { 0.580191810f, -1 }, // expected: -(pi / 4) |
| 66 | { 0.612547327f, -1 }, // expected: -(1 / sqrt(2)) |
| 67 | { 0.618503138f, -1 }, // expected: -(ln(2)) |
| 68 | { 0.643218242f, -1 }, // expected: -(2 / pi) |
| 69 | { 0.740055574f, -1 }, // expected: -(log10(e)) |
| 70 | { 0.802008879f, -1 }, // expected: -(1 / pi) |
| 71 | { 1, 0 }, |
| 72 | { 1.24686899f, 0 }, // expected: 1 / pi |
| 73 | { 1.35124987f, 0 }, // expected: log10(e) |
| 74 | { 1.55468228f, 0 }, // expected: 2 / pi |
| 75 | { 1.61680667f, 0 }, // expected: ln(2) |
| 76 | { 1.63252692f, 0 }, // expected: 1 / sqrt(2) |
| 77 | { 1.72356793f, 0 }, // expected: pi / 4 |
| 78 | { 2, 1 }, |
| 79 | { 2.18612996f, 1 }, // expected: 2 / sqrt(pi) |
| 80 | { 2.66514414f, 1 }, // expected: sqrt(2) |
| 81 | { 2.71828183f, 1 }, // expected: log2(e) value: e |
| 82 | { 2.97068642f, 1 }, // expected: pi / 2 |
| 83 | { 4.93340967f, 2 }, // expected: ln(10) |
| 84 | { 6.58088599f, 2 }, // expected: e |
| 85 | { 8.82497783f, 3 }, // expected: pi |
| 86 | { PAL_NAN, 2147483647 }, |
| 87 | }; |
| 88 | |
| 89 | if (PAL_Initialize(argc, argv) != 0) |
| 90 | { |
| 91 | return FAIL; |
| 92 | } |
| 93 | |
| 94 | for (int i = 0; i < (sizeof(tests) / sizeof(struct test)); i++) |
| 95 | { |
| 96 | validate(tests[i].value, tests[i].expected); |
| 97 | } |
| 98 | |
| 99 | PAL_Terminate(); |
| 100 | return PASS; |
| 101 | } |
| 102 | |