From c445fca58a96d72d5a7010f7cab90b1d28a9a03b Mon Sep 17 00:00:00 2001 From: Danius Michaelides Date: Tue, 11 Aug 2009 06:33:49 +0800 Subject: [PATCH] Implement Array#nitems(). Second try. Signed-off-by: Ted Reed --- Rakefile | 3 ++- src/classes/Array.pir | 16 ++++++++++++++++ t/array/nitems | 16 ++++++++++++++++ 3 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 t/array/nitems diff --git a/Rakefile b/Rakefile index b668b3a..32337c0 100644 --- a/Rakefile +++ b/Rakefile @@ -277,6 +277,7 @@ namespace :test do |ns| test "array/intersection.t" test "array/join.t" test "array/mathop.t" + test "array/nitems.t" test "array/pop.t" test "array/push.t" test "array/reject.t" @@ -290,7 +291,7 @@ namespace :test do |ns| test "array/values_at.t" test "array/warray.t" - task :all => [:array, :assign, :at, :clear, :collect, :compact, :concat, :delete, :empty, :equals, :fetch, :fill, :first, :flatten, :grep, :include, :index, :insert, :intersection, :join, :mathop, :pop, :push, :reject, :replace, :reverse, :shift, :slice, :sort, :to_s, :uniq, :values_at, :warray] + task :all => [:array, :assign, :at, :clear, :collect, :compact, :concat, :delete, :empty, :equals, :fetch, :fill, :first, :flatten, :grep, :include, :index, :insert, :intersection, :join, :mathop, :nitems, :pop, :push, :reject, :replace, :reverse, :shift, :slice, :sort, :to_s, :uniq, :values_at, :warray] end namespace :file do diff --git a/src/classes/Array.pir b/src/classes/Array.pir index 30cd251..98b67dc 100644 --- a/src/classes/Array.pir +++ b/src/classes/Array.pir @@ -1640,6 +1640,22 @@ The zip operator. # TODO: pass warning .end +.sub 'nitems' :method + .local int count + .local pmc it + count = 0 + it = iter self + iter_loop: + unless it goto done + $P0 = shift it + $I0 = isa $P0, "NilClass" + if $I0 goto iter_loop + inc count + goto iter_loop + done: + .return ( count ) +.end + .sub '_cmp' :vtable('cmp') :method .param pmc other .local int i, len, result diff --git a/t/array/nitems b/t/array/nitems new file mode 100644 index 0000000..5200e6a --- /dev/null +++ b/t/array/nitems @@ -0,0 +1,16 @@ +require 'Test' +include Test +plan 4 + +a = [ ] +is a.nitems(), 0, 'nitems, no items' + +a = [ 1, 2, 3, 4 ] +is a.nitems(), 4, 'nitems, 4 non-nil items' + +a = [ 1, 2, 3, nil ] +is a.nitems(), 3, 'nitems, 3 non-nil items, 1 nil' + +a = [ nil, nil, nil, nil ] +is a.nitems(), 0, 'nitems, 4 nil items' +