From: Horst Date: 2003-09-17T09:33:02+09:00 Subject: Re: RAA-stream implicit iterator maybe offbyone ? Simon Strandgaard wrote: > I am testing the 'stream' module.. > http://raa.ruby-lang.org/list.rhtml?name=stream > > > Q1: I have to overload the implicitstream.at_beginning_proc > with a offbyone value.. Why ? > Because the stream is at beginning if the next forward should give the first element in the stream which is 1 in the test case below. Thats why the initial value of the variable x should be zero and the at_beginning_proc should test this. Here ist my version of the stream: # a stream which is aquivalent (1..5).create_stream def new_implicit_stream s = Stream::ImplicitStream.new { |s| x = 0 s.at_beginning_proc = proc {x < 1} s.at_end_proc = proc {x == 5} s.forward_proc = proc {x += 1 } s.backward_proc = proc {|y| y = x; x -= 1; y } } end This version passes all tests that the collection stream passes. See tests TestStream.rb > Q2: Is this me which is thinking wrong, or is it a design flaw > in the 'stream' library? > When designing the library I also had to think more about this. Have you read the stream-doku contained in the RGL-Library which also includes the stream module? I regret that it is not included in tarball of the stream module. See http://rgl.sourceforge.net/files/stream_rb.html Cheers Horst > > I have made two methods which should return virtually the > same stream; [1, 2, 3, 4, 5, 6] > > > > > server> expand -t2 test_misc.rb > require 'test/unit' > require 'stream' > > class TestMisc < Test::Unit::TestCase > def mk_str16 > (1..6).create_stream > end > def mk_str16x > Stream::ImplicitStream.new{|s| > x = 1 > s.at_end_proc = proc {x == 6} > s.forward_proc = proc {x += 1} > s.at_beginning_proc = proc {x == 0} # todo: why is'nt it {x == 1} > s.backward_proc = proc {x -= 1} > } > end > def setup > @data = mk_str16.filtered{|x| x % 2 == 0} > @data += mk_str16x.filtered{|x| x % 2 != 0} > end > def test_toa > assert_equal([2, 4, 6, 1, 3, 5], @data.to_a) > end > def test_revtoa > assert_equal([5, 3, 1, 6, 4, 2], @data.reverse.to_a) > end > end > > if $0 == __FILE__ > require 'test/unit/ui/console/testrunner' > Test::Unit::UI::Console::TestRunner.run(TestMisc) > end > server> ruby test_misc.rb > Loaded suite TestMisc > Started > .. > Finished in 0.008114 seconds. > > 2 tests, 2 assertions, 0 failures, 0 errors > server> > > > > If I change the 'at_beginning_proc' to stop at '1' which I believe > is where it should stop, then I get this error: > > > server> ruby test_misc.rb > Loaded suite TestMisc > Started > FF > Finished in 0.009856 seconds. > > 1) Failure!!! > test_revtoa(TestMisc) [test_misc.rb:25]: > <[5, 3, 1, 6, 4, 2]> expected but was > <[5, 3, 6, 4, 2]> > > 2) Failure!!! > test_toa(TestMisc) [test_misc.rb:22]: > <[2, 4, 6, 1, 3, 5]> expected but was > <[2, 4, 6, 3, 5]> > > 2 tests, 2 assertions, 2 failures, 0 errors > server> > > > -- > Simon Strandgaard