summaryrefslogtreecommitdiffstats
path: root/web/server/h2o/libh2o/deps/mruby/test/t/hash.rb
blob: c63b8c00973ac74489091deca816a6a36414820a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
##
# Hash ISO Test

assert('Hash', '15.2.13') do
  assert_equal Class, Hash.class
end

assert('Hash#==', '15.2.13.4.1') do
  assert_true({ 'abc' => 'abc' } == { 'abc' => 'abc' })
  assert_false({ 'abc' => 'abc' } ==  { 'cba' => 'cba' })
  assert_true({ :equal => 1 } == { :equal => 1.0 })
  assert_false({ :a => 1 } == true)
end

assert('Hash#[]', '15.2.13.4.2') do
  a = { 'abc' => 'abc' }

  assert_equal 'abc', a['abc']

  # Hash#[] should call #default (#3272)
  hash = {}
  def hash.default(k); self[k] = 1; end
  hash[:foo] += 1

  assert_equal 2, hash[:foo]
end

assert('Hash#[]=', '15.2.13.4.3') do
  a = Hash.new
  a['abc'] = 'abc'

  assert_equal 'abc', a['abc']
end

assert('Hash#clear', '15.2.13.4.4') do
  a = { 'abc' => 'abc' }
  a.clear

  assert_equal({ }, a)
end

assert('Hash#dup') do
  a = { 'a' => 1 }
  b = a.dup
  a['a'] = 2
  assert_equal({'a' => 1}, b)

  c = Hash.new { |h, k| h[k] = k.upcase }
  d = c.dup
  assert_equal("FOO", d["foo"])
end

assert('Hash#default', '15.2.13.4.5') do
  a = Hash.new
  b = Hash.new('abc')
  c = Hash.new {|s,k| s[k] = k}

  assert_nil a.default
  assert_equal 'abc', b.default
  assert_nil c.default
  assert_equal 'abc', c.default('abc')
end

assert('Hash#default=', '15.2.13.4.6') do
  a = { 'abc' => 'abc' }
  a.default = 'cba'

  assert_equal 'abc', a['abc']
  assert_equal 'cba', a['notexist']
end

assert('Hash#default_proc', '15.2.13.4.7') do
  a = Hash.new
  b = Hash.new {|s,k| s[k] = k + k}
  c = b[2]
  d = b['cat']

  assert_nil a.default_proc
  assert_equal Proc, b.default_proc.class
  assert_equal 4, c
  assert_equal 'catcat', d
end

assert('Hash#delete', '15.2.13.4.8') do
  a = { 'abc' => 'abc' }
  b = { 'abc' => 'abc' }
  b_tmp_1 = false
  b_tmp_2 = false

  a.delete('abc')
  b.delete('abc') do |k|
    b_tmp_1 = true
  end
  b.delete('abc') do |k|
    b_tmp_2 = true
  end

  assert_nil a.delete('cba')
  assert_false a.has_key?('abc')
  assert_false b_tmp_1
  assert_true b_tmp_2
end

assert('Hash#each', '15.2.13.4.9') do
  a = { 'abc_key' => 'abc_value' }
  key = nil
  value = nil

  a.each  do |k,v|
    key = k
    value = v
  end

  assert_equal 'abc_key', key
  assert_equal 'abc_value', value
end

assert('Hash#each_key', '15.2.13.4.10') do
  a = { 'abc_key' => 'abc_value' }
  key = nil

  a.each_key  do |k|
    key = k
  end

  assert_equal 'abc_key', key
end

assert('Hash#each_value', '15.2.13.4.11') do
  a = { 'abc_key' => 'abc_value' }
  value = nil

  a.each_value  do |v|
    value = v
  end

  assert_equal 'abc_value', value
end

assert('Hash#empty?', '15.2.13.4.12') do
  a = { 'abc_key' => 'abc_value' }
  b = Hash.new

  assert_false a.empty?
  assert_true b.empty?
end

assert('Hash#has_key?', '15.2.13.4.13') do
  a = { 'abc_key' => 'abc_value' }
  b = Hash.new

  assert_true a.has_key?('abc_key')
  assert_false b.has_key?('cba')
end

assert('Hash#has_value?', '15.2.13.4.14') do
  a = { 'abc_key' => 'abc_value' }
  b = Hash.new

  assert_true a.has_value?('abc_value')
  assert_false b.has_value?('cba')
end

assert('Hash#include?', '15.2.13.4.15') do
  a = { 'abc_key' => 'abc_value' }
  b = Hash.new

  assert_true a.include?('abc_key')
  assert_false b.include?('cba')
end

assert('Hash#initialize', '15.2.13.4.16') do
  # Testing initialize by new.
  h = Hash.new
  h2 = Hash.new(:not_found)

  assert_true h.is_a? Hash
  assert_equal({ }, h)
  assert_nil h["hello"]
  assert_equal :not_found, h2["hello"]
end

assert('Hash#initialize_copy', '15.2.13.4.17') do
  a = { 'abc_key' => 'abc_value' }
  b = Hash.new.initialize_copy(a)

  assert_equal({ 'abc_key' => 'abc_value' }, b)
end

assert('Hash#key?', '15.2.13.4.18') do
  a = { 'abc_key' => 'abc_value' }
  b = Hash.new

  assert_true a.key?('abc_key')
  assert_false b.key?('cba')
end

assert('Hash#keys', '15.2.13.4.19') do
  a = { 'abc_key' => 'abc_value' }

  assert_equal ['abc_key'], a.keys
end

assert('Hash#length', '15.2.13.4.20') do
  a = { 'abc_key' => 'abc_value' }
  b = Hash.new

  assert_equal 1, a.length
  assert_equal 0, b.length
end

assert('Hash#member?', '15.2.13.4.21') do
  a = { 'abc_key' => 'abc_value' }
  b = Hash.new

  assert_true a.member?('abc_key')
  assert_false b.member?('cba')
end

assert('Hash#merge', '15.2.13.4.22') do
  a = { 'abc_key' => 'abc_value', 'cba_key' => 'cba_value' }
  b = { 'cba_key' => 'XXX',  'xyz_key' => 'xyz_value' }

  result_1 = a.merge b
  result_2 = a.merge(b) do |key, original, new|
    original
  end

  assert_equal({'abc_key' => 'abc_value', 'cba_key' => 'XXX',
                'xyz_key' => 'xyz_value' }, result_1)
  assert_equal({'abc_key' => 'abc_value', 'cba_key' => 'cba_value',
                'xyz_key' => 'xyz_value' }, result_2)

  assert_raise(TypeError) do
    { 'abc_key' => 'abc_value' }.merge "a"
  end
end

assert('Hash#replace', '15.2.13.4.23') do
  a = { 'abc_key' => 'abc_value' }
  b = Hash.new.replace(a)

  assert_equal({ 'abc_key' => 'abc_value' }, b)

  a = Hash.new(42)
  b = {}
  b.replace(a)
  assert_equal(42, b[1])

  a = Hash.new{|h,x| x}
  b.replace(a)
  assert_equal(127, b[127])

   assert_raise(TypeError) do
    { 'abc_key' => 'abc_value' }.replace "a"
  end
end

assert('Hash#shift', '15.2.13.4.24') do
  a = { 'abc_key' => 'abc_value', 'cba_key' => 'cba_value' }
  b = a.shift

  assert_equal Array, b.class
  assert_equal 2, b.size
  assert_equal 1, a.size

  b = a.shift

  assert_equal Array, b.class
  assert_equal 2, b.size
  assert_equal 0, a.size
end

assert('Hash#size', '15.2.13.4.25') do
  a = { 'abc_key' => 'abc_value' }
  b = Hash.new

  assert_equal 1, a.size
  assert_equal 0, b.size
end

assert('Hash#store', '15.2.13.4.26') do
  a = Hash.new
  a.store('abc', 'abc')

  assert_equal 'abc', a['abc']
end

assert('Hash#value?', '15.2.13.4.27') do
  a = { 'abc_key' => 'abc_value' }
  b = Hash.new

  assert_true a.value?('abc_value')
  assert_false b.value?('cba')
end

assert('Hash#values', '15.2.13.4.28') do
  a = { 'abc_key' => 'abc_value' }

  assert_equal ['abc_value'], a.values
end

# Not ISO specified

assert('Hash#eql?') do
  a = { 'a' => 1, 'b' => 2, 'c' => 3 }
  b = { 'a' => 1, 'b' => 2, 'c' => 3 }
  c = { 'a' => 1.0, 'b' => 2, 'c' => 3 }
  assert_true(a.eql?(b))
  assert_false(a.eql?(c))
  assert_false(a.eql?(true))
end

assert('Hash#reject') do
  h = {:one => 1, :two => 2, :three => 3, :four => 4}
  ret = h.reject do |k,v|
    v % 2 == 0
  end
  assert_equal({:one => 1, :three => 3}, ret)
  assert_equal({:one => 1, :two => 2, :three => 3, :four => 4}, h)
end

assert('Hash#reject!') do
  h = {:one => 1, :two => 2, :three => 3, :four => 4}
  ret = h.reject! do |k,v|
    v % 2 == 0
  end
  assert_equal({:one => 1, :three => 3}, ret)
  assert_equal({:one => 1, :three => 3}, h)
end

assert('Hash#select') do
  h = {:one => 1, :two => 2, :three => 3, :four => 4}
  ret = h.select do |k,v|
    v % 2 == 0
  end
  assert_equal({:two => 2, :four => 4}, ret)
  assert_equal({:one => 1, :two => 2, :three => 3, :four => 4}, h)
end

assert('Hash#select!') do
  h = {:one => 1, :two => 2, :three => 3, :four => 4}
  ret = h.select! do |k,v|
    v % 2 == 0
  end
  assert_equal({:two => 2, :four => 4}, ret)
  assert_equal({:two => 2, :four => 4}, h)
end

# Not ISO specified

assert('Hash#inspect') do
  h = { "c" => 300, "a" => 100, "d" => 400, "c" => 300  }
  ret = h.to_s

  assert_include ret, '"c"=>300'
  assert_include ret, '"a"=>100'
  assert_include ret, '"d"=>400'
end

assert('Hash#rehash') do
  h = {[:a] => "b"}
  # hash key modified
  h.keys[0][0] = :b
  # h[[:b]] => nil
  h.rehash
  assert_equal("b", h[[:b]])
end

assert('Hash#freeze') do
  h = {}.freeze
  assert_raise(RuntimeError) do
    h[:a] = 'b'
  end
end