]> gitweb.factorcode.org Git - factor.git/blob - basis/sequences/generalizations/generalizations-tests.factor
Update some copyright headers to follow the current convention
[factor.git] / basis / sequences / generalizations / generalizations-tests.factor
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
6
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
12
13 { [ 1 2 ] } [ 1 2 2 [ ] nsequence ] unit-test
14
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
17
18 [ 4 nappend ] must-infer
19 [ 4 { } nappend-as ] must-infer
20
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 )
38     [ dup zero? not ]
39     [ [ 2 - ] [ ] [ 1 - ] tri ] { } B{ } 2 nproduce-as
40     [ drop ] 2dip ;
41 : nproduce-test ( n -- a b )
42     [ dup zero? not ]
43     [ [ 2 - ] [ ] [ 1 - ] tri ] 2 nproduce
44     [ drop ] 2dip ;
45
46 { "A1a!
47 B2b@
48 C3c#
49 D4d$
50 " } [
51     { "A" "B" "C" "D" }
52     { "1" "2" "3" "4" }
53     { "a" "b" "c" "d" }
54     { "!" "@" "#" "$" }
55     [ neach-test ] with-string-writer
56 ] unit-test
57
58 { { "A1a!" "B2b@" "C3c#" "D4d$" } }
59 [
60     { "A" "B" "C" "D" }
61     { "1" "2" "3" "4" }
62     { "a" "b" "c" "d" }
63     { "!" "@" "#" "$" }
64     nmap-test
65 ] unit-test
66
67 { [ "A1a!" "B2b@" "C3c#" "D4d$" ] }
68 [
69     { "A" "B" "C" "D" }
70     { "1" "2" "3" "4" }
71     { "a" "b" "c" "d" }
72     { "!" "@" "#" "$" }
73     nmap-as-test
74 ] unit-test
75
76 {
77     { "A" "B" "C" "D" }
78     { "1" "2" "3" "4" }
79     { "a!" "b@" "c#" "d$" }
80 } [
81     { "A" "B" "C" "D" }
82     { "1" "2" "3" "4" }
83     { "a" "b" "c" "d" }
84     { "!" "@" "#" "$" }
85     mnmap-3-test
86 ] unit-test
87
88 {
89     { "A1" "B2" "C3" "D4" }
90     { "a!" "b@" "c#" "d$" }
91 } [
92     { "A" "B" "C" "D" }
93     { "1" "2" "3" "4" }
94     { "a" "b" "c" "d" }
95     { "!" "@" "#" "$" }
96     mnmap-2-test
97 ] unit-test
98
99 {
100     { "A1" "B2" "C3" "D4" }
101     [ "a!" "b@" "c#" "d$" ]
102 } [
103     { "A" "B" "C" "D" }
104     { "1" "2" "3" "4" }
105     { "a" "b" "c" "d" }
106     { "!" "@" "#" "$" }
107     mnmap-as-test
108 ] unit-test
109
110 { { "A1a!" "B2b@" "C3c#" "D4d$" } }
111 [
112     { "A" "B" "C" "D" }
113     { "1" "2" "3" "4" }
114     { "a" "b" "c" "d" }
115     { "!" "@" "#" "$" }
116     mnmap-1-test
117 ] unit-test
118
119 { "A1a!
120 B2b@
121 C3c#
122 D4d$
123 " } [
124     { "A" "B" "C" "D" }
125     { "1" "2" "3" "4" }
126     { "a" "b" "c" "d" }
127     { "!" "@" "#" "$" }
128     [ mnmap-0-test ] with-string-writer
129 ] unit-test
130
131 { { 10 8 6 4 2 } B{ 9 7 5 3 1 } }
132 [ 10 nproduce-as-test ] unit-test
133
134 { { 10 8 6 4 2 } { 9 7 5 3 1 } }
135 [ 10 nproduce-test ] unit-test
136
137 { 45 } [
138     { 1 2 3 } { 4 5 6 } { 7 8 9 } [ + + ] [ + ] 3 nmap-reduce
139 ] unit-test
140
141 { t } [
142     { 1 3 5 } { 2 4 6 } { 4 8 12 } [ + + odd? ] 3 nall?
143 ] unit-test
144
145 { t } [
146     { 2 4 5 } { 4 6 7 } { 6 8 9 }
147     [ [ odd? ] tri@ and and ] 3 nany?
148 ] unit-test
149
150 { f } [
151     { 1 2 3 } { 4 5 6 } { 7 8 9 }
152     [ [ odd? ] tri@ and and ] 3 nany?
153 ] unit-test