forked from factor/factor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmemoize-tests.factor
76 lines (58 loc) · 1.55 KB
/
memoize-tests.factor
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
! Copyright (C) 2007, 2009 Slava Pestov, Daniel Ehrenberg.
! See https://factorcode.org/license.txt for BSD license.
USING: math kernel memoize tools.test parser generalizations
prettyprint io.streams.string sequences eval namespaces see ;
IN: memoize.tests
MEMO: fib ( m -- n )
dup 1 <= [ drop 1 ] [ dup 1 - fib swap 2 - fib + ] if ;
MEMO: x ( a b c d e -- f g h i j )
[ 1 + ] 4 ndip ;
{ 89 } [ 10 fib ] unit-test
{
1 0 0 0 0
1 0 0 0 0
} [
0 0 0 0 0 x
0 0 0 0 0 x
] unit-test
MEMO: see-test ( a -- b ) reverse ;
{ "USING: sequences ;\nIN: memoize.tests\nMEMO: see-test ( a -- b ) reverse ;\n" }
[ [ \ see-test see ] with-string-writer ]
unit-test
{ } [ "IN: memoize.tests : fib ( -- ) ;" eval( -- ) ] unit-test
{ "IN: memoize.tests\n: fib ( -- ) ;\n" } [ [ \ fib see ] with-string-writer ] unit-test
[ sq ] ( a -- b ) memoize-quot "q" set
{ 9 } [ 3 "q" get call ] unit-test
SYMBOL: foo-counter
0 foo-counter set-global
MEMO: foo ( -- ) foo-counter counter drop ;
{ 0 1 1 1 } [
foo-counter get-global
foo
foo-counter get-global
foo
foo-counter get-global
foo
foo-counter get-global
] unit-test
SYMBOL: bar-counter
0 bar-counter set-global
MEMO: bar ( -- x ) bar-counter counter ;
{ 0 1 1 1 } [
bar-counter get-global
bar
bar
bar
] unit-test
SYMBOL: baz-counter
0 baz-counter set-global
MEMO: baz ( -- x ) baz-counter counter drop f ;
{ 0 f 1 f 1 f 1 } [
baz-counter get-global
baz
baz-counter get-global
baz
baz-counter get-global
baz
baz-counter get-global
] unit-test