1 ! Copyright (C) 2009 Joe Groff.
2 ! See https://factorcode.org/license.txt for BSD license.
3 USING: io io.streams.string kernel math sequences
4 sequences.generalizations tools.test ;
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 { } [ { } 0 ?firstn ] unit-test
14 { f } [ { } 1 ?firstn ] unit-test
15 { f f } [ { } 2 ?firstn ] unit-test
16 { 1 f } [ { 1 } 2 ?firstn ] unit-test
17 { 1 2 } [ { 1 2 } 2 ?firstn ] unit-test
18 { 1 2 } [ { 1 2 3 } 2 ?firstn ] unit-test
20 { [ 1 2 ] } [ 1 2 2 [ ] nsequence ] unit-test
21 { { 1 2 3 4 5 } } [ 1 2 3 4 5 { 0 0 0 0 0 } 5 (nsequence) ] unit-test
23 { { 1 2 3 4 } } [ { 1 } { 2 } { 3 } { 4 } 4 nappend ] unit-test
24 { V{ 1 2 3 4 } } [ { 1 } { 2 } { 3 } { 4 } 4 V{ } nappend-as ] unit-test
26 [ 4 nappend ] must-infer
27 [ 4 { } nappend-as ] must-infer
29 : neach-test ( a b c d -- )
30 [ 4 nappend print ] 4 neach ;
31 : nmap-test ( a b c d -- e )
32 [ 4 nappend ] 4 nmap ;
33 : nmap-as-test ( a b c d -- e )
34 [ 4 nappend ] [ ] 4 nmap-as ;
35 : mnmap-3-test ( a b c d -- e f g )
36 [ append ] 4 3 mnmap ;
37 : mnmap-2-test ( a b c d -- e f )
38 [ [ append ] 2bi@ ] 4 2 mnmap ;
39 : mnmap-as-test ( a b c d -- e f )
40 [ [ append ] 2bi@ ] { } [ ] 4 2 mnmap-as ;
41 : mnmap-1-test ( a b c d -- e )
42 [ 4 nappend ] 4 1 mnmap ;
43 : mnmap-0-test ( a b c d -- )
44 [ 4 nappend print ] 4 0 mnmap ;
45 : nproduce-as-test ( n -- a b )
47 [ [ 2 - ] [ ] [ 1 - ] tri ] { } B{ } 2 nproduce-as nipd ;
48 : nproduce-test ( n -- a b )
50 [ [ 2 - ] [ ] [ 1 - ] tri ] 2 nproduce nipd ;
61 [ neach-test ] with-string-writer
64 { { "A1a!" "B2b@" "C3c#" "D4d$" } }
73 { [ "A1a!" "B2b@" "C3c#" "D4d$" ] }
85 { "a!" "b@" "c#" "d$" }
95 { "A1" "B2" "C3" "D4" }
96 { "a!" "b@" "c#" "d$" }
106 { "A1" "B2" "C3" "D4" }
107 [ "a!" "b@" "c#" "d$" ]
116 { { "A1a!" "B2b@" "C3c#" "D4d$" } }
134 [ mnmap-0-test ] with-string-writer
137 { { 10 8 6 4 2 } B{ 9 7 5 3 1 } }
138 [ 10 nproduce-as-test ] unit-test
140 { { 10 8 6 4 2 } { 9 7 5 3 1 } }
141 [ 10 nproduce-test ] unit-test
144 { 1 2 3 } { 4 5 6 } { 7 8 9 } [ + + ] [ + ] 3 nmap-reduce
148 { 1 3 5 } { 2 4 6 } { 4 8 12 } [ + + odd? ] 3 nall?
152 { 2 4 5 } { 4 6 7 } { 6 8 9 }
153 [ [ odd? ] tri@ and and ] 3 nany?
157 { 1 2 3 } { 4 5 6 } { 7 8 9 }
158 [ [ odd? ] tri@ and and ] 3 nany?