fp80_cvtd.c revision 9899:0392ef94d766
1/*
2 * Copyright (c) 2013, Andreas Sandberg
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above
12 *    copyright notice, this list of conditions and the following
13 *    disclaimer in the documentation and/or other materials provided
14 *    with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
19 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
20 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
25 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
27 * OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30#include <fputils/fp80.h>
31
32#include "test_helper.h"
33
34#include <math.h>
35#include <stdio.h>
36#include <stdlib.h>
37
38/* We provide our own version of isinf_sgn since the C99 standard
39 * doesn't guarantee that isinf() returns the sign of the infinity
40 * (most implementations do). */
41static inline int
42isinf_sgn(double x)
43{
44    return isinf(x) ? (signbit(x) ? -1 : 1) : 0;
45}
46
47static void
48test_fp80_cvtd_class(const char *name, fp80_t fin, int class)
49{
50    double d = fp80_cvtd(fin);
51    if (fpclassify(d) != class) {
52        test_diag("wrong class");
53        test_fail(name);
54    } else {
55        test_ok(name);
56    }
57}
58
59static void
60test_fp80_cvtd_inf(const char *name, fp80_t fin, int expected_inf_class)
61{
62    double d = fp80_cvtd(fin);
63    if (isinf_sgn(d) != expected_inf_class) {
64        test_diag("wrong infinity type");
65        test_fail(name);
66    } else {
67        test_ok(name);
68    }
69}
70
71int
72main(int argc, char *argv[])
73{
74    test_init(6);
75
76    test_fp80_cvtd_inf("fp80->double +inf", fp80_pinf, 1);
77    test_fp80_cvtd_inf("fp80->double -inf", fp80_ninf, -1);
78    test_fp80_cvtd_class("fp80->double qnan", fp80_qnan, FP_NAN);
79    test_fp80_cvtd_class("fp80->double qnani", fp80_qnani, FP_NAN);
80    test_fp80_cvtd_class("fp80->double snan", fp80_snan, FP_NAN);
81    test_fp80_cvtd_class("fp80->double nan", fp80_nan, FP_NAN);
82
83    test_exit();
84}
85