t_next.c revision 1.3 1 1.3 riastrad /* $NetBSD: t_next.c,v 1.3 2024/05/05 14:34:58 riastradh Exp $ */
2 1.1 riastrad
3 1.1 riastrad /*-
4 1.1 riastrad * Copyright (c) 2024 The NetBSD Foundation, Inc.
5 1.1 riastrad * All rights reserved.
6 1.1 riastrad *
7 1.1 riastrad * Redistribution and use in source and binary forms, with or without
8 1.1 riastrad * modification, are permitted provided that the following conditions
9 1.1 riastrad * are met:
10 1.1 riastrad * 1. Redistributions of source code must retain the above copyright
11 1.1 riastrad * notice, this list of conditions and the following disclaimer.
12 1.1 riastrad * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 riastrad * notice, this list of conditions and the following disclaimer in the
14 1.1 riastrad * documentation and/or other materials provided with the distribution.
15 1.1 riastrad *
16 1.1 riastrad * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 1.1 riastrad * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 1.1 riastrad * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 1.1 riastrad * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 1.1 riastrad * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 1.1 riastrad * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 1.1 riastrad * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 1.1 riastrad * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 1.1 riastrad * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 1.1 riastrad * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 1.1 riastrad * POSSIBILITY OF SUCH DAMAGE.
27 1.1 riastrad */
28 1.1 riastrad
29 1.1 riastrad #include <sys/cdefs.h>
30 1.3 riastrad __RCSID("$NetBSD: t_next.c,v 1.3 2024/05/05 14:34:58 riastradh Exp $");
31 1.1 riastrad
32 1.1 riastrad #include <atf-c.h>
33 1.1 riastrad #include <float.h>
34 1.1 riastrad #include <math.h>
35 1.1 riastrad
36 1.2 riastrad #ifdef __vax__ /* XXX PR 57881: vax libm is missing various symbols */
37 1.2 riastrad
38 1.3 riastrad ATF_TC(vaxafter);
39 1.2 riastrad ATF_TC_HEAD(vaxafter, tc)
40 1.2 riastrad {
41 1.2 riastrad
42 1.3 riastrad atf_tc_set_md_var(tc, "descr", "vax nextafter/nexttoward reminder");
43 1.3 riastrad }
44 1.3 riastrad ATF_TC_BODY(vaxafter, tc)
45 1.3 riastrad {
46 1.3 riastrad
47 1.3 riastrad atf_tc_expect_fail("PR 57881: vax libm is missing various symbols");
48 1.2 riastrad atf_tc_fail("missing nextafter{,f,l} and nexttoward{,f,l} on vax");
49 1.2 riastrad }
50 1.2 riastrad
51 1.2 riastrad #else /* !__vax__ */
52 1.2 riastrad
53 1.1 riastrad #define CHECK(x, y) \
54 1.1 riastrad ATF_CHECK_EQ_MSG((x), (y), #x"=%La=%Lg, "#y"=%La=%Lg", \
55 1.1 riastrad (long double)(x), (long double)(x), \
56 1.1 riastrad (long double)(y), (long double)(y))
57 1.1 riastrad
58 1.1 riastrad ATF_TC(nextafter);
59 1.1 riastrad ATF_TC_HEAD(nextafter, tc)
60 1.1 riastrad {
61 1.1 riastrad atf_tc_set_md_var(tc, "descr", "nextafter");
62 1.1 riastrad }
63 1.1 riastrad ATF_TC_BODY(nextafter, tc)
64 1.1 riastrad {
65 1.1 riastrad
66 1.1 riastrad CHECK(nextafter(1, 2), 1 + DBL_EPSILON);
67 1.1 riastrad CHECK(nextafter(1 + DBL_EPSILON, 0), 1);
68 1.1 riastrad CHECK(nextafter(1, 0), 1 - DBL_EPSILON/2);
69 1.1 riastrad CHECK(nextafter(1 - DBL_EPSILON/2, 2), 1);
70 1.1 riastrad CHECK(nextafter(-1, -2), -1 - DBL_EPSILON);
71 1.1 riastrad CHECK(nextafter(-1 - DBL_EPSILON, 0), -1);
72 1.1 riastrad CHECK(nextafter(-1, 0), -1 + DBL_EPSILON/2);
73 1.1 riastrad CHECK(nextafter(-1 + DBL_EPSILON/2, -2), -1);
74 1.1 riastrad }
75 1.1 riastrad
76 1.1 riastrad ATF_TC(nextafterf);
77 1.1 riastrad ATF_TC_HEAD(nextafterf, tc)
78 1.1 riastrad {
79 1.1 riastrad atf_tc_set_md_var(tc, "descr", "nextafterf");
80 1.1 riastrad }
81 1.1 riastrad ATF_TC_BODY(nextafterf, tc)
82 1.1 riastrad {
83 1.1 riastrad
84 1.1 riastrad CHECK(nextafterf(1, 2), 1 + FLT_EPSILON);
85 1.1 riastrad CHECK(nextafterf(1 + FLT_EPSILON, 0), 1);
86 1.1 riastrad CHECK(nextafterf(1, 0), 1 - FLT_EPSILON/2);
87 1.1 riastrad CHECK(nextafterf(1 - FLT_EPSILON/2, 2), 1);
88 1.1 riastrad CHECK(nextafterf(-1, -2), -1 - FLT_EPSILON);
89 1.1 riastrad CHECK(nextafterf(-1 - FLT_EPSILON, 0), -1);
90 1.1 riastrad CHECK(nextafterf(-1, 0), -1 + FLT_EPSILON/2);
91 1.1 riastrad CHECK(nextafterf(-1 + FLT_EPSILON/2, -2), -1);
92 1.1 riastrad }
93 1.1 riastrad
94 1.1 riastrad ATF_TC(nextafterl);
95 1.1 riastrad ATF_TC_HEAD(nextafterl, tc)
96 1.1 riastrad {
97 1.1 riastrad atf_tc_set_md_var(tc, "descr", "nextafterl");
98 1.1 riastrad }
99 1.1 riastrad ATF_TC_BODY(nextafterl, tc)
100 1.1 riastrad {
101 1.1 riastrad
102 1.1 riastrad CHECK(nextafterl(1, 2), 1 + LDBL_EPSILON);
103 1.1 riastrad CHECK(nextafterl(1 + LDBL_EPSILON, 0), 1);
104 1.1 riastrad CHECK(nextafterl(1, 0), 1 - LDBL_EPSILON/2);
105 1.1 riastrad CHECK(nextafterl(1 - LDBL_EPSILON/2, 2), 1);
106 1.1 riastrad CHECK(nextafterl(-1, -2), -1 - LDBL_EPSILON);
107 1.1 riastrad CHECK(nextafterl(-1 - LDBL_EPSILON, 0), -1);
108 1.1 riastrad CHECK(nextafterl(-1, 0), -1 + LDBL_EPSILON/2);
109 1.1 riastrad CHECK(nextafterl(-1 + LDBL_EPSILON/2, -2), -1);
110 1.1 riastrad }
111 1.1 riastrad
112 1.1 riastrad ATF_TC(nexttoward);
113 1.1 riastrad ATF_TC_HEAD(nexttoward, tc)
114 1.1 riastrad {
115 1.1 riastrad atf_tc_set_md_var(tc, "descr", "nexttoward");
116 1.1 riastrad }
117 1.1 riastrad ATF_TC_BODY(nexttoward, tc)
118 1.1 riastrad {
119 1.1 riastrad
120 1.1 riastrad CHECK(nexttoward(1, 2), 1 + DBL_EPSILON);
121 1.1 riastrad CHECK(nexttoward(1 + DBL_EPSILON, 0), 1);
122 1.1 riastrad CHECK(nexttoward(1, 0), 1 - DBL_EPSILON/2);
123 1.1 riastrad CHECK(nexttoward(1 - DBL_EPSILON/2, 2), 1);
124 1.1 riastrad CHECK(nexttoward(-1, -2), -1 - DBL_EPSILON);
125 1.1 riastrad CHECK(nexttoward(-1 - DBL_EPSILON, 0), -1);
126 1.1 riastrad CHECK(nexttoward(-1, 0), -1 + DBL_EPSILON/2);
127 1.1 riastrad CHECK(nexttoward(-1 + DBL_EPSILON/2, -2), -1);
128 1.1 riastrad }
129 1.1 riastrad
130 1.1 riastrad ATF_TC(nexttowardf);
131 1.1 riastrad ATF_TC_HEAD(nexttowardf, tc)
132 1.1 riastrad {
133 1.1 riastrad atf_tc_set_md_var(tc, "descr", "nexttowardf");
134 1.1 riastrad }
135 1.1 riastrad ATF_TC_BODY(nexttowardf, tc)
136 1.1 riastrad {
137 1.1 riastrad
138 1.1 riastrad CHECK(nexttowardf(1, 2), 1 + FLT_EPSILON);
139 1.1 riastrad CHECK(nexttowardf(1 + FLT_EPSILON, 0), 1);
140 1.1 riastrad CHECK(nexttowardf(1, 0), 1 - FLT_EPSILON/2);
141 1.1 riastrad CHECK(nexttowardf(1 - FLT_EPSILON/2, 2), 1);
142 1.1 riastrad CHECK(nexttowardf(-1, -2), -1 - FLT_EPSILON);
143 1.1 riastrad CHECK(nexttowardf(-1 - FLT_EPSILON, 0), -1);
144 1.1 riastrad CHECK(nexttowardf(-1, 0), -1 + FLT_EPSILON/2);
145 1.1 riastrad CHECK(nexttowardf(-1 + FLT_EPSILON/2, -2), -1);
146 1.1 riastrad }
147 1.1 riastrad
148 1.1 riastrad ATF_TC(nexttowardl);
149 1.1 riastrad ATF_TC_HEAD(nexttowardl, tc)
150 1.1 riastrad {
151 1.1 riastrad atf_tc_set_md_var(tc, "descr", "nexttowardl");
152 1.1 riastrad }
153 1.1 riastrad ATF_TC_BODY(nexttowardl, tc)
154 1.1 riastrad {
155 1.1 riastrad
156 1.1 riastrad CHECK(nexttowardl(1, 2), 1 + LDBL_EPSILON);
157 1.1 riastrad CHECK(nexttowardl(1 + LDBL_EPSILON, 0), 1);
158 1.1 riastrad CHECK(nexttowardl(1, 0), 1 - LDBL_EPSILON/2);
159 1.1 riastrad CHECK(nexttowardl(1 - LDBL_EPSILON/2, 2), 1);
160 1.1 riastrad CHECK(nexttowardl(-1, -2), -1 - LDBL_EPSILON);
161 1.1 riastrad CHECK(nexttowardl(-1 - LDBL_EPSILON, 0), -1);
162 1.1 riastrad CHECK(nexttowardl(-1, 0), -1 + LDBL_EPSILON/2);
163 1.1 riastrad CHECK(nexttowardl(-1 + LDBL_EPSILON/2, -2), -1);
164 1.1 riastrad }
165 1.1 riastrad
166 1.2 riastrad #endif /* __vax__ */
167 1.2 riastrad
168 1.1 riastrad ATF_TP_ADD_TCS(tp)
169 1.1 riastrad {
170 1.1 riastrad
171 1.2 riastrad #ifdef __vax__
172 1.2 riastrad ATF_TP_ADD_TC(tp, vaxafter);
173 1.2 riastrad #else
174 1.1 riastrad ATF_TP_ADD_TC(tp, nextafter);
175 1.1 riastrad ATF_TP_ADD_TC(tp, nextafterf);
176 1.1 riastrad ATF_TP_ADD_TC(tp, nextafterl);
177 1.1 riastrad ATF_TP_ADD_TC(tp, nexttoward);
178 1.1 riastrad ATF_TP_ADD_TC(tp, nexttowardf);
179 1.1 riastrad ATF_TP_ADD_TC(tp, nexttowardl);
180 1.2 riastrad #endif
181 1.1 riastrad return atf_no_error();
182 1.1 riastrad }
183