Merge remote-tracking branch 'origin/stable-2.0'
[bpt/guile.git] / test-suite / standalone / test-num2integral.c
CommitLineData
4f1ce27a 1/* Copyright (C) 1999,2000,2001,2003,2004, 2006, 2008, 2010, 2011 Free Software Foundation, Inc.
257ca0d7
RB
2 *
3 * This library is free software; you can redistribute it and/or
53befeb7
NJ
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.
257ca0d7 7 *
53befeb7
NJ
8 * This library is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
257ca0d7
RB
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
53befeb7
NJ
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
16 * 02110-1301 USA
257ca0d7
RB
17 */
18
eb0ffdd8 19#ifdef HAVE_CONFIG_H
3394818c
LC
20# include <config.h>
21#endif
22
5995c6d8 23#include <libguile.h>
257ca0d7
RB
24
25#include <stdio.h>
26#include <assert.h>
4f1ce27a 27#include <limits.h>
257ca0d7 28
b313d73a
KR
29SCM out_of_range_handler (void *data, SCM key, SCM args);
30SCM call_num2long_long_body (void *data);
31SCM call_num2ulong_long_body (void *data);
32
33/* expect to catch an `out-of-range' exception */
34SCM
35out_of_range_handler (void *data, SCM key, SCM args)
36{
6bc89846
AW
37 assert (scm_is_true
38 (scm_equal_p (key, scm_from_locale_symbol ("out-of-range"))));
b313d73a
KR
39 return SCM_BOOL_T;
40}
41
42SCM
43call_num2long_long_body (void *data)
44{
220058a8 45 scm_to_long_long (* (SCM *) data);
b313d73a
KR
46 return SCM_BOOL_F;
47}
48
49SCM
50call_num2ulong_long_body (void *data)
51{
220058a8 52 scm_to_ulong_long (* (SCM *) data);
b313d73a
KR
53 return SCM_BOOL_F;
54}
55
257ca0d7
RB
56static void
57test_long_long ()
58{
257ca0d7 59 {
4f1ce27a 60 SCM n = scm_from_long_long (LLONG_MIN);
220058a8 61 long long result = scm_to_long_long(n);
4f1ce27a 62 assert (result == LLONG_MIN);
257ca0d7 63 }
b313d73a
KR
64
65 /* LLONG_MIN - 1 */
66 {
4f1ce27a 67 SCM n = scm_difference (scm_from_long_long (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 {
4f1ce27a
AW
75 SCM n = scm_sum (scm_from_long_long (LLONG_MIN),
76 scm_from_long_long (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 {
4f1ce27a 84 SCM n = scm_sum (scm_from_long_long (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 105 }
b313d73a
KR
106}
107
108static void
109test_ulong_long ()
110{
257ca0d7 111 {
4f1ce27a 112 SCM n = scm_from_ulong_long (ULLONG_MAX);
220058a8 113 unsigned long long result = scm_to_ulong_long(n);
4f1ce27a 114 assert (result == 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 {
4f1ce27a 127 SCM n = scm_sum (scm_from_ulong_long (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 139 }
257ca0d7
RB
140}
141
8ab3d8a0
KR
142static void
143tests (void *data, int argc, char **argv)
257ca0d7 144{
257ca0d7 145 test_long_long ();
b313d73a 146 test_ulong_long ();
8ab3d8a0
KR
147}
148
149int
150main (int argc, char *argv[])
151{
152 scm_boot_guile (argc, argv, tests, NULL);
257ca0d7
RB
153 return 0;
154}