]> gitweb.factorcode.org Git - factor.git/blob - core/continuations/continuations-tests.factor
core: Rename iota to <iota> so we can have TUPLE: iota ... ; instead of TUPLE: iota...
[factor.git] / core / continuations / continuations-tests.factor
1 USING: accessors continuations debugger eval io kernel kernel.private
2 math math.ratios memory namespaces sequences tools.test vectors words
3 ;
4 IN: continuations.tests
5
6 : (callcc1-test) ( n obj -- n' obj )
7     [ 1 - dup ] dip ?push
8     over 0 = [ "test-cc" get continue-with ] when
9     (callcc1-test) ;
10
11 : callcc1-test ( x -- list )
12     [
13         "test-cc" set V{ } clone (callcc1-test)
14     ] callcc1 nip ;
15
16 : callcc-namespace-test ( -- ? )
17     [
18         "test-cc" set
19         5 "x" set
20         H{ } clone [
21             6 "x" set "test-cc" get continue
22         ] with-variables
23     ] callcc0 "x" get 5 = ;
24
25 { t } [ 10 callcc1-test 10 <iota> reverse >vector = ] unit-test
26 { t } [ callcc-namespace-test ] unit-test
27
28 [ 5 throw ] [ 5 = ] must-fail-with
29
30 { t } [
31     [ "Hello" throw ] ignore-errors
32     error get-global
33     "Hello" =
34 ] unit-test
35
36 { 4 f } [
37     [ 20 5 / ] [ division-by-zero? ] ignore-error/f
38     [ 20 0 / ] [ division-by-zero? ] ignore-error/f
39 ] unit-test
40
41 "!!! The following error is part of the test" print
42
43 { } [ [ 6 [ 12 [ "2 car" ] ] ] print-error ] unit-test
44
45 "!!! The following error is part of the test" print
46
47 { } [ [ [ "2 car" ] eval ] try ] unit-test
48
49 [ f throw ] must-fail
50
51 ! Weird PowerPC bug.
52 { } [
53     [ "4" throw ] ignore-errors
54     gc
55     gc
56 ] unit-test
57
58 : don't-compile-me ( -- ) ;
59 : foo ( -- ) get-callstack "c" set don't-compile-me ;
60 : bar ( -- a b ) 1 foo 2 ;
61
62 << { don't-compile-me foo bar } [ t "no-compile" set-word-prop ] each >>
63
64 { 1 2 } [ bar ] unit-test
65
66 { t } [ \ bar def>> "c" get innermost-frame-executing = ] unit-test
67
68 { 1 } [ "c" get innermost-frame-scan ] unit-test
69
70 SYMBOL: always-counter
71 SYMBOL: error-counter
72
73 H{
74     { always-counter 0 }
75     { error-counter 0 }
76 } [
77
78     [ ] [ always-counter inc ] [ error-counter inc ] cleanup
79
80     [ 1 ] [ always-counter get ] unit-test
81     [ 0 ] [ error-counter get ] unit-test
82
83     [
84         [ "a" throw ]
85         [ always-counter inc ]
86         [ error-counter inc ] cleanup
87     ] [ "a" = ] must-fail-with
88
89     [ 2 ] [ always-counter get ] unit-test
90     [ 1 ] [ error-counter get ] unit-test
91
92     [
93         [ ]
94         [ always-counter inc "a" throw ]
95         [ error-counter inc ] cleanup
96     ] [ "a" = ] must-fail-with
97
98     [ 3 ] [ always-counter get ] unit-test
99     [ 1 ] [ error-counter get ] unit-test
100 ] with-variables
101
102 { } [ [ return ] with-return ] unit-test
103
104 [ { } [ ] attempt-all ] [ attempt-all-error? ] must-fail-with
105
106 { { 4 } } [ { 2 2 } [ + ] with-datastack ] unit-test
107
108 [ with-datastack ] must-infer