Commit | Line | Data |
---|---|---|
0d4e6ca3 LC |
1 | ;;;; future.test --- Futures. -*- mode: scheme; coding: utf-8; -*- |
2 | ;;;; | |
3 | ;;;; Ludovic Courtès <ludo@gnu.org> | |
4 | ;;;; | |
3e529bf0 | 5 | ;;;; Copyright (C) 2010, 2012 Free Software Foundation, Inc. |
0d4e6ca3 LC |
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 | ||
21 | (define-module (test-future) | |
22 | #:use-module (test-suite lib) | |
23 | #:use-module (ice-9 futures) | |
24 | #:use-module (srfi srfi-1) | |
3e529bf0 LC |
25 | #:use-module (srfi srfi-26) |
26 | #:use-module (system base compile)) | |
0d4e6ca3 LC |
27 | |
28 | (define specific-exception-key (gensym)) | |
29 | ||
30 | (define specific-exception | |
31 | (cons specific-exception-key ".*")) | |
32 | ||
33 | \f | |
34 | (with-test-prefix "futures" | |
35 | ||
36 | (pass-if "make-future" | |
37 | (future? (make-future (lambda () #f)))) | |
38 | ||
39 | (pass-if "future" | |
40 | (future? (future #t))) | |
41 | ||
42 | (pass-if "true" | |
43 | (touch (future #t))) | |
44 | ||
45 | (pass-if "(+ 2 3)" | |
46 | (= 5 (touch (future (+ 2 3))))) | |
47 | ||
48 | (pass-if "many" | |
49 | (equal? (iota 1234) | |
50 | (map touch | |
51 | (map (lambda (i) | |
52 | (make-future (lambda () i))) | |
53 | (iota 1234))))) | |
54 | ||
55 | (pass-if "touch several times" | |
56 | (let* ((f+ (unfold (cut >= <> 123) | |
57 | (lambda (i) | |
58 | (make-future | |
59 | (let ((x (1- i))) | |
60 | (lambda () | |
61 | (set! x (1+ x)) | |
62 | i)))) | |
63 | 1+ | |
64 | 0)) | |
65 | (r1 (map touch f+)) | |
66 | (r2 (map touch f+)) | |
67 | (r3 (map touch f+))) | |
68 | (equal? (iota 123) r1 r2 r3))) | |
69 | ||
70 | (pass-if "nested" | |
71 | (= (touch (future (+ 2 (touch (future -2)) | |
72 | (reduce + 0 | |
73 | (map touch | |
74 | (map (lambda (i) | |
75 | (future i)) | |
76 | (iota 123))))))) | |
77 | (reduce + 0 (iota 123)))) | |
78 | ||
6c17f7bd LC |
79 | (pass-if "multiple values" |
80 | (let ((lst (iota 123))) | |
81 | (equal? (zip lst lst) | |
82 | (map (lambda (f) | |
83 | (call-with-values (cut touch f) list)) | |
84 | (map (lambda (i) | |
85 | (future (values i i))) | |
86 | lst))))) | |
87 | ||
0d4e6ca3 LC |
88 | (pass-if "no exception" |
89 | (future? (future (throw 'foo 'bar)))) | |
90 | ||
91 | (pass-if-exception "exception" | |
92 | specific-exception | |
93 | (touch (future (throw specific-exception-key 'test "thrown!"))))) | |
3e529bf0 LC |
94 | |
95 | (with-test-prefix "nested futures" | |
96 | ||
97 | (pass-if-equal "simple" 2 | |
98 | (touch (future (1+ (touch (future (1+ (touch (future 0))))))))) | |
99 | ||
100 | (pass-if-equal "loop" (map - (iota 1000)) | |
101 | ;; Compile to avoid stack overflows. | |
102 | (compile '(let loop ((list (iota 1000))) | |
103 | (if (null? list) | |
104 | '() | |
105 | (cons (- (car list)) | |
106 | (touch (future (loop (cdr list))))))) | |
107 | #:to 'value | |
108 | #:env (current-module)))) |