Merge branch 'master' of git://git.savannah.gnu.org/guile into elisp
[bpt/guile.git] / test-suite / standalone / test-num2integral.c
1 /* Copyright (C) 1999,2000,2001,2003,2004, 2006, 2008 Free Software Foundation, Inc.
2 *
3 * This library is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU Lesser General Public License
5 * as published by the Free Software Foundation; either version 3 of
6 * the License, or (at your option) any later version.
7 *
8 * This library is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
12 *
13 * You should have received a copy of the GNU Lesser General Public
14 * License along with this library; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
16 * 02110-1301 USA
17 */
18
19 #ifndef HAVE_CONFIG_H
20 # include <config.h>
21 #endif
22
23 #include <libguile.h>
24
25 #include <stdio.h>
26 #include <assert.h>
27
28 #if SCM_ENABLE_DISCOURAGED == 1
29
30 SCM out_of_range_handler (void *data, SCM key, SCM args);
31 SCM call_num2long_long_body (void *data);
32 SCM call_num2ulong_long_body (void *data);
33
34 /* expect to catch an `out-of-range' exception */
35 SCM
36 out_of_range_handler (void *data, SCM key, SCM args)
37 {
38 assert (scm_equal_p (key, scm_str2symbol ("out-of-range")));
39 return SCM_BOOL_T;
40 }
41
42 SCM
43 call_num2long_long_body (void *data)
44 {
45 scm_num2long_long (* (SCM *) data, SCM_ARG1, "call_num2long_long_body");
46 return SCM_BOOL_F;
47 }
48
49 SCM
50 call_num2ulong_long_body (void *data)
51 {
52 scm_num2ulong_long (* (SCM *) data, SCM_ARG1, "call_num2ulong_long_body");
53 return SCM_BOOL_F;
54 }
55
56 static void
57 test_long_long ()
58 {
59 #if SCM_SIZEOF_LONG_LONG != 0
60 {
61 SCM n = scm_long_long2num (SCM_I_LLONG_MIN);
62 long long result = scm_num2long_long(n, 0, "main");
63 assert (result == SCM_I_LLONG_MIN);
64 }
65
66 /* LLONG_MIN - 1 */
67 {
68 SCM n = scm_difference (scm_long_long2num (SCM_I_LLONG_MIN), scm_from_int (1));
69 SCM caught = scm_internal_catch (SCM_BOOL_T, call_num2long_long_body, &n,
70 out_of_range_handler, NULL);
71 assert (scm_is_true (caught));
72 }
73
74 /* SCM_I_LLONG_MIN + SCM_I_LLONG_MIN/2 */
75 {
76 SCM n = scm_sum (scm_long_long2num (SCM_I_LLONG_MIN),
77 scm_long_long2num (SCM_I_LLONG_MIN / 2));
78 SCM caught = scm_internal_catch (SCM_BOOL_T, call_num2long_long_body, &n,
79 out_of_range_handler, NULL);
80 assert (scm_is_true (caught));
81 }
82
83 /* SCM_I_LLONG_MAX + 1 */
84 {
85 SCM n = scm_sum (scm_long_long2num (SCM_I_LLONG_MAX), scm_from_int (1));
86 SCM caught = scm_internal_catch (SCM_BOOL_T, call_num2long_long_body, &n,
87 out_of_range_handler, NULL);
88 assert (scm_is_true (caught));
89 }
90
91 /* 2^1024 */
92 {
93 SCM n = scm_ash (scm_from_int (1), scm_from_int (1024));
94 SCM caught = scm_internal_catch (SCM_BOOL_T, call_num2long_long_body, &n,
95 out_of_range_handler, NULL);
96 assert (scm_is_true (caught));
97 }
98
99 /* -2^1024 */
100 {
101 SCM n = scm_difference (scm_from_int (0),
102 scm_ash (scm_from_int (1), scm_from_int (1024)));
103 SCM caught = scm_internal_catch (SCM_BOOL_T, call_num2long_long_body, &n,
104 out_of_range_handler, NULL);
105 assert (scm_is_true (caught));
106 }
107
108 #endif /* SCM_SIZEOF_LONG_LONG != 0 */
109 }
110
111 static void
112 test_ulong_long ()
113 {
114 #if SCM_SIZEOF_LONG_LONG != 0
115
116 {
117 SCM n = scm_ulong_long2num (SCM_I_ULLONG_MAX);
118 unsigned long long result = scm_num2ulong_long(n, 0, "main");
119 assert (result == SCM_I_ULLONG_MAX);
120 }
121
122 /* -1 */
123 {
124 SCM n = scm_from_int (-1);
125 SCM caught = scm_internal_catch (SCM_BOOL_T, call_num2ulong_long_body, &n,
126 out_of_range_handler, NULL);
127 assert (scm_is_true (caught));
128 }
129
130 /* SCM_I_ULLONG_MAX + 1 */
131 {
132 SCM n = scm_sum (scm_ulong_long2num (SCM_I_ULLONG_MAX), scm_from_int (1));
133 SCM caught = scm_internal_catch (SCM_BOOL_T, call_num2ulong_long_body, &n,
134 out_of_range_handler, NULL);
135 assert (scm_is_true (caught));
136 }
137
138 /* 2^1024 */
139 {
140 SCM n = scm_ash (scm_from_int (1), scm_from_int (1024));
141 SCM caught = scm_internal_catch (SCM_BOOL_T, call_num2long_long_body, &n,
142 out_of_range_handler, NULL);
143 assert (scm_is_true (caught));
144 }
145
146 #endif /* SCM_SIZEOF_LONG_LONG != 0 */
147 }
148
149 static void
150 tests (void *data, int argc, char **argv)
151 {
152 test_long_long ();
153 test_ulong_long ();
154 }
155
156 int
157 main (int argc, char *argv[])
158 {
159 scm_boot_guile (argc, argv, tests, NULL);
160 return 0;
161 }
162
163 #else /* SCM_ENABLE_DISCOURAGED == 0 */
164
165 int
166 main (int argc, char *argv[])
167 {
168 return 0;
169 }
170
171 #endif /* SCM_ENABLE_DISCOURAGED == 0 */