Merge remote-tracking branch 'origin/stable-2.0'
[bpt/guile.git] / test-suite / standalone / test-num2integral.c
1 /* Copyright (C) 1999,2000,2001,2003,2004, 2006, 2008, 2010, 2011 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 #ifdef HAVE_CONFIG_H
20 # include <config.h>
21 #endif
22
23 #include <libguile.h>
24
25 #include <stdio.h>
26 #include <assert.h>
27 #include <limits.h>
28
29 SCM out_of_range_handler (void *data, SCM key, SCM args);
30 SCM call_num2long_long_body (void *data);
31 SCM call_num2ulong_long_body (void *data);
32
33 /* expect to catch an `out-of-range' exception */
34 SCM
35 out_of_range_handler (void *data, SCM key, SCM args)
36 {
37 assert (scm_is_true
38 (scm_equal_p (key, scm_from_locale_symbol ("out-of-range"))));
39 return SCM_BOOL_T;
40 }
41
42 SCM
43 call_num2long_long_body (void *data)
44 {
45 scm_to_long_long (* (SCM *) data);
46 return SCM_BOOL_F;
47 }
48
49 SCM
50 call_num2ulong_long_body (void *data)
51 {
52 scm_to_ulong_long (* (SCM *) data);
53 return SCM_BOOL_F;
54 }
55
56 static void
57 test_long_long ()
58 {
59 {
60 SCM n = scm_from_long_long (LLONG_MIN);
61 long long result = scm_to_long_long(n);
62 assert (result == LLONG_MIN);
63 }
64
65 /* LLONG_MIN - 1 */
66 {
67 SCM n = scm_difference (scm_from_long_long (LLONG_MIN), scm_from_int (1));
68 SCM caught = scm_internal_catch (SCM_BOOL_T, call_num2long_long_body, &n,
69 out_of_range_handler, NULL);
70 assert (scm_is_true (caught));
71 }
72
73 /* SCM_I_LLONG_MIN + SCM_I_LLONG_MIN/2 */
74 {
75 SCM n = scm_sum (scm_from_long_long (LLONG_MIN),
76 scm_from_long_long (LLONG_MIN / 2));
77 SCM caught = scm_internal_catch (SCM_BOOL_T, call_num2long_long_body, &n,
78 out_of_range_handler, NULL);
79 assert (scm_is_true (caught));
80 }
81
82 /* SCM_I_LLONG_MAX + 1 */
83 {
84 SCM n = scm_sum (scm_from_long_long (LLONG_MAX), scm_from_int (1));
85 SCM caught = scm_internal_catch (SCM_BOOL_T, call_num2long_long_body, &n,
86 out_of_range_handler, NULL);
87 assert (scm_is_true (caught));
88 }
89
90 /* 2^1024 */
91 {
92 SCM n = scm_ash (scm_from_int (1), scm_from_int (1024));
93 SCM caught = scm_internal_catch (SCM_BOOL_T, call_num2long_long_body, &n,
94 out_of_range_handler, NULL);
95 assert (scm_is_true (caught));
96 }
97
98 /* -2^1024 */
99 {
100 SCM n = scm_difference (scm_from_int (0),
101 scm_ash (scm_from_int (1), scm_from_int (1024)));
102 SCM caught = scm_internal_catch (SCM_BOOL_T, call_num2long_long_body, &n,
103 out_of_range_handler, NULL);
104 assert (scm_is_true (caught));
105 }
106 }
107
108 static void
109 test_ulong_long ()
110 {
111 {
112 SCM n = scm_from_ulong_long (ULLONG_MAX);
113 unsigned long long result = scm_to_ulong_long(n);
114 assert (result == ULLONG_MAX);
115 }
116
117 /* -1 */
118 {
119 SCM n = scm_from_int (-1);
120 SCM caught = scm_internal_catch (SCM_BOOL_T, call_num2ulong_long_body, &n,
121 out_of_range_handler, NULL);
122 assert (scm_is_true (caught));
123 }
124
125 /* SCM_I_ULLONG_MAX + 1 */
126 {
127 SCM n = scm_sum (scm_from_ulong_long (ULLONG_MAX), scm_from_int (1));
128 SCM caught = scm_internal_catch (SCM_BOOL_T, call_num2ulong_long_body, &n,
129 out_of_range_handler, NULL);
130 assert (scm_is_true (caught));
131 }
132
133 /* 2^1024 */
134 {
135 SCM n = scm_ash (scm_from_int (1), scm_from_int (1024));
136 SCM caught = scm_internal_catch (SCM_BOOL_T, call_num2long_long_body, &n,
137 out_of_range_handler, NULL);
138 assert (scm_is_true (caught));
139 }
140 }
141
142 static void
143 tests (void *data, int argc, char **argv)
144 {
145 test_long_long ();
146 test_ulong_long ();
147 }
148
149 int
150 main (int argc, char *argv[])
151 {
152 scm_boot_guile (argc, argv, tests, NULL);
153 return 0;
154 }