From 3a48ab46888547056d2a4a088b8f05a4a8b216d1 Mon Sep 17 00:00:00 2001 From: Jon Harper Date: Mon, 29 Jul 2013 22:28:11 +0200 Subject: [PATCH] circular-while, fix of by 1 error; add tests --- basis/circular/circular-tests.factor | 20 ++++++++++++++++++++ basis/circular/circular.factor | 4 ++-- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/basis/circular/circular-tests.factor b/basis/circular/circular-tests.factor index 46a32be773..4dfc88f521 100644 --- a/basis/circular/circular-tests.factor +++ b/basis/circular/circular-tests.factor @@ -43,6 +43,26 @@ IN: circular.tests ] with each >array ] unit-test +[ V{ 1 2 3 } ] [ + { 1 2 3 } V{ } [ + [ push f ] curry circular-while + ] keep +] unit-test + +CONSTANT: test-sequence1 { t f f f } +[ V{ 1 2 3 1 } ] [ + { 1 2 3 } 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 } V{ } [ + [ [ push ] [ length 1 - test-sequence2 nth ] bi ] curry circular-while + ] keep +] unit-test + [ V{ 1 2 3 1 2 } ] [ { 1 2 3 } V{ } [ [ [ push ] [ length 5 < ] bi ] curry circular-while* diff --git a/basis/circular/circular.factor b/basis/circular/circular.factor index 1bd0c15995..c06e5ca531 100644 --- a/basis/circular/circular.factor +++ b/basis/circular/circular.factor @@ -60,14 +60,14 @@ TUPLE: circular-iterator { circular read-only } { n integer } { last-start integer } ; : ( circular -- obj ) - 0 0 circular-iterator boa ; inline + 0 -1 circular-iterator boa ; inline > ] [ 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)