1 ! Copyright (C) 2009 Joe Groff.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: tools.test generalizations kernel math arrays sequences
4 sequences.generalizations ascii fry math.parser io io.streams.string ;
5 IN: sequences.generalizations.tests
7 { 1 2 3 4 } [ { 1 2 3 4 } 4 firstn ] unit-test
8 { { 1 2 3 4 } } [ 1 2 3 4 { f f f f } [ 4 set-firstn ] keep ] unit-test
9 [ 1 2 3 4 { f f f } [ 4 set-firstn ] keep ] must-fail
10 { } [ { } 0 firstn ] unit-test
11 { "a" } [ { "a" } 1 firstn ] unit-test
13 { [ 1 2 ] } [ 1 2 2 [ ] nsequence ] unit-test
15 { { 1 2 3 4 } } [ { 1 } { 2 } { 3 } { 4 } 4 nappend ] unit-test
16 { V{ 1 2 3 4 } } [ { 1 } { 2 } { 3 } { 4 } 4 V{ } nappend-as ] unit-test
18 [ 4 nappend ] must-infer
19 [ 4 { } nappend-as ] must-infer
21 : neach-test ( a b c d -- )
22 [ 4 nappend print ] 4 neach ;
23 : nmap-test ( a b c d -- e )
24 [ 4 nappend ] 4 nmap ;
25 : nmap-as-test ( a b c d -- e )
26 [ 4 nappend ] [ ] 4 nmap-as ;
27 : mnmap-3-test ( a b c d -- e f g )
28 [ append ] 4 3 mnmap ;
29 : mnmap-2-test ( a b c d -- e f )
30 [ [ append ] 2bi@ ] 4 2 mnmap ;
31 : mnmap-as-test ( a b c d -- e f )
32 [ [ append ] 2bi@ ] { } [ ] 4 2 mnmap-as ;
33 : mnmap-1-test ( a b c d -- e )
34 [ 4 nappend ] 4 1 mnmap ;
35 : mnmap-0-test ( a b c d -- )
36 [ 4 nappend print ] 4 0 mnmap ;
37 : nproduce-as-test ( n -- a b )
39 [ [ 2 - ] [ ] [ 1 - ] tri ] { } B{ } 2 nproduce-as
41 : nproduce-test ( n -- a b )
43 [ [ 2 - ] [ ] [ 1 - ] tri ] 2 nproduce
55 [ neach-test ] with-string-writer
58 { { "A1a!" "B2b@" "C3c#" "D4d$" } }
67 { [ "A1a!" "B2b@" "C3c#" "D4d$" ] }
79 { "a!" "b@" "c#" "d$" }
89 { "A1" "B2" "C3" "D4" }
90 { "a!" "b@" "c#" "d$" }
100 { "A1" "B2" "C3" "D4" }
101 [ "a!" "b@" "c#" "d$" ]
110 { { "A1a!" "B2b@" "C3c#" "D4d$" } }
128 [ mnmap-0-test ] with-string-writer
131 { { 10 8 6 4 2 } B{ 9 7 5 3 1 } }
132 [ 10 nproduce-as-test ] unit-test
134 { { 10 8 6 4 2 } { 9 7 5 3 1 } }
135 [ 10 nproduce-test ] unit-test
138 { 1 2 3 } { 4 5 6 } { 7 8 9 } [ + + ] [ + ] 3 nmap-reduce
142 { 1 3 5 } { 2 4 6 } { 4 8 12 } [ + + odd? ] 3 nall?
146 { 2 4 5 } { 4 6 7 } { 6 8 9 }
147 [ [ odd? ] tri@ and and ] 3 nany?
151 { 1 2 3 } { 4 5 6 } { 7 8 9 }
152 [ [ odd? ] tri@ and and ] 3 nany?