Avoid signed overflow and use size_t in bytevectors.c.
[bpt/guile.git] / test-suite / standalone / test-num2integral.c
1 /* Copyright (C) 1999, 2000, 2001, 2003, 2004, 2006, 2008, 2010,
2 * 2012, 2014 Free Software Foundation, Inc.
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public License
6 * as published by the Free Software Foundation; either version 3 of
7 * the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 * 02110-1301 USA
18 */
19
20 #ifdef HAVE_CONFIG_H
21 # include <config.h>
22 #endif
23
24 #undef NDEBUG
25
26 #include <libguile.h>
27
28 #include <stdio.h>
29 #include <assert.h>
30
31 SCM out_of_range_handler (void *data, SCM key, SCM args);
32 SCM call_num2long_long_body (void *data);
33 SCM call_num2ulong_long_body (void *data);
34
35 /* expect to catch an `out-of-range' exception */
36 SCM
37 out_of_range_handler (void *data, SCM key, SCM args)
38 {
39 assert (scm_is_eq (key, scm_from_locale_symbol ("out-of-range")));
40 return SCM_BOOL_T;
41 }
42
43 SCM
44 call_num2long_long_body (void *data)
45 {
46 scm_to_long_long (* (SCM *) data);
47 return SCM_BOOL_F;
48 }
49
50 SCM
51 call_num2ulong_long_body (void *data)
52 {
53 scm_to_ulong_long (* (SCM *) data);
54 return SCM_BOOL_F;
55 }
56
57 static void
58 test_long_long ()
59 {
60 {
61 SCM n = scm_from_long_long (SCM_I_LLONG_MIN);
62 long long result = scm_to_long_long(n);
63 assert (result == SCM_I_LLONG_MIN);
64 }
65
66 /* LLONG_MIN - 1 */
67 {
68 SCM n = scm_difference (scm_from_long_long (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_from_long_long (SCM_I_LLONG_MIN),
77 scm_from_long_long (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_from_long_long (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
109 static void
110 test_ulong_long ()
111 {
112 {
113 SCM n = scm_from_ulong_long (SCM_I_ULLONG_MAX);
114 unsigned long long result = scm_to_ulong_long(n);
115 assert (result == SCM_I_ULLONG_MAX);
116 }
117
118 /* -1 */
119 {
120 SCM n = scm_from_int (-1);
121 SCM caught = scm_internal_catch (SCM_BOOL_T, call_num2ulong_long_body, &n,
122 out_of_range_handler, NULL);
123 assert (scm_is_true (caught));
124 }
125
126 /* SCM_I_ULLONG_MAX + 1 */
127 {
128 SCM n = scm_sum (scm_from_ulong_long (SCM_I_ULLONG_MAX), scm_from_int (1));
129 SCM caught = scm_internal_catch (SCM_BOOL_T, call_num2ulong_long_body, &n,
130 out_of_range_handler, NULL);
131 assert (scm_is_true (caught));
132 }
133
134 /* 2^1024 */
135 {
136 SCM n = scm_ash (scm_from_int (1), scm_from_int (1024));
137 SCM caught = scm_internal_catch (SCM_BOOL_T, call_num2long_long_body, &n,
138 out_of_range_handler, NULL);
139 assert (scm_is_true (caught));
140 }
141 }
142
143 static void
144 tests (void *data, int argc, char **argv)
145 {
146 test_long_long ();
147 test_ulong_long ();
148 }
149
150 int
151 main (int argc, char *argv[])
152 {
153 scm_boot_guile (argc, argv, tests, NULL);
154 return 0;
155 }