New file.
[bpt/guile.git] / test-suite / standalone / test-num2integral.c
CommitLineData
b313d73a 1/* Copyright (C) 1999,2000,2001,2003,2004 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
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 */
17
18#include "libguile.h"
19
20#include <stdio.h>
21#include <assert.h>
22
23/* if you need to change this, change numbers.c as well */
24#if SCM_SIZEOF_LONG_LONG != 0
25# ifndef LLONG_MAX
26# define ULLONG_MAX ((unsigned long long) (-1))
27# define LLONG_MAX ((long long) (ULLONG_MAX >> 1))
28# define LLONG_MIN (~LLONG_MAX)
29# endif
30#endif
31
b313d73a
KR
32
33SCM out_of_range_handler (void *data, SCM key, SCM args);
34SCM call_num2long_long_body (void *data);
35SCM call_num2ulong_long_body (void *data);
36
37/* expect to catch an `out-of-range' exception */
38SCM
39out_of_range_handler (void *data, SCM key, SCM args)
40{
41 assert (scm_equal_p (key, scm_str2symbol ("out-of-range")));
42 return SCM_BOOL_T;
43}
44
45SCM
46call_num2long_long_body (void *data)
47{
48 scm_num2long_long (* (SCM *) data, SCM_ARG1, "call_num2long_long_body");
49 return SCM_BOOL_F;
50}
51
52SCM
53call_num2ulong_long_body (void *data)
54{
55 scm_num2ulong_long (* (SCM *) data, SCM_ARG1, "call_num2ulong_long_body");
56 return SCM_BOOL_F;
57}
58
257ca0d7
RB
59static void
60test_long_long ()
61{
62#if SCM_SIZEOF_LONG_LONG != 0
63 {
64 SCM n = scm_long_long2num (LLONG_MIN);
65 long long result = scm_num2long_long(n, 0, "main");
66 assert (result == LLONG_MIN);
67 }
b313d73a
KR
68
69 /* LLONG_MIN - 1 */
70 {
71 SCM n = scm_difference (scm_long_long2num (LLONG_MIN), SCM_MAKINUM(1));
72 SCM caught = scm_internal_catch (SCM_BOOL_T, call_num2long_long_body, &n,
73 out_of_range_handler, NULL);
74 assert (! SCM_FALSEP (caught));
75 }
76
77 /* LLONG_MIN + LLONG_MIN/2 */
78 {
79 SCM n = scm_sum (scm_long_long2num (LLONG_MIN),
80 scm_long_long2num (LLONG_MIN / 2));
81 SCM caught = scm_internal_catch (SCM_BOOL_T, call_num2long_long_body, &n,
82 out_of_range_handler, NULL);
83 assert (! SCM_FALSEP (caught));
84 }
85
86 /* LLONG_MAX + 1 */
87 {
88 SCM n = scm_sum (scm_long_long2num (LLONG_MAX), SCM_MAKINUM(1));
89 SCM caught = scm_internal_catch (SCM_BOOL_T, call_num2long_long_body, &n,
90 out_of_range_handler, NULL);
91 assert (! SCM_FALSEP (caught));
92 }
93
94 /* 2^1024 */
95 {
96 SCM n = scm_ash (SCM_MAKINUM (1), SCM_MAKINUM (1024));
97 SCM caught = scm_internal_catch (SCM_BOOL_T, call_num2long_long_body, &n,
98 out_of_range_handler, NULL);
99 assert (! SCM_FALSEP (caught));
100 }
101
102 /* -2^1024 */
103 {
104 SCM n = scm_difference (SCM_MAKINUM (0),
105 scm_ash (SCM_MAKINUM (1), SCM_MAKINUM (1024)));
106 SCM caught = scm_internal_catch (SCM_BOOL_T, call_num2long_long_body, &n,
107 out_of_range_handler, NULL);
108 assert (! SCM_FALSEP (caught));
109 }
110
111#endif /* SCM_SIZEOF_LONG_LONG != 0 */
112}
113
114static void
115test_ulong_long ()
116{
117#if SCM_SIZEOF_LONG_LONG != 0
118
257ca0d7
RB
119 {
120 SCM n = scm_ulong_long2num (ULLONG_MAX);
121 unsigned long long result = scm_num2ulong_long(n, 0, "main");
122 assert (result == ULLONG_MAX);
123 }
b313d73a
KR
124
125 /* -1 */
126 {
127 SCM n = SCM_MAKINUM (-1);
128 SCM caught = scm_internal_catch (SCM_BOOL_T, call_num2ulong_long_body, &n,
129 out_of_range_handler, NULL);
130 assert (! SCM_FALSEP (caught));
131 }
132
133 /* ULLONG_MAX + 1 */
134 {
135 SCM n = scm_sum (scm_ulong_long2num (ULLONG_MAX), SCM_MAKINUM(1));
136 SCM caught = scm_internal_catch (SCM_BOOL_T, call_num2ulong_long_body, &n,
137 out_of_range_handler, NULL);
138 assert (! SCM_FALSEP (caught));
139 }
140
141 /* 2^1024 */
142 {
143 SCM n = scm_ash (SCM_MAKINUM (1), SCM_MAKINUM (1024));
144 SCM caught = scm_internal_catch (SCM_BOOL_T, call_num2long_long_body, &n,
145 out_of_range_handler, NULL);
146 assert (! SCM_FALSEP (caught));
147 }
148
257ca0d7
RB
149#endif /* SCM_SIZEOF_LONG_LONG != 0 */
150}
151
152int
153main (int argc, char *argv[])
154{
155 scm_init_guile();
156 test_long_long ();
b313d73a 157 test_ulong_long ();
257ca0d7
RB
158 return 0;
159}