Commit | Line | Data |
---|---|---|
5995c6d8 | 1 | /* Copyright (C) 1999,2000,2001,2003,2004, 2006, 2008 Free Software Foundation, Inc. |
257ca0d7 RB |
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 | |
5 | * License as published by the Free Software Foundation; either | |
6 | * version 2.1 of the License, or (at your option) any later version. | |
7 | * | |
8 | * This library is distributed in the hope that it will be useful, | |
9 | * but 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 | |
92205699 | 15 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
257ca0d7 RB |
16 | */ |
17 | ||
3394818c LC |
18 | #ifndef HAVE_CONFIG_H |
19 | # include <config.h> | |
20 | #endif | |
21 | ||
5995c6d8 | 22 | #include <libguile.h> |
257ca0d7 RB |
23 | |
24 | #include <stdio.h> | |
25 | #include <assert.h> | |
26 | ||
b8f63daf MV |
27 | #if SCM_ENABLE_DISCOURAGED == 1 |
28 | ||
b313d73a KR |
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_equal_p (key, scm_str2symbol ("out-of-range"))); | |
38 | return SCM_BOOL_T; | |
39 | } | |
40 | ||
41 | SCM | |
42 | call_num2long_long_body (void *data) | |
43 | { | |
44 | scm_num2long_long (* (SCM *) data, SCM_ARG1, "call_num2long_long_body"); | |
45 | return SCM_BOOL_F; | |
46 | } | |
47 | ||
48 | SCM | |
49 | call_num2ulong_long_body (void *data) | |
50 | { | |
51 | scm_num2ulong_long (* (SCM *) data, SCM_ARG1, "call_num2ulong_long_body"); | |
52 | return SCM_BOOL_F; | |
53 | } | |
54 | ||
257ca0d7 RB |
55 | static void |
56 | test_long_long () | |
57 | { | |
58 | #if SCM_SIZEOF_LONG_LONG != 0 | |
59 | { | |
67b74a4c | 60 | SCM n = scm_long_long2num (SCM_I_LLONG_MIN); |
257ca0d7 | 61 | long long result = scm_num2long_long(n, 0, "main"); |
67b74a4c | 62 | assert (result == SCM_I_LLONG_MIN); |
257ca0d7 | 63 | } |
b313d73a KR |
64 | |
65 | /* LLONG_MIN - 1 */ | |
66 | { | |
67b74a4c | 67 | SCM n = scm_difference (scm_long_long2num (SCM_I_LLONG_MIN), scm_from_int (1)); |
b313d73a KR |
68 | SCM caught = scm_internal_catch (SCM_BOOL_T, call_num2long_long_body, &n, |
69 | out_of_range_handler, NULL); | |
66dd7f14 | 70 | assert (scm_is_true (caught)); |
b313d73a KR |
71 | } |
72 | ||
67b74a4c | 73 | /* SCM_I_LLONG_MIN + SCM_I_LLONG_MIN/2 */ |
b313d73a | 74 | { |
67b74a4c MV |
75 | SCM n = scm_sum (scm_long_long2num (SCM_I_LLONG_MIN), |
76 | scm_long_long2num (SCM_I_LLONG_MIN / 2)); | |
b313d73a KR |
77 | SCM caught = scm_internal_catch (SCM_BOOL_T, call_num2long_long_body, &n, |
78 | out_of_range_handler, NULL); | |
66dd7f14 | 79 | assert (scm_is_true (caught)); |
b313d73a KR |
80 | } |
81 | ||
67b74a4c | 82 | /* SCM_I_LLONG_MAX + 1 */ |
b313d73a | 83 | { |
67b74a4c | 84 | SCM n = scm_sum (scm_long_long2num (SCM_I_LLONG_MAX), scm_from_int (1)); |
b313d73a KR |
85 | SCM caught = scm_internal_catch (SCM_BOOL_T, call_num2long_long_body, &n, |
86 | out_of_range_handler, NULL); | |
66dd7f14 | 87 | assert (scm_is_true (caught)); |
b313d73a KR |
88 | } |
89 | ||
90 | /* 2^1024 */ | |
91 | { | |
67b74a4c | 92 | SCM n = scm_ash (scm_from_int (1), scm_from_int (1024)); |
b313d73a KR |
93 | SCM caught = scm_internal_catch (SCM_BOOL_T, call_num2long_long_body, &n, |
94 | out_of_range_handler, NULL); | |
66dd7f14 | 95 | assert (scm_is_true (caught)); |
b313d73a KR |
96 | } |
97 | ||
98 | /* -2^1024 */ | |
99 | { | |
67b74a4c MV |
100 | SCM n = scm_difference (scm_from_int (0), |
101 | scm_ash (scm_from_int (1), scm_from_int (1024))); | |
b313d73a KR |
102 | SCM caught = scm_internal_catch (SCM_BOOL_T, call_num2long_long_body, &n, |
103 | out_of_range_handler, NULL); | |
66dd7f14 | 104 | assert (scm_is_true (caught)); |
b313d73a KR |
105 | } |
106 | ||
107 | #endif /* SCM_SIZEOF_LONG_LONG != 0 */ | |
108 | } | |
109 | ||
110 | static void | |
111 | test_ulong_long () | |
112 | { | |
113 | #if SCM_SIZEOF_LONG_LONG != 0 | |
114 | ||
257ca0d7 | 115 | { |
67b74a4c | 116 | SCM n = scm_ulong_long2num (SCM_I_ULLONG_MAX); |
257ca0d7 | 117 | unsigned long long result = scm_num2ulong_long(n, 0, "main"); |
67b74a4c | 118 | assert (result == SCM_I_ULLONG_MAX); |
257ca0d7 | 119 | } |
b313d73a KR |
120 | |
121 | /* -1 */ | |
122 | { | |
67b74a4c | 123 | SCM n = scm_from_int (-1); |
b313d73a KR |
124 | SCM caught = scm_internal_catch (SCM_BOOL_T, call_num2ulong_long_body, &n, |
125 | out_of_range_handler, NULL); | |
66dd7f14 | 126 | assert (scm_is_true (caught)); |
b313d73a KR |
127 | } |
128 | ||
67b74a4c | 129 | /* SCM_I_ULLONG_MAX + 1 */ |
b313d73a | 130 | { |
67b74a4c | 131 | SCM n = scm_sum (scm_ulong_long2num (SCM_I_ULLONG_MAX), scm_from_int (1)); |
b313d73a KR |
132 | SCM caught = scm_internal_catch (SCM_BOOL_T, call_num2ulong_long_body, &n, |
133 | out_of_range_handler, NULL); | |
66dd7f14 | 134 | assert (scm_is_true (caught)); |
b313d73a KR |
135 | } |
136 | ||
137 | /* 2^1024 */ | |
138 | { | |
67b74a4c | 139 | SCM n = scm_ash (scm_from_int (1), scm_from_int (1024)); |
b313d73a KR |
140 | SCM caught = scm_internal_catch (SCM_BOOL_T, call_num2long_long_body, &n, |
141 | out_of_range_handler, NULL); | |
66dd7f14 | 142 | assert (scm_is_true (caught)); |
b313d73a KR |
143 | } |
144 | ||
257ca0d7 RB |
145 | #endif /* SCM_SIZEOF_LONG_LONG != 0 */ |
146 | } | |
147 | ||
8ab3d8a0 KR |
148 | static void |
149 | tests (void *data, int argc, char **argv) | |
257ca0d7 | 150 | { |
257ca0d7 | 151 | test_long_long (); |
b313d73a | 152 | test_ulong_long (); |
8ab3d8a0 KR |
153 | } |
154 | ||
155 | int | |
156 | main (int argc, char *argv[]) | |
157 | { | |
158 | scm_boot_guile (argc, argv, tests, NULL); | |
257ca0d7 RB |
159 | return 0; |
160 | } | |
b8f63daf MV |
161 | |
162 | #else /* SCM_ENABLE_DISCOURAGED == 0 */ | |
163 | ||
164 | int | |
165 | main (int argc, char *argv[]) | |
166 | { | |
167 | return 0; | |
168 | } | |
169 | ||
170 | #endif /* SCM_ENABLE_DISCOURAGED == 0 */ |