Commit | Line | Data |
---|---|---|
6e7d5622 | 1 | /* Copyright (C) 1999,2000,2001,2003,2004, 2006 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 | ||
18 | #include "libguile.h" | |
19 | ||
20 | #include <stdio.h> | |
21 | #include <assert.h> | |
22 | ||
b8f63daf MV |
23 | #if SCM_ENABLE_DISCOURAGED == 1 |
24 | ||
b313d73a KR |
25 | SCM out_of_range_handler (void *data, SCM key, SCM args); |
26 | SCM call_num2long_long_body (void *data); | |
27 | SCM call_num2ulong_long_body (void *data); | |
28 | ||
29 | /* expect to catch an `out-of-range' exception */ | |
30 | SCM | |
31 | out_of_range_handler (void *data, SCM key, SCM args) | |
32 | { | |
33 | assert (scm_equal_p (key, scm_str2symbol ("out-of-range"))); | |
34 | return SCM_BOOL_T; | |
35 | } | |
36 | ||
37 | SCM | |
38 | call_num2long_long_body (void *data) | |
39 | { | |
40 | scm_num2long_long (* (SCM *) data, SCM_ARG1, "call_num2long_long_body"); | |
41 | return SCM_BOOL_F; | |
42 | } | |
43 | ||
44 | SCM | |
45 | call_num2ulong_long_body (void *data) | |
46 | { | |
47 | scm_num2ulong_long (* (SCM *) data, SCM_ARG1, "call_num2ulong_long_body"); | |
48 | return SCM_BOOL_F; | |
49 | } | |
50 | ||
257ca0d7 RB |
51 | static void |
52 | test_long_long () | |
53 | { | |
54 | #if SCM_SIZEOF_LONG_LONG != 0 | |
55 | { | |
67b74a4c | 56 | SCM n = scm_long_long2num (SCM_I_LLONG_MIN); |
257ca0d7 | 57 | long long result = scm_num2long_long(n, 0, "main"); |
67b74a4c | 58 | assert (result == SCM_I_LLONG_MIN); |
257ca0d7 | 59 | } |
b313d73a KR |
60 | |
61 | /* LLONG_MIN - 1 */ | |
62 | { | |
67b74a4c | 63 | SCM n = scm_difference (scm_long_long2num (SCM_I_LLONG_MIN), scm_from_int (1)); |
b313d73a KR |
64 | SCM caught = scm_internal_catch (SCM_BOOL_T, call_num2long_long_body, &n, |
65 | out_of_range_handler, NULL); | |
66dd7f14 | 66 | assert (scm_is_true (caught)); |
b313d73a KR |
67 | } |
68 | ||
67b74a4c | 69 | /* SCM_I_LLONG_MIN + SCM_I_LLONG_MIN/2 */ |
b313d73a | 70 | { |
67b74a4c MV |
71 | SCM n = scm_sum (scm_long_long2num (SCM_I_LLONG_MIN), |
72 | scm_long_long2num (SCM_I_LLONG_MIN / 2)); | |
b313d73a KR |
73 | SCM caught = scm_internal_catch (SCM_BOOL_T, call_num2long_long_body, &n, |
74 | out_of_range_handler, NULL); | |
66dd7f14 | 75 | assert (scm_is_true (caught)); |
b313d73a KR |
76 | } |
77 | ||
67b74a4c | 78 | /* SCM_I_LLONG_MAX + 1 */ |
b313d73a | 79 | { |
67b74a4c | 80 | SCM n = scm_sum (scm_long_long2num (SCM_I_LLONG_MAX), scm_from_int (1)); |
b313d73a KR |
81 | SCM caught = scm_internal_catch (SCM_BOOL_T, call_num2long_long_body, &n, |
82 | out_of_range_handler, NULL); | |
66dd7f14 | 83 | assert (scm_is_true (caught)); |
b313d73a KR |
84 | } |
85 | ||
86 | /* 2^1024 */ | |
87 | { | |
67b74a4c | 88 | SCM n = scm_ash (scm_from_int (1), scm_from_int (1024)); |
b313d73a KR |
89 | SCM caught = scm_internal_catch (SCM_BOOL_T, call_num2long_long_body, &n, |
90 | out_of_range_handler, NULL); | |
66dd7f14 | 91 | assert (scm_is_true (caught)); |
b313d73a KR |
92 | } |
93 | ||
94 | /* -2^1024 */ | |
95 | { | |
67b74a4c MV |
96 | SCM n = scm_difference (scm_from_int (0), |
97 | scm_ash (scm_from_int (1), scm_from_int (1024))); | |
b313d73a KR |
98 | SCM caught = scm_internal_catch (SCM_BOOL_T, call_num2long_long_body, &n, |
99 | out_of_range_handler, NULL); | |
66dd7f14 | 100 | assert (scm_is_true (caught)); |
b313d73a KR |
101 | } |
102 | ||
103 | #endif /* SCM_SIZEOF_LONG_LONG != 0 */ | |
104 | } | |
105 | ||
106 | static void | |
107 | test_ulong_long () | |
108 | { | |
109 | #if SCM_SIZEOF_LONG_LONG != 0 | |
110 | ||
257ca0d7 | 111 | { |
67b74a4c | 112 | SCM n = scm_ulong_long2num (SCM_I_ULLONG_MAX); |
257ca0d7 | 113 | unsigned long long result = scm_num2ulong_long(n, 0, "main"); |
67b74a4c | 114 | assert (result == SCM_I_ULLONG_MAX); |
257ca0d7 | 115 | } |
b313d73a KR |
116 | |
117 | /* -1 */ | |
118 | { | |
67b74a4c | 119 | SCM n = scm_from_int (-1); |
b313d73a KR |
120 | SCM caught = scm_internal_catch (SCM_BOOL_T, call_num2ulong_long_body, &n, |
121 | out_of_range_handler, NULL); | |
66dd7f14 | 122 | assert (scm_is_true (caught)); |
b313d73a KR |
123 | } |
124 | ||
67b74a4c | 125 | /* SCM_I_ULLONG_MAX + 1 */ |
b313d73a | 126 | { |
67b74a4c | 127 | SCM n = scm_sum (scm_ulong_long2num (SCM_I_ULLONG_MAX), scm_from_int (1)); |
b313d73a KR |
128 | SCM caught = scm_internal_catch (SCM_BOOL_T, call_num2ulong_long_body, &n, |
129 | out_of_range_handler, NULL); | |
66dd7f14 | 130 | assert (scm_is_true (caught)); |
b313d73a KR |
131 | } |
132 | ||
133 | /* 2^1024 */ | |
134 | { | |
67b74a4c | 135 | SCM n = scm_ash (scm_from_int (1), scm_from_int (1024)); |
b313d73a KR |
136 | SCM caught = scm_internal_catch (SCM_BOOL_T, call_num2long_long_body, &n, |
137 | out_of_range_handler, NULL); | |
66dd7f14 | 138 | assert (scm_is_true (caught)); |
b313d73a KR |
139 | } |
140 | ||
257ca0d7 RB |
141 | #endif /* SCM_SIZEOF_LONG_LONG != 0 */ |
142 | } | |
143 | ||
8ab3d8a0 KR |
144 | static void |
145 | tests (void *data, int argc, char **argv) | |
257ca0d7 | 146 | { |
257ca0d7 | 147 | test_long_long (); |
b313d73a | 148 | test_ulong_long (); |
8ab3d8a0 KR |
149 | } |
150 | ||
151 | int | |
152 | main (int argc, char *argv[]) | |
153 | { | |
154 | scm_boot_guile (argc, argv, tests, NULL); | |
257ca0d7 RB |
155 | return 0; |
156 | } | |
b8f63daf MV |
157 | |
158 | #else /* SCM_ENABLE_DISCOURAGED == 0 */ | |
159 | ||
160 | int | |
161 | main (int argc, char *argv[]) | |
162 | { | |
163 | return 0; | |
164 | } | |
165 | ||
166 | #endif /* SCM_ENABLE_DISCOURAGED == 0 */ |