future.test 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. ;;;; future.test --- Futures. -*- mode: scheme; coding: utf-8; -*-
  2. ;;;;
  3. ;;;; Ludovic Courtès <ludo@gnu.org>
  4. ;;;;
  5. ;;;; Copyright (C) 2010, 2012, 2013 Free Software Foundation, Inc.
  6. ;;;;
  7. ;;;; This library is free software; you can redistribute it and/or
  8. ;;;; modify it under the terms of the GNU Lesser General Public
  9. ;;;; License as published by the Free Software Foundation; either
  10. ;;;; version 3 of the License, or (at your option) any later version.
  11. ;;;;
  12. ;;;; This library is distributed in the hope that it will be useful,
  13. ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. ;;;; Lesser General Public License for more details.
  16. ;;;;
  17. ;;;; You should have received a copy of the GNU Lesser General Public
  18. ;;;; License along with this library; if not, write to the Free Software
  19. ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. (define-module (test-future)
  21. #:use-module (test-suite lib)
  22. #:use-module (ice-9 futures)
  23. #:use-module (srfi srfi-1)
  24. #:use-module (srfi srfi-26))
  25. (define specific-exception-key (gensym))
  26. (define specific-exception
  27. (cons specific-exception-key ".*"))
  28. (with-test-prefix "futures"
  29. (pass-if "make-future"
  30. (future? (make-future (lambda () #f))))
  31. (pass-if "future"
  32. (future? (future #t)))
  33. (pass-if "true"
  34. (touch (future #t)))
  35. (pass-if "(+ 2 3)"
  36. (= 5 (touch (future (+ 2 3)))))
  37. (pass-if "many"
  38. (equal? (iota 1234)
  39. (map touch
  40. (map (lambda (i)
  41. (make-future (lambda () i)))
  42. (iota 1234)))))
  43. (pass-if "touch several times"
  44. (let* ((f+ (unfold (cut >= <> 123)
  45. (lambda (i)
  46. (make-future
  47. (let ((x (1- i)))
  48. (lambda ()
  49. (set! x (1+ x))
  50. i))))
  51. 1+
  52. 0))
  53. (r1 (map touch f+))
  54. (r2 (map touch f+))
  55. (r3 (map touch f+)))
  56. (equal? (iota 123) r1 r2 r3)))
  57. (pass-if "nested"
  58. (= (touch (future (+ 2 (touch (future -2))
  59. (reduce + 0
  60. (map touch
  61. (map (lambda (i)
  62. (future i))
  63. (iota 123)))))))
  64. (reduce + 0 (iota 123))))
  65. (pass-if "multiple values"
  66. (let ((lst (iota 123)))
  67. (equal? (zip lst lst)
  68. (map (lambda (f)
  69. (call-with-values (cut touch f) list))
  70. (map (lambda (i)
  71. (future (values i i)))
  72. lst)))))
  73. (pass-if "no exception"
  74. (future? (future (throw 'foo 'bar))))
  75. (pass-if-exception "exception"
  76. specific-exception
  77. (touch (future (throw specific-exception-key 'test "thrown!")))))
  78. (with-test-prefix "nested futures"
  79. (pass-if-equal "simple" 2
  80. (touch (future (1+ (touch (future (1+ (touch (future 0)))))))))
  81. (pass-if-equal "loop" (map - (iota 1000))
  82. (let loop ((list (iota 1000)))
  83. (if (null? list)
  84. '()
  85. (cons (- (car list))
  86. (touch (future (loop (cdr list)))))))))