ilogb_test.c revision 136387
1101131Sscottl/*-
2101131Sscottl * Copyright (c) 2004 Stefan Farfeleder
3101131Sscottl * All rights reserved.
4101131Sscottl *
5101131Sscottl * Redistribution and use in source and binary forms, with or without
6101131Sscottl * modification, are permitted provided that the following conditions
7101131Sscottl * are met:
8101131Sscottl * 1. Redistributions of source code must retain the above copyright
9101131Sscottl *    notice, this list of conditions and the following disclaimer.
10101131Sscottl * 2. Redistributions in binary form must reproduce the above copyright
11101131Sscottl *    notice, this list of conditions and the following disclaimer in the
12101131Sscottl *    documentation and/or other materials provided with the distribution.
13101131Sscottl *
14101131Sscottl * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15101131Sscottl * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16101131Sscottl * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 *
26 * $FreeBSD: head/tools/regression/lib/msun/test-ilogb.c 136387 2004-10-11 18:40:45Z stefanf $
27 */
28
29#include <assert.h>
30#include <float.h>
31#include <limits.h>
32#include <math.h>
33#include <stdio.h>
34#include <stdlib.h>
35
36int
37main(void)
38{
39	char buf[128], *end;
40	double d;
41	float f;
42	long double ld;
43	int e, i;
44
45	assert(ilogb(0) == FP_ILOGB0);
46	assert(ilogb(NAN) == FP_ILOGBNAN);
47	assert(ilogb(INFINITY) == INT_MAX);
48	for (e = DBL_MIN_EXP - DBL_MANT_DIG; e < DBL_MAX_EXP; e++) {
49		snprintf(buf, sizeof(buf), "0x1.p%d", e);
50		d = strtod(buf, &end);
51		assert(*end == '\0');
52		i = ilogb(d);
53		assert(i == e);
54	}
55	printf("PASS ilogb\n");
56
57	assert(ilogbf(0) == FP_ILOGB0);
58	assert(ilogbf(NAN) == FP_ILOGBNAN);
59	assert(ilogbf(INFINITY) == INT_MAX);
60	for (e = FLT_MIN_EXP - FLT_MANT_DIG; e < FLT_MAX_EXP; e++) {
61		snprintf(buf, sizeof(buf), "0x1.p%d", e);
62		f = strtof(buf, &end);
63		assert(*end == '\0');
64		i = ilogbf(f);
65		assert(i == e);
66	}
67	printf("PASS ilogbf\n");
68
69	assert(ilogbl(0) == FP_ILOGB0);
70	assert(ilogbl(NAN) == FP_ILOGBNAN);
71	assert(ilogbl(INFINITY) == INT_MAX);
72	for (e = LDBL_MIN_EXP - LDBL_MANT_DIG; e < LDBL_MAX_EXP; e++) {
73		snprintf(buf, sizeof(buf), "0x1.p%d", e);
74		ld = strtold(buf, &end);
75		assert(*end == '\0');
76		i = ilogbl(ld);
77		assert(i == e);
78	}
79	printf("PASS ilogbl\n");
80
81	return (0);
82}
83