1 /* Copyright (C) 1999,2000,2001,2003,2004, 2006, 2007 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
5 * License as published by the Free Software Foundation; either
6 * version 2.1 of the License, or (at your option) any later version.
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.
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 02110-1301 USA
26 #ifdef HAVE_INTTYPES_H
27 # include <inttypes.h>
28 #elif (!defined PRIiMAX)
29 # if (defined SIZEOF_LONG_LONG) && (SIZEOF_LONG_LONG >= 8)
30 # define PRIiMAX "lli"
31 # define PRIuMAX "llu"
40 test_1 (const char *str
, scm_t_intmax min
, scm_t_intmax max
,
43 int r
= scm_is_signed_integer (scm_c_eval_string (str
), min
, max
);
46 fprintf (stderr
, "fail: scm_is_signed_integer (%s, "
47 "%" PRIiMAX
", %" PRIiMAX
") == %d\n",
48 str
, min
, max
, result
);
54 test_is_signed_integer ()
57 SCM_T_INTMAX_MIN
, SCM_T_INTMAX_MAX
,
60 SCM_T_INTMAX_MIN
, SCM_T_INTMAX_MAX
,
62 test_1 ("(inexact->exact 3.0)",
63 SCM_T_INTMAX_MIN
, SCM_T_INTMAX_MAX
,
66 SCM_T_INTMAX_MIN
, SCM_T_INTMAX_MAX
,
68 test_1 ("most-positive-fixnum",
69 SCM_T_INTMAX_MIN
, SCM_T_INTMAX_MAX
,
71 test_1 ("(+ most-positive-fixnum 1)",
72 SCM_T_INTMAX_MIN
, SCM_T_INTMAX_MAX
,
74 test_1 ("most-negative-fixnum",
75 SCM_T_INTMAX_MIN
, SCM_T_INTMAX_MAX
,
77 test_1 ("(- most-negative-fixnum 1)",
78 SCM_T_INTMAX_MIN
, SCM_T_INTMAX_MAX
,
80 if (sizeof (scm_t_intmax
) == 8)
82 test_1 ("(- (expt 2 63) 1)",
83 SCM_T_INTMAX_MIN
, SCM_T_INTMAX_MAX
,
85 test_1 ("(expt 2 63)",
86 SCM_T_INTMAX_MIN
, SCM_T_INTMAX_MAX
,
88 test_1 ("(- (expt 2 63))",
89 SCM_T_INTMAX_MIN
, SCM_T_INTMAX_MAX
,
91 test_1 ("(- (- (expt 2 63)) 1)",
92 SCM_T_INTMAX_MIN
, SCM_T_INTMAX_MAX
,
95 else if (sizeof (scm_t_intmax
) == 4)
97 test_1 ("(- (expt 2 31) 1)",
98 SCM_T_INTMAX_MIN
, SCM_T_INTMAX_MAX
,
100 test_1 ("(expt 2 31)",
101 SCM_T_INTMAX_MIN
, SCM_T_INTMAX_MAX
,
103 test_1 ("(- (expt 2 31))",
104 SCM_T_INTMAX_MIN
, SCM_T_INTMAX_MAX
,
106 test_1 ("(- (- (expt 2 31)) 1)",
107 SCM_T_INTMAX_MIN
, SCM_T_INTMAX_MAX
,
111 fprintf (stderr
, "NOTE: skipped some tests.\n");
113 /* bignum with range that fits into fixnum. */
114 test_1 ("(+ most-positive-fixnum 1)",
118 /* bignum with range that doesn't fit into fixnum, but probably
120 test_1 ("(+ most-positive-fixnum 1)",
121 SCM_MOST_NEGATIVE_FIXNUM
-1, SCM_MOST_POSITIVE_FIXNUM
+1,
126 test_2 (const char *str
, scm_t_uintmax min
, scm_t_uintmax max
,
129 int r
= scm_is_unsigned_integer (scm_c_eval_string (str
), min
, max
);
132 fprintf (stderr
, "fail: scm_is_unsigned_integer (%s, "
133 "%" PRIuMAX
", %" PRIuMAX
") == %d\n",
134 str
, min
, max
, result
);
140 test_is_unsigned_integer ()
143 0, SCM_T_UINTMAX_MAX
,
146 0, SCM_T_UINTMAX_MAX
,
148 test_2 ("(inexact->exact 3.0)",
149 0, SCM_T_UINTMAX_MAX
,
152 0, SCM_T_UINTMAX_MAX
,
154 test_2 ("most-positive-fixnum",
155 0, SCM_T_UINTMAX_MAX
,
157 test_2 ("(+ most-positive-fixnum 1)",
158 0, SCM_T_UINTMAX_MAX
,
160 test_2 ("most-negative-fixnum",
161 0, SCM_T_UINTMAX_MAX
,
163 test_2 ("(- most-negative-fixnum 1)",
164 0, SCM_T_UINTMAX_MAX
,
166 if (sizeof (scm_t_intmax
) == 8)
168 test_2 ("(- (expt 2 64) 1)",
169 0, SCM_T_UINTMAX_MAX
,
171 test_2 ("(expt 2 64)",
172 0, SCM_T_UINTMAX_MAX
,
175 else if (sizeof (scm_t_intmax
) == 4)
177 test_2 ("(- (expt 2 32) 1)",
178 0, SCM_T_UINTMAX_MAX
,
180 test_2 ("(expt 2 32)",
181 0, SCM_T_UINTMAX_MAX
,
185 fprintf (stderr
, "NOTE: skipped some tests.\n");
187 /* bignum with range that fits into fixnum. */
188 test_2 ("(+ most-positive-fixnum 1)",
192 /* bignum with range that doesn't fit into fixnum, but probably
194 test_2 ("(+ most-positive-fixnum 1)",
195 0, SCM_MOST_POSITIVE_FIXNUM
+1,
201 scm_t_intmax min
, max
;
206 out_of_range_handler (void *data
, SCM key
, SCM args
)
208 return scm_equal_p (key
, scm_from_locale_symbol ("out-of-range"));
212 wrong_type_handler (void *data
, SCM key
, SCM args
)
214 return scm_equal_p (key
, scm_from_locale_symbol ("wrong-type-arg"));
218 misc_error_handler (void *data
, SCM key
, SCM args
)
220 return scm_equal_p (key
, scm_from_locale_symbol ("misc-error"));
224 any_handler (void *data
, SCM key
, SCM args
)
230 to_signed_integer_body (void *data
)
232 to_signed_data
*d
= (to_signed_data
*)data
;
233 d
->result
= scm_to_signed_integer (d
->val
, d
->min
, d
->max
);
238 test_3 (const char *str
, scm_t_intmax min
, scm_t_intmax max
,
239 scm_t_intmax result
, int range_error
, int type_error
)
242 data
.val
= scm_c_eval_string (str
);
248 if (scm_is_false (scm_internal_catch (SCM_BOOL_T
,
249 to_signed_integer_body
, &data
,
250 out_of_range_handler
, NULL
)))
253 "fail: scm_to_signed_int (%s, "
254 "%" PRIiMAX
", %" PRIiMAX
") -> out of range\n",
261 if (scm_is_false (scm_internal_catch (SCM_BOOL_T
,
262 to_signed_integer_body
, &data
,
263 wrong_type_handler
, NULL
)))
266 "fail: scm_to_signed_int (%s, "
267 "%" PRIiMAX
", %" PRIiMAX
") -> wrong type\n",
274 if (scm_is_true (scm_internal_catch (SCM_BOOL_T
,
275 to_signed_integer_body
, &data
,
277 || data
.result
!= result
)
280 "fail: scm_to_signed_int (%s, "
281 "%" PRIiMAX
", %" PRIiMAX
") = %" PRIiMAX
"\n",
282 str
, min
, max
, result
);
289 test_to_signed_integer ()
292 SCM_T_INTMAX_MIN
, SCM_T_INTMAX_MAX
,
295 SCM_T_INTMAX_MIN
, SCM_T_INTMAX_MAX
,
298 SCM_T_INTMAX_MIN
, SCM_T_INTMAX_MAX
,
306 test_3 ("most-positive-fixnum",
307 SCM_T_INTMAX_MIN
, SCM_T_INTMAX_MAX
,
308 SCM_MOST_POSITIVE_FIXNUM
, 0, 0);
309 test_3 ("most-negative-fixnum",
310 SCM_T_INTMAX_MIN
, SCM_T_INTMAX_MAX
,
311 SCM_MOST_NEGATIVE_FIXNUM
, 0, 0);
312 test_3 ("(+ most-positive-fixnum 1)",
313 SCM_T_INTMAX_MIN
, SCM_T_INTMAX_MAX
,
314 SCM_MOST_POSITIVE_FIXNUM
+1, 0, 0);
315 test_3 ("(- most-negative-fixnum 1)",
316 SCM_T_INTMAX_MIN
, SCM_T_INTMAX_MAX
,
317 SCM_MOST_NEGATIVE_FIXNUM
-1, 0, 0);
318 if (sizeof (scm_t_intmax
) == 8)
320 test_3 ("(- (expt 2 63) 1)",
321 SCM_T_INTMAX_MIN
, SCM_T_INTMAX_MAX
,
322 SCM_T_INTMAX_MAX
, 0, 0);
323 test_3 ("(+ (- (expt 2 63)) 1)",
324 SCM_T_INTMAX_MIN
, SCM_T_INTMAX_MAX
,
325 SCM_T_INTMAX_MIN
+1, 0, 0);
326 test_3 ("(- (expt 2 63))",
327 SCM_T_INTMAX_MIN
, SCM_T_INTMAX_MAX
,
328 SCM_T_INTMAX_MIN
, 0, 0);
329 test_3 ("(expt 2 63)",
330 SCM_T_INTMAX_MIN
, SCM_T_INTMAX_MAX
,
332 test_3 ("(- (- (expt 2 63)) 1)",
333 SCM_T_INTMAX_MIN
, SCM_T_INTMAX_MAX
,
336 else if (sizeof (scm_t_intmax
) == 4)
338 test_3 ("(- (expt 2 31) 1)",
339 SCM_T_INTMAX_MIN
, SCM_T_INTMAX_MAX
,
340 SCM_T_INTMAX_MAX
, 0, 0);
341 test_3 ("(+ (- (expt 2 31)) 1)",
342 SCM_T_INTMAX_MIN
, SCM_T_INTMAX_MAX
,
343 SCM_T_INTMAX_MIN
+1, 0, 0);
344 test_3 ("(- (expt 2 31))",
345 SCM_T_INTMAX_MIN
, SCM_T_INTMAX_MAX
,
346 SCM_T_INTMAX_MIN
, 0, 0);
347 test_3 ("(expt 2 31)",
348 SCM_T_INTMAX_MIN
, SCM_T_INTMAX_MAX
,
350 test_3 ("(- (- (expt 2 31)) 1)",
351 SCM_T_INTMAX_MIN
, SCM_T_INTMAX_MAX
,
355 fprintf (stderr
, "NOTE: skipped some tests.\n");
360 scm_t_uintmax min
, max
;
361 scm_t_uintmax result
;
365 to_unsigned_integer_body (void *data
)
367 to_unsigned_data
*d
= (to_unsigned_data
*)data
;
368 d
->result
= scm_to_unsigned_integer (d
->val
, d
->min
, d
->max
);
373 test_4 (const char *str
, scm_t_uintmax min
, scm_t_uintmax max
,
374 scm_t_uintmax result
, int range_error
, int type_error
)
376 to_unsigned_data data
;
377 data
.val
= scm_c_eval_string (str
);
383 if (scm_is_false (scm_internal_catch (SCM_BOOL_T
,
384 to_unsigned_integer_body
, &data
,
385 out_of_range_handler
, NULL
)))
388 "fail: scm_to_unsigned_int (%s, "
389 "%" PRIuMAX
", %" PRIuMAX
") -> out of range\n",
396 if (scm_is_false (scm_internal_catch (SCM_BOOL_T
,
397 to_unsigned_integer_body
, &data
,
398 wrong_type_handler
, NULL
)))
401 "fail: scm_to_unsigned_int (%s, "
402 "%" PRIuMAX
", %" PRIuMAX
") -> wrong type\n",
409 if (scm_is_true (scm_internal_catch (SCM_BOOL_T
,
410 to_unsigned_integer_body
, &data
,
412 || data
.result
!= result
)
415 "fail: scm_to_unsigned_int (%s, "
416 "%" PRIuMAX
", %" PRIuMAX
") == %" PRIuMAX
"\n",
417 str
, min
, max
, result
);
424 test_to_unsigned_integer ()
427 0, SCM_T_UINTMAX_MAX
,
430 0, SCM_T_UINTMAX_MAX
,
433 0, SCM_T_UINTMAX_MAX
,
438 test_4 ("most-positive-fixnum",
439 0, SCM_T_UINTMAX_MAX
,
440 SCM_MOST_POSITIVE_FIXNUM
, 0, 0);
441 test_4 ("(+ most-positive-fixnum 1)",
442 0, SCM_T_UINTMAX_MAX
,
443 SCM_MOST_POSITIVE_FIXNUM
+1, 0, 0);
444 if (sizeof (scm_t_intmax
) == 8)
446 test_4 ("(- (expt 2 64) 1)",
447 0, SCM_T_UINTMAX_MAX
,
448 SCM_T_UINTMAX_MAX
, 0, 0);
449 test_4 ("(expt 2 64)",
450 0, SCM_T_UINTMAX_MAX
,
453 else if (sizeof (scm_t_intmax
) == 4)
455 test_4 ("(- (expt 2 32) 1)",
456 0, SCM_T_UINTMAX_MAX
,
457 SCM_T_UINTMAX_MAX
, 0, 0);
458 test_4 ("(expt 2 32)",
459 0, SCM_T_UINTMAX_MAX
,
463 fprintf (stderr
, "NOTE: skipped some tests.\n");
467 test_5 (scm_t_intmax val
, const char *result
)
469 SCM res
= scm_c_eval_string (result
);
470 if (scm_is_false (scm_equal_p (scm_from_signed_integer (val
), res
)))
472 fprintf (stderr
, "fail: scm_from_signed_integer (%" PRIiMAX
") == %s\n",
479 test_from_signed_integer ()
482 if (sizeof (scm_t_intmax
) == 8)
484 test_5 (SCM_T_INTMAX_MAX
, "(- (expt 2 63) 1)");
485 test_5 (SCM_T_INTMAX_MIN
, "(- (expt 2 63))");
487 else if (sizeof (scm_t_intmax
) == 4)
489 test_5 (SCM_T_INTMAX_MAX
, "(- (expt 2 31) 1)");
490 test_5 (SCM_T_INTMAX_MIN
, "(- (expt 2 31))");
492 test_5 (SCM_MOST_POSITIVE_FIXNUM
, "most-positive-fixnum");
493 test_5 (SCM_MOST_NEGATIVE_FIXNUM
, "most-negative-fixnum");
494 test_5 (SCM_MOST_POSITIVE_FIXNUM
+1, "(+ most-positive-fixnum 1)");
495 test_5 (SCM_MOST_NEGATIVE_FIXNUM
-1, "(- most-negative-fixnum 1)");
499 test_6 (scm_t_uintmax val
, const char *result
)
501 SCM res
= scm_c_eval_string (result
);
502 if (scm_is_false (scm_equal_p (scm_from_unsigned_integer (val
), res
)))
504 fprintf (stderr
, "fail: scm_from_unsigned_integer (%"
512 test_from_unsigned_integer ()
515 if (sizeof (scm_t_intmax
) == 8)
517 test_6 (SCM_T_UINTMAX_MAX
, "(- (expt 2 64) 1)");
519 else if (sizeof (scm_t_intmax
) == 4)
521 test_6 (SCM_T_UINTMAX_MAX
, "(- (expt 2 32) 1)");
523 test_6 (SCM_MOST_POSITIVE_FIXNUM
, "most-positive-fixnum");
524 test_6 (SCM_MOST_POSITIVE_FIXNUM
+1, "(+ most-positive-fixnum 1)");
528 test_7s (SCM n
, scm_t_intmax c_n
, const char *result
, const char *func
)
530 SCM r
= scm_c_eval_string (result
);
532 if (scm_is_false (scm_equal_p (n
, r
)))
534 fprintf (stderr
, "fail: %s (%" PRIiMAX
") == %s\n", func
, c_n
, result
);
539 #define TEST_7S(func,arg,res) test_7s (func(arg), arg, res, #func)
542 test_7u (SCM n
, scm_t_uintmax c_n
, const char *result
, const char *func
)
544 SCM r
= scm_c_eval_string (result
);
546 if (scm_is_false (scm_equal_p (n
, r
)))
548 fprintf (stderr
, "fail: %s (%" PRIuMAX
") == %s\n", func
, c_n
, result
);
553 #define TEST_7U(func,arg,res) test_7u (func(arg), arg, res, #func)
557 scm_t_intmax (*func
) (SCM
);
559 } to_signed_func_data
;
562 to_signed_func_body (void *data
)
564 to_signed_func_data
*d
= (to_signed_func_data
*)data
;
565 d
->result
= d
->func (d
->val
);
570 test_8s (const char *str
, scm_t_intmax (*func
) (SCM
), const char *func_name
,
571 scm_t_intmax result
, int range_error
, int type_error
)
573 to_signed_func_data data
;
574 data
.val
= scm_c_eval_string (str
);
579 if (scm_is_false (scm_internal_catch (SCM_BOOL_T
,
580 to_signed_func_body
, &data
,
581 out_of_range_handler
, NULL
)))
584 "fail: %s (%s) -> out of range\n", func_name
, str
);
590 if (scm_is_false (scm_internal_catch (SCM_BOOL_T
,
591 to_signed_func_body
, &data
,
592 wrong_type_handler
, NULL
)))
595 "fail: %s (%s) -> wrong type\n", func_name
, str
);
601 if (scm_is_true (scm_internal_catch (SCM_BOOL_T
,
602 to_signed_func_body
, &data
,
604 || data
.result
!= result
)
607 "fail: %s (%s) = %" PRIiMAX
"\n", func_name
, str
, result
);
615 scm_t_uintmax (*func
) (SCM
);
616 scm_t_uintmax result
;
617 } to_unsigned_func_data
;
620 to_unsigned_func_body (void *data
)
622 to_unsigned_func_data
*d
= (to_unsigned_func_data
*)data
;
623 d
->result
= d
->func (d
->val
);
628 test_8u (const char *str
, scm_t_uintmax (*func
) (SCM
), const char *func_name
,
629 scm_t_uintmax result
, int range_error
, int type_error
)
631 to_unsigned_func_data data
;
632 data
.val
= scm_c_eval_string (str
);
637 if (scm_is_false (scm_internal_catch (SCM_BOOL_T
,
638 to_unsigned_func_body
, &data
,
639 out_of_range_handler
, NULL
)))
642 "fail: %s (%s) -> out of range\n", func_name
, str
);
648 if (scm_is_false (scm_internal_catch (SCM_BOOL_T
,
649 to_unsigned_func_body
, &data
,
650 wrong_type_handler
, NULL
)))
653 "fail: %s (%s) -> wrong type\n", func_name
, str
);
659 if (scm_is_true (scm_internal_catch (SCM_BOOL_T
,
660 to_unsigned_func_body
, &data
,
662 || data
.result
!= result
)
665 "fail: %s (%s) = %" PRIiMAX
"\n", func_name
, str
, result
);
671 /* We can't rely on the scm_to functions being proper functions but we
672 want to pass them to test_8s and test_8u, so we wrap'em. Also, we
673 need to give them a common return type.
676 #define DEFSTST(f) static scm_t_intmax tst_##f (SCM x) { return f(x); }
677 #define DEFUTST(f) static scm_t_uintmax tst_##f (SCM x) { return f(x); }
679 DEFSTST (scm_to_schar
);
680 DEFUTST (scm_to_uchar
);
681 DEFSTST (scm_to_char
);
682 DEFSTST (scm_to_short
);
683 DEFUTST (scm_to_ushort
);
684 DEFSTST (scm_to_int
);
685 DEFUTST (scm_to_uint
);
686 DEFSTST (scm_to_long
);
687 DEFUTST (scm_to_ulong
);
688 #if SCM_SIZEOF_LONG_LONG != 0
689 DEFSTST (scm_to_long_long
);
690 DEFUTST (scm_to_ulong_long
);
692 DEFSTST (scm_to_ssize_t
);
693 DEFUTST (scm_to_size_t
);
695 DEFSTST (scm_to_int8
);
696 DEFUTST (scm_to_uint8
);
697 DEFSTST (scm_to_int16
);
698 DEFUTST (scm_to_uint16
);
699 DEFSTST (scm_to_int32
);
700 DEFUTST (scm_to_uint32
);
701 #ifdef SCM_HAVE_T_INT64
702 DEFSTST (scm_to_int64
);
703 DEFUTST (scm_to_uint64
);
706 #define TEST_8S(v,f,r,re,te) test_8s (v, tst_##f, #f, r, re, te)
707 #define TEST_8U(v,f,r,re,te) test_8u (v, tst_##f, #f, r, re, te)
713 TEST_7U (scm_from_uchar
, 91, "91");
714 TEST_7S (scm_from_schar
, 91, "91");
715 TEST_7S (scm_from_char
, 91, "91");
716 TEST_7S (scm_from_short
, -911, "-911");
717 TEST_7U (scm_from_ushort
, 911, "911");
718 TEST_7S (scm_from_int
, 911, "911");
719 TEST_7U (scm_from_uint
, 911, "911");
720 TEST_7S (scm_from_long
, 911, "911");
721 TEST_7U (scm_from_ulong
, 911, "911");
722 #if SCM_SIZEOF_LONG_LONG != 0
723 TEST_7S (scm_from_long_long
, 911, "911");
724 TEST_7U (scm_from_ulong_long
, 911, "911");
726 TEST_7U (scm_from_size_t
, 911, "911");
727 TEST_7S (scm_from_ssize_t
, 911, "911");
729 TEST_7S (scm_from_int8
, -128, "-128");
730 TEST_7S (scm_from_int8
, 127, "127");
731 TEST_7S (scm_from_int8
, 128, "-128");
732 TEST_7U (scm_from_uint8
, 255, "255");
734 TEST_7S (scm_from_int16
, -32768, "-32768");
735 TEST_7S (scm_from_int16
, 32767, "32767");
736 TEST_7S (scm_from_int16
, 32768, "-32768");
737 TEST_7U (scm_from_uint16
, 65535, "65535");
739 TEST_7S (scm_from_int32
, SCM_T_INT32_MIN
, "-2147483648");
740 TEST_7S (scm_from_int32
, SCM_T_INT32_MAX
, "2147483647");
741 TEST_7S (scm_from_int32
, SCM_T_INT32_MAX
+1LL, "-2147483648");
742 TEST_7U (scm_from_uint32
, SCM_T_UINT32_MAX
, "4294967295");
745 TEST_7S (scm_from_int64
, SCM_T_INT64_MIN
, "-9223372036854775808");
746 TEST_7S (scm_from_int64
, SCM_T_INT64_MAX
, "9223372036854775807");
747 TEST_7U (scm_from_uint64
, SCM_T_UINT64_MAX
, "18446744073709551615");
750 TEST_8S ("91", scm_to_schar
, 91, 0, 0);
751 TEST_8U ("91", scm_to_uchar
, 91, 0, 0);
752 TEST_8S ("91", scm_to_char
, 91, 0, 0);
753 TEST_8S ("-911", scm_to_short
, -911, 0, 0);
754 TEST_8U ("911", scm_to_ushort
, 911, 0, 0);
755 TEST_8S ("-911", scm_to_int
, -911, 0, 0);
756 TEST_8U ("911", scm_to_uint
, 911, 0, 0);
757 TEST_8S ("-911", scm_to_long
, -911, 0, 0);
758 TEST_8U ("911", scm_to_ulong
, 911, 0, 0);
759 #if SCM_SIZEOF_LONG_LONG != 0
760 TEST_8S ("-911", scm_to_long_long
, -911, 0, 0);
761 TEST_8U ("911", scm_to_ulong_long
, 911, 0, 0);
763 TEST_8U ("911", scm_to_size_t
, 911, 0, 0);
764 TEST_8S ("911", scm_to_ssize_t
, 911, 0, 0);
766 TEST_8S ("-128", scm_to_int8
, SCM_T_INT8_MIN
, 0, 0);
767 TEST_8S ("127", scm_to_int8
, SCM_T_INT8_MAX
, 0, 0);
768 TEST_8S ("128", scm_to_int8
, 0, 1, 0);
769 TEST_8S ("#f", scm_to_int8
, 0, 0, 1);
770 TEST_8U ("255", scm_to_uint8
, SCM_T_UINT8_MAX
, 0, 0);
771 TEST_8U ("256", scm_to_uint8
, 0, 1, 0);
772 TEST_8U ("-1", scm_to_uint8
, 0, 1, 0);
773 TEST_8U ("#f", scm_to_uint8
, 0, 0, 1);
775 TEST_8S ("-32768", scm_to_int16
, SCM_T_INT16_MIN
, 0, 0);
776 TEST_8S ("32767", scm_to_int16
, SCM_T_INT16_MAX
, 0, 0);
777 TEST_8S ("32768", scm_to_int16
, 0, 1, 0);
778 TEST_8S ("#f", scm_to_int16
, 0, 0, 1);
779 TEST_8U ("65535", scm_to_uint16
, SCM_T_UINT16_MAX
, 0, 0);
780 TEST_8U ("65536", scm_to_uint16
, 0, 1, 0);
781 TEST_8U ("-1", scm_to_uint16
, 0, 1, 0);
782 TEST_8U ("#f", scm_to_uint16
, 0, 0, 1);
784 TEST_8S ("-2147483648", scm_to_int32
, SCM_T_INT32_MIN
, 0, 0);
785 TEST_8S ("2147483647", scm_to_int32
, SCM_T_INT32_MAX
, 0, 0);
786 TEST_8S ("2147483648", scm_to_int32
, 0, 1, 0);
787 TEST_8S ("#f", scm_to_int32
, 0, 0, 1);
788 TEST_8U ("4294967295", scm_to_uint32
, SCM_T_UINT32_MAX
, 0, 0);
789 TEST_8U ("4294967296", scm_to_uint32
, 0, 1, 0);
790 TEST_8U ("-1", scm_to_uint32
, 0, 1, 0);
791 TEST_8U ("#f", scm_to_uint32
, 0, 0, 1);
794 TEST_8S ("-9223372036854775808", scm_to_int64
, SCM_T_INT64_MIN
, 0, 0);
795 TEST_8S ("9223372036854775807", scm_to_int64
, SCM_T_INT64_MAX
, 0, 0);
796 TEST_8S ("9223372036854775808", scm_to_int64
, 0, 1, 0);
797 TEST_8S ("#f", scm_to_int64
, 0, 0, 1);
798 TEST_8U ("18446744073709551615", scm_to_uint64
, SCM_T_UINT64_MAX
, 0, 0);
799 TEST_8U ("18446744073709551616", scm_to_uint64
, 0, 1, 0);
800 TEST_8U ("-1", scm_to_uint64
, 0, 1, 0);
801 TEST_8U ("#f", scm_to_uint64
, 0, 0, 1);
807 test_9 (double val
, const char *result
)
809 SCM res
= scm_c_eval_string (result
);
810 if (scm_is_false (scm_eqv_p (res
, scm_from_double (val
))))
812 fprintf (stderr
, "fail: scm_from_double (%g) == %s\n", val
, result
);
821 test_9 (0.25, "0.25");
823 test_9 (1.0/0.0, "+inf.0");
824 test_9 (-1.0/0.0, "-inf.0");
825 test_9 (0.0/0.0, "+nan.0");
834 to_double_body (void *data
)
836 to_double_data
*d
= (to_double_data
*)data
;
837 d
->result
= scm_to_double (d
->val
);
842 test_10 (const char *val
, double result
, int type_error
)
845 data
.val
= scm_c_eval_string (val
);
849 if (scm_is_false (scm_internal_catch (SCM_BOOL_T
,
850 to_double_body
, &data
,
851 wrong_type_handler
, NULL
)))
854 "fail: scm_double (%s) -> wrong type\n", val
);
860 if (scm_is_true (scm_internal_catch (SCM_BOOL_T
,
861 to_double_body
, &data
,
863 || data
.result
!= result
)
866 "fail: scm_to_double (%s) = %g\n", val
, result
);
875 test_10 ("#f", 0.0, 1);
876 test_10 ("12", 12.0, 0);
877 test_10 ("0.25", 0.25, 0);
878 test_10 ("1/4", 0.25, 0);
879 test_10 ("+inf.0", 1.0/0.0, 0);
880 test_10 ("-inf.0", -1.0/0.0, 0);
881 test_10 ("+1i", 0.0, 1);
887 } to_locale_string_data
;
890 to_locale_string_body (void *data
)
892 to_locale_string_data
*d
= (to_locale_string_data
*)data
;
893 d
->result
= scm_to_locale_string (d
->val
);
898 test_11 (const char *str
, const char *result
, int misc_error
, int type_error
)
900 to_locale_string_data data
;
901 data
.val
= scm_c_eval_string (str
);
906 if (scm_is_false (scm_internal_catch (SCM_BOOL_T
,
907 to_locale_string_body
, &data
,
908 misc_error_handler
, NULL
)))
911 "fail: scm_to_locale_string (%s) -> misc error\n", str
);
917 if (scm_is_false (scm_internal_catch (SCM_BOOL_T
,
918 to_locale_string_body
, &data
,
919 wrong_type_handler
, NULL
)))
922 "fail: scm_to_locale_string (%s) -> wrong type\n", str
);
928 if (scm_is_true (scm_internal_catch (SCM_BOOL_T
,
929 to_locale_string_body
, &data
,
931 || data
.result
== NULL
|| strcmp (data
.result
, result
))
934 "fail: scm_to_locale_string (%s) = %s\n", str
, result
);
943 test_locale_strings ()
945 const char *lstr
= "This is not a string.";
951 if (!scm_is_string (scm_c_eval_string ("\"foo\"")))
953 fprintf (stderr
, "fail: scm_is_string (\"foo\") = true\n");
957 str
= scm_from_locale_string (lstr
);
959 if (!scm_is_string (str
))
961 fprintf (stderr
, "fail: scm_is_string (str) = true\n");
965 lstr2
= scm_to_locale_string (str
);
966 if (strcmp (lstr
, lstr2
))
968 fprintf (stderr
, "fail: lstr = lstr2\n");
974 len
= scm_to_locale_stringbuf (str
, buf
, 15);
975 if (len
!= strlen (lstr
))
977 fprintf (stderr
, "fail: scm_to_locale_stringbuf (...) = strlen(lstr)\n");
982 fprintf (stderr
, "fail: scm_to_locale_stringbuf (...) no overrun\n");
985 if (strncmp (lstr
, buf
, 15))
987 fprintf (stderr
, "fail: scm_to_locale_stringbuf (...) = lstr\n");
991 str2
= scm_from_locale_stringn (lstr
, 10);
993 if (!scm_is_string (str2
))
995 fprintf (stderr
, "fail: scm_is_string (str2) = true\n");
999 lstr2
= scm_to_locale_string (str2
);
1000 if (strncmp (lstr
, lstr2
, 10))
1002 fprintf (stderr
, "fail: lstr = lstr2\n");
1008 len
= scm_to_locale_stringbuf (str2
, buf
, 20);
1011 fprintf (stderr
, "fail: scm_to_locale_stringbuf (...) = 10\n");
1016 fprintf (stderr
, "fail: scm_to_locale_stringbuf (...) no overrun\n");
1019 if (strncmp (lstr
, buf
, 10))
1021 fprintf (stderr
, "fail: scm_to_locale_stringbuf (...) = lstr\n");
1025 lstr2
= scm_to_locale_stringn (str2
, &len
);
1028 fprintf (stderr
, "fail: scm_to_locale_stringn, len = 10\n");
1032 test_11 ("#f", NULL
, 0, 1);
1033 test_11 ("\"foo\"", "foo", 0, 0);
1034 test_11 ("(string #\\f #\\nul)", NULL
, 1, 0);
1038 tests (void *data
, int argc
, char **argv
)
1040 test_is_signed_integer ();
1041 test_is_unsigned_integer ();
1042 test_to_signed_integer ();
1043 test_to_unsigned_integer ();
1044 test_from_signed_integer ();
1045 test_from_unsigned_integer ();
1047 test_from_double ();
1049 test_locale_strings ();
1053 main (int argc
, char *argv
[])
1055 scm_boot_guile (argc
, argv
, tests
, NULL
);