Commit | Line | Data |
---|---|---|
91ee7515 | 1 | /* Copyright (C) 1999, 2000, 2001, 2003, 2004, 2006, 2008, 2010, 2011 |
0c1f2b0e | 2 | * 2012, 2014 Free Software Foundation, Inc. |
257ca0d7 RB |
3 | * |
4 | * This library is free software; you can redistribute it and/or | |
53befeb7 NJ |
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. | |
257ca0d7 | 8 | * |
53befeb7 NJ |
9 | * This library is distributed in the hope that it will be useful, but |
10 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
257ca0d7 RB |
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 | |
53befeb7 NJ |
16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
17 | * 02110-1301 USA | |
257ca0d7 RB |
18 | */ |
19 | ||
eb0ffdd8 | 20 | #ifdef HAVE_CONFIG_H |
3394818c LC |
21 | # include <config.h> |
22 | #endif | |
23 | ||
0c1f2b0e LC |
24 | #undef NDEBUG |
25 | ||
5995c6d8 | 26 | #include <libguile.h> |
257ca0d7 RB |
27 | |
28 | #include <stdio.h> | |
29 | #include <assert.h> | |
4f1ce27a | 30 | #include <limits.h> |
257ca0d7 | 31 | |
b313d73a KR |
32 | SCM out_of_range_handler (void *data, SCM key, SCM args); |
33 | SCM call_num2long_long_body (void *data); | |
34 | SCM call_num2ulong_long_body (void *data); | |
35 | ||
36 | /* expect to catch an `out-of-range' exception */ | |
37 | SCM | |
38 | out_of_range_handler (void *data, SCM key, SCM args) | |
39 | { | |
20c360b2 | 40 | assert (scm_is_eq (key, scm_from_locale_symbol ("out-of-range"))); |
b313d73a KR |
41 | return SCM_BOOL_T; |
42 | } | |
43 | ||
44 | SCM | |
45 | call_num2long_long_body (void *data) | |
46 | { | |
220058a8 | 47 | scm_to_long_long (* (SCM *) data); |
b313d73a KR |
48 | return SCM_BOOL_F; |
49 | } | |
50 | ||
51 | SCM | |
52 | call_num2ulong_long_body (void *data) | |
53 | { | |
220058a8 | 54 | scm_to_ulong_long (* (SCM *) data); |
b313d73a KR |
55 | return SCM_BOOL_F; |
56 | } | |
57 | ||
257ca0d7 RB |
58 | static void |
59 | test_long_long () | |
60 | { | |
257ca0d7 | 61 | { |
4f1ce27a | 62 | SCM n = scm_from_long_long (LLONG_MIN); |
220058a8 | 63 | long long result = scm_to_long_long(n); |
4f1ce27a | 64 | assert (result == LLONG_MIN); |
257ca0d7 | 65 | } |
b313d73a KR |
66 | |
67 | /* LLONG_MIN - 1 */ | |
68 | { | |
4f1ce27a | 69 | SCM n = scm_difference (scm_from_long_long (LLONG_MIN), scm_from_int (1)); |
b313d73a KR |
70 | SCM caught = scm_internal_catch (SCM_BOOL_T, call_num2long_long_body, &n, |
71 | out_of_range_handler, NULL); | |
66dd7f14 | 72 | assert (scm_is_true (caught)); |
b313d73a KR |
73 | } |
74 | ||
67b74a4c | 75 | /* SCM_I_LLONG_MIN + SCM_I_LLONG_MIN/2 */ |
b313d73a | 76 | { |
4f1ce27a AW |
77 | SCM n = scm_sum (scm_from_long_long (LLONG_MIN), |
78 | scm_from_long_long (LLONG_MIN / 2)); | |
b313d73a KR |
79 | SCM caught = scm_internal_catch (SCM_BOOL_T, call_num2long_long_body, &n, |
80 | out_of_range_handler, NULL); | |
66dd7f14 | 81 | assert (scm_is_true (caught)); |
b313d73a KR |
82 | } |
83 | ||
67b74a4c | 84 | /* SCM_I_LLONG_MAX + 1 */ |
b313d73a | 85 | { |
4f1ce27a | 86 | SCM n = scm_sum (scm_from_long_long (LLONG_MAX), scm_from_int (1)); |
b313d73a KR |
87 | SCM caught = scm_internal_catch (SCM_BOOL_T, call_num2long_long_body, &n, |
88 | out_of_range_handler, NULL); | |
66dd7f14 | 89 | assert (scm_is_true (caught)); |
b313d73a KR |
90 | } |
91 | ||
92 | /* 2^1024 */ | |
93 | { | |
67b74a4c | 94 | SCM n = scm_ash (scm_from_int (1), scm_from_int (1024)); |
b313d73a KR |
95 | SCM caught = scm_internal_catch (SCM_BOOL_T, call_num2long_long_body, &n, |
96 | out_of_range_handler, NULL); | |
66dd7f14 | 97 | assert (scm_is_true (caught)); |
b313d73a KR |
98 | } |
99 | ||
100 | /* -2^1024 */ | |
101 | { | |
67b74a4c MV |
102 | SCM n = scm_difference (scm_from_int (0), |
103 | scm_ash (scm_from_int (1), scm_from_int (1024))); | |
b313d73a KR |
104 | SCM caught = scm_internal_catch (SCM_BOOL_T, call_num2long_long_body, &n, |
105 | out_of_range_handler, NULL); | |
66dd7f14 | 106 | assert (scm_is_true (caught)); |
b313d73a | 107 | } |
b313d73a KR |
108 | } |
109 | ||
110 | static void | |
111 | test_ulong_long () | |
112 | { | |
257ca0d7 | 113 | { |
4f1ce27a | 114 | SCM n = scm_from_ulong_long (ULLONG_MAX); |
220058a8 | 115 | unsigned long long result = scm_to_ulong_long(n); |
4f1ce27a | 116 | assert (result == ULLONG_MAX); |
257ca0d7 | 117 | } |
b313d73a KR |
118 | |
119 | /* -1 */ | |
120 | { | |
67b74a4c | 121 | SCM n = scm_from_int (-1); |
b313d73a KR |
122 | SCM caught = scm_internal_catch (SCM_BOOL_T, call_num2ulong_long_body, &n, |
123 | out_of_range_handler, NULL); | |
66dd7f14 | 124 | assert (scm_is_true (caught)); |
b313d73a KR |
125 | } |
126 | ||
67b74a4c | 127 | /* SCM_I_ULLONG_MAX + 1 */ |
b313d73a | 128 | { |
4f1ce27a | 129 | SCM n = scm_sum (scm_from_ulong_long (ULLONG_MAX), scm_from_int (1)); |
b313d73a KR |
130 | SCM caught = scm_internal_catch (SCM_BOOL_T, call_num2ulong_long_body, &n, |
131 | out_of_range_handler, NULL); | |
66dd7f14 | 132 | assert (scm_is_true (caught)); |
b313d73a KR |
133 | } |
134 | ||
135 | /* 2^1024 */ | |
136 | { | |
67b74a4c | 137 | SCM n = scm_ash (scm_from_int (1), scm_from_int (1024)); |
b313d73a KR |
138 | SCM caught = scm_internal_catch (SCM_BOOL_T, call_num2long_long_body, &n, |
139 | out_of_range_handler, NULL); | |
66dd7f14 | 140 | assert (scm_is_true (caught)); |
b313d73a | 141 | } |
257ca0d7 RB |
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 | } |