1 /* Copyright (C) 1999,2000,2001,2003,2004, 2006, 2007, 2008, 2009, 2010 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 DEFSTST (scm_to_int64
)
707 DEFUTST (scm_to_uint64
)
709 #define TEST_8S(v,f,r,re,te) test_8s (v, tst_##f, #f, r, re, te)
710 #define TEST_8U(v,f,r,re,te) test_8u (v, tst_##f, #f, r, re, te)
716 TEST_7U (scm_from_uchar
, 91, "91");
717 TEST_7S (scm_from_schar
, 91, "91");
718 TEST_7S (scm_from_char
, 91, "91");
719 TEST_7S (scm_from_short
, -911, "-911");
720 TEST_7U (scm_from_ushort
, 911, "911");
721 TEST_7S (scm_from_int
, 911, "911");
722 TEST_7U (scm_from_uint
, 911, "911");
723 TEST_7S (scm_from_long
, 911, "911");
724 TEST_7U (scm_from_ulong
, 911, "911");
725 #if SCM_SIZEOF_LONG_LONG != 0
726 TEST_7S (scm_from_long_long
, 911, "911");
727 TEST_7U (scm_from_ulong_long
, 911, "911");
729 TEST_7U (scm_from_size_t
, 911, "911");
730 TEST_7S (scm_from_ssize_t
, 911, "911");
732 TEST_7S (scm_from_int8
, -128, "-128");
733 TEST_7S (scm_from_int8
, 127, "127");
734 TEST_7S (scm_from_int8
, 128, "-128");
735 TEST_7U (scm_from_uint8
, 255, "255");
737 TEST_7S (scm_from_int16
, -32768, "-32768");
738 TEST_7S (scm_from_int16
, 32767, "32767");
739 TEST_7S (scm_from_int16
, 32768, "-32768");
740 TEST_7U (scm_from_uint16
, 65535, "65535");
742 TEST_7S (scm_from_int32
, SCM_T_INT32_MIN
, "-2147483648");
743 TEST_7S (scm_from_int32
, SCM_T_INT32_MAX
, "2147483647");
744 TEST_7S (scm_from_int32
, SCM_T_INT32_MAX
+1LL, "-2147483648");
745 TEST_7U (scm_from_uint32
, SCM_T_UINT32_MAX
, "4294967295");
747 TEST_7S (scm_from_int64
, SCM_T_INT64_MIN
, "-9223372036854775808");
748 TEST_7S (scm_from_int64
, SCM_T_INT64_MAX
, "9223372036854775807");
749 TEST_7U (scm_from_uint64
, SCM_T_UINT64_MAX
, "18446744073709551615");
751 TEST_8S ("91", scm_to_schar
, 91, 0, 0);
752 TEST_8U ("91", scm_to_uchar
, 91, 0, 0);
753 TEST_8S ("91", scm_to_char
, 91, 0, 0);
754 TEST_8S ("-911", scm_to_short
, -911, 0, 0);
755 TEST_8U ("911", scm_to_ushort
, 911, 0, 0);
756 TEST_8S ("-911", scm_to_int
, -911, 0, 0);
757 TEST_8U ("911", scm_to_uint
, 911, 0, 0);
758 TEST_8S ("-911", scm_to_long
, -911, 0, 0);
759 TEST_8U ("911", scm_to_ulong
, 911, 0, 0);
760 #if SCM_SIZEOF_LONG_LONG != 0
761 TEST_8S ("-911", scm_to_long_long
, -911, 0, 0);
762 TEST_8U ("911", scm_to_ulong_long
, 911, 0, 0);
764 TEST_8U ("911", scm_to_size_t
, 911, 0, 0);
765 TEST_8S ("911", scm_to_ssize_t
, 911, 0, 0);
767 TEST_8S ("-128", scm_to_int8
, SCM_T_INT8_MIN
, 0, 0);
768 TEST_8S ("127", scm_to_int8
, SCM_T_INT8_MAX
, 0, 0);
769 TEST_8S ("128", scm_to_int8
, 0, 1, 0);
770 TEST_8S ("#f", scm_to_int8
, 0, 0, 1);
771 TEST_8U ("255", scm_to_uint8
, SCM_T_UINT8_MAX
, 0, 0);
772 TEST_8U ("256", scm_to_uint8
, 0, 1, 0);
773 TEST_8U ("-1", scm_to_uint8
, 0, 1, 0);
774 TEST_8U ("#f", scm_to_uint8
, 0, 0, 1);
776 TEST_8S ("-32768", scm_to_int16
, SCM_T_INT16_MIN
, 0, 0);
777 TEST_8S ("32767", scm_to_int16
, SCM_T_INT16_MAX
, 0, 0);
778 TEST_8S ("32768", scm_to_int16
, 0, 1, 0);
779 TEST_8S ("#f", scm_to_int16
, 0, 0, 1);
780 TEST_8U ("65535", scm_to_uint16
, SCM_T_UINT16_MAX
, 0, 0);
781 TEST_8U ("65536", scm_to_uint16
, 0, 1, 0);
782 TEST_8U ("-1", scm_to_uint16
, 0, 1, 0);
783 TEST_8U ("#f", scm_to_uint16
, 0, 0, 1);
785 TEST_8S ("-2147483648", scm_to_int32
, SCM_T_INT32_MIN
, 0, 0);
786 TEST_8S ("2147483647", scm_to_int32
, SCM_T_INT32_MAX
, 0, 0);
787 TEST_8S ("2147483648", scm_to_int32
, 0, 1, 0);
788 TEST_8S ("#f", scm_to_int32
, 0, 0, 1);
789 TEST_8U ("4294967295", scm_to_uint32
, SCM_T_UINT32_MAX
, 0, 0);
790 TEST_8U ("4294967296", scm_to_uint32
, 0, 1, 0);
791 TEST_8U ("-1", scm_to_uint32
, 0, 1, 0);
792 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);
806 test_9 (double val
, const char *result
)
808 SCM res
= scm_c_eval_string (result
);
809 if (scm_is_false (scm_eqv_p (res
, scm_from_double (val
))))
811 fprintf (stderr
, "fail: scm_from_double (%g) == %s\n", val
, result
);
816 /* The `infinity' and `not-a-number' values. */
817 static double guile_Inf
, guile_NaN
;
819 /* Initialize GUILE_INF and GUILE_NAN. Taken from `guile_ieee_init ()' in
820 `libguile/numbers.c'. */
825 /* C99 INFINITY, when available.
826 FIXME: The standard allows for INFINITY to be something that overflows
827 at compile time. We ought to have a configure test to check for that
828 before trying to use it. (But in practice we believe this is not a
829 problem on any system guile is likely to target.) */
830 guile_Inf
= INFINITY
;
831 #elif defined HAVE_DINFINITY
833 extern unsigned int DINFINITY
[2];
834 guile_Inf
= (*((double *) (DINFINITY
)));
841 if (guile_Inf
== tmp
)
848 /* C99 NAN, when available */
850 #elif defined HAVE_DQNAN
853 extern unsigned int DQNAN
[2];
854 guile_NaN
= (*((double *)(DQNAN
)));
857 guile_NaN
= guile_Inf
/ guile_Inf
;
865 test_9 (0.25, "0.25");
867 test_9 (guile_Inf
, "+inf.0");
868 test_9 (-guile_Inf
, "-inf.0");
869 test_9 (guile_NaN
, "+nan.0");
878 to_double_body (void *data
)
880 to_double_data
*d
= (to_double_data
*)data
;
881 d
->result
= scm_to_double (d
->val
);
886 test_10 (const char *val
, double result
, int type_error
)
889 data
.val
= scm_c_eval_string (val
);
893 if (scm_is_false (scm_internal_catch (SCM_BOOL_T
,
894 to_double_body
, &data
,
895 wrong_type_handler
, NULL
)))
898 "fail: scm_double (%s) -> wrong type\n", val
);
904 if (scm_is_true (scm_internal_catch (SCM_BOOL_T
,
905 to_double_body
, &data
,
907 || data
.result
!= result
)
910 "fail: scm_to_double (%s) = %g\n", val
, result
);
919 test_10 ("#f", 0.0, 1);
920 test_10 ("12", 12.0, 0);
921 test_10 ("0.25", 0.25, 0);
922 test_10 ("1/4", 0.25, 0);
923 test_10 ("+inf.0", guile_Inf
, 0);
924 test_10 ("-inf.0",-guile_Inf
, 0);
925 test_10 ("+1i", 0.0, 1);
931 } to_locale_string_data
;
934 to_locale_string_body (void *data
)
936 to_locale_string_data
*d
= (to_locale_string_data
*)data
;
937 d
->result
= scm_to_locale_string (d
->val
);
942 test_11 (const char *str
, const char *result
, int misc_error
, int type_error
)
944 to_locale_string_data data
;
945 data
.val
= scm_c_eval_string (str
);
950 if (scm_is_false (scm_internal_catch (SCM_BOOL_T
,
951 to_locale_string_body
, &data
,
952 misc_error_handler
, NULL
)))
955 "fail: scm_to_locale_string (%s) -> misc error\n", str
);
961 if (scm_is_false (scm_internal_catch (SCM_BOOL_T
,
962 to_locale_string_body
, &data
,
963 wrong_type_handler
, NULL
)))
966 "fail: scm_to_locale_string (%s) -> wrong type\n", str
);
972 if (scm_is_true (scm_internal_catch (SCM_BOOL_T
,
973 to_locale_string_body
, &data
,
975 || data
.result
== NULL
|| strcmp (data
.result
, result
))
978 "fail: scm_to_locale_string (%s) = %s\n", str
, result
);
987 test_locale_strings ()
989 const char *lstr
= "This is not a string.";
995 if (!scm_is_string (scm_c_eval_string ("\"foo\"")))
997 fprintf (stderr
, "fail: scm_is_string (\"foo\") = true\n");
1001 str
= scm_from_locale_string (lstr
);
1003 if (!scm_is_string (str
))
1005 fprintf (stderr
, "fail: scm_is_string (str) = true\n");
1006 exit (EXIT_FAILURE
);
1009 lstr2
= scm_to_locale_string (str
);
1010 if (strcmp (lstr
, lstr2
))
1012 fprintf (stderr
, "fail: lstr = lstr2\n");
1013 exit (EXIT_FAILURE
);
1018 len
= scm_to_locale_stringbuf (str
, buf
, 15);
1019 if (len
!= strlen (lstr
))
1021 fprintf (stderr
, "fail: scm_to_locale_stringbuf (...) = strlen(lstr)\n");
1022 exit (EXIT_FAILURE
);
1026 fprintf (stderr
, "fail: scm_to_locale_stringbuf (...) no overrun\n");
1027 exit (EXIT_FAILURE
);
1029 if (strncmp (lstr
, buf
, 15))
1031 fprintf (stderr
, "fail: scm_to_locale_stringbuf (...) = lstr\n");
1032 exit (EXIT_FAILURE
);
1035 str2
= scm_from_locale_stringn (lstr
, 10);
1037 if (!scm_is_string (str2
))
1039 fprintf (stderr
, "fail: scm_is_string (str2) = true\n");
1040 exit (EXIT_FAILURE
);
1043 lstr2
= scm_to_locale_string (str2
);
1044 if (strncmp (lstr
, lstr2
, 10))
1046 fprintf (stderr
, "fail: lstr = lstr2\n");
1047 exit (EXIT_FAILURE
);
1052 len
= scm_to_locale_stringbuf (str2
, buf
, 20);
1055 fprintf (stderr
, "fail: scm_to_locale_stringbuf (...) = 10\n");
1056 exit (EXIT_FAILURE
);
1060 fprintf (stderr
, "fail: scm_to_locale_stringbuf (...) no overrun\n");
1061 exit (EXIT_FAILURE
);
1063 if (strncmp (lstr
, buf
, 10))
1065 fprintf (stderr
, "fail: scm_to_locale_stringbuf (...) = lstr\n");
1066 exit (EXIT_FAILURE
);
1069 lstr2
= scm_to_locale_stringn (str2
, &len
);
1072 fprintf (stderr
, "fail: scm_to_locale_stringn, len = 10\n");
1073 exit (EXIT_FAILURE
);
1076 test_11 ("#f", NULL
, 0, 1);
1077 test_11 ("\"foo\"", "foo", 0, 0);
1078 test_11 ("(string #\\f #\\nul)", NULL
, 1, 0);
1082 test_to_utf8_stringn ()
1084 scm_t_wchar wstr
[] = { 0x20, /* 0x20 */
1085 0xDF, /* 0xC3, 0x9F */
1086 0x65E5, /* 0xE6, 0x97, 0xA5 */
1087 0x1D400 }; /* 0xF0, 0x9D, 0x90, 0x80 */
1089 SCM str0
= scm_from_utf32_stringn (wstr
, 1); /* ASCII */
1090 SCM str1
= scm_from_utf32_stringn (wstr
, 2); /* Narrow */
1091 SCM str2
= scm_from_utf32_stringn (wstr
, 4); /* Wide */
1093 char cstr0
[] = { 0x20, 0 };
1094 char cstr1
[] = { 0x20, 0xC3, 0x9F, 0 };
1095 char cstr2
[] = { 0x20, 0xC3, 0x9F, 0xE6, 0x97, 0xA5,
1096 0xF0, 0x9D, 0x90, 0x80, 0 };
1100 /* Test conversion of ASCII string */
1101 cstr
= scm_to_utf8_stringn (str0
, &len
);
1102 if (len
+ 1 != sizeof (cstr0
) || memcmp (cstr
, cstr0
, len
))
1104 fprintf (stderr
, "fail: scm_to_utf8_stringn (<ASCII>, &len)");
1105 exit (EXIT_FAILURE
);
1108 cstr
= scm_to_utf8_stringn (str0
, NULL
);
1109 if (memcmp (cstr
, cstr0
, len
+ 1))
1111 fprintf (stderr
, "fail: scm_to_utf8_stringn (<ASCII>, NULL)");
1112 exit (EXIT_FAILURE
);
1116 /* Test conversion of narrow string */
1117 cstr
= scm_to_utf8_stringn (str1
, &len
);
1118 if (len
+ 1 != sizeof (cstr1
) || memcmp (cstr
, cstr1
, len
))
1120 fprintf (stderr
, "fail: scm_to_utf8_stringn (<NARROW>, &len)");
1121 exit (EXIT_FAILURE
);
1124 cstr
= scm_to_utf8_stringn (str1
, NULL
);
1125 if (memcmp (cstr
, cstr1
, len
+ 1))
1127 fprintf (stderr
, "fail: scm_to_utf8_stringn (<NARROW>, NULL)");
1128 exit (EXIT_FAILURE
);
1132 /* Test conversion of wide string */
1133 cstr
= scm_to_utf8_stringn (str2
, &len
);
1134 if (len
+ 1 != sizeof (cstr2
) || memcmp (cstr
, cstr2
, len
))
1136 fprintf (stderr
, "fail: scm_to_utf8_stringn (<WIDE>, &len)");
1137 exit (EXIT_FAILURE
);
1140 cstr
= scm_to_utf8_stringn (str2
, NULL
);
1141 if (memcmp (cstr
, cstr2
, len
+ 1))
1143 fprintf (stderr
, "fail: scm_to_utf8_stringn (<WIDE>, NULL)");
1144 exit (EXIT_FAILURE
);
1152 if (1 != scm_is_exact (scm_c_eval_string ("3")))
1154 fprintf (stderr
, "fail: scm_is_exact (\"3\") = 1\n");
1155 exit (EXIT_FAILURE
);
1157 if (0 != scm_is_exact (scm_c_eval_string ("3.0")))
1159 fprintf (stderr
, "fail: scm_is_exact (\"3.0\") = 0\n");
1160 exit (EXIT_FAILURE
);
1167 if (1 !=scm_is_inexact (scm_c_eval_string ("3.0")))
1169 fprintf (stderr
, "fail: scm_is_inexact (\"3.0\") = 1\n");
1170 exit (EXIT_FAILURE
);
1172 if (0 != scm_is_inexact (scm_c_eval_string ("3")))
1174 fprintf (stderr
, "fail: scm_is_inexact (\"3\") = 0\n");
1175 exit (EXIT_FAILURE
);
1181 tests (void *data
, int argc
, char **argv
)
1183 test_is_signed_integer ();
1184 test_is_unsigned_integer ();
1185 test_to_signed_integer ();
1186 test_to_unsigned_integer ();
1187 test_from_signed_integer ();
1188 test_from_unsigned_integer ();
1190 test_from_double ();
1192 test_locale_strings ();
1193 test_to_utf8_stringn ();
1199 main (int argc
, char *argv
[])
1202 scm_boot_guile (argc
, argv
, tests
, NULL
);