mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
fix hash tuple bug
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@6111 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
383964bc30
commit
3d359cecd8
4 changed files with 63 additions and 11 deletions
|
@ -1,3 +1,11 @@
|
||||||
|
Wed Apr 7 00:24:34 2004 Masatoshi SEKI <m_seki@mva.biglobe.ne.jp>
|
||||||
|
|
||||||
|
* lib/rinda/rinda.rb: fix hash tuple bug.
|
||||||
|
|
||||||
|
* lib/rinda/tuplespace.rb: ditto.
|
||||||
|
|
||||||
|
* test/rinda/test_rinda.rb
|
||||||
|
|
||||||
Tue Apr 6 16:46:09 2004 Tanaka Akira <akr@m17n.org>
|
Tue Apr 6 16:46:09 2004 Tanaka Akira <akr@m17n.org>
|
||||||
|
|
||||||
* configure.in: check the size of time_t.
|
* configure.in: check the size of time_t.
|
||||||
|
|
|
@ -18,6 +18,8 @@ require 'thread'
|
||||||
# This is part of +drb+ (dRuby).
|
# This is part of +drb+ (dRuby).
|
||||||
#
|
#
|
||||||
module Rinda
|
module Rinda
|
||||||
|
class RindaError < RuntimeError; end
|
||||||
|
class InvalidHashTupleKey < RindaError; end
|
||||||
class RequestCanceledError < ThreadError; end
|
class RequestCanceledError < ThreadError; end
|
||||||
class RequestExpiredError < ThreadError; end
|
class RequestExpiredError < ThreadError; end
|
||||||
|
|
||||||
|
@ -46,6 +48,10 @@ module Rinda
|
||||||
@tuple[k]
|
@tuple[k]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def fetch(k)
|
||||||
|
@tuple.fetch(k)
|
||||||
|
end
|
||||||
|
|
||||||
# Iterate through the tuple, yielding the index or key, and the
|
# Iterate through the tuple, yielding the index or key, and the
|
||||||
# value, thus ensuring arrays are iterated similarly to hashes.
|
# value, thus ensuring arrays are iterated similarly to hashes.
|
||||||
def each # FIXME
|
def each # FIXME
|
||||||
|
@ -74,7 +80,8 @@ module Rinda
|
||||||
@tuple_size = hash[:size]
|
@tuple_size = hash[:size]
|
||||||
@tuple = Hash.new
|
@tuple = Hash.new
|
||||||
hash.each do |k, v|
|
hash.each do |k, v|
|
||||||
next unless String === k
|
next if k == :size
|
||||||
|
raise InvalidHashTupleKey unless String === k
|
||||||
@tuple[k] = v
|
@tuple[k] = v
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -89,11 +96,16 @@ module Rinda
|
||||||
# matching any value in the corresponding position in the tuple.
|
# matching any value in the corresponding position in the tuple.
|
||||||
def match(tuple)
|
def match(tuple)
|
||||||
return false unless tuple.respond_to?(:size)
|
return false unless tuple.respond_to?(:size)
|
||||||
return false unless tuple.respond_to?(:[])
|
return false unless tuple.respond_to?(:fetch)
|
||||||
return false if @tuple_size && (@tuple_size != tuple.size)
|
return false if @tuple_size && (@tuple_size != tuple.size)
|
||||||
each do |k, v|
|
each do |k, v|
|
||||||
|
begin
|
||||||
|
it = tuple.fetch(k)
|
||||||
|
rescue
|
||||||
|
return false
|
||||||
|
end
|
||||||
next if v.nil?
|
next if v.nil?
|
||||||
return false unless (v === tuple[k] rescue false)
|
return false unless (v === it)
|
||||||
end
|
end
|
||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
|
|
|
@ -89,6 +89,10 @@ module Rinda
|
||||||
@ary[key]
|
@ary[key]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def fetch(key)
|
||||||
|
@ary.fetch(key)
|
||||||
|
end
|
||||||
|
|
||||||
# The size of the tuple.
|
# The size of the tuple.
|
||||||
def size
|
def size
|
||||||
@ary.size
|
@ary.size
|
||||||
|
|
|
@ -36,23 +36,51 @@ module TupleSpaceTestModule
|
||||||
tmpl = Rinda::Template.new({"message"=>String, "name"=>String})
|
tmpl = Rinda::Template.new({"message"=>String, "name"=>String})
|
||||||
assert_equal(2, tmpl.size)
|
assert_equal(2, tmpl.size)
|
||||||
assert(tmpl.match({"message"=>"Hello", "name"=>"Foo"}))
|
assert(tmpl.match({"message"=>"Hello", "name"=>"Foo"}))
|
||||||
assert(tmpl.match({"message"=>"Hello", "name"=>"Foo", 1=>2}))
|
assert(tmpl.match({"message"=>"Hello", "name"=>"Foo", "1"=>2}))
|
||||||
assert(tmpl.match({"message"=>"Hi", "name"=>"Foo", :name=>1}))
|
assert(tmpl.match({"message"=>"Hi", "name"=>"Foo", "age"=>1}))
|
||||||
assert(!tmpl.match({"message"=>"Hello", "no_name"=>"Foo"}))
|
assert(!tmpl.match({"message"=>"Hello", "no_name"=>"Foo"}))
|
||||||
|
|
||||||
tmpl = Rinda::Template.new({:message=>String, "name"=>String})
|
assert_raises(Rinda::InvalidHashTupleKey) do
|
||||||
|
tmpl = Rinda::Template.new({:message=>String, "name"=>String})
|
||||||
|
end
|
||||||
|
tmpl = Rinda::Template.new({"name"=>String})
|
||||||
assert_equal(1, tmpl.size)
|
assert_equal(1, tmpl.size)
|
||||||
assert(tmpl.match({"message"=>"Hello", "name"=>"Foo"}))
|
assert(tmpl.match({"message"=>"Hello", "name"=>"Foo"}))
|
||||||
assert(tmpl.match({"message"=>:symbol, "name"=>"Foo", 1=>2}))
|
assert(tmpl.match({"message"=>:symbol, "name"=>"Foo", "1"=>2}))
|
||||||
assert(tmpl.match({"message"=>"Hi", "name"=>"Foo", :name=>1}))
|
assert(tmpl.match({"message"=>"Hi", "name"=>"Foo", "age"=>1}))
|
||||||
assert(!tmpl.match({"message"=>"Hello", "no_name"=>"Foo"}))
|
assert(!tmpl.match({"message"=>"Hello", "no_name"=>"Foo"}))
|
||||||
|
|
||||||
tmpl = Rinda::Template.new({"message"=>String, "name"=>String, :size=>2})
|
tmpl = Rinda::Template.new({"message"=>String, "name"=>String, :size=>2})
|
||||||
assert_equal(2, tmpl.size)
|
assert_equal(2, tmpl.size)
|
||||||
assert(tmpl.match({"message"=>"Hello", "name"=>"Foo"}))
|
assert(tmpl.match({"message"=>"Hello", "name"=>"Foo"}))
|
||||||
assert(!tmpl.match({"message"=>"Hello", "name"=>"Foo", 1=>2}))
|
assert(!tmpl.match({"message"=>"Hello", "name"=>"Foo", "1"=>2}))
|
||||||
assert(!tmpl.match({"message"=>"Hi", "name"=>"Foo", :name=>1}))
|
assert(!tmpl.match({"message"=>"Hi", "name"=>"Foo", "age"=>1}))
|
||||||
assert(!tmpl.match({"message"=>"Hello", "no_name"=>"Foo"}))
|
assert(!tmpl.match({"message"=>"Hello", "no_name"=>"Foo"}))
|
||||||
|
|
||||||
|
tmpl = Rinda::Template.new({"message"=>String, :size=>2})
|
||||||
|
assert_equal(1, tmpl.size)
|
||||||
|
assert(tmpl.match({"message"=>"Hello", "name"=>"Foo"}))
|
||||||
|
assert(!tmpl.match({"message"=>"Hello", "name"=>"Foo", "1"=>2}))
|
||||||
|
assert(!tmpl.match({"message"=>"Hi", "name"=>"Foo", "age"=>1}))
|
||||||
|
assert(tmpl.match({"message"=>"Hello", "no_name"=>"Foo"}))
|
||||||
|
|
||||||
|
tmpl = Rinda::Template.new({"message"=>String, "name"=>nil})
|
||||||
|
assert_equal(2, tmpl.size)
|
||||||
|
assert(tmpl.match({"message"=>"Hello", "name"=>"Foo"}))
|
||||||
|
assert(tmpl.match({"message"=>"Hello", "name"=>"Foo", "1"=>2}))
|
||||||
|
assert(tmpl.match({"message"=>"Hi", "name"=>"Foo", "age"=>1}))
|
||||||
|
assert(!tmpl.match({"message"=>"Hello", "no_name"=>"Foo"}))
|
||||||
|
|
||||||
|
tmpl = Rinda::Template.new({:size=>2})
|
||||||
|
assert_equal(0, tmpl.size)
|
||||||
|
assert(tmpl.match({"message"=>"Hello", "name"=>"Foo"}))
|
||||||
|
assert(!tmpl.match({"message"=>"Hello", "name"=>"Foo", "1"=>2}))
|
||||||
|
assert(!tmpl.match({"message"=>"Hi", "name"=>"Foo", "age"=>1}))
|
||||||
|
assert(tmpl.match({"message"=>"Hello", "no_name"=>"Foo"}))
|
||||||
|
|
||||||
|
assert_raises(Rinda::InvalidHashTupleKey) do
|
||||||
|
@ts.write({:message=>String, "name"=>String})
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_00_DRbObject
|
def test_00_DRbObject
|
||||||
|
@ -246,7 +274,7 @@ module TupleSpaceTestModule
|
||||||
end
|
end
|
||||||
assert_equal([], ary)
|
assert_equal([], ary)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_cancel_01
|
def test_cancel_01
|
||||||
entry = @ts.write([:removeme, 1])
|
entry = @ts.write([:removeme, 1])
|
||||||
assert_equal([[:removeme, 1]], @ts.read_all([nil, nil]))
|
assert_equal([[:removeme, 1]], @ts.read_all([nil, nil]))
|
||||||
|
|
Loading…
Add table
Reference in a new issue