1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

* test/ruby/test_hash.rb: use @cls.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@42242 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2013-07-29 13:34:05 +00:00
parent ffb9da9fa0
commit 6c9cd508e7

View file

@ -6,8 +6,8 @@ require_relative 'envutil'
class TestHash < Test::Unit::TestCase
def test_hash
x = {1=>2, 2=>4, 3=>6}
y = {1=>2, 2=>4, 3=>6} # y = {1, 2, 2, 4, 3, 6} # 1.9 doesn't support
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
assert_equal(2, x[1])
@ -109,8 +109,8 @@ class TestHash < Test::Unit::TestCase
end
def test_dup_will_rehash
set1 = { }
set2 = { set1 => true}
set1 = @cls[]
set2 = @cls[set1 => true]
set1[set1] = true
@ -313,9 +313,9 @@ class TestHash < Test::Unit::TestCase
end
def test_keep_if
h = {1=>2,3=>4,5=>6}
h = @cls[1=>2,3=>4,5=>6]
assert_equal({3=>4,5=>6}, h.keep_if {|k, v| k + v >= 7 })
h = {1=>2,3=>4,5=>6}
h = @cls[1=>2,3=>4,5=>6]
assert_equal({1=>2,3=>4,5=>6}, h.keep_if{true})
end
@ -336,9 +336,9 @@ class TestHash < Test::Unit::TestCase
end
def test_dup_equality
h = {'k' => 'v'}
h = @cls['k' => 'v']
assert_equal(h, h.dup)
h1 = {h => 1}
h1 = @cls[h => 1]
assert_equal(h1, h1.dup)
h[1] = 2
assert_equal(h1, h1.dup)
@ -726,22 +726,22 @@ class TestHash < Test::Unit::TestCase
end
def test_create
assert_equal({1=>2, 3=>4}, Hash[[[1,2],[3,4]]])
assert_equal({1=>2, 3=>4}, @cls[[[1,2],[3,4]]])
assert_raise(ArgumentError) { Hash[0, 1, 2] }
assert_warning(/wrong element type Fixnum at 1 /) {Hash[[[1, 2], 3]]}
assert_warning(/wrong element type Fixnum at 1 /) {@cls[[[1, 2], 3]]}
bug5406 = '[ruby-core:39945]'
assert_raise(ArgumentError, bug5406) { Hash[[[1, 2], [3, 4, 5]]] }
assert_equal({1=>2, 3=>4}, Hash[1,2,3,4])
assert_raise(ArgumentError, bug5406) { @cls[[[1, 2], [3, 4, 5]]] }
assert_equal({1=>2, 3=>4}, @cls[1,2,3,4])
o = Object.new
def o.to_hash() {1=>2} end
assert_equal({1=>2}, Hash[o], "[ruby-dev:34555]")
assert_equal({1=>2}, @cls[o], "[ruby-dev:34555]")
end
def test_rehash2
h = {1 => 2, 3 => 4}
h = @cls[1 => 2, 3 => 4]
assert_equal(h.dup, h.rehash)
assert_raise(RuntimeError) { h.each { h.rehash } }
assert_equal({}, {}.rehash)
assert_equal({}, @cls[].rehash)
end
def test_fetch2
@ -749,35 +749,35 @@ class TestHash < Test::Unit::TestCase
end
def test_default_proc
h = Hash.new {|hh, k| hh + k + "baz" }
h = @cls.new {|hh, k| hh + k + "baz" }
assert_equal("foobarbaz", h.default_proc.call("foo", "bar"))
assert_nil(h.default_proc = nil)
assert_nil(h.default_proc)
h.default_proc = ->(h, k){ true }
assert(h[:nope])
h = {}
h = @cls[]
assert_nil(h.default_proc)
end
def test_shift2
h = Hash.new {|hh, k| :foo }
h = @cls.new {|hh, k| :foo }
h[1] = 2
assert_equal([1, 2], h.shift)
assert_equal(:foo, h.shift)
assert_equal(:foo, h.shift)
h = Hash.new(:foo)
h = @cls.new(:foo)
h[1] = 2
assert_equal([1, 2], h.shift)
assert_equal(:foo, h.shift)
assert_equal(:foo, h.shift)
h = {1=>2}
h =@cls[1=>2]
h.each { assert_equal([1, 2], h.shift) }
end
def test_shift_none
h = Hash.new {|hh, k| "foo"}
h = @cls.new {|hh, k| "foo"}
def h.default(k = nil)
super.upcase
end
@ -785,33 +785,33 @@ class TestHash < Test::Unit::TestCase
end
def test_reject_bang2
assert_equal({1=>2}, {1=>2,3=>4}.reject! {|k, v| k + v == 7 })
assert_nil({1=>2,3=>4}.reject! {|k, v| k == 5 })
assert_nil({}.reject! { })
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! { })
end
def test_select
assert_equal({3=>4,5=>6}, {1=>2,3=>4,5=>6}.select {|k, v| k + v >= 7 })
assert_equal({3=>4,5=>6}, @cls[1=>2,3=>4,5=>6].select {|k, v| k + v >= 7 })
end
def test_select!
h = {1=>2,3=>4,5=>6}
h = @cls[1=>2,3=>4,5=>6]
assert_equal(h, h.select! {|k, v| k + v >= 7 })
assert_equal({3=>4,5=>6}, h)
h = {1=>2,3=>4,5=>6}
h = @cls[1=>2,3=>4,5=>6]
assert_equal(nil, h.select!{true})
end
def test_clear2
assert_equal({}, {1=>2,3=>4,5=>6}.clear)
h = {1=>2,3=>4,5=>6}
assert_equal({}, @cls[1=>2,3=>4,5=>6].clear)
h = @cls[1=>2,3=>4,5=>6]
h.each { h.clear }
assert_equal({}, h)
end
def test_replace2
h1 = Hash.new { :foo }
h2 = {}
h1 = @cls.new { :foo }
h2 = @cls.new
h2.replace h1
assert_equal(:foo, h2[0])
@ -824,63 +824,65 @@ class TestHash < Test::Unit::TestCase
end
def test_size2
assert_equal(0, {}.size)
assert_equal(0, @cls[].size)
end
def test_equal2
assert_not_equal(0, {})
assert_not_equal(0, @cls[])
o = Object.new
def o.to_hash; {}; end
o.instance_variable_set(:@cls, @cls)
def o.to_hash; @cls[]; end
def o.==(x); true; end
assert_equal({}, o)
def o.==(x); false; end
assert_not_equal({}, o)
h1 = {1=>2}; h2 = {3=>4}
h1 = @cls[1=>2]; h2 = @cls[3=>4]
assert_not_equal(h1, h2)
h1 = {1=>2}; h2 = {1=>4}
h1 = @cls[1=>2]; h2 = @cls[1=>4]
assert_not_equal(h1, h2)
end
def test_eql
assert_not_send([{}, :eql?, 0])
assert_not_send([@cls[], :eql?, 0])
o = Object.new
def o.to_hash; {}; end
o.instance_variable_set(:@cls, @cls)
def o.to_hash; @cls[]; end
def o.eql?(x); true; end
assert_send([{}, :eql?, o])
assert_send([@cls[], :eql?, o])
def o.eql?(x); false; end
assert_not_send([{}, :eql?, o])
assert_not_send([@cls[], :eql?, o])
end
def test_hash2
assert_kind_of(Integer, {}.hash)
h = {1=>2}
assert_kind_of(Integer, @cls[].hash)
h = @cls[1=>2]
h.shift
assert_equal({}.hash, h.hash, '[ruby-core:38650]')
end
def test_update2
h1 = {1=>2, 3=>4}
h1 = @cls[1=>2, 3=>4]
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
h1 = {1=>2, 3=>4}
h1 = @cls[1=>2, 3=>4]
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
assert_equal([3,4], {1=>2, 3=>4, 5=>6}.assoc(3))
assert_nil({1=>2, 3=>4, 5=>6}.assoc(4))
assert_equal([1.0,1], {1.0=>1}.assoc(1))
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))
end
def test_assoc_compare_by_identity
h = {}
h = @cls[]
h.compare_by_identity
h["a"] = 1
h["a"] = 2
@ -888,14 +890,14 @@ class TestHash < Test::Unit::TestCase
end
def test_rassoc
assert_equal([3,4], {1=>2, 3=>4, 5=>6}.rassoc(4))
assert_equal([3,4], @cls[1=>2, 3=>4, 5=>6].rassoc(4))
assert_nil({1=>2, 3=>4, 5=>6}.rassoc(3))
end
def test_flatten
assert_equal([[1], [2]], {[1] => [2]}.flatten)
assert_equal([[1], [2]], @cls[[1] => [2]].flatten)
a = {1=> "one", 2 => [2,"two"], 3 => [3, ["three"]]}
a = @cls[1=> "one", 2 => [2,"two"], 3 => [3, ["three"]]]
assert_equal([1, "one", 2, [2, "two"], 3, [3, ["three"]]], a.flatten)
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(0))
@ -906,7 +908,7 @@ class TestHash < Test::Unit::TestCase
end
def test_callcc
h = {1=>2}
h = @cls[1=>2]
c = nil
f = false
h.each { callcc {|c2| c = c2 } }
@ -916,7 +918,7 @@ class TestHash < Test::Unit::TestCase
end
assert_raise(RuntimeError) { h.each { h.rehash } }
h = {1=>2}
h = @cls[1=>2]
c = nil
assert_raise(RuntimeError) do
h.each { callcc {|c2| c = c2 } }
@ -953,29 +955,29 @@ class TestHash < Test::Unit::TestCase
end
def test_hash_hash
assert_equal({0=>2,11=>1}.hash, {11=>1,0=>2}.hash)
assert_equal({0=>2,11=>1}.hash, @cls[11=>1,0=>2].hash)
o1 = ObjWithHash.new(0,1)
o2 = ObjWithHash.new(11,1)
assert_equal({o1=>1,o2=>2}.hash, {o2=>2,o1=>1}.hash)
assert_equal({o1=>1,o2=>2}.hash, @cls[o2=>2,o1=>1].hash)
end
def test_hash_bignum_hash
x = 2<<(32-3)-1
assert_equal({x=>1}.hash, {x=>1}.hash)
assert_equal({x=>1}.hash, @cls[x=>1].hash)
x = 2<<(64-3)-1
assert_equal({x=>1}.hash, {x=>1}.hash)
assert_equal({x=>1}.hash, @cls[x=>1].hash)
o = Object.new
def o.hash; 2 << 100; end
assert_equal({o=>1}.hash, {o=>1}.hash)
assert_equal({o=>1}.hash, @cls[o=>1].hash)
end
def test_hash_poped
assert_nothing_raised { eval("a = 1; {a => a}; a") }
assert_nothing_raised { eval("a = 1; @cls[a => a]; a") }
end
def test_recursive_key
h = {}
h = @cls[]
assert_nothing_raised { h[h] = :foo }
h.rehash
assert_equal(:foo, h[h])
@ -983,7 +985,7 @@ class TestHash < Test::Unit::TestCase
def test_inverse_hash
feature4262 = '[ruby-core:34334]'
[{1=>2}, {123=>"abc"}].each do |h|
[@cls[1=>2], @cls[123=>"abc"]].each do |h|
assert_not_equal(h.hash, h.invert.hash, feature4262)
end
end