Tests for module 'printf-frexpl'.
[gnulib.git] / 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 #define ASSERT(expr) if (!(expr)) abort ();
28
29 static long double
30 my_ldexp (long double x, int d)
31 {
32   for (; d > 0; d--)
33     x *= 2.0L;
34   for (; d < 0; d++)
35     x *= 0.5L;
36   return x;
37 }
38
39 int
40 main ()
41 {
42   int i;
43   long double x;
44
45   for (i = 1, x = 1.0L; i <= LDBL_MAX_EXP; i++, x *= 2.0L)
46     {
47       int exp = -9999;
48       long double mantissa = printf_frexpl (x, &exp);
49       ASSERT (exp == i - 1);
50       ASSERT (mantissa == 1.0L);
51     }
52   for (i = 1, x = 1.0L; i >= LDBL_MIN_EXP; i--, x *= 0.5L)
53     {
54       int exp = -9999;
55       long double mantissa = printf_frexpl (x, &exp);
56       ASSERT (exp == i - 1);
57       ASSERT (mantissa == 1.0L);
58     }
59   for (; i >= LDBL_MIN_EXP - 100 && x > 0.0L; i--, x *= 0.5L)
60     {
61       int exp = -9999;
62       long double mantissa = printf_frexpl (x, &exp);
63       ASSERT (exp == LDBL_MIN_EXP - 1);
64       ASSERT (mantissa == my_ldexp (1.0L, i - LDBL_MIN_EXP));
65     }
66
67   for (i = 1, x = 1.01L; i <= LDBL_MAX_EXP; i++, x *= 2.0L)
68     {
69       int exp = -9999;
70       long double mantissa = printf_frexpl (x, &exp);
71       ASSERT (exp == i - 1);
72       ASSERT (mantissa == 1.01L);
73     }
74   for (i = 1, x = 1.01L; i >= LDBL_MIN_EXP; i--, x *= 0.5L)
75     {
76       int exp = -9999;
77       long double mantissa = printf_frexpl (x, &exp);
78       ASSERT (exp == i - 1);
79       ASSERT (mantissa == 1.01L);
80     }
81   for (; i >= LDBL_MIN_EXP - 100 && x > 0.0L; i--, x *= 0.5L)
82     {
83       int exp = -9999;
84       long double mantissa = printf_frexpl (x, &exp);
85       ASSERT (exp == LDBL_MIN_EXP - 1);
86       ASSERT (mantissa >= my_ldexp (1.0L, i - LDBL_MIN_EXP));
87       ASSERT (mantissa <= my_ldexp (2.0L, i - LDBL_MIN_EXP));
88       ASSERT (mantissa == my_ldexp (x, - exp));
89     }
90
91   for (i = 1, x = 1.73205L; i <= LDBL_MAX_EXP; i++, x *= 2.0L)
92     {
93       int exp = -9999;
94       long double mantissa = printf_frexpl (x, &exp);
95       ASSERT (exp == i - 1);
96       ASSERT (mantissa == 1.73205L);
97     }
98   for (i = 1, x = 1.73205L; i >= LDBL_MIN_EXP; i--, x *= 0.5L)
99     {
100       int exp = -9999;
101       long double mantissa = printf_frexpl (x, &exp);
102       ASSERT (exp == i - 1);
103       ASSERT (mantissa == 1.73205L);
104     }
105   for (; i >= LDBL_MIN_EXP - 100 && x > 0.0L; i--, x *= 0.5L)
106     {
107       int exp = -9999;
108       long double mantissa = printf_frexpl (x, &exp);
109       ASSERT (exp == LDBL_MIN_EXP - 1);
110       ASSERT (mantissa >= my_ldexp (1.0L, i - LDBL_MIN_EXP));
111       ASSERT (mantissa <= my_ldexp (2.0L, i - LDBL_MIN_EXP));
112       ASSERT (mantissa == my_ldexp (x, - exp));
113     }
114
115   return 0;
116 }