1 ! Copyright (C) 2009 Doug Coleman.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors arrays assocs combinators.smart kernel locals
4 math sequences stack-checker tools.test ;
5 IN: combinators.smart.tests
8 10 [ 1 - ] [ 1 + ] bi ;
10 [ [ test-bi ] output>array ] must-infer
11 { { 9 11 } } [ [ test-bi ] output>array ] unit-test
13 [ { 9 11 } [ + ] input<sequence ] must-infer
14 { 20 } [ { 9 11 } [ + ] input<sequence ] unit-test
16 { 6 } [ [ 1 2 3 ] [ + ] reduce-outputs ] unit-test
18 [ [ 1 2 3 ] [ + ] reduce-outputs ] must-infer
20 { 6 } [ [ 1 2 3 ] sum-outputs ] unit-test
24 [ "a" "b" ] "" append-outputs-as
29 [ ] "" append-outputs-as
39 [ { 1 } { 2 } { 3 } ] B{ } append-outputs-as
43 : nested-smart-combo-test ( -- array )
44 [ [ 1 2 ] output>array [ 3 4 ] output>array ] output>array ;
46 \ nested-smart-combo-test def>> must-infer
48 { { { 1 2 } { 3 4 } } } [ nested-smart-combo-test ] unit-test
50 { 14 } [ [ 1 2 3 ] [ sq ] [ + ] map-reduce-outputs ] unit-test
52 { 2 3 } [ [ + ] preserving ] must-infer-as
54 { 2 0 } [ [ + ] nullary ] must-infer-as
56 { 2 2 } [ [ [ + ] nullary ] preserving ] must-infer-as
58 : smart-if-test ( a b -- b )
59 [ < ] [ swap - ] [ - ] smart-if ;
61 { 7 } [ 10 3 smart-if-test ] unit-test
62 { 16 } [ 25 41 smart-if-test ] unit-test
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
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
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
77 { -1 } [ 1 2 [ + odd? ] [ - ] smart-when* ] unit-test
78 { } [ 2 2 [ + odd? ] [ ] smart-unless* ] unit-test
80 { ( -- x ) } [ [ [ ] [ call ] curry output>array ] infer ] unit-test
82 :: map-reduce-test ( a b c -- d ) [ a b c ] [ a - ] [ b * + ] map-reduce-outputs ;
84 { 10 } [ 1 2 3 map-reduce-test ] unit-test
86 { ( x x -- x ) } [ [ curry inputs ] infer ] unit-test
88 { ( x -- x ) } [ [ [ curry ] curry inputs ] infer ] unit-test
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
94 : test-cleave>sequence ( obj -- seq ) { [ 1 + ] [ sq ] [ 1 - ] } V{ } cleave>sequence ;
95 \ test-cleave>sequence def>> must-infer
97 { V{ 34 1089 32 } } [ 33 test-cleave>sequence ] unit-test
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