1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101
|
;; Filename : test-tail-rec.scm
;; About : unit test for the proper tail recursion
;;
;; Copyright (C) 2005-2006 YAMAMOTO Kengo <yamaken AT bp.iij4u.or.jp>
;; Copyright (c) 2007-2008 SigScheme Project <uim-en AT googlegroups.com>
;;
;; All rights reserved.
;;
;; Redistribution and use in source and binary forms, with or without
;; modification, are permitted provided that the following conditions
;; are met:
;;
;; 1. Redistributions of source code must retain the above copyright
;; notice, this list of conditions and the following disclaimer.
;; 2. Redistributions in binary form must reproduce the above copyright
;; notice, this list of conditions and the following disclaimer in the
;; documentation and/or other materials provided with the distribution.
;; 3. Neither the name of authors nor the names of its contributors
;; may be used to endorse or promote products derived from this software
;; without specific prior written permission.
;;
;; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS
;; IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
;; THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
;; PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
;; CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
;; EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
;; PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
;; PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
;; LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
;; FILE HISTORY:
;; 2005-08-07 contributed by YamaKen (yamaken AT bp.iij4u.or.jp)
;; This file is provided to test the proper tail recursion functionality. See
;; "3.5 Proper tail recursion" of R5RS for the accurate specification.
;;
;; This test must be run as follows to take effect. Use runtest-tail-rec.sh.
;;
;; $ (ulimit -s 128 && ulimit -d 2048 && ./sscm test/test-tail-rec.scm || echo 'exploded')
;;
;; And compare the result with another R5RS implementation (gosh).
;;
;; $ (ulimit -s 128 && ulimit -d 2048 && gosh -I. test/test-tail-rec.scm || echo 'exploded')
(require-extension (srfi 8 23 34))
(require-extension (unittest))
(define test-eval? #f)
(define test-and? #t) ;; #t is required to conform to R5RS
(define test-or? #t) ;; #t is required to conform to R5RS
(define test-improper-and? #f) ;; R5RS compliant implementation explodes if #t
(define test-improper-or? #f) ;; R5RS compliant implementation explodes if #t
(define test-with-exception-handler? #f) ;; improper
(define test-guard? #f) ;; improper
(define KB 1024)
(define heap-limit (* 2048 KB)) ;; specify this by ulimit -d
(define cell-size 8) ;; minimum cell size (32-bit storage-compact)
(define explosive-count (/ heap-limit cell-size))
(define assert-orig assert)
(define assert
(lambda (test-name err-msg exp)
;; current assert implementation cannot print msg before exp has
;; been evaluated
(display err-msg)
(assert-orig test-name err-msg exp)
(display " ...OK\n")))
(define rec-by-eval
(lambda (cnt)
(if (zero? cnt)
'succeeded
(eval (list 'rec-by-eval (- cnt 1))
(interaction-environment)))))
(define rec-by-apply
(lambda (cnt)
(if (zero? cnt)
'succeeded
(apply rec-by-apply (list (- cnt 1))))))
(define rec-by-apply-with-apply
(lambda (cnt)
(if (zero? cnt)
'succeeded
(apply apply (list rec-by-apply-with-apply (list (- cnt 1)))))))
(define rec-by-if-consequent
(lambda (cnt)
(if (not (zero? cnt))
(rec-by-if-consequent (- cnt 1))
'succeeded)))
(define rec-by-if-consequent-with-begin
(lambda (cnt)
(if (not (zero? cnt))
(begin
(+ 1 2) ;; dummy
(rec-by-if-consequent-with-begin (- cnt 1)))
'succeeded)))
(define rec-by-if-consequent-with-let
(lambda (cnt)
(if (not (zero? cnt))
(let ((dummy (+ 1 2))
(dummy2 (+ 3 4)))
(rec-by-if-consequent-with-let (- cnt 1)))
'succeeded)))
(define rec-by-if-consequent-with-let*
(lambda (cnt)
(if (not (zero? cnt))
(let* ((dummy (+ 1 2))
(dummy2 (+ dummy 3)))
(rec-by-if-consequent-with-let* (- cnt 1)))
'succeeded)))
(define rec-by-if-consequent-with-letrec
(lambda (cnt)
(if (not (zero? cnt))
(letrec ((dummy (lambda () dummy2))
(dummy2 (lambda () dummy)))
(rec-by-if-consequent-with-letrec (- cnt 1)))
'succeeded)))
(define rec-by-if-alternate
(lambda (cnt)
(if (zero? cnt)
'succeeded
(rec-by-if-alternate (- cnt 1)))))
(define rec-by-if-alternate-with-begin
(lambda (cnt)
(if (not (zero? cnt))
(begin
(+ 1 2) ;; dummy
(rec-by-if-alternate-with-begin (- cnt 1)))
'succeeded)))
(define rec-by-if-alternate-with-let
(lambda (cnt)
(if (not (zero? cnt))
(let ((dummy (+ 1 2))
(dummy2 (+ 3 4)))
(rec-by-if-alternate-with-let (- cnt 1)))
'succeeded)))
(define rec-by-if-alternate-with-let*
(lambda (cnt)
(if (not (zero? cnt))
(let* ((dummy (+ 1 2))
(dummy2 (+ dummy 3)))
(rec-by-if-alternate-with-let* (- cnt 1)))
'succeeded)))
(define rec-by-if-alternate-with-letrec
(lambda (cnt)
(if (not (zero? cnt))
(letrec ((dummy (lambda () dummy2))
(dummy2 (lambda () dummy)))
(rec-by-if-alternate-with-letrec (- cnt 1)))
'succeeded)))
(define rec-by-cond-1st
(lambda (cnt)
(cond
((positive? cnt)
(rec-by-cond-1st (- cnt 1)))
((zero? cnt)
'succeeded)
((negative? cnt)
'dummy)
(else
'dummy))))
(define rec-by-cond-1st-with-begin
(lambda (cnt)
(cond
((positive? cnt)
(begin
(+ 1 2) ;; dummy
(rec-by-cond-1st-with-begin (- cnt 1))))
((zero? cnt)
'succeeded)
((negative? cnt)
'dummy)
(else
'dummy))))
(define rec-by-cond-1st-with-let
(lambda (cnt)
(cond
((positive? cnt)
(let ((dummy (+ 1 2))
(dummy2 (+ 3 4)))
(rec-by-cond-1st-with-let (- cnt 1))))
((zero? cnt)
'succeeded)
((negative? cnt)
'dummy)
(else
'dummy))))
(define rec-by-cond-1st-with-let*
(lambda (cnt)
(cond
((positive? cnt)
(let* ((dummy (+ 1 2))
(dummy2 (+ dummy 3)))
(rec-by-cond-1st-with-let* (- cnt 1))))
((zero? cnt)
'succeeded)
((negative? cnt)
'dummy)
(else
'dummy))))
(define rec-by-cond-1st-with-letrec
(lambda (cnt)
(cond
((positive? cnt)
(letrec ((dummy (lambda () dummy2))
(dummy2 (lambda () dummy)))
(rec-by-cond-1st-with-letrec (- cnt 1))))
((zero? cnt)
'succeeded)
((negative? cnt)
'dummy)
(else
'dummy))))
(define rec-by-cond-2nd
(lambda (cnt)
(cond
((zero? cnt)
'succeeded)
((positive? cnt)
(rec-by-cond-2nd (- cnt 1)))
((negative? cnt)
'dummy)
(else
'dummy))))
(define rec-by-cond-3rd
(lambda (cnt)
(cond
((zero? cnt)
'succeeded)
((negative? cnt)
'dummy)
((positive? cnt)
(rec-by-cond-3rd (- cnt 1)))
(else
'dummy))))
(define rec-by-cond-3rd-with-=>
(lambda (cnt)
(cond
((zero? cnt)
'succeeded)
((negative? cnt)
'dummy)
((- cnt 1) => rec-by-cond-3rd-with-=>)
(else
'dummy))))
(define rec-by-cond-last
(lambda (cnt)
(cond
((zero? cnt)
'succeeded)
((negative? cnt)
'dummy)
((positive? cnt)
(rec-by-cond-last (- cnt 1))))))
(define rec-by-cond-else
(lambda (cnt)
(cond
((zero? cnt)
'succeeded)
((negative? cnt)
'dummy)
((not (positive? cnt))
'dummy)
(else
(rec-by-cond-else (- cnt 1))))))
(define rec-by-cond-else-with-begin
(lambda (cnt)
(cond
((zero? cnt)
'succeeded)
((negative? cnt)
'dummy)
((not (positive? cnt))
'dummy)
(else
(begin
(+ 1 2) ;; dummy
(rec-by-cond-else (- cnt 1)))))))
(define rec-by-cond-else-with-let
(lambda (cnt)
(cond
((zero? cnt)
'succeeded)
((negative? cnt)
'dummy)
((not (positive? cnt))
'dummy)
(else
(let ((dummy (+ 1 2))
(dummy2 (+ 3 4)))
(rec-by-cond-else (- cnt 1)))))))
(define rec-by-cond-else-with-let*
(lambda (cnt)
(cond
((zero? cnt)
'succeeded)
((negative? cnt)
'dummy)
((not (positive? cnt))
'dummy)
(else
(let* ((dummy (+ 1 2))
(dummy2 (+ dummy 3)))
(rec-by-cond-else (- cnt 1)))))))
(define rec-by-cond-else-with-letrec
(lambda (cnt)
(cond
((zero? cnt)
'succeeded)
((negative? cnt)
'dummy)
((not (positive? cnt))
'dummy)
(else
(letrec ((dummy (lambda () dummy2))
(dummy2 (lambda () dummy)))
(rec-by-cond-else (- cnt 1)))))))
(define rec-by-case-1st
(lambda (cnt)
(case (if (positive? cnt)
1
cnt)
((1)
(rec-by-case-1st (- cnt 1)))
((0)
'succeeded)
((-1)
'dummy)
(else
'dummy))))
(define rec-by-case-1st-with-begin
(lambda (cnt)
(case (if (positive? cnt)
1
cnt)
((1)
(begin
(+ 1 2) ;; dummy
(rec-by-case-1st-with-begin (- cnt 1))))
((0)
'succeeded)
((-1)
'dummy)
(else
'dummy))))
(define rec-by-case-1st-with-let
(lambda (cnt)
(case (if (positive? cnt)
1
cnt)
((1)
(let ((dummy (+ 1 2))
(dummy2 (+ 3 4)))
(rec-by-case-1st-with-let (- cnt 1))))
((0)
'succeeded)
((-1)
'dummy)
(else
'dummy))))
(define rec-by-case-1st-with-let*
(lambda (cnt)
(case (if (positive? cnt)
1
cnt)
((1)
(let* ((dummy (+ 1 2))
(dummy2 (+ dummy 3)))
(rec-by-case-1st-with-let* (- cnt 1))))
((0)
'succeeded)
((-1)
'dummy)
(else
'dummy))))
(define rec-by-case-1st-with-letrec
(lambda (cnt)
(case (if (positive? cnt)
1
cnt)
((1)
(letrec ((dummy (lambda () dummy2))
(dummy2 (lambda () dummy)))
(rec-by-case-1st-with-letrec (- cnt 1))))
((0)
'succeeded)
((-1)
'dummy)
(else
'dummy))))
(define rec-by-case-2nd
(lambda (cnt)
(case (if (positive? cnt)
1
cnt)
((0)
'succeeded)
((1)
(rec-by-case-2nd (- cnt 1)))
((-1)
'dummy)
(else
'dummy))))
(define rec-by-case-3rd
(lambda (cnt)
(case (if (positive? cnt)
1
cnt)
((0)
'succeeded)
((-1)
'dummy)
((1)
(rec-by-case-3rd (- cnt 1)))
(else
'dummy))))
(define rec-by-case-last
(lambda (cnt)
(case (if (positive? cnt)
1
cnt)
((0)
'succeeded)
((-1)
'dummy)
((1)
(rec-by-case-last (- cnt 1))))))
(define rec-by-case-else
(lambda (cnt)
(case (if (positive? cnt)
1
cnt)
((0)
'succeeded)
((-1)
'dummy)
((2)
'dummy)
(else
(rec-by-case-else (- cnt 1))))))
(define rec-by-case-else-with-begin
(lambda (cnt)
(case (if (positive? cnt)
1
cnt)
((0)
'succeeded)
((-1)
'dummy)
((2)
'dummy)
(else
(begin
(+ 1 2) ;; dummy
(rec-by-case-else (- cnt 1)))))))
(define rec-by-case-else-with-let
(lambda (cnt)
(case (if (positive? cnt)
1
cnt)
((0)
'succeeded)
((-1)
'dummy)
((2)
'dummy)
(else
(let ((dummy (+ 1 2))
(dummy2 (+ 3 4)))
(rec-by-case-else (- cnt 1)))))))
(define rec-by-case-else-with-let*
(lambda (cnt)
(case (if (positive? cnt)
1
cnt)
((0)
'succeeded)
((-1)
'dummy)
((2)
'dummy)
(else
(let* ((dummy (+ 1 2))
(dummy2 (+ dummy 3)))
(rec-by-case-else (- cnt 1)))))))
(define rec-by-case-else-with-letrec
(lambda (cnt)
(case (if (positive? cnt)
1
cnt)
((0)
'succeeded)
((-1)
'dummy)
((2)
'dummy)
(else
(letrec ((dummy (lambda () dummy2))
(dummy2 (lambda () dummy)))
(rec-by-case-else (- cnt 1)))))))
(define rec-by-and-tail
(lambda (cnt)
(and (not (zero? cnt))
(rec-by-and-tail (- cnt 1)))))
(define rec-by-and-tail-with-begin
(lambda (cnt)
(and (not (zero? cnt))
(begin
(+ 1 2) ;; dummy
(rec-by-and-tail-with-begin (- cnt 1))))))
(define rec-by-and-tail-with-let
(lambda (cnt)
(and (not (zero? cnt))
(let ((dummy (+ 1 2))
(dummy2 (+ 3 4)))
(rec-by-and-tail-with-let (- cnt 1))))))
(define rec-by-and-tail-with-let*
(lambda (cnt)
(and (not (zero? cnt))
(let* ((dummy (+ 1 2))
(dummy2 (+ dummy 3)))
(rec-by-and-tail-with-let* (- cnt 1))))))
(define rec-by-and-tail-with-letrec
(lambda (cnt)
(and (not (zero? cnt))
(letrec ((dummy (lambda () dummy2))
(dummy2 (lambda () dummy)))
(rec-by-and-tail-with-letrec (- cnt 1))))))
(define improper-rec-by-and-tail
(lambda (cnt)
(and (not (zero? cnt))
(improper-rec-by-and-tail (- cnt 1)))
'succeeded))
(define rec-by-or-tail
(lambda (cnt)
(or (zero? cnt)
(rec-by-or-tail (- cnt 1)))))
(define rec-by-or-tail-with-begin
(lambda (cnt)
(or (zero? cnt)
(begin
(+ 1 2) ;; dummy
(rec-by-or-tail-with-begin (- cnt 1))))))
(define rec-by-or-tail-with-let
(lambda (cnt)
(or (zero? cnt)
(let ((dummy (+ 1 2))
(dummy2 (+ 3 4)))
(rec-by-or-tail-with-let (- cnt 1))))))
(define rec-by-or-tail-with-let*
(lambda (cnt)
(or (zero? cnt)
(let* ((dummy (+ 1 2))
(dummy2 (+ dummy 3)))
(rec-by-or-tail-with-let* (- cnt 1))))))
(define rec-by-or-tail-with-letrec
(lambda (cnt)
(or (zero? cnt)
(letrec ((dummy (lambda () dummy2))
(dummy2 (lambda () dummy)))
(rec-by-or-tail-with-letrec (- cnt 1))))))
(define improper-rec-by-or-tail
(lambda (cnt)
(or (zero? cnt)
(improper-rec-by-or-tail (- cnt 1)))
'succeeded))
(define rec-even?
(lambda (n)
(if (zero? n)
#t
(rec-odd? (- n 1)))))
(define rec-odd?
(lambda (n)
(if (zero? n)
#f
(rec-even? (- n 1)))))
(define rec-even-with-begin?
(lambda (n)
(if (zero? n)
#t
(begin
(+ 1 2) ;; dummy
(rec-odd-with-begin? (- n 1))))))
(define rec-odd-with-begin?
(lambda (n)
(if (zero? n)
#f
(begin
(+ 1 2) ;; dummy
(rec-even-with-begin? (- n 1))))))
(define rec-even-with-let?
(lambda (n)
(if (zero? n)
#t
(let ((dummy (+ 1 2))
(dummy2 (+ 3 4)))
(rec-odd-with-let? (- n 1))))))
(define rec-odd-with-let?
(lambda (n)
(if (zero? n)
#f
(let ((dummy (+ 1 2))
(dummy2 (+ 3 4)))
(rec-even-with-let? (- n 1))))))
(define rec-even-with-let*?
(lambda (n)
(if (zero? n)
#t
(let* ((dummy (+ 1 2))
(dummy2 (+ dummy 3)))
(rec-odd-with-let*? (- n 1))))))
(define rec-odd-with-let*?
(lambda (n)
(if (zero? n)
#f
(let* ((dummy (+ 1 2))
(dummy2 (+ dummy 3)))
(rec-even-with-let*? (- n 1))))))
(define rec-even-with-letrec?
(lambda (n)
(if (zero? n)
#t
(letrec ((dummy (lambda () dummy2))
(dummy2 (lambda () dummy)))
(rec-odd-with-letrec? (- n 1))))))
(define rec-odd-with-letrec?
(lambda (n)
(if (zero? n)
#f
(letrec ((dummy (lambda () dummy2))
(dummy2 (lambda () dummy)))
(rec-even-with-letrec? (- n 1))))))
(define rec-continuation
(lambda (n)
(if (zero? n)
'succeeded
(call-with-current-continuation
(lambda (cont)
(rec-continuation (- n 1)))))))
(define rec-call-with-values
(lambda (n)
(if (zero? n)
'succeeded
(call-with-values
(lambda () (values 2 3 n))
(lambda (dummy1 dummy2 n)
(rec-call-with-values (- n 1)))))))
(define rec-call-with-values-2
(lambda (n)
(letrec ((producer (lambda ()
(if (zero? n)
(values values (lambda () 'succeeded))
(begin (set! n (- n 1))
(values producer call-with-values))))))
(call-with-values producer call-with-values))))
(define rec-with-exception-handler
(lambda (n)
(if (zero? n)
'succeeded
(with-exception-handler (lambda (x) (error "handler called"))
(rec-with-exception-handler (- n 1))))))
(define rec-guard
(lambda (n)
(if (zero? n)
'succeeded
(guard (e (#f #f))
(rec-guard (- n 1))))))
(define rec-receive
(lambda (n)
(if (zero? n)
'succeeded
(receive (dummy1 dummy2 n) (values 2 3 n)
(rec-receive (- n 1))))))
(define rec-proper-infinite
(lambda (cnt)
(rec-proper-infinite (+ cnt 1))))
(define rec-improper-infinite
(lambda (cnt)
(if (zero? cnt)
(error "explicit explosion of improper infinite tail recursion failed")
(rec-improper-infinite (- cnt 1)))
'dummy))
;; eval
;; SigScheme, Guile 1.6.7 and Gauche 0.8.6 fail. Should we make this test
;; passed? -- YamaKen 2006-09-25
(if test-eval?
(assert-equal? "proper tail recursion by eval"
'succeeded
(rec-by-eval explosive-count)))
;; apply
(assert-equal? "proper tail recursion by apply"
'succeeded
(rec-by-apply explosive-count))
(assert-equal? "proper tail recursion by apply with apply"
'succeeded
(rec-by-apply-with-apply explosive-count))
;; if
(assert-equal? "proper tail recursion by if-consequent"
'succeeded
(rec-by-if-consequent explosive-count))
(assert-equal? "proper tail recursion by if-consequent with begin"
'succeeded
(rec-by-if-consequent-with-begin explosive-count))
(assert-equal? "proper tail recursion by if-consequent with let"
'succeeded
(rec-by-if-consequent-with-let explosive-count))
(assert-equal? "proper tail recursion by if-consequent with let*"
'succeeded
(rec-by-if-consequent-with-let* explosive-count))
(assert-equal? "proper tail recursion by if-consequent with letrec"
'succeeded
(rec-by-if-consequent-with-letrec explosive-count))
(assert-equal? "proper tail recursion by if-alternate"
'succeeded
(rec-by-if-alternate explosive-count))
(assert-equal? "proper tail recursion by if-alternate with begin"
'succeeded
(rec-by-if-alternate-with-begin explosive-count))
(assert-equal? "proper tail recursion by if-alternate with let"
'succeeded
(rec-by-if-alternate-with-let explosive-count))
(assert-equal? "proper tail recursion by if-alternate with let*"
'succeeded
(rec-by-if-alternate-with-let* explosive-count))
(assert-equal? "proper tail recursion by if-alternate with letrec"
'succeeded
(rec-by-if-alternate-with-letrec explosive-count))
;; cond
(assert-equal? "proper tail recursion by 1st clause of cond"
'succeeded
(rec-by-cond-1st explosive-count))
(assert-equal? "proper tail recursion by 1st clause of cond with begin"
'succeeded
(rec-by-cond-1st-with-begin explosive-count))
(assert-equal? "proper tail recursion by 1st clause of cond with let"
'succeeded
(rec-by-cond-1st-with-let explosive-count))
(assert-equal? "proper tail recursion by 1st clause of cond with let*"
'succeeded
(rec-by-cond-1st-with-let* explosive-count))
(assert-equal? "proper tail recursion by 1st clause of cond with letrec"
'succeeded
(rec-by-cond-1st-with-letrec explosive-count))
(assert-equal? "proper tail recursion by 2nd clause of cond"
'succeeded
(rec-by-cond-2nd explosive-count))
(assert-equal? "proper tail recursion by 3rd clause of cond"
'succeeded
(rec-by-cond-3rd explosive-count))
(assert-equal? "proper tail recursion by 3rd clause of cond with => expression"
'succeeded
(rec-by-cond-3rd-with-=> explosive-count))
(assert-equal? "proper tail recursion by last clause of cond"
'succeeded
(rec-by-cond-last explosive-count))
(assert-equal? "proper tail recursion by cond-else"
'succeeded
(rec-by-cond-else explosive-count))
(assert-equal? "proper tail recursion by cond-else with begin"
'succeeded
(rec-by-cond-else-with-begin explosive-count))
(assert-equal? "proper tail recursion by cond-else with let"
'succeeded
(rec-by-cond-else-with-let explosive-count))
(assert-equal? "proper tail recursion by cond-else with let*"
'succeeded
(rec-by-cond-else-with-let* explosive-count))
(assert-equal? "proper tail recursion by cond-else with letrec"
'succeeded
(rec-by-cond-else-with-letrec explosive-count))
;; case
(assert-equal? "proper tail recursion by 1st clause of case"
'succeeded
(rec-by-case-1st explosive-count))
(assert-equal? "proper tail recursion by 1st clause of case with begin"
'succeeded
(rec-by-case-1st-with-begin explosive-count))
(assert-equal? "proper tail recursion by 1st clause of case with let"
'succeeded
(rec-by-case-1st-with-let explosive-count))
(assert-equal? "proper tail recursion by 1st clause of case with let*"
'succeeded
(rec-by-case-1st-with-let* explosive-count))
(assert-equal? "proper tail recursion by 1st clause of case with letrec"
'succeeded
(rec-by-case-1st-with-letrec explosive-count))
(assert-equal? "proper tail recursion by 2nd clause of case"
'succeeded
(rec-by-case-2nd explosive-count))
(assert-equal? "proper tail recursion by 3rd clause of case"
'succeeded
(rec-by-case-3rd explosive-count))
(assert-equal? "proper tail recursion by last clause of case"
'succeeded
(rec-by-case-last explosive-count))
(assert-equal? "proper tail recursion by case-else"
'succeeded
(rec-by-case-else explosive-count))
(assert-equal? "proper tail recursion by case-else with begin"
'succeeded
(rec-by-case-else-with-begin explosive-count))
(assert-equal? "proper tail recursion by case-else with let"
'succeeded
(rec-by-case-else-with-let explosive-count))
(assert-equal? "proper tail recursion by case-else with let*"
'succeeded
(rec-by-case-else-with-let* explosive-count))
(assert-equal? "proper tail recursion by case-else with letrec"
'succeeded
(rec-by-case-else-with-letrec explosive-count))
;; and
(if test-and?
(begin
(assert-equal? "proper tail recursion by and-tail"
'succeeded
(or (rec-by-and-tail explosive-count)
'succeeded))
(assert-equal? "proper tail recursion by and-tail with begin"
'succeeded
(or (rec-by-and-tail-with-begin explosive-count)
'succeeded))
(assert-equal? "proper tail recursion by and-tail with let"
'succeeded
(or (rec-by-and-tail-with-let explosive-count)
'succeeded))
(assert-equal? "proper tail recursion by and-tail with let*"
'succeeded
(or (rec-by-and-tail-with-let* explosive-count)
'succeeded))
(assert-equal? "proper tail recursion by and-tail with letrec"
'succeeded
(or (rec-by-and-tail-with-letrec explosive-count)
'succeeded))))
;; improper and: intentionally explodes
(if test-improper-and?
(assert-equal? "improper tail recursion by and-tail"
'succeeded
(improper-rec-by-and-tail explosive-count)))
;; or
(if test-or?
(begin
(assert-equal? "proper tail recursion by or-tail"
'succeeded
(and (rec-by-or-tail explosive-count)
'succeeded))
(assert-equal? "proper tail recursion by or-tail with begin"
'succeeded
(and (rec-by-or-tail-with-begin explosive-count)
'succeeded))
(assert-equal? "proper tail recursion by or-tail with let"
'succeeded
(and (rec-by-or-tail-with-let explosive-count)
'succeeded))
(assert-equal? "proper tail recursion by or-tail with let*"
'succeeded
(and (rec-by-or-tail-with-let* explosive-count)
'succeeded))
(assert-equal? "proper tail recursion by or-tail with letrec"
'succeeded
(and (rec-by-or-tail-with-letrec explosive-count)
'succeeded))))
;; improper or: intentionally explodes
(if test-improper-or?
(assert-equal? "improper tail recursion by or-tail"
'succeeded
(improper-rec-by-or-tail explosive-count)))
;; do
(assert-equal? "iteration by do"
'succeeded
(do ((cnt explosive-count (- cnt 1))
(dummy 0 (+ dummy 1)))
((zero? cnt)
'succeeded)
(+ cnt dummy)))
(assert-equal? "proper tail recursion with do"
'succeeded
(let loop ((loop-cnt explosive-count))
(if (zero? loop-cnt)
'succeeded
(do ((cnt 3 (- cnt 1))
(dummy 0 (+ dummy 1)))
((zero? cnt)
(loop (- loop-cnt 1)))
(+ cnt dummy)))))
;; flip-flop procs
(assert-equal? "proper tail recursion by flip-flop procs"
'succeeded
(and (rec-even? explosive-count)
'succeeded))
(assert-equal? "proper tail recursion by flip-flop procs with begin"
'succeeded
(and (rec-even-with-begin? explosive-count)
'succeeded))
(assert-equal? "proper tail recursion by flip-flop procs with let"
'succeeded
(and (rec-even-with-let? explosive-count)
'succeeded))
(assert-equal? "proper tail recursion by flip-flop procs with let*"
'succeeded
(and (rec-even-with-let*? explosive-count)
'succeeded))
(assert-equal? "proper tail recursion by flip-flop procs with letrec"
'succeeded
(and (rec-even-with-letrec? explosive-count)
'succeeded))
;; flip-flop procs in letrec
(assert-equal? "proper tail recursion by flip-flop procs defined by letrec"
'succeeded
(letrec ((my-even? (lambda (n)
(if (zero? n)
#t
(my-odd? (- n 1)))))
(my-odd? (lambda (n)
(if (zero? n)
#f
(my-even? (- n 1))))))
(and (my-even? explosive-count)
'succeeded)))
;; named let
(assert-equal? "proper tail recursion by named let"
'succeeded
(let loop ((cnt explosive-count))
(if (zero? cnt)
'succeeded
(loop (- cnt 1)))))
(assert-equal? "proper tail recursion by named let with begin"
'succeeded
(let loop ((cnt explosive-count))
(if (zero? cnt)
'succeeded
(begin
(+ 1 2) ;; dummy
(loop (- cnt 1))))))
(assert-equal? "proper tail recursion by named let with let"
'succeeded
(let loop ((cnt explosive-count))
(if (zero? cnt)
'succeeded
(let ((dummy (+ 1 2))
(dummy2 (+ 3 4)))
(loop (- cnt 1))))))
(assert-equal? "proper tail recursion by named let with let*"
'succeeded
(let loop ((cnt explosive-count))
(if (zero? cnt)
'succeeded
(let* ((dummy (+ 1 2))
(dummy2 (+ dummy 3)))
(loop (- cnt 1))))))
(assert-equal? "proper tail recursion by named let with letrec"
'succeeded
(let loop ((cnt explosive-count))
(if (zero? cnt)
'succeeded
(letrec ((dummy (+ 1 2))
(dummy2 (+ 3 4)))
(loop (- cnt 1))))))
;; call/cc
;; Current SigScheme implementation cannot run this test as proper tail
;; recursion. The stack grows.
(if (not (and (provided? "sigscheme")
(provided? "nested-continuation-only")))
(assert-equal? "proper tail recursion by call/cc"
'succeeded
(rec-continuation explosive-count)))
;; call-with-values
(assert-equal? "proper tail recursion by call-with-values #1"
'succeeded
(rec-call-with-values explosive-count))
;; call-with-values
(assert-equal? "proper tail recursion by call-with-values #2"
'succeeded
(rec-call-with-values-2 explosive-count))
;; with-exception-handler (not properly recursive because of underlying
;; dynamic-wind)
(if test-with-exception-handler?
(assert-equal? "improper tail recursion by with-exception-handler"
'succeeded
(rec-with-exception-handler explosive-count)))
;; guard (not properly recursive because of underlying dynamic-wind)
(if test-guard?
(assert-equal? "improper tail recursion by guard"
'succeeded
(rec-guard explosive-count)))
;; receive
(assert-equal? "proper tail recursion by receive"
'succeeded
(rec-receive explosive-count))
;; This test is succeeded if reported as follows.
;;
;; OK: 1 tests, ?? assertions, ?? successes, 0 failures, 0 errors
;; All normal tests have been passed.
;; <system dependent ulimit exceeded error message>
;; All tests finished successfully only if the message "All normal tests have been passed" and subsequent segmentation fault message are printed above.
(total-report)
(display "All normal tests have been passed.")
(newline)
;; test whether the explosive-count is actually explosive
(assert-equal? "improper infinite tail recursion"
'succeeded
(rec-improper-infinite explosive-count))
;; test failed if reached here
|