Skip to content

Commit

Permalink
Implement Array#fetch() with test.
Browse files Browse the repository at this point in the history
Signed-off-by: Ted Reed <ted.reed@gmail.com>
  • Loading branch information
dtm authored and treed committed Jul 28, 2009
1 parent 166f0ee commit d830bad
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 2 deletions.
3 changes: 2 additions & 1 deletion Rakefile
Expand Up @@ -245,6 +245,7 @@ namespace :test do |ns|
test "array/delete.t"
test "array/empty.t"
test "array/equals.t"
test "array/fetch.t"
test "array/fill.t"
test "array/first.t"
test "array/flatten.t"
Expand All @@ -265,7 +266,7 @@ namespace :test do |ns|
test "array/values_at.t"
test "array/warray.t"

task :all => [:array, :at, :clear, :collect, :compact, :concat, :delete, :empty, :equals, :fill, :first, :flatten, :grep, :include, :index, :intersection, :join, :mathop, :pop, :reject, :reverse, :shift, :slice, :sort, :to_s, :uniq, :values_at, :warray]
task :all => [:array, :at, :clear, :collect, :compact, :concat, :delete, :empty, :equals, :fetch, :fill, :first, :flatten, :grep, :include, :index, :intersection, :join, :mathop, :pop, :reject, :reverse, :shift, :slice, :sort, :to_s, :uniq, :values_at, :warray]
end

namespace :file do
Expand Down
39 changes: 38 additions & 1 deletion src/classes/Array.pir
@@ -1,4 +1,4 @@
## $Id$
# $Id$

=head1 NAME

Expand Down Expand Up @@ -1448,6 +1448,43 @@ The zip operator.
.return (values)
.end

.sub 'fetch' :method
.param int index
.param pmc default_value :optional
.param int has_default_value :opt_flag
.param pmc default_block :optional :named('!BLOCK')
.param int has_default_block :opt_flag
.local int length

$I0 = has_default_block + has_default_value
if $I0 > 1 goto incorrect_args

length = elements self

if index >= length goto out_of_bounds
$I0 = index
if index >= 0 goto skip_negative
$I0 = index + length
if index < 0 goto out_of_bounds

skip_negative:
$P0=self[$I0]
.return ( $P0 )

out_of_bounds:
if has_default_block goto do_block # block supersedes default value
if has_default_value goto do_value
# TODO: throw exception IndexError
.return ( index )
do_value:
.return ( default_value )
do_block:
$P0=default_block( index )
.return ( $P0 )
incorrect_args:
# TODO: pass warning
.end

.sub '_cmp' :vtable('cmp') :method
.param pmc other
.local int i, len, result
Expand Down
24 changes: 24 additions & 0 deletions t/array/fetch.t
@@ -0,0 +1,24 @@
require 'Test'
include Test
plan 8

a = [ 1, 2, 3, 4 ]

is a.fetch(0), 1, 'fetch, valid lookup'
is a.fetch(3), 4, 'fetch, valid lookup'
# TODO is a.fetch(5) throws IndexError exception

is a.fetch(3, 5), 4, 'fetch, valid lookup, default value'
is a.fetch(5, 5), 5, 'fetch, out of bounds, default value'
is a.fetch(5, 'cat'), 'cat', 'fetch, out of bounds, default value'

b=a.fetch(3) {|x| x*x }
is b, 4, 'fetch, valid lookup, default block'

b=a.fetch(5) {|x| 'dog' }
is b, 'dog', 'fetch, out of bounds, default block'

b=a.fetch(5) {|x| x*x }
is b, 25, 'fetch, out of bounds, default block'


0 comments on commit d830bad

Please sign in to comment.