Remove now unneeded `putenv.c'.
[bpt/guile.git] / test-suite / standalone / test-conversion.c
CommitLineData
d0f452d1 1/* Copyright (C) 1999,2000,2001,2003,2004, 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
170bb182
MV
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.
170bb182 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
170bb182
MV
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
170bb182
MV
17 */
18
5995c6d8
LC
19#if HAVE_CONFIG_H
20# include <config.h>
21#endif
22
23#include <libguile.h>
170bb182
MV
24
25#include <stdio.h>
26#include <assert.h>
9bd10f46 27#include <string.h>
170bb182 28
c8bb98a9
LC
29#ifdef HAVE_INTTYPES_H
30# include <inttypes.h>
fcbc0868
LC
31#endif
32
33#ifndef PRIiMAX
c8bb98a9
LC
34# if (defined SIZEOF_LONG_LONG) && (SIZEOF_LONG_LONG >= 8)
35# define PRIiMAX "lli"
36# define PRIuMAX "llu"
37# else
38# define PRIiMAX "li"
39# define PRIuMAX "lu"
40# endif
41#endif
42
43
170bb182
MV
44static void
45test_1 (const char *str, scm_t_intmax min, scm_t_intmax max,
46 int result)
47{
48 int r = scm_is_signed_integer (scm_c_eval_string (str), min, max);
49 if (r != result)
50 {
c8bb98a9
LC
51 fprintf (stderr, "fail: scm_is_signed_integer (%s, "
52 "%" PRIiMAX ", %" PRIiMAX ") == %d\n",
170bb182
MV
53 str, min, max, result);
54 exit (1);
55 }
56}
57
58static void
59test_is_signed_integer ()
60{
61 test_1 ("'foo",
62 SCM_T_INTMAX_MIN, SCM_T_INTMAX_MAX,
63 0);
64 test_1 ("3.0",
abe1308c
MV
65 SCM_T_INTMAX_MIN, SCM_T_INTMAX_MAX,
66 0);
67 test_1 ("(inexact->exact 3.0)",
170bb182
MV
68 SCM_T_INTMAX_MIN, SCM_T_INTMAX_MAX,
69 1);
70 test_1 ("3.5",
71 SCM_T_INTMAX_MIN, SCM_T_INTMAX_MAX,
72 0);
73 test_1 ("most-positive-fixnum",
74 SCM_T_INTMAX_MIN, SCM_T_INTMAX_MAX,
75 1);
76 test_1 ("(+ most-positive-fixnum 1)",
77 SCM_T_INTMAX_MIN, SCM_T_INTMAX_MAX,
78 1);
79 test_1 ("most-negative-fixnum",
80 SCM_T_INTMAX_MIN, SCM_T_INTMAX_MAX,
81 1);
82 test_1 ("(- most-negative-fixnum 1)",
83 SCM_T_INTMAX_MIN, SCM_T_INTMAX_MAX,
84 1);
85 if (sizeof (scm_t_intmax) == 8)
86 {
87 test_1 ("(- (expt 2 63) 1)",
88 SCM_T_INTMAX_MIN, SCM_T_INTMAX_MAX,
89 1);
90 test_1 ("(expt 2 63)",
91 SCM_T_INTMAX_MIN, SCM_T_INTMAX_MAX,
92 0);
93 test_1 ("(- (expt 2 63))",
94 SCM_T_INTMAX_MIN, SCM_T_INTMAX_MAX,
95 1);
96 test_1 ("(- (- (expt 2 63)) 1)",
97 SCM_T_INTMAX_MIN, SCM_T_INTMAX_MAX,
98 0);
99 }
100 else if (sizeof (scm_t_intmax) == 4)
101 {
102 test_1 ("(- (expt 2 31) 1)",
103 SCM_T_INTMAX_MIN, SCM_T_INTMAX_MAX,
104 1);
105 test_1 ("(expt 2 31)",
106 SCM_T_INTMAX_MIN, SCM_T_INTMAX_MAX,
107 0);
108 test_1 ("(- (expt 2 31))",
109 SCM_T_INTMAX_MIN, SCM_T_INTMAX_MAX,
110 1);
111 test_1 ("(- (- (expt 2 31)) 1)",
112 SCM_T_INTMAX_MIN, SCM_T_INTMAX_MAX,
113 0);
114 }
115 else
116 fprintf (stderr, "NOTE: skipped some tests.\n");
117
118 /* bignum with range that fits into fixnum. */
119 test_1 ("(+ most-positive-fixnum 1)",
120 -32768, 32767,
121 0);
122
123 /* bignum with range that doesn't fit into fixnum, but probably
124 fits into long. */
125 test_1 ("(+ most-positive-fixnum 1)",
126 SCM_MOST_NEGATIVE_FIXNUM-1, SCM_MOST_POSITIVE_FIXNUM+1,
127 1);
128}
129
130static void
131test_2 (const char *str, scm_t_uintmax min, scm_t_uintmax max,
132 int result)
133{
134 int r = scm_is_unsigned_integer (scm_c_eval_string (str), min, max);
135 if (r != result)
136 {
c8bb98a9
LC
137 fprintf (stderr, "fail: scm_is_unsigned_integer (%s, "
138 "%" PRIuMAX ", %" PRIuMAX ") == %d\n",
170bb182
MV
139 str, min, max, result);
140 exit (1);
141 }
142}
143
144static void
145test_is_unsigned_integer ()
146{
147 test_2 ("'foo",
afdb04ef 148 0, SCM_T_UINTMAX_MAX,
170bb182
MV
149 0);
150 test_2 ("3.0",
abe1308c
MV
151 0, SCM_T_UINTMAX_MAX,
152 0);
153 test_2 ("(inexact->exact 3.0)",
afdb04ef 154 0, SCM_T_UINTMAX_MAX,
170bb182
MV
155 1);
156 test_2 ("3.5",
afdb04ef 157 0, SCM_T_UINTMAX_MAX,
170bb182
MV
158 0);
159 test_2 ("most-positive-fixnum",
afdb04ef 160 0, SCM_T_UINTMAX_MAX,
170bb182
MV
161 1);
162 test_2 ("(+ most-positive-fixnum 1)",
afdb04ef 163 0, SCM_T_UINTMAX_MAX,
170bb182
MV
164 1);
165 test_2 ("most-negative-fixnum",
afdb04ef 166 0, SCM_T_UINTMAX_MAX,
170bb182
MV
167 0);
168 test_2 ("(- most-negative-fixnum 1)",
afdb04ef 169 0, SCM_T_UINTMAX_MAX,
170bb182
MV
170 0);
171 if (sizeof (scm_t_intmax) == 8)
172 {
173 test_2 ("(- (expt 2 64) 1)",
afdb04ef 174 0, SCM_T_UINTMAX_MAX,
170bb182
MV
175 1);
176 test_2 ("(expt 2 64)",
afdb04ef 177 0, SCM_T_UINTMAX_MAX,
170bb182
MV
178 0);
179 }
180 else if (sizeof (scm_t_intmax) == 4)
181 {
182 test_2 ("(- (expt 2 32) 1)",
afdb04ef 183 0, SCM_T_UINTMAX_MAX,
170bb182
MV
184 1);
185 test_2 ("(expt 2 32)",
afdb04ef 186 0, SCM_T_UINTMAX_MAX,
170bb182
MV
187 0);
188 }
189 else
190 fprintf (stderr, "NOTE: skipped some tests.\n");
191
192 /* bignum with range that fits into fixnum. */
193 test_2 ("(+ most-positive-fixnum 1)",
194 0, 32767,
195 0);
196
197 /* bignum with range that doesn't fit into fixnum, but probably
198 fits into long. */
199 test_2 ("(+ most-positive-fixnum 1)",
200 0, SCM_MOST_POSITIVE_FIXNUM+1,
201 1);
202}
203
204typedef struct {
205 SCM val;
206 scm_t_intmax min, max;
207 scm_t_intmax result;
208} to_signed_data;
209
210static SCM
211out_of_range_handler (void *data, SCM key, SCM args)
212{
ad6dec05 213 return scm_equal_p (key, scm_from_locale_symbol ("out-of-range"));
170bb182
MV
214}
215
216static SCM
217wrong_type_handler (void *data, SCM key, SCM args)
218{
ad6dec05 219 return scm_equal_p (key, scm_from_locale_symbol ("wrong-type-arg"));
170bb182
MV
220}
221
9bd10f46
MV
222static SCM
223misc_error_handler (void *data, SCM key, SCM args)
224{
ad6dec05 225 return scm_equal_p (key, scm_from_locale_symbol ("misc-error"));
9bd10f46
MV
226}
227
170bb182
MV
228static SCM
229any_handler (void *data, SCM key, SCM args)
230{
231 return SCM_BOOL_T;
232}
233
234static SCM
235to_signed_integer_body (void *data)
236{
237 to_signed_data *d = (to_signed_data *)data;
238 d->result = scm_to_signed_integer (d->val, d->min, d->max);
239 return SCM_BOOL_F;
240}
241
242static void
243test_3 (const char *str, scm_t_intmax min, scm_t_intmax max,
244 scm_t_intmax result, int range_error, int type_error)
245{
246 to_signed_data data;
247 data.val = scm_c_eval_string (str);
248 data.min = min;
249 data.max = max;
250
251 if (range_error)
252 {
253 if (scm_is_false (scm_internal_catch (SCM_BOOL_T,
254 to_signed_integer_body, &data,
255 out_of_range_handler, NULL)))
256 {
257 fprintf (stderr,
c8bb98a9
LC
258 "fail: scm_to_signed_int (%s, "
259 "%" PRIiMAX ", %" PRIiMAX ") -> out of range\n",
170bb182
MV
260 str, min, max);
261 exit (1);
262 }
263 }
264 else if (type_error)
265 {
266 if (scm_is_false (scm_internal_catch (SCM_BOOL_T,
267 to_signed_integer_body, &data,
268 wrong_type_handler, NULL)))
269 {
270 fprintf (stderr,
c8bb98a9
LC
271 "fail: scm_to_signed_int (%s, "
272 "%" PRIiMAX", %" PRIiMAX ") -> wrong type\n",
170bb182
MV
273 str, min, max);
274 exit (1);
275 }
276 }
277 else
278 {
279 if (scm_is_true (scm_internal_catch (SCM_BOOL_T,
280 to_signed_integer_body, &data,
281 any_handler, NULL))
282 || data.result != result)
283 {
284 fprintf (stderr,
c8bb98a9
LC
285 "fail: scm_to_signed_int (%s, "
286 "%" PRIiMAX ", %" PRIiMAX ") = %" PRIiMAX "\n",
170bb182
MV
287 str, min, max, result);
288 exit (1);
289 }
290 }
291}
292
293static void
294test_to_signed_integer ()
295{
296 test_3 ("'foo",
297 SCM_T_INTMAX_MIN, SCM_T_INTMAX_MAX,
298 0, 0, 1);
299 test_3 ("3.5",
300 SCM_T_INTMAX_MIN, SCM_T_INTMAX_MAX,
301 0, 0, 1);
302 test_3 ("12",
303 SCM_T_INTMAX_MIN, SCM_T_INTMAX_MAX,
304 12, 0, 0);
305 test_3 ("1000",
306 -999, 999,
307 0, 1, 0);
308 test_3 ("-1000",
309 -999, 999,
310 0, 1, 0);
311 test_3 ("most-positive-fixnum",
312 SCM_T_INTMAX_MIN, SCM_T_INTMAX_MAX,
313 SCM_MOST_POSITIVE_FIXNUM, 0, 0);
314 test_3 ("most-negative-fixnum",
315 SCM_T_INTMAX_MIN, SCM_T_INTMAX_MAX,
316 SCM_MOST_NEGATIVE_FIXNUM, 0, 0);
317 test_3 ("(+ most-positive-fixnum 1)",
318 SCM_T_INTMAX_MIN, SCM_T_INTMAX_MAX,
319 SCM_MOST_POSITIVE_FIXNUM+1, 0, 0);
320 test_3 ("(- most-negative-fixnum 1)",
321 SCM_T_INTMAX_MIN, SCM_T_INTMAX_MAX,
322 SCM_MOST_NEGATIVE_FIXNUM-1, 0, 0);
323 if (sizeof (scm_t_intmax) == 8)
324 {
325 test_3 ("(- (expt 2 63) 1)",
326 SCM_T_INTMAX_MIN, SCM_T_INTMAX_MAX,
327 SCM_T_INTMAX_MAX, 0, 0);
328 test_3 ("(+ (- (expt 2 63)) 1)",
329 SCM_T_INTMAX_MIN, SCM_T_INTMAX_MAX,
330 SCM_T_INTMAX_MIN+1, 0, 0);
331 test_3 ("(- (expt 2 63))",
332 SCM_T_INTMAX_MIN, SCM_T_INTMAX_MAX,
333 SCM_T_INTMAX_MIN, 0, 0);
334 test_3 ("(expt 2 63)",
335 SCM_T_INTMAX_MIN, SCM_T_INTMAX_MAX,
336 0, 1, 0);
337 test_3 ("(- (- (expt 2 63)) 1)",
338 SCM_T_INTMAX_MIN, SCM_T_INTMAX_MAX,
339 0, 1, 0);
340 }
341 else if (sizeof (scm_t_intmax) == 4)
342 {
343 test_3 ("(- (expt 2 31) 1)",
344 SCM_T_INTMAX_MIN, SCM_T_INTMAX_MAX,
345 SCM_T_INTMAX_MAX, 0, 0);
346 test_3 ("(+ (- (expt 2 31)) 1)",
347 SCM_T_INTMAX_MIN, SCM_T_INTMAX_MAX,
348 SCM_T_INTMAX_MIN+1, 0, 0);
349 test_3 ("(- (expt 2 31))",
350 SCM_T_INTMAX_MIN, SCM_T_INTMAX_MAX,
351 SCM_T_INTMAX_MIN, 0, 0);
352 test_3 ("(expt 2 31)",
353 SCM_T_INTMAX_MIN, SCM_T_INTMAX_MAX,
354 0, 1, 0);
355 test_3 ("(- (- (expt 2 31)) 1)",
356 SCM_T_INTMAX_MIN, SCM_T_INTMAX_MAX,
357 0, 1, 0);
358 }
359 else
360 fprintf (stderr, "NOTE: skipped some tests.\n");
361}
362
363typedef struct {
364 SCM val;
365 scm_t_uintmax min, max;
366 scm_t_uintmax result;
367} to_unsigned_data;
368
369static SCM
370to_unsigned_integer_body (void *data)
371{
372 to_unsigned_data *d = (to_unsigned_data *)data;
373 d->result = scm_to_unsigned_integer (d->val, d->min, d->max);
374 return SCM_BOOL_F;
375}
376
377static void
378test_4 (const char *str, scm_t_uintmax min, scm_t_uintmax max,
379 scm_t_uintmax result, int range_error, int type_error)
380{
381 to_unsigned_data data;
382 data.val = scm_c_eval_string (str);
383 data.min = min;
384 data.max = max;
385
386 if (range_error)
387 {
388 if (scm_is_false (scm_internal_catch (SCM_BOOL_T,
389 to_unsigned_integer_body, &data,
390 out_of_range_handler, NULL)))
391 {
392 fprintf (stderr,
c8bb98a9
LC
393 "fail: scm_to_unsigned_int (%s, "
394 "%" PRIuMAX ", %" PRIuMAX ") -> out of range\n",
170bb182
MV
395 str, min, max);
396 exit (1);
397 }
398 }
399 else if (type_error)
400 {
401 if (scm_is_false (scm_internal_catch (SCM_BOOL_T,
402 to_unsigned_integer_body, &data,
403 wrong_type_handler, NULL)))
404 {
405 fprintf (stderr,
c8bb98a9
LC
406 "fail: scm_to_unsigned_int (%s, "
407 "%" PRIuMAX ", %" PRIuMAX ") -> wrong type\n",
170bb182
MV
408 str, min, max);
409 exit (1);
410 }
411 }
412 else
413 {
414 if (scm_is_true (scm_internal_catch (SCM_BOOL_T,
415 to_unsigned_integer_body, &data,
416 any_handler, NULL))
417 || data.result != result)
418 {
419 fprintf (stderr,
c8bb98a9
LC
420 "fail: scm_to_unsigned_int (%s, "
421 "%" PRIuMAX ", %" PRIuMAX ") == %" PRIuMAX "\n",
170bb182
MV
422 str, min, max, result);
423 exit (1);
424 }
425 }
426}
427
428static void
429test_to_unsigned_integer ()
430{
431 test_4 ("'foo",
afdb04ef 432 0, SCM_T_UINTMAX_MAX,
170bb182
MV
433 0, 0, 1);
434 test_4 ("3.5",
afdb04ef 435 0, SCM_T_UINTMAX_MAX,
170bb182
MV
436 0, 0, 1);
437 test_4 ("12",
afdb04ef 438 0, SCM_T_UINTMAX_MAX,
170bb182
MV
439 12, 0, 0);
440 test_4 ("1000",
441 0, 999,
442 0, 1, 0);
443 test_4 ("most-positive-fixnum",
afdb04ef 444 0, SCM_T_UINTMAX_MAX,
170bb182
MV
445 SCM_MOST_POSITIVE_FIXNUM, 0, 0);
446 test_4 ("(+ most-positive-fixnum 1)",
afdb04ef 447 0, SCM_T_UINTMAX_MAX,
170bb182
MV
448 SCM_MOST_POSITIVE_FIXNUM+1, 0, 0);
449 if (sizeof (scm_t_intmax) == 8)
450 {
451 test_4 ("(- (expt 2 64) 1)",
afdb04ef 452 0, SCM_T_UINTMAX_MAX,
170bb182
MV
453 SCM_T_UINTMAX_MAX, 0, 0);
454 test_4 ("(expt 2 64)",
afdb04ef 455 0, SCM_T_UINTMAX_MAX,
170bb182
MV
456 0, 1, 0);
457 }
458 else if (sizeof (scm_t_intmax) == 4)
459 {
460 test_4 ("(- (expt 2 32) 1)",
afdb04ef 461 0, SCM_T_UINTMAX_MAX,
170bb182
MV
462 SCM_T_UINTMAX_MAX, 0, 0);
463 test_4 ("(expt 2 32)",
afdb04ef 464 0, SCM_T_UINTMAX_MAX,
170bb182
MV
465 0, 1, 0);
466 }
467 else
468 fprintf (stderr, "NOTE: skipped some tests.\n");
469}
470
471static void
472test_5 (scm_t_intmax val, const char *result)
473{
474 SCM res = scm_c_eval_string (result);
475 if (scm_is_false (scm_equal_p (scm_from_signed_integer (val), res)))
476 {
c8bb98a9 477 fprintf (stderr, "fail: scm_from_signed_integer (%" PRIiMAX ") == %s\n",
170bb182
MV
478 val, result);
479 exit (1);
480 }
481}
482
483static void
484test_from_signed_integer ()
485{
486 test_5 (12, "12");
487 if (sizeof (scm_t_intmax) == 8)
488 {
489 test_5 (SCM_T_INTMAX_MAX, "(- (expt 2 63) 1)");
490 test_5 (SCM_T_INTMAX_MIN, "(- (expt 2 63))");
491 }
492 else if (sizeof (scm_t_intmax) == 4)
493 {
494 test_5 (SCM_T_INTMAX_MAX, "(- (expt 2 31) 1)");
495 test_5 (SCM_T_INTMAX_MIN, "(- (expt 2 31))");
496 }
497 test_5 (SCM_MOST_POSITIVE_FIXNUM, "most-positive-fixnum");
498 test_5 (SCM_MOST_NEGATIVE_FIXNUM, "most-negative-fixnum");
499 test_5 (SCM_MOST_POSITIVE_FIXNUM+1, "(+ most-positive-fixnum 1)");
500 test_5 (SCM_MOST_NEGATIVE_FIXNUM-1, "(- most-negative-fixnum 1)");
501}
502
503static void
504test_6 (scm_t_uintmax val, const char *result)
505{
506 SCM res = scm_c_eval_string (result);
507 if (scm_is_false (scm_equal_p (scm_from_unsigned_integer (val), res)))
508 {
c8bb98a9
LC
509 fprintf (stderr, "fail: scm_from_unsigned_integer (%"
510 PRIuMAX ") == %s\n",
170bb182
MV
511 val, result);
512 exit (1);
513 }
514}
515
516static void
517test_from_unsigned_integer ()
518{
519 test_6 (12, "12");
520 if (sizeof (scm_t_intmax) == 8)
521 {
522 test_6 (SCM_T_UINTMAX_MAX, "(- (expt 2 64) 1)");
523 }
524 else if (sizeof (scm_t_intmax) == 4)
525 {
526 test_6 (SCM_T_UINTMAX_MAX, "(- (expt 2 32) 1)");
527 }
528 test_6 (SCM_MOST_POSITIVE_FIXNUM, "most-positive-fixnum");
529 test_6 (SCM_MOST_POSITIVE_FIXNUM+1, "(+ most-positive-fixnum 1)");
530}
531
3838c384
MV
532static void
533test_7s (SCM n, scm_t_intmax c_n, const char *result, const char *func)
534{
535 SCM r = scm_c_eval_string (result);
536
537 if (scm_is_false (scm_equal_p (n, r)))
538 {
c8bb98a9 539 fprintf (stderr, "fail: %s (%" PRIiMAX ") == %s\n", func, c_n, result);
3838c384
MV
540 exit (1);
541 }
542}
543
544#define TEST_7S(func,arg,res) test_7s (func(arg), arg, res, #func)
545
546static void
547test_7u (SCM n, scm_t_uintmax c_n, const char *result, const char *func)
548{
549 SCM r = scm_c_eval_string (result);
550
551 if (scm_is_false (scm_equal_p (n, r)))
552 {
c8bb98a9 553 fprintf (stderr, "fail: %s (%" PRIuMAX ") == %s\n", func, c_n, result);
3838c384
MV
554 exit (1);
555 }
556}
557
558#define TEST_7U(func,arg,res) test_7u (func(arg), arg, res, #func)
559
560typedef struct {
561 SCM val;
562 scm_t_intmax (*func) (SCM);
563 scm_t_intmax result;
564} to_signed_func_data;
565
566static SCM
567to_signed_func_body (void *data)
568{
569 to_signed_func_data *d = (to_signed_func_data *)data;
570 d->result = d->func (d->val);
571 return SCM_BOOL_F;
572}
573
574static void
575test_8s (const char *str, scm_t_intmax (*func) (SCM), const char *func_name,
576 scm_t_intmax result, int range_error, int type_error)
577{
578 to_signed_func_data data;
579 data.val = scm_c_eval_string (str);
580 data.func = func;
581
582 if (range_error)
583 {
584 if (scm_is_false (scm_internal_catch (SCM_BOOL_T,
585 to_signed_func_body, &data,
586 out_of_range_handler, NULL)))
587 {
588 fprintf (stderr,
589 "fail: %s (%s) -> out of range\n", func_name, str);
590 exit (1);
591 }
592 }
593 else if (type_error)
594 {
595 if (scm_is_false (scm_internal_catch (SCM_BOOL_T,
596 to_signed_func_body, &data,
597 wrong_type_handler, NULL)))
598 {
599 fprintf (stderr,
600 "fail: %s (%s) -> wrong type\n", func_name, str);
601 exit (1);
602 }
603 }
604 else
605 {
606 if (scm_is_true (scm_internal_catch (SCM_BOOL_T,
607 to_signed_func_body, &data,
608 any_handler, NULL))
609 || data.result != result)
610 {
611 fprintf (stderr,
c8bb98a9 612 "fail: %s (%s) = %" PRIiMAX "\n", func_name, str, result);
3838c384
MV
613 exit (1);
614 }
615 }
616}
617
618typedef struct {
619 SCM val;
620 scm_t_uintmax (*func) (SCM);
621 scm_t_uintmax result;
622} to_unsigned_func_data;
623
624static SCM
625to_unsigned_func_body (void *data)
626{
627 to_unsigned_func_data *d = (to_unsigned_func_data *)data;
628 d->result = d->func (d->val);
629 return SCM_BOOL_F;
630}
631
632static void
633test_8u (const char *str, scm_t_uintmax (*func) (SCM), const char *func_name,
634 scm_t_uintmax result, int range_error, int type_error)
635{
636 to_unsigned_func_data data;
637 data.val = scm_c_eval_string (str);
638 data.func = func;
639
640 if (range_error)
641 {
642 if (scm_is_false (scm_internal_catch (SCM_BOOL_T,
643 to_unsigned_func_body, &data,
644 out_of_range_handler, NULL)))
645 {
646 fprintf (stderr,
647 "fail: %s (%s) -> out of range\n", func_name, str);
648 exit (1);
649 }
650 }
651 else if (type_error)
652 {
653 if (scm_is_false (scm_internal_catch (SCM_BOOL_T,
654 to_unsigned_func_body, &data,
655 wrong_type_handler, NULL)))
656 {
657 fprintf (stderr,
658 "fail: %s (%s) -> wrong type\n", func_name, str);
659 exit (1);
660 }
661 }
662 else
663 {
664 if (scm_is_true (scm_internal_catch (SCM_BOOL_T,
665 to_unsigned_func_body, &data,
666 any_handler, NULL))
667 || data.result != result)
668 {
669 fprintf (stderr,
c8bb98a9 670 "fail: %s (%s) = %" PRIiMAX "\n", func_name, str, result);
3838c384
MV
671 exit (1);
672 }
673 }
674}
675
676/* We can't rely on the scm_to functions being proper functions but we
677 want to pass them to test_8s and test_8u, so we wrap'em. Also, we
678 need to give them a common return type.
679*/
680
681#define DEFSTST(f) static scm_t_intmax tst_##f (SCM x) { return f(x); }
682#define DEFUTST(f) static scm_t_uintmax tst_##f (SCM x) { return f(x); }
683
d0f452d1
LC
684DEFSTST (scm_to_schar)
685DEFUTST (scm_to_uchar)
686DEFSTST (scm_to_char)
687DEFSTST (scm_to_short)
688DEFUTST (scm_to_ushort)
689DEFSTST (scm_to_int)
690DEFUTST (scm_to_uint)
691DEFSTST (scm_to_long)
692DEFUTST (scm_to_ulong)
3838c384 693#if SCM_SIZEOF_LONG_LONG != 0
d0f452d1
LC
694DEFSTST (scm_to_long_long)
695DEFUTST (scm_to_ulong_long)
3838c384 696#endif
d0f452d1
LC
697DEFSTST (scm_to_ssize_t)
698DEFUTST (scm_to_size_t)
699
700DEFSTST (scm_to_int8)
701DEFUTST (scm_to_uint8)
702DEFSTST (scm_to_int16)
703DEFUTST (scm_to_uint16)
704DEFSTST (scm_to_int32)
705DEFUTST (scm_to_uint32)
3838c384 706#ifdef SCM_HAVE_T_INT64
d0f452d1
LC
707DEFSTST (scm_to_int64)
708DEFUTST (scm_to_uint64)
3838c384
MV
709#endif
710
711#define TEST_8S(v,f,r,re,te) test_8s (v, tst_##f, #f, r, re, te)
712#define TEST_8U(v,f,r,re,te) test_8u (v, tst_##f, #f, r, re, te)
713
714
afdb04ef
MV
715static void
716test_int_sizes ()
717{
3838c384
MV
718 TEST_7U (scm_from_uchar, 91, "91");
719 TEST_7S (scm_from_schar, 91, "91");
720 TEST_7S (scm_from_char, 91, "91");
721 TEST_7S (scm_from_short, -911, "-911");
722 TEST_7U (scm_from_ushort, 911, "911");
723 TEST_7S (scm_from_int, 911, "911");
724 TEST_7U (scm_from_uint, 911, "911");
725 TEST_7S (scm_from_long, 911, "911");
726 TEST_7U (scm_from_ulong, 911, "911");
afdb04ef 727#if SCM_SIZEOF_LONG_LONG != 0
3838c384
MV
728 TEST_7S (scm_from_long_long, 911, "911");
729 TEST_7U (scm_from_ulong_long, 911, "911");
afdb04ef 730#endif
3838c384
MV
731 TEST_7U (scm_from_size_t, 911, "911");
732 TEST_7S (scm_from_ssize_t, 911, "911");
733
734 TEST_7S (scm_from_int8, -128, "-128");
735 TEST_7S (scm_from_int8, 127, "127");
736 TEST_7S (scm_from_int8, 128, "-128");
737 TEST_7U (scm_from_uint8, 255, "255");
738
739 TEST_7S (scm_from_int16, -32768, "-32768");
740 TEST_7S (scm_from_int16, 32767, "32767");
741 TEST_7S (scm_from_int16, 32768, "-32768");
742 TEST_7U (scm_from_uint16, 65535, "65535");
743
744 TEST_7S (scm_from_int32, SCM_T_INT32_MIN, "-2147483648");
745 TEST_7S (scm_from_int32, SCM_T_INT32_MAX, "2147483647");
746 TEST_7S (scm_from_int32, SCM_T_INT32_MAX+1LL, "-2147483648");
747 TEST_7U (scm_from_uint32, SCM_T_UINT32_MAX, "4294967295");
748
afdb04ef 749#if SCM_HAVE_T_INT64
3838c384
MV
750 TEST_7S (scm_from_int64, SCM_T_INT64_MIN, "-9223372036854775808");
751 TEST_7S (scm_from_int64, SCM_T_INT64_MAX, "9223372036854775807");
752 TEST_7U (scm_from_uint64, SCM_T_UINT64_MAX, "18446744073709551615");
afdb04ef
MV
753#endif
754
3838c384
MV
755 TEST_8S ("91", scm_to_schar, 91, 0, 0);
756 TEST_8U ("91", scm_to_uchar, 91, 0, 0);
757 TEST_8S ("91", scm_to_char, 91, 0, 0);
758 TEST_8S ("-911", scm_to_short, -911, 0, 0);
759 TEST_8U ("911", scm_to_ushort, 911, 0, 0);
760 TEST_8S ("-911", scm_to_int, -911, 0, 0);
761 TEST_8U ("911", scm_to_uint, 911, 0, 0);
762 TEST_8S ("-911", scm_to_long, -911, 0, 0);
763 TEST_8U ("911", scm_to_ulong, 911, 0, 0);
afdb04ef 764#if SCM_SIZEOF_LONG_LONG != 0
3838c384
MV
765 TEST_8S ("-911", scm_to_long_long, -911, 0, 0);
766 TEST_8U ("911", scm_to_ulong_long, 911, 0, 0);
afdb04ef 767#endif
3838c384
MV
768 TEST_8U ("911", scm_to_size_t, 911, 0, 0);
769 TEST_8S ("911", scm_to_ssize_t, 911, 0, 0);
770
771 TEST_8S ("-128", scm_to_int8, SCM_T_INT8_MIN, 0, 0);
772 TEST_8S ("127", scm_to_int8, SCM_T_INT8_MAX, 0, 0);
773 TEST_8S ("128", scm_to_int8, 0, 1, 0);
774 TEST_8S ("#f", scm_to_int8, 0, 0, 1);
775 TEST_8U ("255", scm_to_uint8, SCM_T_UINT8_MAX, 0, 0);
776 TEST_8U ("256", scm_to_uint8, 0, 1, 0);
777 TEST_8U ("-1", scm_to_uint8, 0, 1, 0);
778 TEST_8U ("#f", scm_to_uint8, 0, 0, 1);
779
780 TEST_8S ("-32768", scm_to_int16, SCM_T_INT16_MIN, 0, 0);
781 TEST_8S ("32767", scm_to_int16, SCM_T_INT16_MAX, 0, 0);
782 TEST_8S ("32768", scm_to_int16, 0, 1, 0);
783 TEST_8S ("#f", scm_to_int16, 0, 0, 1);
784 TEST_8U ("65535", scm_to_uint16, SCM_T_UINT16_MAX, 0, 0);
785 TEST_8U ("65536", scm_to_uint16, 0, 1, 0);
786 TEST_8U ("-1", scm_to_uint16, 0, 1, 0);
787 TEST_8U ("#f", scm_to_uint16, 0, 0, 1);
788
789 TEST_8S ("-2147483648", scm_to_int32, SCM_T_INT32_MIN, 0, 0);
790 TEST_8S ("2147483647", scm_to_int32, SCM_T_INT32_MAX, 0, 0);
791 TEST_8S ("2147483648", scm_to_int32, 0, 1, 0);
792 TEST_8S ("#f", scm_to_int32, 0, 0, 1);
793 TEST_8U ("4294967295", scm_to_uint32, SCM_T_UINT32_MAX, 0, 0);
794 TEST_8U ("4294967296", scm_to_uint32, 0, 1, 0);
795 TEST_8U ("-1", scm_to_uint32, 0, 1, 0);
796 TEST_8U ("#f", scm_to_uint32, 0, 0, 1);
797
afdb04ef 798#if SCM_HAVE_T_INT64
3838c384
MV
799 TEST_8S ("-9223372036854775808", scm_to_int64, SCM_T_INT64_MIN, 0, 0);
800 TEST_8S ("9223372036854775807", scm_to_int64, SCM_T_INT64_MAX, 0, 0);
801 TEST_8S ("9223372036854775808", scm_to_int64, 0, 1, 0);
802 TEST_8S ("#f", scm_to_int64, 0, 0, 1);
803 TEST_8U ("18446744073709551615", scm_to_uint64, SCM_T_UINT64_MAX, 0, 0);
804 TEST_8U ("18446744073709551616", scm_to_uint64, 0, 1, 0);
805 TEST_8U ("-1", scm_to_uint64, 0, 1, 0);
806 TEST_8U ("#f", scm_to_uint64, 0, 0, 1);
afdb04ef
MV
807#endif
808
809}
810
eae5018e
MV
811static void
812test_9 (double val, const char *result)
813{
814 SCM res = scm_c_eval_string (result);
815 if (scm_is_false (scm_eqv_p (res, scm_from_double (val))))
816 {
817 fprintf (stderr, "fail: scm_from_double (%g) == %s\n", val, result);
818 exit (1);
819 }
820}
821
e2e85d14
LC
822/* The `infinity' and `not-a-number' values. */
823static double guile_Inf, guile_NaN;
824
825/* Initialize GUILE_INF and GUILE_NAN. Taken from `guile_ieee_init ()' in
826 `libguile/numbers.c'. */
827static void
828ieee_init (void)
829{
830#ifdef INFINITY
831 /* C99 INFINITY, when available.
832 FIXME: The standard allows for INFINITY to be something that overflows
833 at compile time. We ought to have a configure test to check for that
834 before trying to use it. (But in practice we believe this is not a
835 problem on any system guile is likely to target.) */
836 guile_Inf = INFINITY;
837#elif HAVE_DINFINITY
838 /* OSF */
839 extern unsigned int DINFINITY[2];
840 guile_Inf = (*((double *) (DINFINITY)));
841#else
842 double tmp = 1e+10;
843 guile_Inf = tmp;
844 for (;;)
845 {
846 guile_Inf *= 1e+10;
847 if (guile_Inf == tmp)
848 break;
849 tmp = guile_Inf;
850 }
851#endif
852
853#ifdef NAN
854 /* C99 NAN, when available */
855 guile_NaN = NAN;
856#elif HAVE_DQNAN
857 {
858 /* OSF */
859 extern unsigned int DQNAN[2];
860 guile_NaN = (*((double *)(DQNAN)));
861 }
862#else
863 guile_NaN = guile_Inf / guile_Inf;
864#endif
865}
866
eae5018e
MV
867static void
868test_from_double ()
869{
870 test_9 (12, "12.0");
871 test_9 (0.25, "0.25");
872 test_9 (0.1, "0.1");
e2e85d14
LC
873 test_9 (guile_Inf, "+inf.0");
874 test_9 (-guile_Inf, "-inf.0");
875 test_9 (guile_NaN, "+nan.0");
eae5018e
MV
876}
877
878typedef struct {
879 SCM val;
880 double result;
881} to_double_data;
882
883static SCM
884to_double_body (void *data)
885{
886 to_double_data *d = (to_double_data *)data;
887 d->result = scm_to_double (d->val);
888 return SCM_BOOL_F;
889}
890
891static void
892test_10 (const char *val, double result, int type_error)
893{
894 to_double_data data;
895 data.val = scm_c_eval_string (val);
896
897 if (type_error)
898 {
899 if (scm_is_false (scm_internal_catch (SCM_BOOL_T,
900 to_double_body, &data,
901 wrong_type_handler, NULL)))
902 {
903 fprintf (stderr,
904 "fail: scm_double (%s) -> wrong type\n", val);
905 exit (1);
906 }
907 }
908 else
909 {
910 if (scm_is_true (scm_internal_catch (SCM_BOOL_T,
911 to_double_body, &data,
912 any_handler, NULL))
913 || data.result != result)
914 {
915 fprintf (stderr,
916 "fail: scm_to_double (%s) = %g\n", val, result);
917 exit (1);
918 }
919 }
920}
921
922static void
923test_to_double ()
924{
925 test_10 ("#f", 0.0, 1);
926 test_10 ("12", 12.0, 0);
927 test_10 ("0.25", 0.25, 0);
928 test_10 ("1/4", 0.25, 0);
e2e85d14
LC
929 test_10 ("+inf.0", guile_Inf, 0);
930 test_10 ("-inf.0",-guile_Inf, 0);
eae5018e
MV
931 test_10 ("+1i", 0.0, 1);
932}
933
9bd10f46
MV
934typedef struct {
935 SCM val;
936 char *result;
937} to_locale_string_data;
938
939static SCM
940to_locale_string_body (void *data)
941{
942 to_locale_string_data *d = (to_locale_string_data *)data;
943 d->result = scm_to_locale_string (d->val);
944 return SCM_BOOL_F;
945}
946
947static void
948test_11 (const char *str, const char *result, int misc_error, int type_error)
949{
950 to_locale_string_data data;
951 data.val = scm_c_eval_string (str);
952 data.result = NULL;
953
954 if (misc_error)
955 {
956 if (scm_is_false (scm_internal_catch (SCM_BOOL_T,
957 to_locale_string_body, &data,
958 misc_error_handler, NULL)))
959 {
960 fprintf (stderr,
961 "fail: scm_to_locale_string (%s) -> misc error\n", str);
962 exit (1);
963 }
964 }
965 else if (type_error)
966 {
967 if (scm_is_false (scm_internal_catch (SCM_BOOL_T,
968 to_locale_string_body, &data,
969 wrong_type_handler, NULL)))
970 {
971 fprintf (stderr,
972 "fail: scm_to_locale_string (%s) -> wrong type\n", str);
973 exit (1);
974 }
975 }
976 else
977 {
978 if (scm_is_true (scm_internal_catch (SCM_BOOL_T,
979 to_locale_string_body, &data,
980 any_handler, NULL))
981 || data.result == NULL || strcmp (data.result, result))
982 {
983 fprintf (stderr,
984 "fail: scm_to_locale_string (%s) = %s\n", str, result);
985 exit (1);
986 }
987 }
988
989 free (data.result);
990}
991
992static void
993test_locale_strings ()
994{
995 const char *lstr = "This is not a string.";
996 char *lstr2;
997 SCM str, str2;
998 char buf[20];
999 size_t len;
1000
1001 if (!scm_is_string (scm_c_eval_string ("\"foo\"")))
1002 {
1003 fprintf (stderr, "fail: scm_is_string (\"foo\") = true\n");
1004 exit (1);
1005 }
1006
1007 str = scm_from_locale_string (lstr);
1008
1009 if (!scm_is_string (str))
1010 {
1011 fprintf (stderr, "fail: scm_is_string (str) = true\n");
1012 exit (1);
1013 }
1014
1015 lstr2 = scm_to_locale_string (str);
1016 if (strcmp (lstr, lstr2))
1017 {
1018 fprintf (stderr, "fail: lstr = lstr2\n");
1019 exit (1);
1020 }
1021 free (lstr2);
1022
1023 buf[15] = 'x';
1024 len = scm_to_locale_stringbuf (str, buf, 15);
1025 if (len != strlen (lstr))
1026 {
1027 fprintf (stderr, "fail: scm_to_locale_stringbuf (...) = strlen(lstr)\n");
1028 exit (1);
1029 }
1030 if (buf[15] != 'x')
1031 {
1032 fprintf (stderr, "fail: scm_to_locale_stringbuf (...) no overrun\n");
1033 exit (1);
1034 }
1035 if (strncmp (lstr, buf, 15))
1036 {
1037 fprintf (stderr, "fail: scm_to_locale_stringbuf (...) = lstr\n");
1038 exit (1);
1039 }
1040
1041 str2 = scm_from_locale_stringn (lstr, 10);
1042
1043 if (!scm_is_string (str2))
1044 {
1045 fprintf (stderr, "fail: scm_is_string (str2) = true\n");
1046 exit (1);
1047 }
1048
1049 lstr2 = scm_to_locale_string (str2);
1050 if (strncmp (lstr, lstr2, 10))
1051 {
1052 fprintf (stderr, "fail: lstr = lstr2\n");
1053 exit (1);
1054 }
1055 free (lstr2);
1056
1057 buf[10] = 'x';
1058 len = scm_to_locale_stringbuf (str2, buf, 20);
1059 if (len != 10)
1060 {
1061 fprintf (stderr, "fail: scm_to_locale_stringbuf (...) = 10\n");
1062 exit (1);
1063 }
1064 if (buf[10] != 'x')
1065 {
1066 fprintf (stderr, "fail: scm_to_locale_stringbuf (...) no overrun\n");
1067 exit (1);
1068 }
1069 if (strncmp (lstr, buf, 10))
1070 {
1071 fprintf (stderr, "fail: scm_to_locale_stringbuf (...) = lstr\n");
1072 exit (1);
1073 }
1074
1075 lstr2 = scm_to_locale_stringn (str2, &len);
1076 if (len != 10)
1077 {
1078 fprintf (stderr, "fail: scm_to_locale_stringn, len = 10\n");
1079 exit (1);
1080 }
1081
1082 test_11 ("#f", NULL, 0, 1);
1083 test_11 ("\"foo\"", "foo", 0, 0);
1084 test_11 ("(string #\\f #\\nul)", NULL, 1, 0);
1085}
1086
8ab3d8a0
KR
1087static void
1088tests (void *data, int argc, char **argv)
170bb182 1089{
170bb182
MV
1090 test_is_signed_integer ();
1091 test_is_unsigned_integer ();
1092 test_to_signed_integer ();
1093 test_to_unsigned_integer ();
1094 test_from_signed_integer ();
1095 test_from_unsigned_integer ();
afdb04ef 1096 test_int_sizes ();
eae5018e
MV
1097 test_from_double ();
1098 test_to_double ();
9bd10f46 1099 test_locale_strings ();
8ab3d8a0
KR
1100}
1101
1102int
1103main (int argc, char *argv[])
1104{
e2e85d14 1105 ieee_init ();
8ab3d8a0 1106 scm_boot_guile (argc, argv, tests, NULL);
170bb182
MV
1107 return 0;
1108}