srfi-45-test2.scm 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. ; Run: kawa -f testsuite/srfi-45-test2.scm -e '(leak-test-1)'
  2. ; and so on up to '(leak-test-7)
  3. ; Should run contunuously without running out of memory or stack space;
  4. ; (leak-test-6) and (leak-test-7) do finish after running a while.
  5. ; If run as-is, does finish in a short time.
  6. ;=========================================================================
  7. ; Test leaks: All the leak tests should run in bounded space.
  8. ;=========================================================================
  9. ; Leak test 1: Infinite loop in bounded space.
  10. (define (loop) (lazy (loop)))
  11. (define (leak-test-1)
  12. (force (loop)))
  13. ;(leak-test-1)
  14. ;=========================================================================
  15. ; Leak test 2: Pending memos should not accumulate
  16. ; in shared structures.
  17. (define s2 (loop))
  18. (define (leak-test-2)
  19. (force s2))
  20. ;(leak-test-2)
  21. ;=========================================================================
  22. ; Leak test 3: Safely traversing infinite stream.
  23. (define (from n)
  24. (delay (cons n (from (+ n 1)))))
  25. (define (traverse s)
  26. (lazy (traverse (cdr (force s)))))
  27. (define (leak-test-3)
  28. (force (traverse (from 0)))) ;==> bounded space
  29. ;(leak-test-3)
  30. ;=========================================================================
  31. ; Leak test 4: Safely traversing infinite stream
  32. ; while pointer to head of result exists.
  33. (define s4 (traverse (from 0)))
  34. (define (leak-test-4)
  35. (force s4)) ;==> bounded space
  36. ;(leak-test-4)
  37. ;=========================================================================
  38. ; Convenient list deconstructor used below.
  39. (define-syntax match
  40. (syntax-rules ()
  41. ((match exp
  42. (() exp1)
  43. ((h . t) exp2))
  44. (let ((lst exp))
  45. (cond ((null? lst) exp1)
  46. ((pair? lst) (let ((h (car lst))
  47. (t (cdr lst)))
  48. exp2))
  49. (else 'match-error))))))
  50. ;========================================================================
  51. ; Leak test 5: Naive stream-filter should run in bounded space.
  52. ; Simplest case.
  53. (define (stream-filter p? s)
  54. (lazy (match (force s)
  55. (() (delay '()))
  56. ((h . t) (if (p? h)
  57. (delay (cons h (stream-filter p? t)))
  58. (stream-filter p? t))))))
  59. (define (leak-test-5)
  60. (force (stream-filter (lambda (n) (= n 10000000000))
  61. (from 0))))
  62. ;(leak=test-5) ;==> bounded space
  63. ;========================================================================
  64. ; Leak test 6: Another long traversal should run in bounded space.
  65. ; The stream-ref procedure below does not strictly need to be lazy.
  66. ; It is defined lazy for the purpose of testing safe compostion of
  67. ; lazy procedures in the times3 benchmark below (previous
  68. ; candidate solutions had failed this).
  69. (define (stream-ref s index)
  70. (lazy
  71. (match (force s)
  72. (() 'error)
  73. ((h . t) (if (zero? index)
  74. (delay h)
  75. (stream-ref t (- index 1)))))))
  76. ; Check that evenness is correctly implemented - should terminate:
  77. (force (stream-ref (stream-filter zero? (from 0))
  78. 0)) ;==> 0
  79. (display (force (stream-ref (from 0) 200))) (newline)
  80. ;; Output: 200
  81. (define s6 (stream-ref (from 0) 100000000))
  82. (define (leak-test-6)
  83. (force s6)) ;==> bounded space
  84. ;(leak-test-6)
  85. ;======================================================================
  86. ; Leak test 7: Infamous example from SRFI 40.
  87. (define (times3 n)
  88. (stream-ref (stream-filter
  89. (lambda (x) (zero? (modulo x n)))
  90. (from 0))
  91. 3))
  92. (display (force (times3 7)))
  93. (newline)
  94. ;; Output: 21
  95. (define (leak-test-7)
  96. (force (times3 100000000))) ;==> bounded space
  97. ;(leak-test-7)
  98. (display "Ok.") (newline)
  99. ;; Output: Ok.