t_round.c revision 1.3 1 /* $NetBSD: t_round.c,v 1.3 2011/09/13 08:58:41 jruoho Exp $ */
2
3 /*-
4 * Copyright (c) 2011 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include <atf-c.h>
30 #include <math.h>
31
32 /*
33 * This tests for a bug in the initial implementation where
34 * precision was lost in an internal substraction, leading to
35 * rounding into the wrong direction.
36 */
37
38 /* 0.5 - EPSILON */
39 #define VAL 0x0.7ffffffffffffcp0
40 #define VALF 0x0.7fffff8p0
41
42 #ifdef __vax__
43 #define SMALL_NUM 1.0e-38
44 #else
45 #define SMALL_NUM 1.0e-40
46 #endif
47
48 ATF_TC(round_dir);
49 ATF_TC_HEAD(round_dir, tc)
50 {
51 atf_tc_set_md_var(tc, "descr","Check for rounding in wrong direction");
52 }
53
54 ATF_TC_BODY(round_dir, tc)
55 {
56 double a = VAL, b, c;
57 float af = VALF, bf, cf;
58
59 b = round(a);
60 bf = roundf(af);
61
62 ATF_CHECK(fabs(b) < SMALL_NUM);
63 ATF_CHECK(fabsf(bf) < SMALL_NUM);
64
65 c = round(-a);
66 cf = roundf(-af);
67
68 ATF_CHECK(fabs(c) < SMALL_NUM);
69 ATF_CHECK(fabsf(cf) < SMALL_NUM);
70 }
71
72 ATF_TP_ADD_TCS(tp)
73 {
74
75 ATF_TP_ADD_TC(tp, round_dir);
76
77 return atf_no_error();
78 }
79