]> gitweb.factorcode.org Git - factor.git/blob - core/continuations/continuations-tests.factor
hashtables: remove 2hashtable (not really used).
[factor.git] / core / continuations / continuations-tests.factor
1 USING: accessors continuations debugger eval hashtables io
2 kernel kernel.private math memory namespaces sequences
3 tools.test vectors words ;
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 "!!! The following error is part of the test" print
37
38 { } [ [ 6 [ 12 [ "2 car" ] ] ] print-error ] unit-test
39
40 "!!! The following error is part of the test" print
41
42 { } [ [ [ "2 car" ] eval ] try ] unit-test
43
44 [ f throw ] must-fail
45
46 ! Weird PowerPC bug.
47 { } [
48     [ "4" throw ] ignore-errors
49     gc
50     gc
51 ] unit-test
52
53 : don't-compile-me ( -- ) ;
54 : foo ( -- ) get-callstack "c" set don't-compile-me ;
55 : bar ( -- a b ) 1 foo 2 ;
56
57 << { don't-compile-me foo bar } [ t "no-compile" set-word-prop ] each >>
58
59 { 1 2 } [ bar ] unit-test
60
61 { t } [ \ bar def>> "c" get innermost-frame-executing = ] unit-test
62
63 { 1 } [ "c" get innermost-frame-scan ] unit-test
64
65 SYMBOL: always-counter
66 SYMBOL: error-counter
67
68 H{
69     { always-counter 0 }
70     { error-counter 0 }
71 } [
72
73     [ ] [ always-counter inc ] [ error-counter inc ] cleanup
74
75     [ 1 ] [ always-counter get ] unit-test
76     [ 0 ] [ error-counter get ] unit-test
77
78     [
79         [ "a" throw ]
80         [ always-counter inc ]
81         [ error-counter inc ] cleanup
82     ] [ "a" = ] must-fail-with
83
84     [ 2 ] [ always-counter get ] unit-test
85     [ 1 ] [ error-counter get ] unit-test
86
87     [
88         [ ]
89         [ always-counter inc "a" throw ]
90         [ error-counter inc ] cleanup
91     ] [ "a" = ] must-fail-with
92
93     [ 3 ] [ always-counter get ] unit-test
94     [ 1 ] [ error-counter get ] unit-test
95 ] with-variables
96
97 { } [ [ return ] with-return ] unit-test
98
99 [ { } [ ] attempt-all ] [ attempt-all-error? ] must-fail-with
100
101 { { 4 } } [ { 2 2 } [ + ] with-datastack ] unit-test
102
103 [ with-datastack ] must-infer