stream 0.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,49 @@
1
+ require "benchmark"
2
+ require "stream"
3
+ require "generator"
4
+
5
+ include Benchmark
6
+
7
+ a =(1..5000).to_a
8
+ s = a.create_stream
9
+ n = 10
10
+ g = Generator.new a
11
+
12
+ bm(15) do |test|
13
+ test.report("A.each:") do
14
+ n.times {a.each {|x| x}}
15
+ end
16
+ test.report("S.each:") do
17
+ n.times {s.each {|x| x}}
18
+ end
19
+ test.report("G.each:") do
20
+ n.times {g.each {|x| x}}
21
+ end
22
+
23
+ test.report("A.collect.first:") do
24
+ n.times {a.collect {|x| x}.collect {|x| x}.first}
25
+ end
26
+ test.report("S.collect.first:") do
27
+ n.times {s.collect {|x| x}.collect {|x| x}.first}
28
+ end
29
+ test.report("G.collect.first:") do
30
+ n.times {g.collect {|x| x}.collect {|x| x}.first}
31
+ end
32
+
33
+ test.report("A.reverse:") do
34
+ n.times {a.reverse.reverse.reverse.each {|x| x}}
35
+ end
36
+ test.report("S.reverse:") do
37
+ n.times {s.reverse.reverse.reverse.each {|x| x}}
38
+ end
39
+
40
+ test.report("A.collect:") do
41
+ n.times {a.collect {|x| x}.collect {|x| x}.each {|x| x}}
42
+ end
43
+ test.report("S.collect:") do
44
+ n.times {s.collect {|x| x}.collect {|x| x}.each {|x| x}}
45
+ end
46
+ test.report("G.collect:") do
47
+ n.times {g.collect {|x| x}.collect {|x| x}.each {|x| x}}
48
+ end
49
+ end
@@ -0,0 +1,24 @@
1
+ require 'test/unit'
2
+ require 'generator2stream'
3
+
4
+ class TestGenerator < Test::Unit::TestCase
5
+ include Stream
6
+
7
+ def test_generator
8
+ # Use WrappedStream to ensure that the stream protocol is used and
9
+ # not the Enumerable protocol.
10
+ g = Generator.new { |g|
11
+ for i in 1..3
12
+ g.yield i
13
+ end
14
+ g.yield 0
15
+ }
16
+ assert_equal([1, 2, 3, 0], WrappedStream.new(g).to_a)
17
+
18
+ # Generators are not reversable
19
+ assert_raises(NotImplementedError) {g.reverse.first}
20
+
21
+ a = (1..10).to_a
22
+ assert_equal(a, WrappedStream.new(Generator.new(a)).to_a)
23
+ end
24
+ end
@@ -0,0 +1,177 @@
1
+ require 'test/unit'
2
+ require 'stream'
3
+
4
+ module StreamExamples
5
+ def new_collection_stream; (1..5).create_stream; end
6
+
7
+ # a stream which is aquivalent to new_collection_stream
8
+ def new_implicit_stream
9
+ s = Stream::ImplicitStream.new { |s|
10
+ x = 0
11
+ s.at_beginning_proc = proc {x < 1}
12
+ s.at_end_proc = proc {x == 5}
13
+ s.forward_proc = proc {x += 1 }
14
+ s.backward_proc = proc {y = x; x -= 1; y }
15
+ }
16
+ end
17
+
18
+ def filtered_streams
19
+ [
20
+ new_collection_stream.filtered { |x| true },
21
+ new_collection_stream.filtered { |x| false },
22
+ new_collection_stream.filtered { |x| x > 3 },
23
+ new_collection_stream.filtered { |x| x % 2 == 0 },
24
+ new_collection_stream.filtered { |x| x % 2 == 0 }.filtered { |x| x > 3 }
25
+ ]
26
+ end
27
+
28
+ def new_concatenated_stream
29
+ [1, 2, 3].create_stream.concatenate_collected { |i|
30
+ [i,-i].create_stream
31
+ }
32
+ end
33
+
34
+ def all_examples
35
+ [
36
+ new_collection_stream,
37
+ new_implicit_stream,
38
+ new_collection_stream.remove_first,
39
+ new_collection_stream.remove_last,
40
+ new_collection_stream.remove_first.remove_last,
41
+ Stream::WrappedStream.new(new_collection_stream),
42
+ Stream::IntervalStream.new(6),
43
+ Stream::IntervalStream.new(1),
44
+ new_collection_stream.collect { |x| x * 2 },
45
+
46
+ # Some concatenated streams
47
+ Stream::EmptyStream.instance.concatenate,
48
+ new_concatenated_stream,
49
+ # concatenated inside concatenated
50
+ new_concatenated_stream + new_concatenated_stream,
51
+ new_collection_stream + Stream::EmptyStream.instance,
52
+ Stream::EmptyStream.instance + new_collection_stream
53
+ ].concat(filtered_streams)
54
+ end
55
+
56
+ private
57
+
58
+ def standard_tests_for(s)
59
+ arrayStream = s.to_a.create_stream # A collection stream that should be OK
60
+ assert_equal(s.to_a, arrayStream.to_a)
61
+ # Tests at end of stream
62
+ assert_equal(s.at_end?, arrayStream.at_end?)
63
+ assert_raises(Stream::EndOfStreamException) {s.forward}
64
+ assert_equal(s.at_beginning?, arrayStream.at_beginning?)
65
+ assert_equal(s.peek,s)
66
+ unless arrayStream.at_beginning?
67
+ assert_equal(arrayStream.current,s.current)
68
+ assert_equal(arrayStream.backward, s.backward)
69
+ end
70
+ assert_equal(arrayStream.at_end?, s.at_end?)
71
+
72
+ # Tests at begin of stream
73
+ s.set_to_begin; arrayStream.set_to_begin
74
+ assert_raises(Stream::EndOfStreamException) {s.backward}
75
+ assert_equal(s.at_beginning?,arrayStream.at_beginning?)
76
+ assert_equal(s.at_end?,arrayStream.at_end?)
77
+ assert_equal(s.current,s)
78
+ unless arrayStream.at_end?
79
+ assert_equal(s.peek,arrayStream.peek)
80
+ assert_equal(s.forward,arrayStream.forward)
81
+ assert_equal(s.current,arrayStream.current)
82
+ unless arrayStream.at_end?
83
+ assert_equal(s.peek,arrayStream.peek)
84
+ end
85
+ end
86
+ end
87
+
88
+ end
89
+
90
+ class TestStream < Test::Unit::TestCase
91
+ include StreamExamples
92
+
93
+ def test_enumerable
94
+ s = new_collection_stream
95
+ assert_equal([2, 4, 6, 8, 10], s.collect {|x| x*2}.entries)
96
+ assert_equal([2,4],s.select {|x| x[0]== 0})# even numbers
97
+ end
98
+
99
+ def test_collection_stream
100
+ s = new_collection_stream
101
+ assert_equal([1, 2, 3, 4, 5], s.entries)
102
+ assert(s.at_end?)
103
+ assert_raises(Stream::EndOfStreamException) {s.forward}
104
+ assert_equal(5, s.backward)
105
+ assert_equal(5, s.forward)
106
+ assert_equal(5, s.current)
107
+ assert_equal(s, s.peek)
108
+
109
+ s.set_to_begin
110
+ assert(s.at_beginning?)
111
+ assert_raises(Stream::EndOfStreamException) {s.backward}
112
+ assert_equal(s, s.current)
113
+ assert_equal(1, s.peek)
114
+ assert_equal(1, s.forward)
115
+ assert_equal(1, s.current)
116
+ assert_equal(2, s.peek)
117
+
118
+ # move_forward_until
119
+ assert_equal(4, s.move_forward_until { |x| x > 3 })
120
+ assert_equal(nil, s.move_forward_until { |x| x < 3 })
121
+ assert(s.at_end?)
122
+
123
+ s.set_to_begin
124
+ assert_equal(nil, s.move_forward_until { |x| x > 6 })
125
+ end
126
+
127
+ def test_standard_for_examples
128
+ all_examples.each do |exampleStream|
129
+ standard_tests_for(exampleStream)
130
+ end
131
+ end
132
+
133
+ def test_for_examples_reversed
134
+ all_examples.each do |exampleStream|
135
+ assert_equal(exampleStream.entries.reverse, exampleStream.reverse.entries)
136
+ assert_equal(exampleStream.entries, exampleStream.reverse.reverse.entries)
137
+ standard_tests_for(exampleStream.reverse)
138
+ standard_tests_for(exampleStream.reverse.reverse)
139
+ end
140
+ end
141
+
142
+ def test_filtered_stream
143
+ [(1..6).create_stream.filtered {|x| x % 2 == 0 }].each do |s|
144
+ standard_tests_for(s)
145
+ end
146
+ end
147
+
148
+ def test_interval_stream
149
+ s = Stream::IntervalStream.new 6
150
+ standard_tests_for(s)
151
+ assert_equal([0, 1, 2, 3, 4, 5], s.entries)
152
+ s.increment_stop
153
+ assert(!s.at_end?)
154
+ assert_equal([0, 1, 2, 3, 4, 5, 6], s.entries)
155
+ standard_tests_for(s)
156
+ end
157
+
158
+ def test_enumerable_protocol
159
+ s = [1, 2, 3, 4, 5].create_stream
160
+ assert(s.include?(2))
161
+ assert_equal(3,s.detect {|x| x > 2})
162
+ assert_equal(nil,s.detect {|x| x < 0})
163
+ assert_equal([1,2],s.find_all {|x| x < 3})
164
+ end
165
+
166
+ def test_modified_stream
167
+ a = [1,2,3]
168
+ assert_equal([2,3],a.create_stream.remove_first.to_a)
169
+ assert_equal([1,2],a.create_stream.remove_last.to_a)
170
+ assert_raises(Stream::EndOfStreamException) {
171
+ [1].create_stream.remove_last.forward
172
+ }
173
+ assert_raises(Stream::EndOfStreamException) {
174
+ [1].create_stream.remove_first.forward
175
+ }
176
+ end
177
+ end
metadata ADDED
@@ -0,0 +1,64 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: stream
3
+ version: !ruby/object:Gem::Version
4
+ version: "0.5"
5
+ platform: ruby
6
+ authors:
7
+ - Horst Duchene
8
+ autorequire: stream
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-11-24 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: " Module Stream defines an interface for external iterators.\n"
17
+ email: hd.at.clr@hduchene.de
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files: []
23
+
24
+ files:
25
+ - install.rb
26
+ - Rakefile
27
+ - README
28
+ - lib/generator2stream.rb
29
+ - lib/stream.rb
30
+ - test/bm.rb
31
+ - test/testgenerator.rb
32
+ - test/teststream.rb
33
+ - examples/examples.rb
34
+ - examples/streamtester.rb
35
+ has_rdoc: true
36
+ homepage: http://rgl.rubyforge.org
37
+ licenses: []
38
+
39
+ post_install_message:
40
+ rdoc_options: []
41
+
42
+ require_paths:
43
+ - lib
44
+ required_ruby_version: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: "0"
49
+ version:
50
+ required_rubygems_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: "0"
55
+ version:
56
+ requirements: []
57
+
58
+ rubyforge_project: rgl
59
+ rubygems_version: 1.3.5
60
+ signing_key:
61
+ specification_version: 3
62
+ summary: Stream - Extended External Iterators
63
+ test_files: []
64
+