ilogb_test.c revision 302408
1210284Sjmallett/*- 2232812Sjmallett * Copyright (c) 2004 Stefan Farfeleder 3215990Sjmallett * All rights reserved. 4210284Sjmallett * 5210284Sjmallett * Redistribution and use in source and binary forms, with or without 6215990Sjmallett * modification, are permitted provided that the following conditions 7215990Sjmallett * are met: 8215990Sjmallett * 1. Redistributions of source code must retain the above copyright 9210284Sjmallett * notice, this list of conditions and the following disclaimer. 10215990Sjmallett * 2. Redistributions in binary form must reproduce the above copyright 11215990Sjmallett * notice, this list of conditions and the following disclaimer in the 12210284Sjmallett * documentation and/or other materials provided with the distribution. 13215990Sjmallett * 14215990Sjmallett * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15215990Sjmallett * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16215990Sjmallett * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17215990Sjmallett * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18232812Sjmallett * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19215990Sjmallett * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20215990Sjmallett * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21215990Sjmallett * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22215990Sjmallett * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23215990Sjmallett * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24215990Sjmallett * SUCH DAMAGE. 25215990Sjmallett * 26215990Sjmallett * $FreeBSD: stable/11/lib/msun/tests/ilogb_test.c 292328 2015-12-16 09:11:11Z ngie $ 27215990Sjmallett */ 28215990Sjmallett 29232812Sjmallett#include <assert.h> 30215990Sjmallett#include <float.h> 31215990Sjmallett#include <limits.h> 32215990Sjmallett#include <math.h> 33215990Sjmallett#include <stdio.h> 34215990Sjmallett#include <stdlib.h> 35215990Sjmallett 36215990Sjmallettint 37215990Sjmallettmain(void) 38210284Sjmallett{ 39210284Sjmallett char buf[128], *end; 40210284Sjmallett double d; 41210284Sjmallett float f; 42210284Sjmallett long double ld; 43210284Sjmallett int e, i; 44210284Sjmallett 45215990Sjmallett printf("1..3\n"); 46210284Sjmallett assert(ilogb(0) == FP_ILOGB0); 47210284Sjmallett assert(ilogb(NAN) == FP_ILOGBNAN); 48210284Sjmallett assert(ilogb(INFINITY) == INT_MAX); 49210284Sjmallett for (e = DBL_MIN_EXP - DBL_MANT_DIG; e < DBL_MAX_EXP; e++) { 50210284Sjmallett snprintf(buf, sizeof(buf), "0x1.p%d", e); 51210284Sjmallett d = strtod(buf, &end); 52232812Sjmallett assert(*end == '\0'); 53210284Sjmallett i = ilogb(d); 54215990Sjmallett assert(i == e); 55215990Sjmallett } 56215990Sjmallett printf("ok 1 - ilogb\n"); 57215990Sjmallett 58215990Sjmallett assert(ilogbf(0) == FP_ILOGB0); 59215990Sjmallett assert(ilogbf(NAN) == FP_ILOGBNAN); 60215990Sjmallett assert(ilogbf(INFINITY) == INT_MAX); 61215990Sjmallett for (e = FLT_MIN_EXP - FLT_MANT_DIG; e < FLT_MAX_EXP; e++) { 62215990Sjmallett snprintf(buf, sizeof(buf), "0x1.p%d", e); 63215990Sjmallett f = strtof(buf, &end); 64215990Sjmallett assert(*end == '\0'); 65215990Sjmallett i = ilogbf(f); 66215990Sjmallett assert(i == e); 67210284Sjmallett } 68210284Sjmallett printf("ok 2 - ilogbf\n"); 69215990Sjmallett 70215990Sjmallett assert(ilogbl(0) == FP_ILOGB0); 71215990Sjmallett assert(ilogbl(NAN) == FP_ILOGBNAN); 72215990Sjmallett assert(ilogbl(INFINITY) == INT_MAX); 73215990Sjmallett for (e = LDBL_MIN_EXP - LDBL_MANT_DIG; e < LDBL_MAX_EXP; e++) { 74215990Sjmallett snprintf(buf, sizeof(buf), "0x1.p%d", e); 75215990Sjmallett ld = strtold(buf, &end); 76210284Sjmallett assert(*end == '\0'); 77232812Sjmallett i = ilogbl(ld); 78232812Sjmallett assert(i == e); 79232812Sjmallett } 80232812Sjmallett printf("ok 3 - ilogbl\n"); 81232812Sjmallett 82232812Sjmallett return (0); 83210284Sjmallett} 84210284Sjmallett