]> gitweb.factorcode.org Git - factor.git/commitdiff
circular-while, fix of by 1 error; add tests
authorJon Harper <jon.harper87@gmail.com>
Mon, 29 Jul 2013 20:28:11 +0000 (22:28 +0200)
committerJohn Benediktsson <mrjbq7@gmail.com>
Fri, 6 Sep 2013 16:57:43 +0000 (09:57 -0700)
basis/circular/circular-tests.factor
basis/circular/circular.factor

index 46a32be773e6cadcb200df415ffb064d776b6ee6..4dfc88f521754e565c69a4153ce58fa374d725f3 100644 (file)
@@ -43,6 +43,26 @@ IN: circular.tests
     ] with each >array
 ] unit-test
 
+[ V{ 1 2 3 } ] [
+    { 1 2 3 } <circular> V{ } [
+        [ push f ] curry circular-while
+    ] keep
+] unit-test
+
+CONSTANT: test-sequence1 { t f f f }
+[ V{ 1 2 3 1 } ] [
+    { 1 2 3 } <circular> V{ } [
+        [ [ push ] [ length 1 - test-sequence1 nth ] bi ] curry circular-while
+    ] keep
+] unit-test
+
+CONSTANT: test-sequence2 { t f t t f f t t t f f f }
+[ V{ 1 2 3 1 2 3 1 2 3 1 2 3 } ] [
+    { 1 2 3 } <circular> V{ } [
+        [ [ push ] [ length 1 - test-sequence2 nth ] bi ] curry circular-while
+    ] keep
+] unit-test
+
 [ V{ 1 2 3 1 2 } ] [
     { 1 2 3 } <circular> V{ } [
         [ [ push ] [ length 5 < ] bi ] curry circular-while*
index 1bd0c15995c0bf48d8e012e20bf6a1d7c7bc91ad..c06e5ca531c30c93d4f8d0f7f46879728716534a 100644 (file)
@@ -60,14 +60,14 @@ TUPLE: circular-iterator
     { circular read-only } { n integer } { last-start integer } ;
 
 : <circular-iterator> ( circular -- obj )
-    0 0 circular-iterator boa ; inline
+    0 -1 circular-iterator boa ; inline
 
 <PRIVATE
 
 : (circular-while) ( ... iterator quot: ( ... obj -- ... ? ) -- ... )
     [ [ [ n>> ] [ circular>> ] bi nth ] dip call ] 2keep
     rot [ [ dup n>> >>last-start ] dip ] when
-    over [ n>> ] [ [ last-start>> ] [ circular>> length ] bi + 1 - ] bi = [
+    over [ n>> ] [ [ last-start>> ] [ circular>> length ] bi + ] bi = [
         2drop
     ] [
         [ [ 1 + ] change-n ] dip (circular-while)