| 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 ilogb returns correct values. |
| 10 | ** |
| 11 | **===================================================================*/ |
| 12 | |
| 13 | #include <palsuite.h> |
| 14 | |
| 15 | #define PAL_NAN sqrt(-1.0) |
| 16 | #define PAL_POSINF -log(0.0) |
| 17 | #define PAL_NEGINF log(0.0) |
| 18 | |
| 19 | /** |
| 20 | * Helper test structure |
| 21 | */ |
| 22 | struct test |
| 23 | { |
| 24 | double 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(double value, int expected) |
| 34 | { |
| 35 | int result = ilogb(value); |
| 36 | |
| 37 | if (result != expected) |
| 38 | { |
| 39 | Fail("ilogb(%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.11331473229676087, -4 }, // expected: -(pi) |
| 58 | { 0.15195522325791297, -3 }, // expected: -(e) |
| 59 | { 0.20269956628651730, -3 }, // expected: -(ln(10)) |
| 60 | { 0.33662253682241906, -2 }, // expected: -(pi / 2) |
| 61 | { 0.36787944117144232, -2 }, // expected: -(log2(e)) |
| 62 | { 0.37521422724648177, -2 }, // expected: -(sqrt(2)) |
| 63 | { 0.45742934732229695, -2 }, // expected: -(2 / sqrt(pi)) |
| 64 | { 0.5, -1 }, // expected: -(1) |
| 65 | { 0.58019181037172444, -1 }, // expected: -(pi / 4) |
| 66 | { 0.61254732653606592, -1 }, // expected: -(1 / sqrt(2)) |
| 67 | { 0.61850313780157598, -1 }, // expected: -(ln(2)) |
| 68 | { 0.64321824193300488, -1 }, // expected: -(2 / pi) |
| 69 | { 0.74005557395545179, -1 }, // expected: -(log10(e)) |
| 70 | { 0.80200887896145195, -1 }, // expected: -(1 / pi) |
| 71 | { 1, 0 }, |
| 72 | { 1.2468689889006383, 0 }, // expected: 1 / pi |
| 73 | { 1.3512498725672678, 0 }, // expected: log10(e) |
| 74 | { 1.5546822754821001, 0 }, // expected: 2 / pi |
| 75 | { 1.6168066722416747, 0 }, // expected: ln(2) |
| 76 | { 1.6325269194381528, 0 }, // expected: 1 / sqrt(2) |
| 77 | { 1.7235679341273495, 0 }, // expected: pi / 4 |
| 78 | { 2, 1 }, |
| 79 | { 2.1861299583286618, 1 }, // expected: 2 / sqrt(pi) |
| 80 | { 2.6651441426902252, 1 }, // expected: sqrt(2) |
| 81 | { 2.7182818284590452, 1 }, // expected: log2(e) value: e |
| 82 | { 2.9706864235520193, 1 }, // expected: pi / 2 |
| 83 | { 4.9334096679145963, 2 }, // expected: ln(10) |
| 84 | { 6.5808859910179210, 2 }, // expected: e |
| 85 | { 8.8249778270762876, 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 | |