ilogb_test.c revision 136387
1136387Sstefanf/*- 2136387Sstefanf * Copyright (c) 2004 Stefan Farfeleder 3136387Sstefanf * All rights reserved. 4136387Sstefanf * 5136387Sstefanf * Redistribution and use in source and binary forms, with or without 6136387Sstefanf * modification, are permitted provided that the following conditions 7136387Sstefanf * are met: 8136387Sstefanf * 1. Redistributions of source code must retain the above copyright 9136387Sstefanf * notice, this list of conditions and the following disclaimer. 10136387Sstefanf * 2. Redistributions in binary form must reproduce the above copyright 11136387Sstefanf * notice, this list of conditions and the following disclaimer in the 12136387Sstefanf * documentation and/or other materials provided with the distribution. 13136387Sstefanf * 14136387Sstefanf * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15136387Sstefanf * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16136387Sstefanf * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17136387Sstefanf * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18136387Sstefanf * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19136387Sstefanf * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20136387Sstefanf * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21136387Sstefanf * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22136387Sstefanf * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23136387Sstefanf * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24136387Sstefanf * SUCH DAMAGE. 25136387Sstefanf * 26136387Sstefanf * $FreeBSD: head/tools/regression/lib/msun/test-ilogb.c 136387 2004-10-11 18:40:45Z stefanf $ 27136387Sstefanf */ 28136387Sstefanf 29136387Sstefanf#include <assert.h> 30136387Sstefanf#include <float.h> 31136387Sstefanf#include <limits.h> 32136387Sstefanf#include <math.h> 33136387Sstefanf#include <stdio.h> 34136387Sstefanf#include <stdlib.h> 35136387Sstefanf 36136387Sstefanfint 37136387Sstefanfmain(void) 38136387Sstefanf{ 39136387Sstefanf char buf[128], *end; 40136387Sstefanf double d; 41136387Sstefanf float f; 42136387Sstefanf long double ld; 43136387Sstefanf int e, i; 44136387Sstefanf 45136387Sstefanf assert(ilogb(0) == FP_ILOGB0); 46136387Sstefanf assert(ilogb(NAN) == FP_ILOGBNAN); 47136387Sstefanf assert(ilogb(INFINITY) == INT_MAX); 48136387Sstefanf for (e = DBL_MIN_EXP - DBL_MANT_DIG; e < DBL_MAX_EXP; e++) { 49136387Sstefanf snprintf(buf, sizeof(buf), "0x1.p%d", e); 50136387Sstefanf d = strtod(buf, &end); 51136387Sstefanf assert(*end == '\0'); 52136387Sstefanf i = ilogb(d); 53136387Sstefanf assert(i == e); 54136387Sstefanf } 55136387Sstefanf printf("PASS ilogb\n"); 56136387Sstefanf 57136387Sstefanf assert(ilogbf(0) == FP_ILOGB0); 58136387Sstefanf assert(ilogbf(NAN) == FP_ILOGBNAN); 59136387Sstefanf assert(ilogbf(INFINITY) == INT_MAX); 60136387Sstefanf for (e = FLT_MIN_EXP - FLT_MANT_DIG; e < FLT_MAX_EXP; e++) { 61136387Sstefanf snprintf(buf, sizeof(buf), "0x1.p%d", e); 62136387Sstefanf f = strtof(buf, &end); 63136387Sstefanf assert(*end == '\0'); 64136387Sstefanf i = ilogbf(f); 65136387Sstefanf assert(i == e); 66136387Sstefanf } 67136387Sstefanf printf("PASS ilogbf\n"); 68136387Sstefanf 69136387Sstefanf assert(ilogbl(0) == FP_ILOGB0); 70136387Sstefanf assert(ilogbl(NAN) == FP_ILOGBNAN); 71136387Sstefanf assert(ilogbl(INFINITY) == INT_MAX); 72136387Sstefanf for (e = LDBL_MIN_EXP - LDBL_MANT_DIG; e < LDBL_MAX_EXP; e++) { 73136387Sstefanf snprintf(buf, sizeof(buf), "0x1.p%d", e); 74136387Sstefanf ld = strtold(buf, &end); 75136387Sstefanf assert(*end == '\0'); 76136387Sstefanf i = ilogbl(ld); 77136387Sstefanf assert(i == e); 78136387Sstefanf } 79136387Sstefanf printf("PASS ilogbl\n"); 80136387Sstefanf 81136387Sstefanf return (0); 82136387Sstefanf} 83