]> gitweb.factorcode.org Git - factor.git/blobdiff - extra/sequences/extras/extras-tests.factor
sequences.extras: adding nth-index
[factor.git] / extra / sequences / extras / extras-tests.factor
index 15664c64b32990eb877ebdc931ec6804050f4587..53104befe7033f88fa843fa2a16b4dfef3bd71b0 100644 (file)
@@ -1,6 +1,5 @@
 USING: accessors arrays ascii io io.streams.string kernel make
-math math.vectors random sequences sequences.extras strings
-tools.test vectors vocabs ;
+math prettyprint sequences sequences.extras strings tools.test ;
 
 { V{ { 0 104 } { 2 108 } { 3 108 } } } [ "hello" [ even? ] find-all ] unit-test
 
@@ -54,13 +53,13 @@ tools.test vectors vocabs ;
 { { 2 4 6 } } [ { 1 2 3 4 5 6 } odd-indices ] unit-test
 
 { "a b c d e" }
-[ "a      b  \t \n \r  c   d \n    e   " [ blank? ] " " compact ] unit-test
+[ "a      b  \t \n \r  c   d \n    e   " [ ascii:blank? ] " " compact ] unit-test
 
 { " a b c d e " }
-[ " a      b  c   d    e   " [ blank? ] " " collapse ] unit-test
+[ " a      b  c   d    e   " [ ascii:blank? ] " " collapse ] unit-test
 
 { { "hello," " " "world!" " " " " } }
-[ "hello, world!  " [ blank? ] slice-when [ >string ] map ] unit-test
+[ "hello, world!  " [ ascii:blank? ] slice-when [ >string ] map ] unit-test
 
 { t }
 [ "abc" sequence>slice slice? ] unit-test
@@ -123,15 +122,15 @@ tools.test vectors vocabs ;
 { "ADEBFC" } [ { "ABC" "D" "EF" } round-robin >string ] unit-test
 
 { { } } [ "ABC" [ ] { } trim-as ] unit-test
-{ "ABC" } [ { 32 65 66 67 32 } [ blank? ] "" trim-as ] unit-test
+{ "ABC" } [ { 32 65 66 67 32 } [ ascii:blank? ] "" trim-as ] unit-test
 
-{ t } [ "ABC" dup [ blank? ] ?trim [ identity-hashcode ] same? ] unit-test
-{ "ABC" } [ " ABC " [ blank? ] ?trim ] unit-test
+{ t } [ "ABC" dup [ ascii:blank? ] ?trim [ identity-hashcode ] same? ] unit-test
+{ "ABC" } [ " ABC " [ ascii:blank? ] ?trim ] unit-test
 
-{ t } [ "ABC" dup [ blank? ] ?trim-head [ identity-hashcode ] same? ] unit-test
-{ t } [ "ABC" dup [ blank? ] ?trim-tail [ identity-hashcode ] same? ] unit-test
-{ "ABC " } [ " ABC " [ blank? ] ?trim-head ] unit-test
-{ " ABC" } [ " ABC " [ blank? ] ?trim-tail ] unit-test
+{ t } [ "ABC" dup [ ascii:blank? ] ?trim-head [ identity-hashcode ] same? ] unit-test
+{ t } [ "ABC" dup [ ascii:blank? ] ?trim-tail [ identity-hashcode ] same? ] unit-test
+{ "ABC " } [ " ABC " [ ascii:blank? ] ?trim-head ] unit-test
+{ " ABC" } [ " ABC " [ ascii:blank? ] ?trim-tail ] unit-test
 
 { "" } [ "" "" "" unsurround ] unit-test
 { "" } [ "  " " " " " unsurround ] unit-test
@@ -150,10 +149,12 @@ tools.test vectors vocabs ;
 { { } } [ { } <evens> >array ] unit-test
 { { 0 2 } } [ 4 <iota> <evens> >array ] unit-test
 { { 0 2 4 } } [ 5 <iota> <evens> >array ] unit-test
+{ "bbddff" } [ "abcdef" <evens> [ 1 + ] map! seq>> ] unit-test
 
 { { } } [ { } <odds> >array ] unit-test
 { { 1 3 } } [ 5 <iota> <odds> >array ] unit-test
 { { 1 3 5 } } [ 6 <iota> <odds> >array ] unit-test
+{ "acceeg" } [ "abcdef" <odds> [ 1 + ] map! seq>> ] unit-test
 
 { 1 } [ { 1 7 3 7 6 3 7 } arg-max ] unit-test
 { 2 } [ { 0 1 99 } arg-max ] unit-test
@@ -200,10 +201,6 @@ tools.test vectors vocabs ;
 { { 1 0 0 1 0 0 0 1 0 0 } }
 [ 1 { 0 3 7 } 10 0 <array> [ set-nths-unsafe ] keep ] unit-test
 
-{ V{ 1 } } [ 1 flatten1 ] unit-test
-{ { 1 2 3 } } [ { 1 2 3 } flatten1 ] unit-test
-{ { 1 2 3 { { 4 } } } } [ { 1 { 2 } { 3 { { 4 } } } } flatten1 ] unit-test
-
 { t 3 3 } [ 10 <iota> [ [ odd? ] [ 1 > ] bi* and ] map-find-index ] unit-test
 { f f f } [ 10 <iota> [ [ odd? ] [ 9 > ] bi* and ] map-find-index ] unit-test
 
@@ -213,10 +210,10 @@ tools.test vectors vocabs ;
 { "cdef" } [ 2 f "abcdef" subseq* ] unit-test
 { "cd" } [ -4 -2 "abcdef" subseq* ] unit-test
 
-{ "foo" "" } [ "foo" [ blank? ] cut-when ] unit-test
-{ "foo" " " } [ "foo " [ blank? ] cut-when ] unit-test
-{ "" " foo" } [ " foo" [ blank? ] cut-when ] unit-test
-{ "foo" " bar" } [ "foo bar" [ blank? ] cut-when ] unit-test
+{ "foo" "" } [ "foo" [ ascii:blank? ] cut-when ] unit-test
+{ "foo" " " } [ "foo " [ ascii:blank? ] cut-when ] unit-test
+{ "" " foo" } [ " foo" [ ascii:blank? ] cut-when ] unit-test
+{ "foo" " bar" } [ "foo bar" [ ascii:blank? ] cut-when ] unit-test
 
 { { 4 0 3 1 2 } } [ { 0 4 1 3 2 } 5 <iota> [ nth* ] curry map ] unit-test
 
@@ -236,25 +233,14 @@ tools.test vectors vocabs ;
 
 { 3/10 } [ 10 <iota> [ 3 < ] count* ] unit-test
 
-{ { 0 } } [ "ABA" "ABABA" start-all ] unit-test
-{ { 0 2 } } [ "ABA" "ABABA" start-all* ] unit-test
-{ { 0 3 } } [ "ABA" "ABAABA" start-all ] unit-test
-{ 1 } [ "ABA" "ABABA" count-subseq ] unit-test
-{ 2 } [ "ABA" "ABABA" count-subseq* ] unit-test
+{ { 0 } } [ "ABABA" "ABA" start-all ] unit-test
+{ { 0 2 } } [ "ABABA" "ABA" start-all* ] unit-test
+{ { 0 3 } } [ "ABAABA" "ABA" start-all ] unit-test
+{ 1 } [ "ABABA" "ABA" count-subseq ] unit-test
+{ 2 } [ "ABABA" "ABA" count-subseq* ] unit-test
 
 { 120000 } [ { 10 20 30 40 50 60 } 1 [ * ] 3 reduce-from ] unit-test
 
-{
-    {
-        { 2 4 }
-        { 3 6 }
-        { 4 8 }
-    }
-} [ { 2 3 4 } [ 2 * ] map-zip ] unit-test
-
-{ }
-[ "test:" all-words [ name>> over prepend ] map-zip 2drop ] unit-test
-
 { { 0 1 2 3 } } [ 8 <iota> [ 4 < ] take-while >array ] unit-test
 { { } } [ { 15 16 } [ 4 < ] take-while >array ] unit-test
 { { 0 1 2 } } [ 3 <iota> [ 4 < ] take-while >array ] unit-test
@@ -263,6 +249,82 @@ tools.test vectors vocabs ;
 { { 15 16 } } [ { 15 16 } [ 4 < ] drop-while >array ] unit-test
 { { } } [ 3 <iota> [ 4 < ] drop-while >array ] unit-test
 
-{ { } } [ { } ", " join-with ] unit-test
-{ { 1 } } [ { 1 } ", " join-with ] unit-test
-{ { 1 ", " 2 } } [ { 1 2 } ", " join-with ] unit-test
+{ { } } [ { } ", " interleaved ] unit-test
+{ { 1 } } [ { 1 } ", " interleaved ] unit-test
+{ { 1 ", " 2 } } [ { 1 2 } ", " interleaved ] unit-test
+{ "" } [ "" CHAR: _ interleaved ] unit-test
+{ "a" } [ "a" CHAR: _ interleaved ] unit-test
+{ "a_b" } [ "ab" CHAR: _ interleaved ] unit-test
+{ "a_b_c" } [ "abc" CHAR: _ interleaved ] unit-test
+{ "a_b_c_d" } [ "abcd" CHAR: _ interleaved ] unit-test
+
+{ 0 } [ { 1 2 3 4 } [ 5 > ] count-head ] unit-test
+{ 2 } [ { 1 2 3 4 } [ 3 < ] count-head ] unit-test
+{ 4 } [ { 1 2 3 4 } [ 5 < ] count-head ] unit-test
+
+{ 0 } [ { 1 2 3 4 } [ 5 > ] count-tail ] unit-test
+{ 2 } [ { 1 2 3 4 } [ 2 > ] count-tail ] unit-test
+{ 4 } [ { 1 2 3 4 } [ 5 < ] count-tail ] unit-test
+
+{ SBUF" aco" SBUF" ftr"  } [ SBUF" factor" dup [ even? ] extract! ] unit-test
+
+{ 25 5 1 } [ { 4 5 6 } [ sq ] [ 20 > ] find-pred ] unit-test
+{ f f f } [ { 4 5 6 } [ sq ] [ 200 > ] find-pred ] unit-test
+
+{ -1/0. } [ { } max-subarray-sum ] unit-test
+{ -2 } [ { -3 -2 } max-subarray-sum ] unit-test
+{ 7 } [ { 1 2 3 -4 5 } max-subarray-sum ] unit-test
+{ 6 } [ { 1 2 3 -4 1 1 } max-subarray-sum ] unit-test
+
+{ { 9 7 5 } } [ -1 -6 -2 10 <iota> <step-slice> >array ] unit-test
+{ { 9 7 } } [ -1 -5 -2 10 <iota> <step-slice> >array ] unit-test
+{ { 9 7 } } [ -1 -4 -2 10 <iota> <step-slice> >array ] unit-test
+{ { 9 } } [ -1 -3 -2 10 <iota> <step-slice> >array ] unit-test
+{ { } } [ -4 10 -2 10 <iota> <step-slice> >array ] unit-test
+{ { 6 8 } } [ -4 15 2 10 <iota> <step-slice> >array ] unit-test
+{ { 1 3 } } [ 1 4 2 10 <iota> <step-slice> >array ] unit-test
+{ { 1 3 } } [ 1 5 2 10 <iota> <step-slice> >array ] unit-test
+{ { 1 3 5 } } [ 1 6 2 10 <iota> <step-slice> >array ] unit-test
+
+{ { 102 306 1530 } } [
+    { 2 3 5 } [ swap [ * ] [ 100 + ] if* ] map-with-previous
+] unit-test
+
+{ { } } [
+    { } [ nip ] map-with-previous
+] unit-test
+
+{ { -1 2 -3 4 -5 } } [ { 1 2 3 4 5 } [ odd? ] [ neg ] map-if ] unit-test
+
+{ { { 100 0 } { 200 1 } { 300 2 } { 400 3 } } } [
+    { 100 200 300 400 } <zip-index> >array
+] unit-test
+
+{ } [
+    { } [ - . ] each-prior
+] unit-test
+
+{ } [
+    { 5 16 42 103 } [ - . ] each-prior
+] unit-test
+
+{ { } } [
+    { } [ - ] map-prior
+] unit-test
+
+{ V{ 5 11 26 61 } } [
+    V{ 5 16 42 103 } [ - ] map-prior
+] unit-test
+
+{ V{ } } [
+    { } [ - ] V{ } map-prior-as
+] unit-test
+
+{ { 5 11 26 61 } } [
+    V{ 5 16 42 103 } [ - ] { } map-prior-as
+] unit-test
+
+{ f } [ 0 CHAR: a "foo" nth-index ] unit-test
+{ 0 } [ 0 CHAR: a "abba" nth-index ] unit-test
+{ 3 } [ 1 CHAR: a "abba" nth-index ] unit-test
+{ f } [ 2 CHAR: a "abba" nth-index ] unit-test