git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@6111 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
seki 2004-04-06 15:26:25 +00:00
Родитель 383964bc30
Коммит 3d359cecd8
4 изменённых файлов: 63 добавлений и 11 удалений

Просмотреть файл

@ -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>
* configure.in: check the size of time_t.

Просмотреть файл

@ -18,6 +18,8 @@ require 'thread'
# This is part of +drb+ (dRuby).
#
module Rinda
class RindaError < RuntimeError; end
class InvalidHashTupleKey < RindaError; end
class RequestCanceledError < ThreadError; end
class RequestExpiredError < ThreadError; end
@ -46,6 +48,10 @@ module Rinda
@tuple[k]
end
def fetch(k)
@tuple.fetch(k)
end
# Iterate through the tuple, yielding the index or key, and the
# value, thus ensuring arrays are iterated similarly to hashes.
def each # FIXME
@ -74,7 +80,8 @@ module Rinda
@tuple_size = hash[:size]
@tuple = Hash.new
hash.each do |k, v|
next unless String === k
next if k == :size
raise InvalidHashTupleKey unless String === k
@tuple[k] = v
end
end
@ -89,11 +96,16 @@ module Rinda
# matching any value in the corresponding position in the tuple.
def match(tuple)
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)
each do |k, v|
begin
it = tuple.fetch(k)
rescue
return false
end
next if v.nil?
return false unless (v === tuple[k] rescue false)
return false unless (v === it)
end
return true
end

Просмотреть файл

@ -89,6 +89,10 @@ module Rinda
@ary[key]
end
def fetch(key)
@ary.fetch(key)
end
# The size of the tuple.
def size
@ary.size

Просмотреть файл

@ -36,23 +36,51 @@ module TupleSpaceTestModule
tmpl = Rinda::Template.new({"message"=>String, "name"=>String})
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", :name=>1}))
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"=>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(tmpl.match({"message"=>"Hello", "name"=>"Foo"}))
assert(tmpl.match({"message"=>:symbol, "name"=>"Foo", 1=>2}))
assert(tmpl.match({"message"=>"Hi", "name"=>"Foo", :name=>1}))
assert(tmpl.match({"message"=>:symbol, "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"=>String, :size=>2})
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", :name=>1}))
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, :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
def test_00_DRbObject
@ -246,7 +274,7 @@ module TupleSpaceTestModule
end
assert_equal([], ary)
end
def test_cancel_01
entry = @ts.write([:removeme, 1])
assert_equal([[:removeme, 1]], @ts.read_all([nil, nil]))