15cd91d85bef0af4cbeb9592fa64c1ce83c6050f
[pspp] / tests / test-printf-frexpl.c
1 /* Test of splitting a 'long double' into fraction and mantissa.
2    Copyright (C) 2007 Free Software Foundation, Inc.
3
4    This program is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; either version 2, or (at your option)
7    any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program; if not, write to the Free Software Foundation,
16    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
17
18 /* Written by Bruno Haible <bruno@clisp.org>, 2007.  */
19
20 #include <config.h>
21
22 #include "printf-frexpl.h"
23
24 #include <float.h>
25 #include <stdlib.h>
26
27 #include "fpucw.h"
28
29 #define ASSERT(expr) if (!(expr)) abort ();
30
31 static long double
32 my_ldexp (long double x, int d)
33 {
34   for (; d > 0; d--)
35     x *= 2.0L;
36   for (; d < 0; d++)
37     x *= 0.5L;
38   return x;
39 }
40
41 int
42 main ()
43 {
44   int i;
45   long double x;
46   DECL_LONG_DOUBLE_ROUNDING
47
48   BEGIN_LONG_DOUBLE_ROUNDING ();
49
50   for (i = 1, x = 1.0L; i <= LDBL_MAX_EXP; i++, x *= 2.0L)
51     {
52       int exp = -9999;
53       long double mantissa = printf_frexpl (x, &exp);
54       ASSERT (exp == i - 1);
55       ASSERT (mantissa == 1.0L);
56     }
57   for (i = 1, x = 1.0L; i >= LDBL_MIN_EXP; i--, x *= 0.5L)
58     {
59       int exp = -9999;
60       long double mantissa = printf_frexpl (x, &exp);
61       ASSERT (exp == i - 1);
62       ASSERT (mantissa == 1.0L);
63     }
64   for (; i >= LDBL_MIN_EXP - 100 && x > 0.0L; i--, x *= 0.5L)
65     {
66       int exp = -9999;
67       long double mantissa = printf_frexpl (x, &exp);
68       ASSERT (exp == LDBL_MIN_EXP - 1);
69       ASSERT (mantissa == my_ldexp (1.0L, i - LDBL_MIN_EXP));
70     }
71
72   for (i = 1, x = 1.01L; i <= LDBL_MAX_EXP; i++, x *= 2.0L)
73     {
74       int exp = -9999;
75       long double mantissa = printf_frexpl (x, &exp);
76       ASSERT (exp == i - 1);
77       ASSERT (mantissa == 1.01L);
78     }
79   for (i = 1, x = 1.01L; i >= LDBL_MIN_EXP; i--, x *= 0.5L)
80     {
81       int exp = -9999;
82       long double mantissa = printf_frexpl (x, &exp);
83       ASSERT (exp == i - 1);
84       ASSERT (mantissa == 1.01L);
85     }
86   for (; i >= LDBL_MIN_EXP - 100 && x > 0.0L; i--, x *= 0.5L)
87     {
88       int exp = -9999;
89       long double mantissa = printf_frexpl (x, &exp);
90       ASSERT (exp == LDBL_MIN_EXP - 1);
91       ASSERT (mantissa >= my_ldexp (1.0L, i - LDBL_MIN_EXP));
92       ASSERT (mantissa <= my_ldexp (2.0L, i - LDBL_MIN_EXP));
93       ASSERT (mantissa == my_ldexp (x, - exp));
94     }
95
96   for (i = 1, x = 1.73205L; i <= LDBL_MAX_EXP; i++, x *= 2.0L)
97     {
98       int exp = -9999;
99       long double mantissa = printf_frexpl (x, &exp);
100       ASSERT (exp == i - 1);
101       ASSERT (mantissa == 1.73205L);
102     }
103   for (i = 1, x = 1.73205L; i >= LDBL_MIN_EXP; i--, x *= 0.5L)
104     {
105       int exp = -9999;
106       long double mantissa = printf_frexpl (x, &exp);
107       ASSERT (exp == i - 1);
108       ASSERT (mantissa == 1.73205L);
109     }
110   for (; i >= LDBL_MIN_EXP - 100 && x > 0.0L; i--, x *= 0.5L)
111     {
112       int exp = -9999;
113       long double mantissa = printf_frexpl (x, &exp);
114       ASSERT (exp == LDBL_MIN_EXP - 1);
115       ASSERT (mantissa >= my_ldexp (1.0L, i - LDBL_MIN_EXP));
116       ASSERT (mantissa <= my_ldexp (2.0L, i - LDBL_MIN_EXP));
117       ASSERT (mantissa == my_ldexp (x, - exp));
118     }
119
120   return 0;
121 }