2012-11-11 01:39:08 -05:00
|
|
|
# -*- coding: us-ascii -*-
|
2003-09-04 12:18:59 -04:00
|
|
|
require 'test/unit'
|
2014-11-28 19:36:33 -05:00
|
|
|
EnvUtil.suppress_warning {require 'continuation'}
|
2003-09-04 12:18:59 -04:00
|
|
|
|
|
|
|
class TestHash < Test::Unit::TestCase
|
2006-12-31 10:02:22 -05:00
|
|
|
|
2003-09-04 12:18:59 -04:00
|
|
|
def test_hash
|
2013-07-29 09:34:05 -04:00
|
|
|
x = @cls[1=>2, 2=>4, 3=>6]
|
|
|
|
y = @cls[1=>2, 2=>4, 3=>6] # y = {1, 2, 2, 4, 3, 6} # 1.9 doesn't support
|
2003-09-05 11:15:43 -04:00
|
|
|
|
2003-10-27 21:27:06 -05:00
|
|
|
assert_equal(2, x[1])
|
2003-09-05 11:15:43 -04:00
|
|
|
|
|
|
|
assert(begin
|
2003-10-27 21:27:06 -05:00
|
|
|
for k,v in y
|
2003-09-04 12:18:59 -04:00
|
|
|
raise if k*2 != v
|
|
|
|
end
|
|
|
|
true
|
|
|
|
rescue
|
|
|
|
false
|
|
|
|
end)
|
2003-09-05 11:15:43 -04:00
|
|
|
|
2003-10-27 21:27:06 -05:00
|
|
|
assert_equal(3, x.length)
|
2013-04-09 03:21:50 -04:00
|
|
|
assert_send([x, :has_key?, 1])
|
|
|
|
assert_send([x, :has_value?, 4])
|
2003-10-27 21:27:06 -05:00
|
|
|
assert_equal([4,6], x.values_at(2,3))
|
|
|
|
assert_equal({1=>2, 2=>4, 3=>6}, x)
|
2003-09-05 11:15:43 -04:00
|
|
|
|
2003-10-27 21:27:06 -05:00
|
|
|
z = y.keys.join(":")
|
|
|
|
assert_equal("1:2:3", z)
|
2003-09-05 11:15:43 -04:00
|
|
|
|
2003-10-27 21:27:06 -05:00
|
|
|
z = y.values.join(":")
|
|
|
|
assert_equal("2:4:6", z)
|
|
|
|
assert_equal(x, y)
|
2003-09-05 11:15:43 -04:00
|
|
|
|
2003-10-27 21:27:06 -05:00
|
|
|
y.shift
|
|
|
|
assert_equal(2, y.length)
|
2003-09-05 11:15:43 -04:00
|
|
|
|
2003-10-27 21:27:06 -05:00
|
|
|
z = [1,2]
|
|
|
|
y[z] = 256
|
|
|
|
assert_equal(256, y[z])
|
2003-09-05 11:15:43 -04:00
|
|
|
|
2003-10-27 21:27:06 -05:00
|
|
|
x = Hash.new(0)
|
|
|
|
x[1] = 1
|
|
|
|
assert_equal(1, x[1])
|
|
|
|
assert_equal(0, x[2])
|
2003-09-05 11:15:43 -04:00
|
|
|
|
2003-10-27 21:27:06 -05:00
|
|
|
x = Hash.new([])
|
|
|
|
assert_equal([], x[22])
|
|
|
|
assert_same(x[22], x[22])
|
2003-09-05 11:15:43 -04:00
|
|
|
|
2003-10-27 21:27:06 -05:00
|
|
|
x = Hash.new{[]}
|
|
|
|
assert_equal([], x[22])
|
|
|
|
assert_not_same(x[22], x[22])
|
2003-09-05 11:15:43 -04:00
|
|
|
|
2010-01-25 18:12:50 -05:00
|
|
|
x = Hash.new{|h,kk| z = kk; h[kk] = kk*2}
|
2003-10-27 21:27:06 -05:00
|
|
|
z = 0
|
|
|
|
assert_equal(44, x[22])
|
|
|
|
assert_equal(22, z)
|
|
|
|
z = 0
|
|
|
|
assert_equal(44, x[22])
|
|
|
|
assert_equal(0, z)
|
|
|
|
x.default = 5
|
|
|
|
assert_equal(5, x[23])
|
2003-09-05 11:15:43 -04:00
|
|
|
|
2003-10-27 21:27:06 -05:00
|
|
|
x = Hash.new
|
|
|
|
def x.default(k)
|
2003-09-04 12:18:59 -04:00
|
|
|
$z = k
|
|
|
|
self[k] = k*2
|
|
|
|
end
|
|
|
|
$z = 0
|
2003-10-27 21:27:06 -05:00
|
|
|
assert_equal(44, x[22])
|
2003-09-05 11:15:43 -04:00
|
|
|
assert_equal(22, $z)
|
2003-09-04 12:18:59 -04:00
|
|
|
$z = 0
|
2003-10-27 21:27:06 -05:00
|
|
|
assert_equal(44, x[22])
|
2003-09-05 11:15:43 -04:00
|
|
|
assert_equal(0, $z)
|
2003-09-04 12:18:59 -04:00
|
|
|
end
|
2006-12-31 10:02:22 -05:00
|
|
|
|
|
|
|
# From rubicon
|
|
|
|
|
|
|
|
def setup
|
2012-04-23 23:46:55 -04:00
|
|
|
@cls ||= Hash
|
2006-12-31 10:02:22 -05:00
|
|
|
@h = @cls[
|
|
|
|
1 => 'one', 2 => 'two', 3 => 'three',
|
|
|
|
self => 'self', true => 'true', nil => 'nil',
|
|
|
|
'nil' => nil
|
|
|
|
]
|
2008-02-05 10:50:25 -05:00
|
|
|
@verbose = $VERBOSE
|
|
|
|
$VERBOSE = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def teardown
|
|
|
|
$VERBOSE = @verbose
|
2006-12-31 10:02:22 -05:00
|
|
|
end
|
|
|
|
|
2012-10-16 17:33:59 -04:00
|
|
|
def test_bad_initialize_copy
|
|
|
|
h = Class.new(Hash) {
|
|
|
|
def initialize_copy(h)
|
|
|
|
super(Object.new)
|
|
|
|
end
|
|
|
|
}.new
|
2013-10-09 04:41:13 -04:00
|
|
|
assert_raise(TypeError) { h.dup }
|
2012-10-16 17:33:59 -04:00
|
|
|
end
|
|
|
|
|
2013-07-29 03:51:23 -04:00
|
|
|
def test_clear_initialize_copy
|
|
|
|
h = @cls[1=>2]
|
|
|
|
h.instance_eval {initialize_copy({})}
|
|
|
|
assert_empty(h)
|
|
|
|
end
|
|
|
|
|
2014-02-21 06:42:03 -05:00
|
|
|
def test_self_initialize_copy
|
|
|
|
h = @cls[1=>2]
|
|
|
|
h.instance_eval {initialize_copy(h)}
|
|
|
|
assert_equal(2, h[1])
|
|
|
|
end
|
|
|
|
|
2012-10-17 20:24:00 -04:00
|
|
|
def test_dup_will_rehash
|
2013-07-29 09:34:05 -04:00
|
|
|
set1 = @cls[]
|
|
|
|
set2 = @cls[set1 => true]
|
2012-10-17 20:24:00 -04:00
|
|
|
|
|
|
|
set1[set1] = true
|
|
|
|
|
|
|
|
assert_equal set2, set2.dup
|
|
|
|
end
|
|
|
|
|
2006-12-31 10:02:22 -05:00
|
|
|
def test_s_AREF
|
|
|
|
h = @cls["a" => 100, "b" => 200]
|
|
|
|
assert_equal(100, h['a'])
|
|
|
|
assert_equal(200, h['b'])
|
|
|
|
assert_nil(h['c'])
|
|
|
|
|
|
|
|
h = @cls.[]("a" => 100, "b" => 200)
|
|
|
|
assert_equal(100, h['a'])
|
|
|
|
assert_equal(200, h['b'])
|
|
|
|
assert_nil(h['c'])
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_s_new
|
|
|
|
h = @cls.new
|
|
|
|
assert_instance_of(@cls, h)
|
|
|
|
assert_nil(h.default)
|
|
|
|
assert_nil(h['spurious'])
|
|
|
|
|
|
|
|
h = @cls.new('default')
|
|
|
|
assert_instance_of(@cls, h)
|
|
|
|
assert_equal('default', h.default)
|
|
|
|
assert_equal('default', h['spurious'])
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2006-12-31 10:02:22 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_AREF # '[]'
|
|
|
|
t = Time.now
|
|
|
|
h = @cls[
|
|
|
|
1 => 'one', 2 => 'two', 3 => 'three',
|
|
|
|
self => 'self', t => 'time', nil => 'nil',
|
|
|
|
'nil' => nil
|
|
|
|
]
|
|
|
|
|
|
|
|
assert_equal('one', h[1])
|
|
|
|
assert_equal('two', h[2])
|
|
|
|
assert_equal('three', h[3])
|
|
|
|
assert_equal('self', h[self])
|
|
|
|
assert_equal('time', h[t])
|
|
|
|
assert_equal('nil', h[nil])
|
|
|
|
assert_equal(nil, h['nil'])
|
|
|
|
assert_equal(nil, h['koala'])
|
|
|
|
|
|
|
|
h1 = h.dup
|
|
|
|
h1.default = :default
|
|
|
|
|
|
|
|
assert_equal('one', h1[1])
|
|
|
|
assert_equal('two', h1[2])
|
|
|
|
assert_equal('three', h1[3])
|
|
|
|
assert_equal('self', h1[self])
|
|
|
|
assert_equal('time', h1[t])
|
|
|
|
assert_equal('nil', h1[nil])
|
|
|
|
assert_equal(nil, h1['nil'])
|
|
|
|
assert_equal(:default, h1['koala'])
|
|
|
|
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_ASET # '[]='
|
|
|
|
t = Time.now
|
|
|
|
h = @cls.new
|
|
|
|
h[1] = 'one'
|
|
|
|
h[2] = 'two'
|
|
|
|
h[3] = 'three'
|
|
|
|
h[self] = 'self'
|
|
|
|
h[t] = 'time'
|
|
|
|
h[nil] = 'nil'
|
|
|
|
h['nil'] = nil
|
|
|
|
assert_equal('one', h[1])
|
|
|
|
assert_equal('two', h[2])
|
|
|
|
assert_equal('three', h[3])
|
|
|
|
assert_equal('self', h[self])
|
|
|
|
assert_equal('time', h[t])
|
|
|
|
assert_equal('nil', h[nil])
|
|
|
|
assert_equal(nil, h['nil'])
|
|
|
|
assert_equal(nil, h['koala'])
|
|
|
|
|
|
|
|
h[1] = 1
|
|
|
|
h[nil] = 99
|
|
|
|
h['nil'] = nil
|
|
|
|
z = [1,2]
|
|
|
|
h[z] = 256
|
|
|
|
assert_equal(1, h[1])
|
|
|
|
assert_equal('two', h[2])
|
|
|
|
assert_equal('three', h[3])
|
|
|
|
assert_equal('self', h[self])
|
|
|
|
assert_equal('time', h[t])
|
|
|
|
assert_equal(99, h[nil])
|
|
|
|
assert_equal(nil, h['nil'])
|
|
|
|
assert_equal(nil, h['koala'])
|
|
|
|
assert_equal(256, h[z])
|
|
|
|
end
|
|
|
|
|
2014-01-09 23:54:08 -05:00
|
|
|
def test_AREF_fstring_key
|
|
|
|
h = {"abc" => 1}
|
2014-09-09 07:22:23 -04:00
|
|
|
before = GC.stat(:total_allocated_objects)
|
2014-01-09 23:54:08 -05:00
|
|
|
5.times{ h["abc"] }
|
2014-09-09 07:22:23 -04:00
|
|
|
assert_equal before, GC.stat(:total_allocated_objects)
|
2014-01-09 23:54:08 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_ASET_fstring_key
|
|
|
|
a, b = {}, {}
|
|
|
|
assert_equal 1, a["abc"] = 1
|
|
|
|
assert_equal 1, b["abc"] = 1
|
|
|
|
assert_same a.keys[0], b.keys[0]
|
|
|
|
end
|
|
|
|
|
2013-12-07 20:52:28 -05:00
|
|
|
def test_NEWHASH_fstring_key
|
2013-11-27 00:28:55 -05:00
|
|
|
a = {"ABC" => :t}
|
|
|
|
b = {"ABC" => :t}
|
2013-11-27 01:03:37 -05:00
|
|
|
assert_same a.keys[0], b.keys[0]
|
2013-12-07 20:52:28 -05:00
|
|
|
assert_same "ABC".freeze, a.keys[0]
|
2013-11-27 00:28:55 -05:00
|
|
|
end
|
|
|
|
|
2006-12-31 10:02:22 -05:00
|
|
|
def test_EQUAL # '=='
|
|
|
|
h1 = @cls[ "a" => 1, "c" => 2 ]
|
|
|
|
h2 = @cls[ "a" => 1, "c" => 2, 7 => 35 ]
|
|
|
|
h3 = @cls[ "a" => 1, "c" => 2, 7 => 35 ]
|
|
|
|
h4 = @cls[ ]
|
2013-04-09 03:21:50 -04:00
|
|
|
assert_equal(h1, h1)
|
|
|
|
assert_equal(h2, h2)
|
|
|
|
assert_equal(h3, h3)
|
|
|
|
assert_equal(h4, h4)
|
|
|
|
assert_not_equal(h1, h2)
|
|
|
|
assert_equal(h2, h3)
|
|
|
|
assert_not_equal(h3, h4)
|
2006-12-31 10:02:22 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_clear
|
2013-04-09 03:21:50 -04:00
|
|
|
assert_operator(@h.size, :>, 0)
|
2006-12-31 10:02:22 -05:00
|
|
|
@h.clear
|
|
|
|
assert_equal(0, @h.size)
|
|
|
|
assert_nil(@h[1])
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_clone
|
|
|
|
for taint in [ false, true ]
|
2013-06-13 01:41:44 -04:00
|
|
|
for frozen in [ false, true ]
|
|
|
|
a = @h.clone
|
|
|
|
a.taint if taint
|
|
|
|
a.freeze if frozen
|
|
|
|
b = a.clone
|
|
|
|
|
|
|
|
assert_equal(a, b)
|
|
|
|
assert_not_same(a, b)
|
|
|
|
assert_equal(a.frozen?, b.frozen?)
|
|
|
|
assert_equal(a.tainted?, b.tainted?)
|
2006-12-31 10:02:22 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_default
|
|
|
|
assert_nil(@h.default)
|
|
|
|
h = @cls.new(:xyzzy)
|
|
|
|
assert_equal(:xyzzy, h.default)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_default=
|
|
|
|
assert_nil(@h.default)
|
|
|
|
@h.default = :xyzzy
|
|
|
|
assert_equal(:xyzzy, @h.default)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_delete
|
|
|
|
h1 = @cls[ 1 => 'one', 2 => 'two', true => 'true' ]
|
|
|
|
h2 = @cls[ 1 => 'one', 2 => 'two' ]
|
|
|
|
h3 = @cls[ 2 => 'two' ]
|
|
|
|
|
|
|
|
assert_equal('true', h1.delete(true))
|
|
|
|
assert_equal(h2, h1)
|
|
|
|
|
|
|
|
assert_equal('one', h1.delete(1))
|
|
|
|
assert_equal(h3, h1)
|
|
|
|
|
|
|
|
assert_equal('two', h1.delete(2))
|
|
|
|
assert_equal(@cls[], h1)
|
|
|
|
|
|
|
|
assert_nil(h1.delete(99))
|
|
|
|
assert_equal(@cls[], h1)
|
|
|
|
|
|
|
|
assert_equal('default 99', h1.delete(99) {|i| "default #{i}" })
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_delete_if
|
|
|
|
base = @cls[ 1 => 'one', 2 => false, true => 'true', 'cat' => 99 ]
|
|
|
|
h1 = @cls[ 1 => 'one', 2 => false, true => 'true' ]
|
|
|
|
h2 = @cls[ 2 => false, 'cat' => 99 ]
|
|
|
|
h3 = @cls[ 2 => false ]
|
|
|
|
|
|
|
|
h = base.dup
|
|
|
|
assert_equal(h, h.delete_if { false })
|
|
|
|
assert_equal(@cls[], h.delete_if { true })
|
|
|
|
|
|
|
|
h = base.dup
|
|
|
|
assert_equal(h1, h.delete_if {|k,v| k.instance_of?(String) })
|
|
|
|
assert_equal(h1, h)
|
|
|
|
|
|
|
|
h = base.dup
|
|
|
|
assert_equal(h2, h.delete_if {|k,v| v.instance_of?(String) })
|
|
|
|
assert_equal(h2, h)
|
|
|
|
|
|
|
|
h = base.dup
|
|
|
|
assert_equal(h3, h.delete_if {|k,v| v })
|
|
|
|
assert_equal(h3, h)
|
2007-08-15 00:50:12 -04:00
|
|
|
|
|
|
|
h = base.dup
|
|
|
|
n = 0
|
|
|
|
h.delete_if {|*a|
|
|
|
|
n += 1
|
|
|
|
assert_equal(2, a.size)
|
|
|
|
assert_equal(base[a[0]], a[1])
|
|
|
|
h.shift
|
|
|
|
true
|
|
|
|
}
|
|
|
|
assert_equal(base.size, n)
|
2006-12-31 10:02:22 -05:00
|
|
|
end
|
|
|
|
|
2010-03-03 12:33:45 -05:00
|
|
|
def test_keep_if
|
2013-07-29 09:34:05 -04:00
|
|
|
h = @cls[1=>2,3=>4,5=>6]
|
2010-03-03 04:44:44 -05:00
|
|
|
assert_equal({3=>4,5=>6}, h.keep_if {|k, v| k + v >= 7 })
|
2013-07-29 09:34:05 -04:00
|
|
|
h = @cls[1=>2,3=>4,5=>6]
|
2010-03-03 04:44:44 -05:00
|
|
|
assert_equal({1=>2,3=>4,5=>6}, h.keep_if{true})
|
|
|
|
end
|
|
|
|
|
2006-12-31 10:02:22 -05:00
|
|
|
def test_dup
|
|
|
|
for taint in [ false, true ]
|
2013-06-13 01:41:44 -04:00
|
|
|
for frozen in [ false, true ]
|
|
|
|
a = @h.dup
|
|
|
|
a.taint if taint
|
|
|
|
a.freeze if frozen
|
|
|
|
b = a.dup
|
|
|
|
|
|
|
|
assert_equal(a, b)
|
|
|
|
assert_not_same(a, b)
|
|
|
|
assert_equal(false, b.frozen?)
|
|
|
|
assert_equal(a.tainted?, b.tainted?)
|
2006-12-31 10:02:22 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-10-16 22:06:34 -04:00
|
|
|
def test_dup_equality
|
2013-07-29 09:34:05 -04:00
|
|
|
h = @cls['k' => 'v']
|
2012-10-16 22:06:34 -04:00
|
|
|
assert_equal(h, h.dup)
|
2013-07-29 09:34:05 -04:00
|
|
|
h1 = @cls[h => 1]
|
2012-10-16 22:06:34 -04:00
|
|
|
assert_equal(h1, h1.dup)
|
|
|
|
h[1] = 2
|
|
|
|
assert_equal(h1, h1.dup)
|
|
|
|
end
|
|
|
|
|
2006-12-31 10:02:22 -05:00
|
|
|
def test_each
|
|
|
|
count = 0
|
|
|
|
@cls[].each { |k, v| count + 1 }
|
|
|
|
assert_equal(0, count)
|
|
|
|
|
|
|
|
h = @h
|
|
|
|
h.each do |k, v|
|
|
|
|
assert_equal(v, h.delete(k))
|
|
|
|
end
|
|
|
|
assert_equal(@cls[], h)
|
2013-07-30 09:25:40 -04:00
|
|
|
|
|
|
|
h = @cls[]
|
|
|
|
h[1] = 1
|
|
|
|
h[2] = 2
|
|
|
|
assert_equal([[1,1],[2,2]], h.each.to_a)
|
2006-12-31 10:02:22 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_each_key
|
|
|
|
count = 0
|
|
|
|
@cls[].each_key { |k| count + 1 }
|
|
|
|
assert_equal(0, count)
|
|
|
|
|
|
|
|
h = @h
|
|
|
|
h.each_key do |k|
|
|
|
|
h.delete(k)
|
|
|
|
end
|
|
|
|
assert_equal(@cls[], h)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_each_pair
|
|
|
|
count = 0
|
|
|
|
@cls[].each_pair { |k, v| count + 1 }
|
|
|
|
assert_equal(0, count)
|
|
|
|
|
|
|
|
h = @h
|
|
|
|
h.each_pair do |k, v|
|
|
|
|
assert_equal(v, h.delete(k))
|
|
|
|
end
|
|
|
|
assert_equal(@cls[], h)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_each_value
|
|
|
|
res = []
|
|
|
|
@cls[].each_value { |v| res << v }
|
|
|
|
assert_equal(0, [].length)
|
|
|
|
|
|
|
|
@h.each_value { |v| res << v }
|
|
|
|
assert_equal(0, [].length)
|
|
|
|
|
|
|
|
expected = []
|
|
|
|
@h.each { |k, v| expected << v }
|
|
|
|
|
|
|
|
assert_equal([], expected - res)
|
|
|
|
assert_equal([], res - expected)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_empty?
|
2013-04-09 03:21:50 -04:00
|
|
|
assert_empty(@cls[])
|
|
|
|
assert_not_empty(@h)
|
2006-12-31 10:02:22 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_fetch
|
|
|
|
assert_equal('gumbygumby', @h.fetch('gumby') {|k| k * 2 })
|
|
|
|
assert_equal('pokey', @h.fetch('gumby', 'pokey'))
|
|
|
|
|
|
|
|
assert_equal('one', @h.fetch(1))
|
|
|
|
assert_equal(nil, @h.fetch('nil'))
|
|
|
|
assert_equal('nil', @h.fetch(nil))
|
|
|
|
end
|
|
|
|
|
2011-01-29 23:03:19 -05:00
|
|
|
def test_fetch_error
|
|
|
|
assert_raise(KeyError) { @cls[].fetch(1) }
|
|
|
|
assert_raise(KeyError) { @h.fetch('gumby') }
|
|
|
|
e = assert_raise(KeyError) { @h.fetch('gumby'*20) }
|
|
|
|
assert_match(/key not found: "gumbygumby/, e.message)
|
|
|
|
assert_match(/\.\.\.\z/, e.message)
|
|
|
|
end
|
|
|
|
|
2010-02-13 22:09:53 -05:00
|
|
|
def test_key2?
|
2012-03-30 18:18:52 -04:00
|
|
|
assert_not_send([@cls[], :key?, 1])
|
|
|
|
assert_not_send([@cls[], :key?, nil])
|
|
|
|
assert_send([@h, :key?, nil])
|
|
|
|
assert_send([@h, :key?, 1])
|
|
|
|
assert_not_send([@h, :key?, 'gumby'])
|
2006-12-31 10:02:22 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_value?
|
2012-03-30 18:18:52 -04:00
|
|
|
assert_not_send([@cls[], :value?, 1])
|
|
|
|
assert_not_send([@cls[], :value?, nil])
|
|
|
|
assert_send([@h, :value?, 'one'])
|
|
|
|
assert_send([@h, :value?, nil])
|
|
|
|
assert_not_send([@h, :value?, 'gumby'])
|
2006-12-31 10:02:22 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_include?
|
2012-03-30 18:18:52 -04:00
|
|
|
assert_not_send([@cls[], :include?, 1])
|
|
|
|
assert_not_send([@cls[], :include?, nil])
|
|
|
|
assert_send([@h, :include?, nil])
|
|
|
|
assert_send([@h, :include?, 1])
|
|
|
|
assert_not_send([@h, :include?, 'gumby'])
|
2006-12-31 10:02:22 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_key
|
|
|
|
assert_equal(1, @h.key('one'))
|
|
|
|
assert_equal(nil, @h.key('nil'))
|
|
|
|
assert_equal('nil', @h.key(nil))
|
|
|
|
|
|
|
|
assert_equal(nil, @h.key('gumby'))
|
|
|
|
assert_equal(nil, @cls[].key('gumby'))
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_values_at
|
|
|
|
res = @h.values_at('dog', 'cat', 'horse')
|
2013-04-09 03:21:50 -04:00
|
|
|
assert_equal(3, res.length)
|
2006-12-31 10:02:22 -05:00
|
|
|
assert_equal([nil, nil, nil], res)
|
|
|
|
|
|
|
|
res = @h.values_at
|
2013-04-09 03:21:50 -04:00
|
|
|
assert_equal(0, res.length)
|
2006-12-31 10:02:22 -05:00
|
|
|
|
|
|
|
res = @h.values_at(3, 2, 1, nil)
|
|
|
|
assert_equal 4, res.length
|
|
|
|
assert_equal %w( three two one nil ), res
|
|
|
|
|
|
|
|
res = @h.values_at(3, 99, 1, nil)
|
|
|
|
assert_equal 4, res.length
|
|
|
|
assert_equal ['three', nil, 'one', 'nil'], res
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def test_invert
|
|
|
|
h = @h.invert
|
|
|
|
assert_equal(1, h['one'])
|
|
|
|
assert_equal(true, h['true'])
|
|
|
|
assert_equal(nil, h['nil'])
|
|
|
|
|
|
|
|
h.each do |k, v|
|
2013-04-09 03:21:50 -04:00
|
|
|
assert_send([@h, :key?, v]) # not true in general, but works here
|
2006-12-31 10:02:22 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
h = @cls[ 'a' => 1, 'b' => 2, 'c' => 1].invert
|
|
|
|
assert_equal(2, h.length)
|
2013-12-13 04:18:05 -05:00
|
|
|
assert_include(%w[a c], h[1])
|
2006-12-31 10:02:22 -05:00
|
|
|
assert_equal('b', h[2])
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_key?
|
2012-03-30 18:18:52 -04:00
|
|
|
assert_not_send([@cls[], :key?, 1])
|
|
|
|
assert_not_send([@cls[], :key?, nil])
|
|
|
|
assert_send([@h, :key?, nil])
|
|
|
|
assert_send([@h, :key?, 1])
|
|
|
|
assert_not_send([@h, :key?, 'gumby'])
|
2006-12-31 10:02:22 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_keys
|
|
|
|
assert_equal([], @cls[].keys)
|
|
|
|
|
|
|
|
keys = @h.keys
|
|
|
|
expected = []
|
|
|
|
@h.each { |k, v| expected << k }
|
|
|
|
assert_equal([], keys - expected)
|
|
|
|
assert_equal([], expected - keys)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_length
|
|
|
|
assert_equal(0, @cls[].length)
|
|
|
|
assert_equal(7, @h.length)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_member?
|
2012-03-30 18:18:52 -04:00
|
|
|
assert_not_send([@cls[], :member?, 1])
|
|
|
|
assert_not_send([@cls[], :member?, nil])
|
|
|
|
assert_send([@h, :member?, nil])
|
|
|
|
assert_send([@h, :member?, 1])
|
|
|
|
assert_not_send([@h, :member?, 'gumby'])
|
2006-12-31 10:02:22 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_rehash
|
|
|
|
a = [ "a", "b" ]
|
|
|
|
c = [ "c", "d" ]
|
|
|
|
h = @cls[ a => 100, c => 300 ]
|
|
|
|
assert_equal(100, h[a])
|
|
|
|
a[0] = "z"
|
|
|
|
assert_nil(h[a])
|
|
|
|
h.rehash
|
|
|
|
assert_equal(100, h[a])
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_reject
|
2013-12-11 02:01:29 -05:00
|
|
|
assert_equal({3=>4,5=>6}, @cls[1=>2,3=>4,5=>6].reject {|k, v| k + v < 7 })
|
|
|
|
|
2006-12-31 10:02:22 -05:00
|
|
|
base = @cls[ 1 => 'one', 2 => false, true => 'true', 'cat' => 99 ]
|
|
|
|
h1 = @cls[ 1 => 'one', 2 => false, true => 'true' ]
|
|
|
|
h2 = @cls[ 2 => false, 'cat' => 99 ]
|
|
|
|
h3 = @cls[ 2 => false ]
|
|
|
|
|
|
|
|
h = base.dup
|
|
|
|
assert_equal(h, h.reject { false })
|
|
|
|
assert_equal(@cls[], h.reject { true })
|
|
|
|
|
|
|
|
h = base.dup
|
|
|
|
assert_equal(h1, h.reject {|k,v| k.instance_of?(String) })
|
|
|
|
|
|
|
|
assert_equal(h2, h.reject {|k,v| v.instance_of?(String) })
|
|
|
|
|
|
|
|
assert_equal(h3, h.reject {|k,v| v })
|
|
|
|
assert_equal(base, h)
|
2013-12-08 06:25:06 -05:00
|
|
|
|
2013-12-11 02:01:29 -05:00
|
|
|
h.instance_variable_set(:@foo, :foo)
|
|
|
|
h.default = 42
|
|
|
|
h.taint
|
2013-12-23 01:53:51 -05:00
|
|
|
h = EnvUtil.suppress_warning {h.reject {false}}
|
2013-12-11 02:01:29 -05:00
|
|
|
assert_instance_of(Hash, h)
|
|
|
|
assert_not_predicate(h, :tainted?)
|
|
|
|
assert_nil(h.default)
|
|
|
|
assert_not_send([h, :instance_variable_defined?, :@foo])
|
2006-12-31 10:02:22 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_reject!
|
|
|
|
base = @cls[ 1 => 'one', 2 => false, true => 'true', 'cat' => 99 ]
|
|
|
|
h1 = @cls[ 1 => 'one', 2 => false, true => 'true' ]
|
|
|
|
h2 = @cls[ 2 => false, 'cat' => 99 ]
|
|
|
|
h3 = @cls[ 2 => false ]
|
|
|
|
|
|
|
|
h = base.dup
|
|
|
|
assert_equal(nil, h.reject! { false })
|
|
|
|
assert_equal(@cls[], h.reject! { true })
|
|
|
|
|
|
|
|
h = base.dup
|
|
|
|
assert_equal(h1, h.reject! {|k,v| k.instance_of?(String) })
|
|
|
|
assert_equal(h1, h)
|
|
|
|
|
|
|
|
h = base.dup
|
|
|
|
assert_equal(h2, h.reject! {|k,v| v.instance_of?(String) })
|
|
|
|
assert_equal(h2, h)
|
|
|
|
|
|
|
|
h = base.dup
|
|
|
|
assert_equal(h3, h.reject! {|k,v| v })
|
|
|
|
assert_equal(h3, h)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_replace
|
|
|
|
h = @cls[ 1 => 2, 3 => 4 ]
|
|
|
|
h1 = h.replace(@cls[ 9 => 8, 7 => 6 ])
|
|
|
|
assert_equal(h, h1)
|
|
|
|
assert_equal(8, h[9])
|
|
|
|
assert_equal(6, h[7])
|
|
|
|
assert_nil(h[1])
|
|
|
|
assert_nil(h[2])
|
|
|
|
end
|
|
|
|
|
2013-12-09 05:06:49 -05:00
|
|
|
def test_replace_bug9230
|
|
|
|
h = @cls[]
|
|
|
|
h.replace(@cls[])
|
|
|
|
assert_empty h
|
|
|
|
|
|
|
|
h = @cls[]
|
|
|
|
h.replace(@cls[].compare_by_identity)
|
|
|
|
assert_predicate(h, :compare_by_identity?)
|
|
|
|
end
|
|
|
|
|
2006-12-31 10:02:22 -05:00
|
|
|
def test_shift
|
|
|
|
h = @h.dup
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2006-12-31 10:02:22 -05:00
|
|
|
@h.length.times {
|
|
|
|
k, v = h.shift
|
2012-03-30 18:18:52 -04:00
|
|
|
assert_send([@h, :key?, k])
|
2006-12-31 10:02:22 -05:00
|
|
|
assert_equal(@h[k], v)
|
|
|
|
}
|
|
|
|
|
|
|
|
assert_equal(0, h.length)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_size
|
|
|
|
assert_equal(0, @cls[].length)
|
|
|
|
assert_equal(7, @h.length)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_sort
|
|
|
|
h = @cls[].sort
|
|
|
|
assert_equal([], h)
|
|
|
|
|
|
|
|
h = @cls[ 1 => 1, 2 => 1 ].sort
|
|
|
|
assert_equal([[1,1], [2,1]], h)
|
|
|
|
|
|
|
|
h = @cls[ 'cat' => 'feline', 'ass' => 'asinine', 'bee' => 'beeline' ]
|
|
|
|
h1 = h.sort
|
|
|
|
assert_equal([ %w(ass asinine), %w(bee beeline), %w(cat feline)], h1)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_store
|
|
|
|
t = Time.now
|
|
|
|
h = @cls.new
|
|
|
|
h.store(1, 'one')
|
|
|
|
h.store(2, 'two')
|
|
|
|
h.store(3, 'three')
|
|
|
|
h.store(self, 'self')
|
|
|
|
h.store(t, 'time')
|
|
|
|
h.store(nil, 'nil')
|
|
|
|
h.store('nil', nil)
|
|
|
|
assert_equal('one', h[1])
|
|
|
|
assert_equal('two', h[2])
|
|
|
|
assert_equal('three', h[3])
|
|
|
|
assert_equal('self', h[self])
|
|
|
|
assert_equal('time', h[t])
|
|
|
|
assert_equal('nil', h[nil])
|
|
|
|
assert_equal(nil, h['nil'])
|
|
|
|
assert_equal(nil, h['koala'])
|
|
|
|
|
|
|
|
h.store(1, 1)
|
|
|
|
h.store(nil, 99)
|
|
|
|
h.store('nil', nil)
|
|
|
|
assert_equal(1, h[1])
|
|
|
|
assert_equal('two', h[2])
|
|
|
|
assert_equal('three', h[3])
|
|
|
|
assert_equal('self', h[self])
|
|
|
|
assert_equal('time', h[t])
|
|
|
|
assert_equal(99, h[nil])
|
|
|
|
assert_equal(nil, h['nil'])
|
|
|
|
assert_equal(nil, h['koala'])
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_to_a
|
|
|
|
assert_equal([], @cls[].to_a)
|
|
|
|
assert_equal([[1,2]], @cls[ 1=>2 ].to_a)
|
|
|
|
a = @cls[ 1=>2, 3=>4, 5=>6 ].to_a
|
|
|
|
assert_equal([1,2], a.delete([1,2]))
|
|
|
|
assert_equal([3,4], a.delete([3,4]))
|
|
|
|
assert_equal([5,6], a.delete([5,6]))
|
|
|
|
assert_equal(0, a.length)
|
* object.c (rb_obj_untrusted): new method Object#untrusted?.
(rb_obj_untrust): new method Object#untrust.
(rb_obj_trust): new method Object#trust.
* array.c, debug.c, time.c, include/ruby/ruby.h, re.c, variable.c,
string.c, io.c, dir.c, vm_method.c, struct.c, class.c, hash.c,
ruby.c, marshal.c: fixes for Object#untrusted?.
* test/ruby/test_module.rb, test/ruby/test_array.rb,
test/ruby/test_object.rb, test/ruby/test_string.rb,
test/ruby/test_marshal.rb, test/ruby/test_hash.rb: added tests for
Object#untrusted?.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@18568 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-08-13 03:25:05 -04:00
|
|
|
|
|
|
|
h = @cls[ 1=>2, 3=>4, 5=>6 ]
|
|
|
|
h.taint
|
|
|
|
a = h.to_a
|
|
|
|
assert_equal(true, a.tainted?)
|
2006-12-31 10:02:22 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_to_hash
|
|
|
|
h = @h.to_hash
|
|
|
|
assert_equal(@h, h)
|
2012-04-23 23:46:55 -04:00
|
|
|
assert_instance_of(@cls, h)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_to_h
|
|
|
|
h = @h.to_h
|
|
|
|
assert_equal(@h, h)
|
|
|
|
assert_instance_of(Hash, h)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_nil_to_h
|
|
|
|
h = nil.to_h
|
|
|
|
assert_equal({}, h)
|
|
|
|
assert_nil(h.default)
|
|
|
|
assert_nil(h.default_proc)
|
2006-12-31 10:02:22 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_to_s
|
|
|
|
h = @cls[ 1 => 2, "cat" => "dog", 1.5 => :fred ]
|
|
|
|
assert_equal(h.inspect, h.to_s)
|
|
|
|
$, = ":"
|
|
|
|
assert_equal(h.inspect, h.to_s)
|
|
|
|
h = @cls[]
|
|
|
|
assert_equal(h.inspect, h.to_s)
|
2008-10-24 05:25:05 -04:00
|
|
|
ensure
|
2006-12-31 10:02:22 -05:00
|
|
|
$, = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_update
|
|
|
|
h1 = @cls[ 1 => 2, 2 => 3, 3 => 4 ]
|
|
|
|
h2 = @cls[ 2 => 'two', 4 => 'four' ]
|
|
|
|
|
|
|
|
ha = @cls[ 1 => 2, 2 => 'two', 3 => 4, 4 => 'four' ]
|
|
|
|
hb = @cls[ 1 => 2, 2 => 3, 3 => 4, 4 => 'four' ]
|
|
|
|
|
|
|
|
assert_equal(ha, h1.update(h2))
|
|
|
|
assert_equal(ha, h1)
|
|
|
|
|
|
|
|
h1 = @cls[ 1 => 2, 2 => 3, 3 => 4 ]
|
|
|
|
h2 = @cls[ 2 => 'two', 4 => 'four' ]
|
|
|
|
|
|
|
|
assert_equal(hb, h2.update(h1))
|
|
|
|
assert_equal(hb, h2)
|
|
|
|
end
|
|
|
|
|
2010-02-13 22:09:53 -05:00
|
|
|
def test_value2?
|
2012-03-30 18:18:52 -04:00
|
|
|
assert_not_send([@cls[], :value?, 1])
|
|
|
|
assert_not_send([@cls[], :value?, nil])
|
|
|
|
assert_send([@h, :value?, nil])
|
|
|
|
assert_send([@h, :value?, 'one'])
|
|
|
|
assert_not_send([@h, :value?, 'gumby'])
|
2006-12-31 10:02:22 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_values
|
|
|
|
assert_equal([], @cls[].values)
|
|
|
|
|
|
|
|
vals = @h.values
|
|
|
|
expected = []
|
|
|
|
@h.each { |k, v| expected << v }
|
|
|
|
assert_equal([], vals - expected)
|
|
|
|
assert_equal([], expected - vals)
|
|
|
|
end
|
|
|
|
|
2008-02-05 10:50:25 -05:00
|
|
|
def test_intialize_wrong_arguments
|
|
|
|
assert_raise(ArgumentError) do
|
|
|
|
Hash.new(0) { }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_create
|
2013-07-29 09:34:05 -04:00
|
|
|
assert_equal({1=>2, 3=>4}, @cls[[[1,2],[3,4]]])
|
2008-02-05 10:50:25 -05:00
|
|
|
assert_raise(ArgumentError) { Hash[0, 1, 2] }
|
2013-07-29 09:34:05 -04:00
|
|
|
assert_warning(/wrong element type Fixnum at 1 /) {@cls[[[1, 2], 3]]}
|
2012-07-14 04:08:15 -04:00
|
|
|
bug5406 = '[ruby-core:39945]'
|
2013-07-29 09:34:05 -04:00
|
|
|
assert_raise(ArgumentError, bug5406) { @cls[[[1, 2], [3, 4, 5]]] }
|
|
|
|
assert_equal({1=>2, 3=>4}, @cls[1,2,3,4])
|
2008-05-01 05:12:09 -04:00
|
|
|
o = Object.new
|
|
|
|
def o.to_hash() {1=>2} end
|
2013-07-29 09:34:05 -04:00
|
|
|
assert_equal({1=>2}, @cls[o], "[ruby-dev:34555]")
|
2008-02-05 10:50:25 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_rehash2
|
2013-07-29 09:34:05 -04:00
|
|
|
h = @cls[1 => 2, 3 => 4]
|
2008-02-05 10:50:25 -05:00
|
|
|
assert_equal(h.dup, h.rehash)
|
|
|
|
assert_raise(RuntimeError) { h.each { h.rehash } }
|
2013-07-29 09:34:05 -04:00
|
|
|
assert_equal({}, @cls[].rehash)
|
2008-02-05 10:50:25 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_fetch2
|
|
|
|
assert_equal(:bar, @h.fetch(0, :foo) { :bar })
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_default_proc
|
2013-07-29 09:34:05 -04:00
|
|
|
h = @cls.new {|hh, k| hh + k + "baz" }
|
2008-02-05 10:50:25 -05:00
|
|
|
assert_equal("foobarbaz", h.default_proc.call("foo", "bar"))
|
2012-04-09 00:07:53 -04:00
|
|
|
assert_nil(h.default_proc = nil)
|
|
|
|
assert_nil(h.default_proc)
|
2013-12-23 01:52:56 -05:00
|
|
|
h.default_proc = ->(_,_){ true }
|
2013-12-13 04:18:05 -05:00
|
|
|
assert_equal(true, h[:nope])
|
2013-07-29 09:34:05 -04:00
|
|
|
h = @cls[]
|
2008-02-05 10:50:25 -05:00
|
|
|
assert_nil(h.default_proc)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_shift2
|
2013-07-29 09:34:05 -04:00
|
|
|
h = @cls.new {|hh, k| :foo }
|
2008-02-05 10:50:25 -05:00
|
|
|
h[1] = 2
|
|
|
|
assert_equal([1, 2], h.shift)
|
|
|
|
assert_equal(:foo, h.shift)
|
|
|
|
assert_equal(:foo, h.shift)
|
|
|
|
|
2013-07-29 09:34:05 -04:00
|
|
|
h = @cls.new(:foo)
|
2008-02-05 10:50:25 -05:00
|
|
|
h[1] = 2
|
|
|
|
assert_equal([1, 2], h.shift)
|
|
|
|
assert_equal(:foo, h.shift)
|
|
|
|
assert_equal(:foo, h.shift)
|
|
|
|
|
2013-07-29 09:34:05 -04:00
|
|
|
h =@cls[1=>2]
|
2008-02-05 10:50:25 -05:00
|
|
|
h.each { assert_equal([1, 2], h.shift) }
|
|
|
|
end
|
|
|
|
|
2012-03-31 01:23:01 -04:00
|
|
|
def test_shift_none
|
2013-07-29 09:34:05 -04:00
|
|
|
h = @cls.new {|hh, k| "foo"}
|
2012-03-31 01:23:01 -04:00
|
|
|
def h.default(k = nil)
|
2012-03-31 04:13:58 -04:00
|
|
|
super.upcase
|
2012-03-31 01:23:01 -04:00
|
|
|
end
|
|
|
|
assert_equal("FOO", h.shift)
|
|
|
|
end
|
|
|
|
|
2008-02-05 10:50:25 -05:00
|
|
|
def test_reject_bang2
|
2013-07-29 09:34:05 -04:00
|
|
|
assert_equal({1=>2}, @cls[1=>2,3=>4].reject! {|k, v| k + v == 7 })
|
|
|
|
assert_nil(@cls[1=>2,3=>4].reject! {|k, v| k == 5 })
|
|
|
|
assert_nil(@cls[].reject! { })
|
2008-02-05 10:50:25 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_select
|
2013-07-29 09:34:05 -04:00
|
|
|
assert_equal({3=>4,5=>6}, @cls[1=>2,3=>4,5=>6].select {|k, v| k + v >= 7 })
|
2013-12-11 02:01:29 -05:00
|
|
|
|
|
|
|
base = @cls[ 1 => 'one', '2' => false, true => 'true', 'cat' => 99 ]
|
|
|
|
h1 = @cls[ '2' => false, 'cat' => 99 ]
|
|
|
|
h2 = @cls[ 1 => 'one', true => 'true' ]
|
|
|
|
h3 = @cls[ 1 => 'one', true => 'true', 'cat' => 99 ]
|
|
|
|
|
|
|
|
h = base.dup
|
|
|
|
assert_equal(h, h.select { true })
|
|
|
|
assert_equal(@cls[], h.select { false })
|
|
|
|
|
|
|
|
h = base.dup
|
|
|
|
assert_equal(h1, h.select {|k,v| k.instance_of?(String) })
|
|
|
|
|
|
|
|
assert_equal(h2, h.select {|k,v| v.instance_of?(String) })
|
|
|
|
|
|
|
|
assert_equal(h3, h.select {|k,v| v })
|
|
|
|
assert_equal(base, h)
|
|
|
|
|
|
|
|
h.instance_variable_set(:@foo, :foo)
|
|
|
|
h.default = 42
|
|
|
|
h.taint
|
|
|
|
h = h.select {true}
|
|
|
|
assert_instance_of(Hash, h)
|
|
|
|
assert_not_predicate(h, :tainted?)
|
|
|
|
assert_nil(h.default)
|
|
|
|
assert_not_send([h, :instance_variable_defined?, :@foo])
|
2008-02-05 10:50:25 -05:00
|
|
|
end
|
|
|
|
|
2010-03-03 04:44:44 -05:00
|
|
|
def test_select!
|
2013-07-29 09:34:05 -04:00
|
|
|
h = @cls[1=>2,3=>4,5=>6]
|
2010-03-03 04:44:44 -05:00
|
|
|
assert_equal(h, h.select! {|k, v| k + v >= 7 })
|
|
|
|
assert_equal({3=>4,5=>6}, h)
|
2013-07-29 09:34:05 -04:00
|
|
|
h = @cls[1=>2,3=>4,5=>6]
|
2010-03-03 04:44:44 -05:00
|
|
|
assert_equal(nil, h.select!{true})
|
|
|
|
end
|
|
|
|
|
2008-05-13 11:57:22 -04:00
|
|
|
def test_clear2
|
2013-07-29 09:34:05 -04:00
|
|
|
assert_equal({}, @cls[1=>2,3=>4,5=>6].clear)
|
|
|
|
h = @cls[1=>2,3=>4,5=>6]
|
2008-02-05 10:50:25 -05:00
|
|
|
h.each { h.clear }
|
|
|
|
assert_equal({}, h)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_replace2
|
2013-07-29 09:34:05 -04:00
|
|
|
h1 = @cls.new { :foo }
|
|
|
|
h2 = @cls.new
|
2008-02-05 10:50:25 -05:00
|
|
|
h2.replace h1
|
|
|
|
assert_equal(:foo, h2[0])
|
2010-02-10 02:31:06 -05:00
|
|
|
|
|
|
|
assert_raise(ArgumentError) { h2.replace() }
|
|
|
|
assert_raise(TypeError) { h2.replace(1) }
|
|
|
|
h2.freeze
|
|
|
|
assert_raise(ArgumentError) { h2.replace() }
|
|
|
|
assert_raise(RuntimeError) { h2.replace(h1) }
|
|
|
|
assert_raise(RuntimeError) { h2.replace(42) }
|
2008-02-05 10:50:25 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_size2
|
2013-07-29 09:34:05 -04:00
|
|
|
assert_equal(0, @cls[].size)
|
2008-02-05 10:50:25 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_equal2
|
2013-07-29 09:34:05 -04:00
|
|
|
assert_not_equal(0, @cls[])
|
2008-02-05 10:50:25 -05:00
|
|
|
o = Object.new
|
2013-07-29 09:34:05 -04:00
|
|
|
o.instance_variable_set(:@cls, @cls)
|
|
|
|
def o.to_hash; @cls[]; end
|
2008-02-05 10:50:25 -05:00
|
|
|
def o.==(x); true; end
|
2013-04-09 03:21:50 -04:00
|
|
|
assert_equal({}, o)
|
2008-02-05 10:50:25 -05:00
|
|
|
def o.==(x); false; end
|
2013-04-09 03:21:50 -04:00
|
|
|
assert_not_equal({}, o)
|
2008-02-05 10:50:25 -05:00
|
|
|
|
2013-07-29 09:34:05 -04:00
|
|
|
h1 = @cls[1=>2]; h2 = @cls[3=>4]
|
2013-04-09 03:21:50 -04:00
|
|
|
assert_not_equal(h1, h2)
|
2013-07-29 09:34:05 -04:00
|
|
|
h1 = @cls[1=>2]; h2 = @cls[1=>4]
|
2013-04-09 03:21:50 -04:00
|
|
|
assert_not_equal(h1, h2)
|
2008-02-05 10:50:25 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_eql
|
2013-07-29 09:34:05 -04:00
|
|
|
assert_not_send([@cls[], :eql?, 0])
|
2008-02-05 10:50:25 -05:00
|
|
|
o = Object.new
|
2013-07-29 09:34:05 -04:00
|
|
|
o.instance_variable_set(:@cls, @cls)
|
|
|
|
def o.to_hash; @cls[]; end
|
2008-02-05 10:50:25 -05:00
|
|
|
def o.eql?(x); true; end
|
2013-07-29 09:34:05 -04:00
|
|
|
assert_send([@cls[], :eql?, o])
|
2008-02-05 10:50:25 -05:00
|
|
|
def o.eql?(x); false; end
|
2013-07-29 09:34:05 -04:00
|
|
|
assert_not_send([@cls[], :eql?, o])
|
2008-02-05 10:50:25 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_hash2
|
2013-07-29 09:34:05 -04:00
|
|
|
assert_kind_of(Integer, @cls[].hash)
|
|
|
|
h = @cls[1=>2]
|
2011-08-01 23:03:18 -04:00
|
|
|
h.shift
|
|
|
|
assert_equal({}.hash, h.hash, '[ruby-core:38650]')
|
2013-12-10 09:44:51 -05:00
|
|
|
bug9231 = '[ruby-core:58993] [Bug #9231]'
|
|
|
|
assert_not_equal(0, @cls[].hash, bug9231)
|
2008-02-05 10:50:25 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_update2
|
2013-07-29 09:34:05 -04:00
|
|
|
h1 = @cls[1=>2, 3=>4]
|
2008-02-05 10:50:25 -05:00
|
|
|
h2 = {1=>3, 5=>7}
|
|
|
|
h1.update(h2) {|k, v1, v2| k + v1 + v2 }
|
|
|
|
assert_equal({1=>6, 3=>4, 5=>7}, h1)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_merge
|
2013-07-29 09:34:05 -04:00
|
|
|
h1 = @cls[1=>2, 3=>4]
|
2008-02-05 10:50:25 -05:00
|
|
|
h2 = {1=>3, 5=>7}
|
|
|
|
assert_equal({1=>3, 3=>4, 5=>7}, h1.merge(h2))
|
|
|
|
assert_equal({1=>6, 3=>4, 5=>7}, h1.merge(h2) {|k, v1, v2| k + v1 + v2 })
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_assoc
|
2013-07-29 09:34:05 -04:00
|
|
|
assert_equal([3,4], @cls[1=>2, 3=>4, 5=>6].assoc(3))
|
|
|
|
assert_nil(@cls[1=>2, 3=>4, 5=>6].assoc(4))
|
|
|
|
assert_equal([1.0,1], @cls[1.0=>1].assoc(1))
|
2008-02-05 10:50:25 -05:00
|
|
|
end
|
|
|
|
|
2013-07-29 08:39:21 -04:00
|
|
|
def test_assoc_compare_by_identity
|
2013-07-29 09:34:05 -04:00
|
|
|
h = @cls[]
|
2013-07-29 08:39:21 -04:00
|
|
|
h.compare_by_identity
|
|
|
|
h["a"] = 1
|
2014-01-09 23:54:08 -05:00
|
|
|
h["a".dup] = 2
|
2013-07-29 08:39:21 -04:00
|
|
|
assert_equal(["a",1], h.assoc("a"))
|
|
|
|
end
|
|
|
|
|
2008-02-05 10:50:25 -05:00
|
|
|
def test_rassoc
|
2013-07-29 09:34:05 -04:00
|
|
|
assert_equal([3,4], @cls[1=>2, 3=>4, 5=>6].rassoc(4))
|
2008-02-05 10:50:25 -05:00
|
|
|
assert_nil({1=>2, 3=>4, 5=>6}.rassoc(3))
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_flatten
|
2013-07-29 09:34:05 -04:00
|
|
|
assert_equal([[1], [2]], @cls[[1] => [2]].flatten)
|
2013-07-20 16:44:49 -04:00
|
|
|
|
2013-07-29 09:34:05 -04:00
|
|
|
a = @cls[1=> "one", 2 => [2,"two"], 3 => [3, ["three"]]]
|
2013-07-20 16:44:49 -04:00
|
|
|
assert_equal([1, "one", 2, [2, "two"], 3, [3, ["three"]]], a.flatten)
|
2014-02-19 22:09:13 -05:00
|
|
|
assert_equal([[1, "one"], [2, [2, "two"]], [3, [3, ["three"]]]], a.flatten(0))
|
2013-07-20 16:44:49 -04:00
|
|
|
assert_equal([1, "one", 2, [2, "two"], 3, [3, ["three"]]], a.flatten(1))
|
|
|
|
assert_equal([1, "one", 2, 2, "two", 3, 3, ["three"]], a.flatten(2))
|
|
|
|
assert_equal([1, "one", 2, 2, "two", 3, 3, "three"], a.flatten(3))
|
2014-02-19 21:27:30 -05:00
|
|
|
assert_equal([1, "one", 2, 2, "two", 3, 3, "three"], a.flatten(-1))
|
2013-07-20 16:44:49 -04:00
|
|
|
assert_raise(TypeError){ a.flatten(Object) }
|
2008-02-05 10:50:25 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_callcc
|
2013-07-29 09:34:05 -04:00
|
|
|
h = @cls[1=>2]
|
2008-02-05 10:50:25 -05:00
|
|
|
c = nil
|
|
|
|
f = false
|
|
|
|
h.each { callcc {|c2| c = c2 } }
|
|
|
|
unless f
|
|
|
|
f = true
|
|
|
|
c.call
|
|
|
|
end
|
|
|
|
assert_raise(RuntimeError) { h.each { h.rehash } }
|
|
|
|
|
2013-07-29 09:34:05 -04:00
|
|
|
h = @cls[1=>2]
|
2008-02-05 10:50:25 -05:00
|
|
|
c = nil
|
|
|
|
assert_raise(RuntimeError) do
|
|
|
|
h.each { callcc {|c2| c = c2 } }
|
|
|
|
h.clear
|
|
|
|
c.call
|
|
|
|
end
|
2013-11-15 08:21:38 -05:00
|
|
|
end
|
2013-11-13 21:35:40 -05:00
|
|
|
|
2013-11-15 08:21:38 -05:00
|
|
|
def test_callcc_iter_level
|
2013-11-13 21:35:40 -05:00
|
|
|
bug9105 = '[ruby-dev:47803] [Bug #9105]'
|
|
|
|
h = @cls[1=>2, 3=>4]
|
|
|
|
c = nil
|
|
|
|
f = false
|
|
|
|
h.each {callcc {|c2| c = c2}}
|
|
|
|
unless f
|
|
|
|
f = true
|
|
|
|
c.call
|
|
|
|
end
|
|
|
|
assert_nothing_raised(RuntimeError, bug9105) do
|
|
|
|
h.each {|i, j|
|
|
|
|
h.delete(i);
|
|
|
|
assert_not_equal(false, i, bug9105)
|
|
|
|
}
|
|
|
|
end
|
2008-02-05 10:50:25 -05:00
|
|
|
end
|
|
|
|
|
2013-11-15 19:24:39 -05:00
|
|
|
def test_callcc_escape
|
|
|
|
bug9105 = '[ruby-dev:47803] [Bug #9105]'
|
|
|
|
assert_nothing_raised(RuntimeError, bug9105) do
|
|
|
|
h=@cls[]
|
|
|
|
cnt=0
|
2013-12-23 01:52:56 -05:00
|
|
|
c = callcc {|cc|cc}
|
2013-11-15 19:24:39 -05:00
|
|
|
h[cnt] = true
|
|
|
|
h.each{|i|
|
|
|
|
cnt+=1
|
|
|
|
c.call if cnt == 1
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_callcc_reenter
|
|
|
|
bug9105 = '[ruby-dev:47803] [Bug #9105]'
|
|
|
|
assert_nothing_raised(RuntimeError, bug9105) do
|
|
|
|
h = @cls[1=>2,3=>4]
|
|
|
|
c = nil
|
|
|
|
f = false
|
|
|
|
h.each { |i|
|
|
|
|
callcc {|c2| c = c2 } unless c
|
|
|
|
h.delete(1) if f
|
|
|
|
}
|
|
|
|
unless f
|
|
|
|
f = true
|
|
|
|
c.call
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-11-15 08:21:38 -05:00
|
|
|
def test_threaded_iter_level
|
|
|
|
bug9105 = '[ruby-dev:47807] [Bug #9105]'
|
|
|
|
h = @cls[1=>2]
|
|
|
|
2.times.map {
|
|
|
|
f = false
|
|
|
|
th = Thread.start {h.each {f = true; sleep}}
|
|
|
|
Thread.pass until f
|
|
|
|
Thread.pass until th.stop?
|
|
|
|
th
|
|
|
|
}.each {|th| th.run; th.join}
|
|
|
|
assert_nothing_raised(RuntimeError, bug9105) do
|
|
|
|
h[5] = 6
|
|
|
|
end
|
|
|
|
assert_equal(6, h[5], bug9105)
|
|
|
|
end
|
|
|
|
|
2008-02-05 10:50:25 -05:00
|
|
|
def test_compare_by_identity
|
|
|
|
a = "foo"
|
2013-07-29 03:51:23 -04:00
|
|
|
assert_not_predicate(@cls[], :compare_by_identity?)
|
|
|
|
h = @cls[a => "bar"]
|
2013-04-09 03:21:50 -04:00
|
|
|
assert_not_predicate(h, :compare_by_identity?)
|
2008-02-05 10:50:25 -05:00
|
|
|
h.compare_by_identity
|
2013-04-09 03:21:50 -04:00
|
|
|
assert_predicate(h, :compare_by_identity?)
|
2008-02-05 10:50:25 -05:00
|
|
|
#assert_equal("bar", h[a])
|
|
|
|
assert_nil(h["foo"])
|
2013-07-29 03:51:23 -04:00
|
|
|
|
|
|
|
bug8703 = '[ruby-core:56256] [Bug #8703] copied identhash'
|
|
|
|
h.clear
|
|
|
|
assert_predicate(h.dup, :compare_by_identity?, bug8703)
|
2008-02-05 10:50:25 -05:00
|
|
|
end
|
2008-06-07 12:19:47 -04:00
|
|
|
|
2014-04-19 23:58:22 -04:00
|
|
|
def test_same_key
|
|
|
|
bug9646 = '[ruby-dev:48047] [Bug #9646] Infinite loop at Hash#each'
|
|
|
|
h = @cls[a=[], 1]
|
|
|
|
a << 1
|
|
|
|
h[[]] = 2
|
|
|
|
a.clear
|
|
|
|
cnt = 0
|
|
|
|
r = h.each{ break nil if (cnt+=1) > 100 }
|
|
|
|
assert_not_nil(r,bug9646)
|
|
|
|
end
|
|
|
|
|
2009-02-15 00:19:39 -05:00
|
|
|
class ObjWithHash
|
|
|
|
def initialize(value, hash)
|
|
|
|
@value = value
|
|
|
|
@hash = hash
|
|
|
|
end
|
|
|
|
attr_reader :value, :hash
|
|
|
|
|
|
|
|
def eql?(other)
|
|
|
|
@value == other.value
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-06-07 12:19:47 -04:00
|
|
|
def test_hash_hash
|
2013-07-29 09:34:05 -04:00
|
|
|
assert_equal({0=>2,11=>1}.hash, @cls[11=>1,0=>2].hash)
|
2009-02-15 00:19:39 -05:00
|
|
|
o1 = ObjWithHash.new(0,1)
|
|
|
|
o2 = ObjWithHash.new(11,1)
|
2013-07-29 09:34:05 -04:00
|
|
|
assert_equal({o1=>1,o2=>2}.hash, @cls[o2=>2,o1=>1].hash)
|
2008-06-07 12:19:47 -04:00
|
|
|
end
|
2009-02-14 13:53:40 -05:00
|
|
|
|
|
|
|
def test_hash_bignum_hash
|
|
|
|
x = 2<<(32-3)-1
|
2013-07-29 09:34:05 -04:00
|
|
|
assert_equal({x=>1}.hash, @cls[x=>1].hash)
|
2009-02-14 13:53:40 -05:00
|
|
|
x = 2<<(64-3)-1
|
2013-07-29 09:34:05 -04:00
|
|
|
assert_equal({x=>1}.hash, @cls[x=>1].hash)
|
2009-02-14 13:53:40 -05:00
|
|
|
|
|
|
|
o = Object.new
|
2011-01-11 18:37:15 -05:00
|
|
|
def o.hash; 2 << 100; end
|
2013-07-29 09:34:05 -04:00
|
|
|
assert_equal({o=>1}.hash, @cls[o=>1].hash)
|
2009-02-14 13:53:40 -05:00
|
|
|
end
|
2010-01-21 11:08:40 -05:00
|
|
|
|
|
|
|
def test_hash_poped
|
2013-07-29 09:34:05 -04:00
|
|
|
assert_nothing_raised { eval("a = 1; @cls[a => a]; a") }
|
2010-01-21 11:08:40 -05:00
|
|
|
end
|
2010-01-22 10:03:32 -05:00
|
|
|
|
2010-03-26 06:32:36 -04:00
|
|
|
def test_recursive_key
|
2013-07-29 09:34:05 -04:00
|
|
|
h = @cls[]
|
2010-03-26 06:32:36 -04:00
|
|
|
assert_nothing_raised { h[h] = :foo }
|
|
|
|
h.rehash
|
|
|
|
assert_equal(:foo, h[h])
|
2010-01-22 10:03:32 -05:00
|
|
|
end
|
2011-01-11 18:37:15 -05:00
|
|
|
|
|
|
|
def test_inverse_hash
|
|
|
|
feature4262 = '[ruby-core:34334]'
|
2013-07-29 09:34:05 -04:00
|
|
|
[@cls[1=>2], @cls[123=>"abc"]].each do |h|
|
2011-01-11 18:37:15 -05:00
|
|
|
assert_not_equal(h.hash, h.invert.hash, feature4262)
|
|
|
|
end
|
|
|
|
end
|
2012-04-23 23:46:55 -04:00
|
|
|
|
2013-12-05 00:38:13 -05:00
|
|
|
def test_recursive_hash_value_struct
|
2013-11-26 08:43:40 -05:00
|
|
|
bug9151 = '[ruby-core:58567] [Bug #9151]'
|
|
|
|
|
|
|
|
s = Struct.new(:x) {def hash; [x,""].hash; end}
|
|
|
|
a = s.new
|
|
|
|
b = s.new
|
|
|
|
a.x = b
|
|
|
|
b.x = a
|
2013-12-05 00:38:13 -05:00
|
|
|
assert_nothing_raised(SystemStackError, bug9151) {a.hash}
|
|
|
|
assert_nothing_raised(SystemStackError, bug9151) {b.hash}
|
|
|
|
|
|
|
|
h = @cls[]
|
|
|
|
h[[a,"hello"]] = 1
|
|
|
|
assert_equal(1, h.size)
|
|
|
|
h[[b,"world"]] = 2
|
|
|
|
assert_equal(2, h.size)
|
|
|
|
|
|
|
|
obj = Object.new
|
|
|
|
h = @cls[a => obj]
|
|
|
|
assert_same(obj, h[b])
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_recursive_hash_value_array
|
|
|
|
h = @cls[]
|
|
|
|
h[[[1]]] = 1
|
|
|
|
assert_equal(1, h.size)
|
|
|
|
h[[[2]]] = 1
|
|
|
|
assert_equal(2, h.size)
|
|
|
|
|
|
|
|
a = []
|
|
|
|
a << a
|
|
|
|
|
|
|
|
h = @cls[]
|
|
|
|
h[[a, 1]] = 1
|
|
|
|
assert_equal(1, h.size)
|
|
|
|
h[[a, 2]] = 2
|
|
|
|
assert_equal(2, h.size)
|
|
|
|
h[[a, a]] = 3
|
|
|
|
assert_equal(3, h.size)
|
|
|
|
|
|
|
|
obj = Object.new
|
|
|
|
h = @cls[a => obj]
|
|
|
|
assert_same(obj, h[[[a]]])
|
2013-11-26 08:43:40 -05:00
|
|
|
end
|
|
|
|
|
2013-12-05 00:38:15 -05:00
|
|
|
def test_recursive_hash_value_array_hash
|
|
|
|
h = @cls[]
|
|
|
|
rec = [h]
|
|
|
|
h[:x] = rec
|
|
|
|
|
|
|
|
obj = Object.new
|
|
|
|
h2 = {rec => obj}
|
|
|
|
[h, {x: rec}].each do |k|
|
|
|
|
k = [k]
|
|
|
|
assert_same(obj, h2[k], ->{k.inspect})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_recursive_hash_value_hash_array
|
|
|
|
h = @cls[]
|
|
|
|
rec = [h]
|
|
|
|
h[:x] = rec
|
|
|
|
|
|
|
|
obj = Object.new
|
|
|
|
h2 = {h => obj}
|
|
|
|
[rec, [h]].each do |k|
|
|
|
|
k = {x: k}
|
|
|
|
assert_same(obj, h2[k], ->{k.inspect})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-12-02 07:59:31 -05:00
|
|
|
def test_exception_in_rehash
|
2014-03-11 01:16:06 -04:00
|
|
|
return unless @cls == Hash
|
|
|
|
|
2013-12-02 07:59:31 -05:00
|
|
|
bug9187 = '[ruby-core:58728] [Bug #9187]'
|
|
|
|
|
|
|
|
prepare = <<-EOS
|
|
|
|
class Foo
|
|
|
|
def initialize
|
|
|
|
@raise = false
|
|
|
|
end
|
|
|
|
|
|
|
|
def hash
|
|
|
|
raise if @raise
|
|
|
|
@raise = true
|
|
|
|
return 0
|
|
|
|
end
|
|
|
|
end
|
2014-03-11 01:16:06 -04:00
|
|
|
h = {Foo.new => true}
|
2013-12-02 07:59:31 -05:00
|
|
|
EOS
|
|
|
|
|
|
|
|
code = <<-EOS
|
|
|
|
10_0000.times do
|
|
|
|
h.rehash rescue nil
|
|
|
|
end
|
|
|
|
GC.start
|
|
|
|
EOS
|
|
|
|
|
|
|
|
assert_no_memory_leak([], prepare, code, bug9187)
|
|
|
|
end
|
|
|
|
|
2014-01-08 01:55:24 -05:00
|
|
|
def test_wrapper_of_special_const
|
|
|
|
bug9381 = '[ruby-core:59638] [Bug #9381]'
|
|
|
|
|
|
|
|
wrapper = Class.new do
|
|
|
|
def initialize(obj)
|
|
|
|
@obj = obj
|
|
|
|
end
|
|
|
|
|
|
|
|
def hash
|
|
|
|
@obj.hash
|
|
|
|
end
|
|
|
|
|
|
|
|
def eql?(other)
|
|
|
|
@obj.eql?(other)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
hash = {5 => bug9381}
|
|
|
|
assert_equal(bug9381, hash[wrapper.new(5)])
|
|
|
|
end
|
|
|
|
|
2014-09-19 21:48:43 -04:00
|
|
|
def test_label_syntax
|
|
|
|
return unless @cls == Hash
|
|
|
|
|
|
|
|
feature4935 = '[ruby-core:37553] [Feature #4935]'
|
|
|
|
x = 'world'
|
|
|
|
hash = assert_nothing_raised(SyntaxError) do
|
2014-12-04 19:46:08 -05:00
|
|
|
break eval(%q({foo: 1, "foo-bar": 2, "hello-#{x}": 3, 'hello-#{x}': 4, 'bar': {}}))
|
2014-09-19 21:48:43 -04:00
|
|
|
end
|
2014-12-04 19:46:08 -05:00
|
|
|
assert_equal({:foo => 1, :'foo-bar' => 2, :'hello-world' => 3, :'hello-#{x}' => 4, :bar => {}}, hash)
|
2014-09-19 21:48:43 -04:00
|
|
|
end
|
|
|
|
|
2012-04-23 23:46:55 -04:00
|
|
|
class TestSubHash < TestHash
|
|
|
|
class SubHash < Hash
|
2013-12-23 01:53:51 -05:00
|
|
|
def reject(*)
|
|
|
|
super
|
|
|
|
end
|
2012-04-23 23:46:55 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def setup
|
|
|
|
@cls = SubHash
|
|
|
|
super
|
|
|
|
end
|
|
|
|
end
|
2003-09-04 12:18:59 -04:00
|
|
|
end
|