4 assert('Hash', '15.2.13') do
5 assert_equal Class, Hash.class
8 assert('Hash#==', '15.2.13.4.1') do
9 assert_true({ 'abc' => 'abc' } == { 'abc' => 'abc' })
10 assert_false({ 'abc' => 'abc' } == { 'cba' => 'cba' })
11 assert_false({ :a => 1 } == true)
12 skip unless Object.const_defined?(:Float)
13 assert_true({ :equal => 1 } == { :equal => 1.0 })
16 assert('Hash#[]', '15.2.13.4.2') do
17 a = { 'abc' => 'abc' }
19 assert_equal 'abc', a['abc']
21 # Hash#[] should call #default (#3272)
23 def hash.default(k); self[k] = 1; end
26 assert_equal 2, hash[:foo]
29 assert('Hash#[]=', '15.2.13.4.3') do
33 assert_equal 'abc', a['abc']
36 assert('Hash#clear', '15.2.13.4.4') do
37 a = { 'abc' => 'abc' }
47 assert_equal({'a' => 1}, b)
49 c = Hash.new { |h, k| h[k] = k.upcase }
51 assert_equal("FOO", d["foo"])
54 assert('Hash#default', '15.2.13.4.5') do
57 c = Hash.new {|s,k| s[k] = k}
60 assert_equal 'abc', b.default
62 assert_equal 'abc', c.default('abc')
65 assert('Hash#default=', '15.2.13.4.6') do
66 a = { 'abc' => 'abc' }
69 assert_equal 'abc', a['abc']
70 assert_equal 'cba', a['notexist']
73 assert('Hash#default_proc', '15.2.13.4.7') do
75 b = Hash.new {|s,k| s[k] = k + k}
79 assert_nil a.default_proc
80 assert_equal Proc, b.default_proc.class
82 assert_equal 'catcat', d
85 assert('Hash#delete', '15.2.13.4.8') do
86 a = { 'abc' => 'ABC' }
87 b = { 'abc' => 'ABC' }
91 assert_equal 'ABC', a.delete('abc')
92 b.delete('abc') do |k|
95 b.delete('abc') do |k|
99 assert_nil a.delete('cba')
100 assert_false a.has_key?('abc')
105 assert('Hash#each', '15.2.13.4.9') do
106 a = { 'abc_key' => 'abc_value' }
115 assert_equal 'abc_key', key
116 assert_equal 'abc_value', value
119 assert('Hash#each_key', '15.2.13.4.10') do
120 a = { 'abc_key' => 'abc_value' }
127 assert_equal 'abc_key', key
130 assert('Hash#each_value', '15.2.13.4.11') do
131 a = { 'abc_key' => 'abc_value' }
138 assert_equal 'abc_value', value
141 assert('Hash#empty?', '15.2.13.4.12') do
142 a = { 'abc_key' => 'abc_value' }
145 assert_false a.empty?
149 assert('Hash#has_key?', '15.2.13.4.13') do
150 a = { 'abc_key' => 'abc_value' }
153 assert_true a.has_key?('abc_key')
154 assert_false b.has_key?('cba')
157 assert('Hash#has_value?', '15.2.13.4.14') do
158 a = { 'abc_key' => 'abc_value' }
161 assert_true a.has_value?('abc_value')
162 assert_false b.has_value?('cba')
165 assert('Hash#include?', '15.2.13.4.15') do
166 a = { 'abc_key' => 'abc_value' }
169 assert_true a.include?('abc_key')
170 assert_false b.include?('cba')
173 assert('Hash#initialize', '15.2.13.4.16') do
174 # Testing initialize by new.
176 h2 = Hash.new(:not_found)
178 assert_true h.is_a? Hash
180 assert_nil h["hello"]
181 assert_equal :not_found, h2["hello"]
184 assert('Hash#initialize_copy', '15.2.13.4.17') do
185 a = { 'abc_key' => 'abc_value' }
186 b = Hash.new.initialize_copy(a)
188 assert_equal({ 'abc_key' => 'abc_value' }, b)
191 assert('Hash#key?', '15.2.13.4.18') do
192 a = { 'abc_key' => 'abc_value' }
195 assert_true a.key?('abc_key')
196 assert_false b.key?('cba')
199 assert('Hash#keys', '15.2.13.4.19') do
200 a = { 'abc_key' => 'abc_value' }
202 assert_equal ['abc_key'], a.keys
205 assert('Hash#length', '15.2.13.4.20') do
206 a = { 'abc_key' => 'abc_value' }
209 assert_equal 1, a.length
210 assert_equal 0, b.length
213 assert('Hash#member?', '15.2.13.4.21') do
214 a = { 'abc_key' => 'abc_value' }
217 assert_true a.member?('abc_key')
218 assert_false b.member?('cba')
221 assert('Hash#merge', '15.2.13.4.22') do
222 a = { 'abc_key' => 'abc_value', 'cba_key' => 'cba_value' }
223 b = { 'cba_key' => 'XXX', 'xyz_key' => 'xyz_value' }
226 result_2 = a.merge(b) do |key, original, new|
230 assert_equal({'abc_key' => 'abc_value', 'cba_key' => 'XXX',
231 'xyz_key' => 'xyz_value' }, result_1)
232 assert_equal({'abc_key' => 'abc_value', 'cba_key' => 'cba_value',
233 'xyz_key' => 'xyz_value' }, result_2)
235 assert_raise(TypeError) do
236 { 'abc_key' => 'abc_value' }.merge "a"
240 assert('Hash#replace', '15.2.13.4.23') do
241 a = { 'abc_key' => 'abc_value' }
242 b = Hash.new.replace(a)
244 assert_equal({ 'abc_key' => 'abc_value' }, b)
249 assert_equal(42, b[1])
251 a = Hash.new{|h,x| x}
253 assert_equal(127, b[127])
255 assert_raise(TypeError) do
256 { 'abc_key' => 'abc_value' }.replace "a"
260 assert('Hash#shift', '15.2.13.4.24') do
261 a = { 'abc_key' => 'abc_value', 'cba_key' => 'cba_value' }
264 assert_equal Array, b.class
265 assert_equal 2, b.size
266 assert_equal 1, a.size
270 assert_equal Array, b.class
271 assert_equal 2, b.size
272 assert_equal 0, a.size
275 assert('Hash#size', '15.2.13.4.25') do
276 a = { 'abc_key' => 'abc_value' }
279 assert_equal 1, a.size
280 assert_equal 0, b.size
283 assert('Hash#store', '15.2.13.4.26') do
285 a.store('abc', 'abc')
287 assert_equal 'abc', a['abc']
290 assert('Hash#value?', '15.2.13.4.27') do
291 a = { 'abc_key' => 'abc_value' }
294 assert_true a.value?('abc_value')
295 assert_false b.value?('cba')
298 assert('Hash#values', '15.2.13.4.28') do
299 a = { 'abc_key' => 'abc_value' }
301 assert_equal ['abc_value'], a.values
306 assert('Hash#eql?') do
307 a = { 'a' => 1, 'b' => 2, 'c' => 3 }
308 b = { 'a' => 1, 'b' => 2, 'c' => 3 }
309 c = { 'a' => 1.0, 'b' => 2, 'c' => 3 }
310 assert_true(a.eql?(b))
311 assert_false(a.eql?(c))
312 assert_false(a.eql?(true))
315 assert('Hash#reject') do
316 h = {:one => 1, :two => 2, :three => 3, :four => 4}
317 ret = h.reject do |k,v|
320 assert_equal({:one => 1, :three => 3}, ret)
321 assert_equal({:one => 1, :two => 2, :three => 3, :four => 4}, h)
324 assert('Hash#reject!') do
325 h = {:one => 1, :two => 2, :three => 3, :four => 4}
326 ret = h.reject! do |k,v|
329 assert_equal({:one => 1, :three => 3}, ret)
330 assert_equal({:one => 1, :three => 3}, h)
333 assert('Hash#select') do
334 h = {:one => 1, :two => 2, :three => 3, :four => 4}
335 ret = h.select do |k,v|
338 assert_equal({:two => 2, :four => 4}, ret)
339 assert_equal({:one => 1, :two => 2, :three => 3, :four => 4}, h)
342 assert('Hash#select!') do
343 h = {:one => 1, :two => 2, :three => 3, :four => 4}
344 ret = h.select! do |k,v|
347 assert_equal({:two => 2, :four => 4}, ret)
348 assert_equal({:two => 2, :four => 4}, h)
353 assert('Hash#inspect') do
354 h = { "c" => 300, "a" => 100, "d" => 400, "c" => 300 }
357 assert_include ret, '"c"=>300'
358 assert_include ret, '"a"=>100'
359 assert_include ret, '"d"=>400'
362 assert('Hash#rehash') do
368 assert_equal("b", h[[:b]])
371 assert('Hash#freeze') do
373 assert_raise(FrozenError) do