[macruby-changes] [3351] MacRuby/trunk

source_changes at macosforge.org source_changes at macosforge.org
Tue Jan 26 16:14:26 PST 2010


Revision: 3351
          http://trac.macosforge.org/projects/ruby/changeset/3351
Author:   ernest.prabhakar at gmail.com
Date:     2010-01-26 16:14:25 -0800 (Tue, 26 Jan 2010)
Log Message:
-----------
Cleaned up dispatch queue_source

Modified Paths:
--------------
    MacRuby/trunk/lib/dispatch/queue.rb
    MacRuby/trunk/spec/macruby/library/dispatch/queue_spec.rb

Modified: MacRuby/trunk/lib/dispatch/queue.rb
===================================================================
--- MacRuby/trunk/lib/dispatch/queue.rb	2010-01-27 00:14:23 UTC (rev 3350)
+++ MacRuby/trunk/lib/dispatch/queue.rb	2010-01-27 00:14:25 UTC (rev 3351)
@@ -1,16 +1,12 @@
 module Dispatch
   class Queue
     # Combines +&block+ up to +stride+ times before passing to Queue::Apply
-    def stride(count, stride=1, &block)
-      sub_count = (count / stride).to_int
-      puts "\nsub_count: #{sub_count} (#{count} / #{stride})"
-      apply(sub_count) do |i|
-        i0 = i*stride
-        (i0..i0+stride).each { |j| "inner #{j}"; block.call(j) } #nested dispatch blocks fails
+    def stride(count, stride, &block)
+      n_strides = (count / stride).to_int
+      apply(n_strides) do |i|
+        (i*stride...(i+1)*stride).each { |j| block.call(j) }
       end
-      done = sub_count*stride;
-      puts "\ndone: #{done} (#{sub_count}*#{stride})"
-      (done..count).each { |j| p "inner #{j}"; block.call(j) }
+      (n_strides*stride...count).each { |j| block.call(j) }
     end
   end
 end

Modified: MacRuby/trunk/spec/macruby/library/dispatch/queue_spec.rb
===================================================================
--- MacRuby/trunk/spec/macruby/library/dispatch/queue_spec.rb	2010-01-27 00:14:23 UTC (rev 3350)
+++ MacRuby/trunk/spec/macruby/library/dispatch/queue_spec.rb	2010-01-27 00:14:25 UTC (rev 3351)
@@ -5,23 +5,45 @@
   describe "Dispatch::Queue" do
     before :each do
       @q = Dispatch::Queue.new('org.macruby.gcd_spec.prelude')
+      @count = 4
+      @sum = 0
     end
     
     describe "stride" do
-      it "accepts a count, stride and block and yields it that many times, with an index" do
-        @i = 0
-        @q.stride(10) { |j| @i += j; p "outer #{j}" }
-        @i.should == 45
-        @i = 0
-        @q.stride(10, 3) { |j| @i += j }
-        @i.should == 45
-        @i = 0
-        @q.stride(12, 3) { |j| @i += j }
-        @i.should == 66
-        @i = 42
-        @q.stride(0, 1) { |j| @i += 1 }
-        @i.should == 42
+      it "expects a count, stride and block " do
+        lambda { @q.stride(@count) { |j| @sum += 1 } }.should raise_error(ArgumentError)
+        lambda { @q.stride(@count, 1) }.should raise_error(NoMethodError)
       end
+
+      it "runs the block +count+ number of times" do
+        @q.stride(@count, 1) { |j| @sum += 1 }
+        @sum.should == @count
+      end
+
+      it "runs the block passing the current index" do
+        @q.stride(@count, 1) { |j| @sum += j }
+        @sum.should == (@count*(@count-1)/2)
+      end
+
+      it "does not run the block if the count is zero" do
+        @q.stride(0, 1) { |j| @sum += 1 }
+        @sum.should == 0
+      end
+      
+      it "properly combines blocks with even stride > 1" do
+        @q.stride(@count, 2) { |j| @sum += j }
+        @sum.should == (@count*(@count-1)/2)
+      end
+
+      it "properly combines blocks with uneven stride > 1" do
+        @q.stride(5, 2) { |j| @sum += j }
+        @sum.should == (5*(5-1)/2)
+      end
+
+      it "properly rounds stride fractions > 0.5" do
+        @q.stride(7, 4) { |j| @sum += j }
+        @sum.should == (7*(7-1)/2)
+      end
     end
   end
 end
\ No newline at end of file
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/macruby-changes/attachments/20100126/d710487b/attachment-0001.html>


More information about the macruby-changes mailing list