1 /* Copyright (C) 1999,2000,2001,2003,2004, 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
3 * This library is free software; you can redistribute it and/or
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.
8 * This library is distributed in the hope that it will be useful, but
9 * 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.
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., 51 Franklin Street, Fifth Floor, Boston, MA
29 #ifdef HAVE_INTTYPES_H
30 # include <inttypes.h>
34 # if (defined SIZEOF_LONG_LONG) && (SIZEOF_LONG_LONG >= 8)
35 # define PRIiMAX "lli"
36 # define PRIuMAX "llu"
45 test_1 (const char *str
, scm_t_intmax min
, scm_t_intmax max
,
48 int r
= scm_is_signed_integer (scm_c_eval_string (str
), min
, max
);
51 fprintf (stderr
, "fail: scm_is_signed_integer (%s, "
52 "%" PRIiMAX
", %" PRIiMAX
") == %d\n",
53 str
, min
, max
, result
);
59 test_is_signed_integer ()
62 SCM_T_INTMAX_MIN
, SCM_T_INTMAX_MAX
,
65 SCM_T_INTMAX_MIN
, SCM_T_INTMAX_MAX
,
67 test_1 ("(inexact->exact 3.0)",
68 SCM_T_INTMAX_MIN
, SCM_T_INTMAX_MAX
,
71 SCM_T_INTMAX_MIN
, SCM_T_INTMAX_MAX
,
73 test_1 ("most-positive-fixnum",
74 SCM_T_INTMAX_MIN
, SCM_T_INTMAX_MAX
,
76 test_1 ("(+ most-positive-fixnum 1)",
77 SCM_T_INTMAX_MIN
, SCM_T_INTMAX_MAX
,
79 test_1 ("most-negative-fixnum",
80 SCM_T_INTMAX_MIN
, SCM_T_INTMAX_MAX
,
82 test_1 ("(- most-negative-fixnum 1)",
83 SCM_T_INTMAX_MIN
, SCM_T_INTMAX_MAX
,
85 if (sizeof (scm_t_intmax
) == 8)
87 test_1 ("(- (expt 2 63) 1)",
88 SCM_T_INTMAX_MIN
, SCM_T_INTMAX_MAX
,
90 test_1 ("(expt 2 63)",
91 SCM_T_INTMAX_MIN
, SCM_T_INTMAX_MAX
,
93 test_1 ("(- (expt 2 63))",
94 SCM_T_INTMAX_MIN
, SCM_T_INTMAX_MAX
,
96 test_1 ("(- (- (expt 2 63)) 1)",
97 SCM_T_INTMAX_MIN
, SCM_T_INTMAX_MAX
,
100 else if (sizeof (scm_t_intmax
) == 4)
102 test_1 ("(- (expt 2 31) 1)",
103 SCM_T_INTMAX_MIN
, SCM_T_INTMAX_MAX
,
105 test_1 ("(expt 2 31)",
106 SCM_T_INTMAX_MIN
, SCM_T_INTMAX_MAX
,
108 test_1 ("(- (expt 2 31))",
109 SCM_T_INTMAX_MIN
, SCM_T_INTMAX_MAX
,
111 test_1 ("(- (- (expt 2 31)) 1)",
112 SCM_T_INTMAX_MIN
, SCM_T_INTMAX_MAX
,
116 fprintf (stderr
, "NOTE: skipped some tests.\n");
118 /* bignum with range that fits into fixnum. */
119 test_1 ("(+ most-positive-fixnum 1)",
123 /* bignum with range that doesn't fit into fixnum, but probably
125 test_1 ("(+ most-positive-fixnum 1)",
126 SCM_MOST_NEGATIVE_FIXNUM
-1, SCM_MOST_POSITIVE_FIXNUM
+1,
131 test_2 (const char *str
, scm_t_uintmax min
, scm_t_uintmax max
,
134 int r
= scm_is_unsigned_integer (scm_c_eval_string (str
), min
, max
);
137 fprintf (stderr
, "fail: scm_is_unsigned_integer (%s, "
138 "%" PRIuMAX
", %" PRIuMAX
") == %d\n",
139 str
, min
, max
, result
);
145 test_is_unsigned_integer ()
148 0, SCM_T_UINTMAX_MAX
,
151 0, SCM_T_UINTMAX_MAX
,
153 test_2 ("(inexact->exact 3.0)",
154 0, SCM_T_UINTMAX_MAX
,
157 0, SCM_T_UINTMAX_MAX
,
159 test_2 ("most-positive-fixnum",
160 0, SCM_T_UINTMAX_MAX
,
162 test_2 ("(+ most-positive-fixnum 1)",
163 0, SCM_T_UINTMAX_MAX
,
165 test_2 ("most-negative-fixnum",
166 0, SCM_T_UINTMAX_MAX
,
168 test_2 ("(- most-negative-fixnum 1)",
169 0, SCM_T_UINTMAX_MAX
,
171 if (sizeof (scm_t_intmax
) == 8)
173 test_2 ("(- (expt 2 64) 1)",
174 0, SCM_T_UINTMAX_MAX
,
176 test_2 ("(expt 2 64)",
177 0, SCM_T_UINTMAX_MAX
,
180 else if (sizeof (scm_t_intmax
) == 4)
182 test_2 ("(- (expt 2 32) 1)",
183 0, SCM_T_UINTMAX_MAX
,
185 test_2 ("(expt 2 32)",
186 0, SCM_T_UINTMAX_MAX
,
190 fprintf (stderr
, "NOTE: skipped some tests.\n");
192 /* bignum with range that fits into fixnum. */
193 test_2 ("(+ most-positive-fixnum 1)",
197 /* bignum with range that doesn't fit into fixnum, but probably
199 test_2 ("(+ most-positive-fixnum 1)",
200 0, SCM_MOST_POSITIVE_FIXNUM
+1,
206 scm_t_intmax min
, max
;
211 out_of_range_handler (void *data
, SCM key
, SCM args
)
213 return scm_equal_p (key
, scm_from_locale_symbol ("out-of-range"));
217 wrong_type_handler (void *data
, SCM key
, SCM args
)
219 return scm_equal_p (key
, scm_from_locale_symbol ("wrong-type-arg"));
223 misc_error_handler (void *data
, SCM key
, SCM args
)
225 return scm_equal_p (key
, scm_from_locale_symbol ("misc-error"));
229 any_handler (void *data
, SCM key
, SCM args
)
235 to_signed_integer_body (void *data
)
237 to_signed_data
*d
= (to_signed_data
*)data
;
238 d
->result
= scm_to_signed_integer (d
->val
, d
->min
, d
->max
);
243 test_3 (const char *str
, scm_t_intmax min
, scm_t_intmax max
,
244 scm_t_intmax result
, int range_error
, int type_error
)
247 data
.val
= scm_c_eval_string (str
);
253 if (scm_is_false (scm_internal_catch (SCM_BOOL_T
,
254 to_signed_integer_body
, &data
,
255 out_of_range_handler
, NULL
)))
258 "fail: scm_to_signed_int (%s, "
259 "%" PRIiMAX
", %" PRIiMAX
") -> out of range\n",
266 if (scm_is_false (scm_internal_catch (SCM_BOOL_T
,
267 to_signed_integer_body
, &data
,
268 wrong_type_handler
, NULL
)))
271 "fail: scm_to_signed_int (%s, "
272 "%" PRIiMAX
", %" PRIiMAX
") -> wrong type\n",
279 if (scm_is_true (scm_internal_catch (SCM_BOOL_T
,
280 to_signed_integer_body
, &data
,
282 || data
.result
!= result
)
285 "fail: scm_to_signed_int (%s, "
286 "%" PRIiMAX
", %" PRIiMAX
") = %" PRIiMAX
"\n",
287 str
, min
, max
, result
);
294 test_to_signed_integer ()
297 SCM_T_INTMAX_MIN
, SCM_T_INTMAX_MAX
,
300 SCM_T_INTMAX_MIN
, SCM_T_INTMAX_MAX
,
303 SCM_T_INTMAX_MIN
, SCM_T_INTMAX_MAX
,
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)
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
,
337 test_3 ("(- (- (expt 2 63)) 1)",
338 SCM_T_INTMAX_MIN
, SCM_T_INTMAX_MAX
,
341 else if (sizeof (scm_t_intmax
) == 4)
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
,
355 test_3 ("(- (- (expt 2 31)) 1)",
356 SCM_T_INTMAX_MIN
, SCM_T_INTMAX_MAX
,
360 fprintf (stderr
, "NOTE: skipped some tests.\n");
365 scm_t_uintmax min
, max
;
366 scm_t_uintmax result
;
370 to_unsigned_integer_body (void *data
)
372 to_unsigned_data
*d
= (to_unsigned_data
*)data
;
373 d
->result
= scm_to_unsigned_integer (d
->val
, d
->min
, d
->max
);
378 test_4 (const char *str
, scm_t_uintmax min
, scm_t_uintmax max
,
379 scm_t_uintmax result
, int range_error
, int type_error
)
381 to_unsigned_data data
;
382 data
.val
= scm_c_eval_string (str
);
388 if (scm_is_false (scm_internal_catch (SCM_BOOL_T
,
389 to_unsigned_integer_body
, &data
,
390 out_of_range_handler
, NULL
)))
393 "fail: scm_to_unsigned_int (%s, "
394 "%" PRIuMAX
", %" PRIuMAX
") -> out of range\n",
401 if (scm_is_false (scm_internal_catch (SCM_BOOL_T
,
402 to_unsigned_integer_body
, &data
,
403 wrong_type_handler
, NULL
)))
406 "fail: scm_to_unsigned_int (%s, "
407 "%" PRIuMAX
", %" PRIuMAX
") -> wrong type\n",
414 if (scm_is_true (scm_internal_catch (SCM_BOOL_T
,
415 to_unsigned_integer_body
, &data
,
417 || data
.result
!= result
)
420 "fail: scm_to_unsigned_int (%s, "
421 "%" PRIuMAX
", %" PRIuMAX
") == %" PRIuMAX
"\n",
422 str
, min
, max
, result
);
429 test_to_unsigned_integer ()
432 0, SCM_T_UINTMAX_MAX
,
435 0, SCM_T_UINTMAX_MAX
,
438 0, SCM_T_UINTMAX_MAX
,
443 test_4 ("most-positive-fixnum",
444 0, SCM_T_UINTMAX_MAX
,
445 SCM_MOST_POSITIVE_FIXNUM
, 0, 0);
446 test_4 ("(+ most-positive-fixnum 1)",
447 0, SCM_T_UINTMAX_MAX
,
448 SCM_MOST_POSITIVE_FIXNUM
+1, 0, 0);
449 if (sizeof (scm_t_intmax
) == 8)
451 test_4 ("(- (expt 2 64) 1)",
452 0, SCM_T_UINTMAX_MAX
,
453 SCM_T_UINTMAX_MAX
, 0, 0);
454 test_4 ("(expt 2 64)",
455 0, SCM_T_UINTMAX_MAX
,
458 else if (sizeof (scm_t_intmax
) == 4)
460 test_4 ("(- (expt 2 32) 1)",
461 0, SCM_T_UINTMAX_MAX
,
462 SCM_T_UINTMAX_MAX
, 0, 0);
463 test_4 ("(expt 2 32)",
464 0, SCM_T_UINTMAX_MAX
,
468 fprintf (stderr
, "NOTE: skipped some tests.\n");
472 test_5 (scm_t_intmax val
, const char *result
)
474 SCM res
= scm_c_eval_string (result
);
475 if (scm_is_false (scm_equal_p (scm_from_signed_integer (val
), res
)))
477 fprintf (stderr
, "fail: scm_from_signed_integer (%" PRIiMAX
") == %s\n",
484 test_from_signed_integer ()
487 if (sizeof (scm_t_intmax
) == 8)
489 test_5 (SCM_T_INTMAX_MAX
, "(- (expt 2 63) 1)");
490 test_5 (SCM_T_INTMAX_MIN
, "(- (expt 2 63))");
492 else if (sizeof (scm_t_intmax
) == 4)
494 test_5 (SCM_T_INTMAX_MAX
, "(- (expt 2 31) 1)");
495 test_5 (SCM_T_INTMAX_MIN
, "(- (expt 2 31))");
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)");
504 test_6 (scm_t_uintmax val
, const char *result
)
506 SCM res
= scm_c_eval_string (result
);
507 if (scm_is_false (scm_equal_p (scm_from_unsigned_integer (val
), res
)))
509 fprintf (stderr
, "fail: scm_from_unsigned_integer (%"
517 test_from_unsigned_integer ()
520 if (sizeof (scm_t_intmax
) == 8)
522 test_6 (SCM_T_UINTMAX_MAX
, "(- (expt 2 64) 1)");
524 else if (sizeof (scm_t_intmax
) == 4)
526 test_6 (SCM_T_UINTMAX_MAX
, "(- (expt 2 32) 1)");
528 test_6 (SCM_MOST_POSITIVE_FIXNUM
, "most-positive-fixnum");
529 test_6 (SCM_MOST_POSITIVE_FIXNUM
+1, "(+ most-positive-fixnum 1)");
533 test_7s (SCM n
, scm_t_intmax c_n
, const char *result
, const char *func
)
535 SCM r
= scm_c_eval_string (result
);
537 if (scm_is_false (scm_equal_p (n
, r
)))
539 fprintf (stderr
, "fail: %s (%" PRIiMAX
") == %s\n", func
, c_n
, result
);
544 #define TEST_7S(func,arg,res) test_7s (func(arg), arg, res, #func)
547 test_7u (SCM n
, scm_t_uintmax c_n
, const char *result
, const char *func
)
549 SCM r
= scm_c_eval_string (result
);
551 if (scm_is_false (scm_equal_p (n
, r
)))
553 fprintf (stderr
, "fail: %s (%" PRIuMAX
") == %s\n", func
, c_n
, result
);
558 #define TEST_7U(func,arg,res) test_7u (func(arg), arg, res, #func)
562 scm_t_intmax (*func
) (SCM
);
564 } to_signed_func_data
;
567 to_signed_func_body (void *data
)
569 to_signed_func_data
*d
= (to_signed_func_data
*)data
;
570 d
->result
= d
->func (d
->val
);
575 test_8s (const char *str
, scm_t_intmax (*func
) (SCM
), const char *func_name
,
576 scm_t_intmax result
, int range_error
, int type_error
)
578 to_signed_func_data data
;
579 data
.val
= scm_c_eval_string (str
);
584 if (scm_is_false (scm_internal_catch (SCM_BOOL_T
,
585 to_signed_func_body
, &data
,
586 out_of_range_handler
, NULL
)))
589 "fail: %s (%s) -> out of range\n", func_name
, str
);
595 if (scm_is_false (scm_internal_catch (SCM_BOOL_T
,
596 to_signed_func_body
, &data
,
597 wrong_type_handler
, NULL
)))
600 "fail: %s (%s) -> wrong type\n", func_name
, str
);
606 if (scm_is_true (scm_internal_catch (SCM_BOOL_T
,
607 to_signed_func_body
, &data
,
609 || data
.result
!= result
)
612 "fail: %s (%s) = %" PRIiMAX
"\n", func_name
, str
, result
);
620 scm_t_uintmax (*func
) (SCM
);
621 scm_t_uintmax result
;
622 } to_unsigned_func_data
;
625 to_unsigned_func_body (void *data
)
627 to_unsigned_func_data
*d
= (to_unsigned_func_data
*)data
;
628 d
->result
= d
->func (d
->val
);
633 test_8u (const char *str
, scm_t_uintmax (*func
) (SCM
), const char *func_name
,
634 scm_t_uintmax result
, int range_error
, int type_error
)
636 to_unsigned_func_data data
;
637 data
.val
= scm_c_eval_string (str
);
642 if (scm_is_false (scm_internal_catch (SCM_BOOL_T
,
643 to_unsigned_func_body
, &data
,
644 out_of_range_handler
, NULL
)))
647 "fail: %s (%s) -> out of range\n", func_name
, str
);
653 if (scm_is_false (scm_internal_catch (SCM_BOOL_T
,
654 to_unsigned_func_body
, &data
,
655 wrong_type_handler
, NULL
)))
658 "fail: %s (%s) -> wrong type\n", func_name
, str
);
664 if (scm_is_true (scm_internal_catch (SCM_BOOL_T
,
665 to_unsigned_func_body
, &data
,
667 || data
.result
!= result
)
670 "fail: %s (%s) = %" PRIiMAX
"\n", func_name
, str
, result
);
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.
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); }
684 DEFSTST (scm_to_schar
)
685 DEFUTST (scm_to_uchar
)
686 DEFSTST (scm_to_char
)
687 DEFSTST (scm_to_short
)
688 DEFUTST (scm_to_ushort
)
690 DEFUTST (scm_to_uint
)
691 DEFSTST (scm_to_long
)
692 DEFUTST (scm_to_ulong
)
693 #if SCM_SIZEOF_LONG_LONG != 0
694 DEFSTST (scm_to_long_long
)
695 DEFUTST (scm_to_ulong_long
)
697 DEFSTST (scm_to_ssize_t
)
698 DEFUTST (scm_to_size_t
)
700 DEFSTST (scm_to_int8
)
701 DEFUTST (scm_to_uint8
)
702 DEFSTST (scm_to_int16
)
703 DEFUTST (scm_to_uint16
)
704 DEFSTST (scm_to_int32
)
705 DEFUTST (scm_to_uint32
)
706 #ifdef SCM_HAVE_T_INT64
707 DEFSTST (scm_to_int64
)
708 DEFUTST (scm_to_uint64
)
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)
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");
727 #if SCM_SIZEOF_LONG_LONG != 0
728 TEST_7S (scm_from_long_long
, 911, "911");
729 TEST_7U (scm_from_ulong_long
, 911, "911");
731 TEST_7U (scm_from_size_t
, 911, "911");
732 TEST_7S (scm_from_ssize_t
, 911, "911");
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");
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");
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");
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");
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);
764 #if SCM_SIZEOF_LONG_LONG != 0
765 TEST_8S ("-911", scm_to_long_long
, -911, 0, 0);
766 TEST_8U ("911", scm_to_ulong_long
, 911, 0, 0);
768 TEST_8U ("911", scm_to_size_t
, 911, 0, 0);
769 TEST_8S ("911", scm_to_ssize_t
, 911, 0, 0);
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);
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);
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);
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);
812 test_9 (double val
, const char *result
)
814 SCM res
= scm_c_eval_string (result
);
815 if (scm_is_false (scm_eqv_p (res
, scm_from_double (val
))))
817 fprintf (stderr
, "fail: scm_from_double (%g) == %s\n", val
, result
);
822 /* The `infinity' and `not-a-number' values. */
823 static double guile_Inf
, guile_NaN
;
825 /* Initialize GUILE_INF and GUILE_NAN. Taken from `guile_ieee_init ()' in
826 `libguile/numbers.c'. */
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
;
839 extern unsigned int DINFINITY
[2];
840 guile_Inf
= (*((double *) (DINFINITY
)));
847 if (guile_Inf
== tmp
)
854 /* C99 NAN, when available */
859 extern unsigned int DQNAN
[2];
860 guile_NaN
= (*((double *)(DQNAN
)));
863 guile_NaN
= guile_Inf
/ guile_Inf
;
871 test_9 (0.25, "0.25");
873 test_9 (guile_Inf
, "+inf.0");
874 test_9 (-guile_Inf
, "-inf.0");
875 test_9 (guile_NaN
, "+nan.0");
884 to_double_body (void *data
)
886 to_double_data
*d
= (to_double_data
*)data
;
887 d
->result
= scm_to_double (d
->val
);
892 test_10 (const char *val
, double result
, int type_error
)
895 data
.val
= scm_c_eval_string (val
);
899 if (scm_is_false (scm_internal_catch (SCM_BOOL_T
,
900 to_double_body
, &data
,
901 wrong_type_handler
, NULL
)))
904 "fail: scm_double (%s) -> wrong type\n", val
);
910 if (scm_is_true (scm_internal_catch (SCM_BOOL_T
,
911 to_double_body
, &data
,
913 || data
.result
!= result
)
916 "fail: scm_to_double (%s) = %g\n", val
, result
);
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);
929 test_10 ("+inf.0", guile_Inf
, 0);
930 test_10 ("-inf.0",-guile_Inf
, 0);
931 test_10 ("+1i", 0.0, 1);
937 } to_locale_string_data
;
940 to_locale_string_body (void *data
)
942 to_locale_string_data
*d
= (to_locale_string_data
*)data
;
943 d
->result
= scm_to_locale_string (d
->val
);
948 test_11 (const char *str
, const char *result
, int misc_error
, int type_error
)
950 to_locale_string_data data
;
951 data
.val
= scm_c_eval_string (str
);
956 if (scm_is_false (scm_internal_catch (SCM_BOOL_T
,
957 to_locale_string_body
, &data
,
958 misc_error_handler
, NULL
)))
961 "fail: scm_to_locale_string (%s) -> misc error\n", str
);
967 if (scm_is_false (scm_internal_catch (SCM_BOOL_T
,
968 to_locale_string_body
, &data
,
969 wrong_type_handler
, NULL
)))
972 "fail: scm_to_locale_string (%s) -> wrong type\n", str
);
978 if (scm_is_true (scm_internal_catch (SCM_BOOL_T
,
979 to_locale_string_body
, &data
,
981 || data
.result
== NULL
|| strcmp (data
.result
, result
))
984 "fail: scm_to_locale_string (%s) = %s\n", str
, result
);
993 test_locale_strings ()
995 const char *lstr
= "This is not a string.";
1001 if (!scm_is_string (scm_c_eval_string ("\"foo\"")))
1003 fprintf (stderr
, "fail: scm_is_string (\"foo\") = true\n");
1007 str
= scm_from_locale_string (lstr
);
1009 if (!scm_is_string (str
))
1011 fprintf (stderr
, "fail: scm_is_string (str) = true\n");
1015 lstr2
= scm_to_locale_string (str
);
1016 if (strcmp (lstr
, lstr2
))
1018 fprintf (stderr
, "fail: lstr = lstr2\n");
1024 len
= scm_to_locale_stringbuf (str
, buf
, 15);
1025 if (len
!= strlen (lstr
))
1027 fprintf (stderr
, "fail: scm_to_locale_stringbuf (...) = strlen(lstr)\n");
1032 fprintf (stderr
, "fail: scm_to_locale_stringbuf (...) no overrun\n");
1035 if (strncmp (lstr
, buf
, 15))
1037 fprintf (stderr
, "fail: scm_to_locale_stringbuf (...) = lstr\n");
1041 str2
= scm_from_locale_stringn (lstr
, 10);
1043 if (!scm_is_string (str2
))
1045 fprintf (stderr
, "fail: scm_is_string (str2) = true\n");
1049 lstr2
= scm_to_locale_string (str2
);
1050 if (strncmp (lstr
, lstr2
, 10))
1052 fprintf (stderr
, "fail: lstr = lstr2\n");
1058 len
= scm_to_locale_stringbuf (str2
, buf
, 20);
1061 fprintf (stderr
, "fail: scm_to_locale_stringbuf (...) = 10\n");
1066 fprintf (stderr
, "fail: scm_to_locale_stringbuf (...) no overrun\n");
1069 if (strncmp (lstr
, buf
, 10))
1071 fprintf (stderr
, "fail: scm_to_locale_stringbuf (...) = lstr\n");
1075 lstr2
= scm_to_locale_stringn (str2
, &len
);
1078 fprintf (stderr
, "fail: scm_to_locale_stringn, len = 10\n");
1082 test_11 ("#f", NULL
, 0, 1);
1083 test_11 ("\"foo\"", "foo", 0, 0);
1084 test_11 ("(string #\\f #\\nul)", NULL
, 1, 0);
1088 tests (void *data
, int argc
, char **argv
)
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 ();
1097 test_from_double ();
1099 test_locale_strings ();
1103 main (int argc
, char *argv
[])
1106 scm_boot_guile (argc
, argv
, tests
, NULL
);