Home | History | Annotate | Line # | Download | only in libm
t_next.c revision 1.1
      1  1.1  riastrad /*	$NetBSD: t_next.c,v 1.1 2024/05/05 02:53:02 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.1  riastrad __RCSID("$NetBSD: t_next.c,v 1.1 2024/05/05 02:53:02 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.1  riastrad #define	CHECK(x, y)							      \
     37  1.1  riastrad 	ATF_CHECK_EQ_MSG((x), (y), #x"=%La=%Lg, "#y"=%La=%Lg",		      \
     38  1.1  riastrad 	    (long double)(x), (long double)(x),				      \
     39  1.1  riastrad 	    (long double)(y), (long double)(y))
     40  1.1  riastrad 
     41  1.1  riastrad ATF_TC(nextafter);
     42  1.1  riastrad ATF_TC_HEAD(nextafter, tc)
     43  1.1  riastrad {
     44  1.1  riastrad 	atf_tc_set_md_var(tc, "descr", "nextafter");
     45  1.1  riastrad }
     46  1.1  riastrad ATF_TC_BODY(nextafter, tc)
     47  1.1  riastrad {
     48  1.1  riastrad 
     49  1.1  riastrad 	CHECK(nextafter(1, 2), 1 + DBL_EPSILON);
     50  1.1  riastrad 	CHECK(nextafter(1 + DBL_EPSILON, 0), 1);
     51  1.1  riastrad 	CHECK(nextafter(1, 0), 1 - DBL_EPSILON/2);
     52  1.1  riastrad 	CHECK(nextafter(1 - DBL_EPSILON/2, 2), 1);
     53  1.1  riastrad 	CHECK(nextafter(-1, -2), -1 - DBL_EPSILON);
     54  1.1  riastrad 	CHECK(nextafter(-1 - DBL_EPSILON, 0), -1);
     55  1.1  riastrad 	CHECK(nextafter(-1, 0), -1 + DBL_EPSILON/2);
     56  1.1  riastrad 	CHECK(nextafter(-1 + DBL_EPSILON/2, -2), -1);
     57  1.1  riastrad }
     58  1.1  riastrad 
     59  1.1  riastrad ATF_TC(nextafterf);
     60  1.1  riastrad ATF_TC_HEAD(nextafterf, tc)
     61  1.1  riastrad {
     62  1.1  riastrad 	atf_tc_set_md_var(tc, "descr", "nextafterf");
     63  1.1  riastrad }
     64  1.1  riastrad ATF_TC_BODY(nextafterf, tc)
     65  1.1  riastrad {
     66  1.1  riastrad 
     67  1.1  riastrad 	CHECK(nextafterf(1, 2), 1 + FLT_EPSILON);
     68  1.1  riastrad 	CHECK(nextafterf(1 + FLT_EPSILON, 0), 1);
     69  1.1  riastrad 	CHECK(nextafterf(1, 0), 1 - FLT_EPSILON/2);
     70  1.1  riastrad 	CHECK(nextafterf(1 - FLT_EPSILON/2, 2), 1);
     71  1.1  riastrad 	CHECK(nextafterf(-1, -2), -1 - FLT_EPSILON);
     72  1.1  riastrad 	CHECK(nextafterf(-1 - FLT_EPSILON, 0), -1);
     73  1.1  riastrad 	CHECK(nextafterf(-1, 0), -1 + FLT_EPSILON/2);
     74  1.1  riastrad 	CHECK(nextafterf(-1 + FLT_EPSILON/2, -2), -1);
     75  1.1  riastrad }
     76  1.1  riastrad 
     77  1.1  riastrad ATF_TC(nextafterl);
     78  1.1  riastrad ATF_TC_HEAD(nextafterl, tc)
     79  1.1  riastrad {
     80  1.1  riastrad 	atf_tc_set_md_var(tc, "descr", "nextafterl");
     81  1.1  riastrad }
     82  1.1  riastrad ATF_TC_BODY(nextafterl, tc)
     83  1.1  riastrad {
     84  1.1  riastrad 
     85  1.1  riastrad 	CHECK(nextafterl(1, 2), 1 + LDBL_EPSILON);
     86  1.1  riastrad 	CHECK(nextafterl(1 + LDBL_EPSILON, 0), 1);
     87  1.1  riastrad 	CHECK(nextafterl(1, 0), 1 - LDBL_EPSILON/2);
     88  1.1  riastrad 	CHECK(nextafterl(1 - LDBL_EPSILON/2, 2), 1);
     89  1.1  riastrad 	CHECK(nextafterl(-1, -2), -1 - LDBL_EPSILON);
     90  1.1  riastrad 	CHECK(nextafterl(-1 - LDBL_EPSILON, 0), -1);
     91  1.1  riastrad 	CHECK(nextafterl(-1, 0), -1 + LDBL_EPSILON/2);
     92  1.1  riastrad 	CHECK(nextafterl(-1 + LDBL_EPSILON/2, -2), -1);
     93  1.1  riastrad }
     94  1.1  riastrad 
     95  1.1  riastrad ATF_TC(nexttoward);
     96  1.1  riastrad ATF_TC_HEAD(nexttoward, tc)
     97  1.1  riastrad {
     98  1.1  riastrad 	atf_tc_set_md_var(tc, "descr", "nexttoward");
     99  1.1  riastrad }
    100  1.1  riastrad ATF_TC_BODY(nexttoward, tc)
    101  1.1  riastrad {
    102  1.1  riastrad 
    103  1.1  riastrad 	CHECK(nexttoward(1, 2), 1 + DBL_EPSILON);
    104  1.1  riastrad 	CHECK(nexttoward(1 + DBL_EPSILON, 0), 1);
    105  1.1  riastrad 	CHECK(nexttoward(1, 0), 1 - DBL_EPSILON/2);
    106  1.1  riastrad 	CHECK(nexttoward(1 - DBL_EPSILON/2, 2), 1);
    107  1.1  riastrad 	CHECK(nexttoward(-1, -2), -1 - DBL_EPSILON);
    108  1.1  riastrad 	CHECK(nexttoward(-1 - DBL_EPSILON, 0), -1);
    109  1.1  riastrad 	CHECK(nexttoward(-1, 0), -1 + DBL_EPSILON/2);
    110  1.1  riastrad 	CHECK(nexttoward(-1 + DBL_EPSILON/2, -2), -1);
    111  1.1  riastrad }
    112  1.1  riastrad 
    113  1.1  riastrad ATF_TC(nexttowardf);
    114  1.1  riastrad ATF_TC_HEAD(nexttowardf, tc)
    115  1.1  riastrad {
    116  1.1  riastrad 	atf_tc_set_md_var(tc, "descr", "nexttowardf");
    117  1.1  riastrad }
    118  1.1  riastrad ATF_TC_BODY(nexttowardf, tc)
    119  1.1  riastrad {
    120  1.1  riastrad 
    121  1.1  riastrad 	CHECK(nexttowardf(1, 2), 1 + FLT_EPSILON);
    122  1.1  riastrad 	CHECK(nexttowardf(1 + FLT_EPSILON, 0), 1);
    123  1.1  riastrad 	CHECK(nexttowardf(1, 0), 1 - FLT_EPSILON/2);
    124  1.1  riastrad 	CHECK(nexttowardf(1 - FLT_EPSILON/2, 2), 1);
    125  1.1  riastrad 	CHECK(nexttowardf(-1, -2), -1 - FLT_EPSILON);
    126  1.1  riastrad 	CHECK(nexttowardf(-1 - FLT_EPSILON, 0), -1);
    127  1.1  riastrad 	CHECK(nexttowardf(-1, 0), -1 + FLT_EPSILON/2);
    128  1.1  riastrad 	CHECK(nexttowardf(-1 + FLT_EPSILON/2, -2), -1);
    129  1.1  riastrad }
    130  1.1  riastrad 
    131  1.1  riastrad ATF_TC(nexttowardl);
    132  1.1  riastrad ATF_TC_HEAD(nexttowardl, tc)
    133  1.1  riastrad {
    134  1.1  riastrad 	atf_tc_set_md_var(tc, "descr", "nexttowardl");
    135  1.1  riastrad }
    136  1.1  riastrad ATF_TC_BODY(nexttowardl, tc)
    137  1.1  riastrad {
    138  1.1  riastrad 
    139  1.1  riastrad 	CHECK(nexttowardl(1, 2), 1 + LDBL_EPSILON);
    140  1.1  riastrad 	CHECK(nexttowardl(1 + LDBL_EPSILON, 0), 1);
    141  1.1  riastrad 	CHECK(nexttowardl(1, 0), 1 - LDBL_EPSILON/2);
    142  1.1  riastrad 	CHECK(nexttowardl(1 - LDBL_EPSILON/2, 2), 1);
    143  1.1  riastrad 	CHECK(nexttowardl(-1, -2), -1 - LDBL_EPSILON);
    144  1.1  riastrad 	CHECK(nexttowardl(-1 - LDBL_EPSILON, 0), -1);
    145  1.1  riastrad 	CHECK(nexttowardl(-1, 0), -1 + LDBL_EPSILON/2);
    146  1.1  riastrad 	CHECK(nexttowardl(-1 + LDBL_EPSILON/2, -2), -1);
    147  1.1  riastrad }
    148  1.1  riastrad 
    149  1.1  riastrad ATF_TP_ADD_TCS(tp)
    150  1.1  riastrad {
    151  1.1  riastrad 
    152  1.1  riastrad 	ATF_TP_ADD_TC(tp, nextafter);
    153  1.1  riastrad 	ATF_TP_ADD_TC(tp, nextafterf);
    154  1.1  riastrad 	ATF_TP_ADD_TC(tp, nextafterl);
    155  1.1  riastrad 	ATF_TP_ADD_TC(tp, nexttoward);
    156  1.1  riastrad 	ATF_TP_ADD_TC(tp, nexttowardf);
    157  1.1  riastrad 	ATF_TP_ADD_TC(tp, nexttowardl);
    158  1.1  riastrad 	return atf_no_error();
    159  1.1  riastrad }
    160