]> gitweb.factorcode.org Git - factor.git/blob - basis/combinators/smart/smart-tests.factor
factor: trim using lists
[factor.git] / basis / combinators / smart / smart-tests.factor
1 ! Copyright (C) 2009 Doug Coleman.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors arrays assocs combinators.smart kernel
4 math random sequences stack-checker tools.test ;
5 IN: combinators.smart.tests
6
7 : test-bi ( -- 9 11 )
8     10 [ 1 - ] [ 1 + ] bi ;
9
10 [ [ test-bi ] output>array ] must-infer
11 { { 9 11 } } [ [ test-bi ] output>array ] unit-test
12
13 [ { 9 11 } [ + ] input<sequence ] must-infer
14 { 20 } [ { 9 11 } [ + ] input<sequence ] unit-test
15
16 { 6 } [ [ 1 2 3 ] [ + ] reduce-outputs ] unit-test
17
18 [ [ 1 2 3 ] [ + ] reduce-outputs ] must-infer
19
20 { 6 } [ [ 1 2 3 ] sum-outputs ] unit-test
21
22 { "ab" }
23 [
24     [ "a" "b" ] "" append-outputs-as
25 ] unit-test
26
27 { "" }
28 [
29     [ ] "" append-outputs-as
30 ] unit-test
31
32 { { } }
33 [
34     [ ] append-outputs
35 ] unit-test
36
37 { B{ 1 2 3 } }
38 [
39     [ { 1 } { 2 } { 3 } ] B{ } append-outputs-as
40 ] unit-test
41
42 ! Test nesting
43 : nested-smart-combo-test ( -- array )
44     [ [ 1 2 ] output>array [ 3 4 ] output>array ] output>array ;
45
46 \ nested-smart-combo-test def>> must-infer
47
48 { { { 1 2 } { 3 4 } } } [ nested-smart-combo-test ] unit-test
49
50 { 14 } [ [ 1 2 3 ] [ sq ] [ + ] map-reduce-outputs ] unit-test
51
52 { 2 3 } [ [ + ] preserving ] must-infer-as
53
54 { 2 0 } [ [ + ] nullary ] must-infer-as
55
56 { 2 2 } [ [ [ + ] nullary ] preserving ] must-infer-as
57
58 : smart-if-test ( a b -- b )
59     [ < ] [ swap - ] [ - ] smart-if ;
60
61 { 7 } [ 10 3 smart-if-test ] unit-test
62 { 16 } [ 25 41 smart-if-test ] unit-test
63
64 { { 1 2 } { 3 4 } { 5 6 } } [ 1 2 3 4 5 6 [ 2array ] 3 smart-apply ] unit-test
65 { { 1 2 3 } { 4 5 6 } } [ 1 2 3 4 5 6 [ 3array ] 2 smart-apply ] unit-test
66
67 { 4 } [ 2 [ even? ] [ 2 + ] smart-when ] unit-test
68 { 3 } [ 3 [ even? ] [ 2 + ] smart-when ] unit-test
69 { 4 } [ 2 [ odd? ] [ 2 + ] smart-unless ] unit-test
70 { 3 } [ 3 [ odd? ] [ 2 + ] smart-unless ] unit-test
71
72 { 4 } [ 2 [ even? ] [ 2 + ] smart-when* ] unit-test
73 { } [ 3 [ even? ] [ 2 + ] smart-when* ] unit-test
74 { 3 } [ 2 [ odd? ] [ 3 ] smart-unless* ] unit-test
75 { 3 } [ 3 [ odd? ] [ 5 ] smart-unless* ] unit-test
76
77 { -1 } [ 1 2 [ + odd? ] [ - ] smart-when* ] unit-test
78 { } [ 2 2 [ + odd? ] [ ] smart-unless* ] unit-test
79
80 { ( -- x ) } [ [ [ ] [ call ] curry output>array ] infer ] unit-test
81
82 :: map-reduce-test ( a b c -- d ) [ a b c ] [ a - ] [ b * + ] map-reduce-outputs ;
83
84 { 10 } [ 1 2 3 map-reduce-test ] unit-test
85
86 { ( x x -- x ) } [ [ curry inputs ] infer ] unit-test
87
88 { ( x -- x ) } [ [ [ curry ] curry inputs ] infer ] unit-test
89
90 { 1 1 1 } [ 1 3 [ ] smart-with times ] unit-test
91 { "BCD" } [ 1 "ABC" [ + ] smart-with map ] unit-test
92 { H{ { 1 2 } } } [ 1 H{ { 1 2 } { 3 4 } } [ drop = ] smart-with assoc-filter ] unit-test
93
94 : test-cleave>sequence ( obj -- seq )  { [ 1 + ] [ sq ] [ 1 - ] } V{ } cleave>sequence ;
95 \ test-cleave>sequence def>> must-infer
96
97 { V{ 34 1089 32 } } [ 33 test-cleave>sequence ] unit-test
98
99 { 60 6000 } [
100     { 10 20 30 } {
101         { 0 [ + ] }
102         { 1 [ * ] }
103     } smart-reduce
104 ] unit-test
105
106 { 1400 60 } [
107     { 10 20 30 } {
108         { [ sq ] [ + ] }
109         { [ ] [ + ] }
110     } smart-map-reduce
111 ] unit-test
112
113 { 0 12 } [
114     { 1 2 3 } dup {
115         { 0 [ - + ] }
116         { 0 [ + + ] }
117     } smart-2reduce
118 ] unit-test
119
120 { 36 12 } [
121     { 1 2 3 } dup {
122         { [ * ] [ * ] }
123         { [ + ] [ + ] }
124     } smart-2map-reduce
125 ] unit-test
126
127 { { 1 2 3 4 } 5 6 } [ [ 1 2 3 4 5 6 ] 2 output>array-n ] unit-test
128 { { } 5 6 } [ [ 5 6 ] 2 output>array-n ] unit-test
129 { { 1 2 } 3 4 5 6 } [ [ 1 2 3 4 5 6 ] 4 output>array-n ] unit-test
130
131 { t } [ [ 10 random dup even? ] smart-loop odd? ] unit-test