[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

RAA-stream implicit iterator maybe offbyone ?

Simon Strandgaard

9/14/2003 10:41:00 PM

I am testing the 'stream' module..
http://raa.ruby-lang.org/list.rhtml?n...


Q1: I have to overload the implicitstream.at_beginning_proc
with a offbyone value.. Why ?

Q2: Is this me which is thinking wrong, or is it a design flaw
in the 'stream' library?


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
2 Answers

Horst

9/17/2003 12:32:00 AM

0

Simon Strandgaard wrote:

> I am testing the ''stream'' module..
> http://raa.ruby-lang.org/list.rhtml?n...
>
>
> 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/stre...

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

Simon Strandgaard

9/17/2003 6:02:00 PM

0

On Wed, 17 Sep 2003 03:32:13 +0200, Horst wrote:

> Simon Strandgaard wrote:
>
>> I am testing the ''stream'' module..
>> http://raa.ruby-lang.org/list.rhtml?n...
>>
>>
>> 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.

OK, I were just wondering why. Now I see :-)

[snip]


>> 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/stre...

Perhaps you should add an example of a backward implicit stream, because it
is sligthly different from forward implicit streams ?


--
Simon Strandgaard