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