PageRenderTime 54ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/projects/jruby-1.7.3/test/externals/ruby1.9/ruby/test_hash.rb

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Ruby | 923 lines | 769 code | 152 blank | 2 comment | 36 complexity | 646957daa76aab98b93753f71cd04f80 MD5 | raw file
  1. require 'test/unit'
  2. require 'continuation'
  3. class TestHash < Test::Unit::TestCase
  4. def test_hash
  5. x = {1=>2, 2=>4, 3=>6}
  6. y = {1=>2, 2=>4, 3=>6} # y = {1, 2, 2, 4, 3, 6} # 1.9 doesn't support
  7. assert_equal(2, x[1])
  8. assert(begin
  9. for k,v in y
  10. raise if k*2 != v
  11. end
  12. true
  13. rescue
  14. false
  15. end)
  16. assert_equal(3, x.length)
  17. assert(x.has_key?(1))
  18. assert(x.has_value?(4))
  19. assert_equal([4,6], x.values_at(2,3))
  20. assert_equal({1=>2, 2=>4, 3=>6}, x)
  21. z = y.keys.join(":")
  22. assert_equal("1:2:3", z)
  23. z = y.values.join(":")
  24. assert_equal("2:4:6", z)
  25. assert_equal(x, y)
  26. y.shift
  27. assert_equal(2, y.length)
  28. z = [1,2]
  29. y[z] = 256
  30. assert_equal(256, y[z])
  31. x = Hash.new(0)
  32. x[1] = 1
  33. assert_equal(1, x[1])
  34. assert_equal(0, x[2])
  35. x = Hash.new([])
  36. assert_equal([], x[22])
  37. assert_same(x[22], x[22])
  38. x = Hash.new{[]}
  39. assert_equal([], x[22])
  40. assert_not_same(x[22], x[22])
  41. x = Hash.new{|h,kk| z = kk; h[kk] = kk*2}
  42. z = 0
  43. assert_equal(44, x[22])
  44. assert_equal(22, z)
  45. z = 0
  46. assert_equal(44, x[22])
  47. assert_equal(0, z)
  48. x.default = 5
  49. assert_equal(5, x[23])
  50. x = Hash.new
  51. def x.default(k)
  52. $z = k
  53. self[k] = k*2
  54. end
  55. $z = 0
  56. assert_equal(44, x[22])
  57. assert_equal(22, $z)
  58. $z = 0
  59. assert_equal(44, x[22])
  60. assert_equal(0, $z)
  61. end
  62. # From rubicon
  63. def setup
  64. @cls = Hash
  65. @h = @cls[
  66. 1 => 'one', 2 => 'two', 3 => 'three',
  67. self => 'self', true => 'true', nil => 'nil',
  68. 'nil' => nil
  69. ]
  70. @verbose = $VERBOSE
  71. $VERBOSE = nil
  72. end
  73. def teardown
  74. $VERBOSE = @verbose
  75. end
  76. def test_s_AREF
  77. h = @cls["a" => 100, "b" => 200]
  78. assert_equal(100, h['a'])
  79. assert_equal(200, h['b'])
  80. assert_nil(h['c'])
  81. h = @cls.[]("a" => 100, "b" => 200)
  82. assert_equal(100, h['a'])
  83. assert_equal(200, h['b'])
  84. assert_nil(h['c'])
  85. end
  86. def test_s_new
  87. h = @cls.new
  88. assert_instance_of(@cls, h)
  89. assert_nil(h.default)
  90. assert_nil(h['spurious'])
  91. h = @cls.new('default')
  92. assert_instance_of(@cls, h)
  93. assert_equal('default', h.default)
  94. assert_equal('default', h['spurious'])
  95. end
  96. def test_AREF # '[]'
  97. t = Time.now
  98. h = @cls[
  99. 1 => 'one', 2 => 'two', 3 => 'three',
  100. self => 'self', t => 'time', nil => 'nil',
  101. 'nil' => nil
  102. ]
  103. assert_equal('one', h[1])
  104. assert_equal('two', h[2])
  105. assert_equal('three', h[3])
  106. assert_equal('self', h[self])
  107. assert_equal('time', h[t])
  108. assert_equal('nil', h[nil])
  109. assert_equal(nil, h['nil'])
  110. assert_equal(nil, h['koala'])
  111. h1 = h.dup
  112. h1.default = :default
  113. assert_equal('one', h1[1])
  114. assert_equal('two', h1[2])
  115. assert_equal('three', h1[3])
  116. assert_equal('self', h1[self])
  117. assert_equal('time', h1[t])
  118. assert_equal('nil', h1[nil])
  119. assert_equal(nil, h1['nil'])
  120. assert_equal(:default, h1['koala'])
  121. end
  122. def test_ASET # '[]='
  123. t = Time.now
  124. h = @cls.new
  125. h[1] = 'one'
  126. h[2] = 'two'
  127. h[3] = 'three'
  128. h[self] = 'self'
  129. h[t] = 'time'
  130. h[nil] = 'nil'
  131. h['nil'] = nil
  132. assert_equal('one', h[1])
  133. assert_equal('two', h[2])
  134. assert_equal('three', h[3])
  135. assert_equal('self', h[self])
  136. assert_equal('time', h[t])
  137. assert_equal('nil', h[nil])
  138. assert_equal(nil, h['nil'])
  139. assert_equal(nil, h['koala'])
  140. h[1] = 1
  141. h[nil] = 99
  142. h['nil'] = nil
  143. z = [1,2]
  144. h[z] = 256
  145. assert_equal(1, h[1])
  146. assert_equal('two', h[2])
  147. assert_equal('three', h[3])
  148. assert_equal('self', h[self])
  149. assert_equal('time', h[t])
  150. assert_equal(99, h[nil])
  151. assert_equal(nil, h['nil'])
  152. assert_equal(nil, h['koala'])
  153. assert_equal(256, h[z])
  154. end
  155. def test_EQUAL # '=='
  156. h1 = @cls[ "a" => 1, "c" => 2 ]
  157. h2 = @cls[ "a" => 1, "c" => 2, 7 => 35 ]
  158. h3 = @cls[ "a" => 1, "c" => 2, 7 => 35 ]
  159. h4 = @cls[ ]
  160. assert(h1 == h1)
  161. assert(h2 == h2)
  162. assert(h3 == h3)
  163. assert(h4 == h4)
  164. assert(!(h1 == h2))
  165. assert(h2 == h3)
  166. assert(!(h3 == h4))
  167. end
  168. def test_clear
  169. assert(@h.size > 0)
  170. @h.clear
  171. assert_equal(0, @h.size)
  172. assert_nil(@h[1])
  173. end
  174. def test_clone
  175. for taint in [ false, true ]
  176. for untrust in [ false, true ]
  177. for frozen in [ false, true ]
  178. a = @h.clone
  179. a.taint if taint
  180. a.untrust if untrust
  181. a.freeze if frozen
  182. b = a.clone
  183. assert_equal(a, b)
  184. assert(a.__id__ != b.__id__)
  185. assert_equal(a.frozen?, b.frozen?)
  186. assert_equal(a.untrusted?, b.untrusted?)
  187. assert_equal(a.tainted?, b.tainted?)
  188. end
  189. end
  190. end
  191. end
  192. def test_default
  193. assert_nil(@h.default)
  194. h = @cls.new(:xyzzy)
  195. assert_equal(:xyzzy, h.default)
  196. end
  197. def test_default=
  198. assert_nil(@h.default)
  199. @h.default = :xyzzy
  200. assert_equal(:xyzzy, @h.default)
  201. end
  202. def test_delete
  203. h1 = @cls[ 1 => 'one', 2 => 'two', true => 'true' ]
  204. h2 = @cls[ 1 => 'one', 2 => 'two' ]
  205. h3 = @cls[ 2 => 'two' ]
  206. assert_equal('true', h1.delete(true))
  207. assert_equal(h2, h1)
  208. assert_equal('one', h1.delete(1))
  209. assert_equal(h3, h1)
  210. assert_equal('two', h1.delete(2))
  211. assert_equal(@cls[], h1)
  212. assert_nil(h1.delete(99))
  213. assert_equal(@cls[], h1)
  214. assert_equal('default 99', h1.delete(99) {|i| "default #{i}" })
  215. end
  216. def test_delete_if
  217. base = @cls[ 1 => 'one', 2 => false, true => 'true', 'cat' => 99 ]
  218. h1 = @cls[ 1 => 'one', 2 => false, true => 'true' ]
  219. h2 = @cls[ 2 => false, 'cat' => 99 ]
  220. h3 = @cls[ 2 => false ]
  221. h = base.dup
  222. assert_equal(h, h.delete_if { false })
  223. assert_equal(@cls[], h.delete_if { true })
  224. h = base.dup
  225. assert_equal(h1, h.delete_if {|k,v| k.instance_of?(String) })
  226. assert_equal(h1, h)
  227. h = base.dup
  228. assert_equal(h2, h.delete_if {|k,v| v.instance_of?(String) })
  229. assert_equal(h2, h)
  230. h = base.dup
  231. assert_equal(h3, h.delete_if {|k,v| v })
  232. assert_equal(h3, h)
  233. h = base.dup
  234. n = 0
  235. h.delete_if {|*a|
  236. n += 1
  237. assert_equal(2, a.size)
  238. assert_equal(base[a[0]], a[1])
  239. h.shift
  240. true
  241. }
  242. assert_equal(base.size, n)
  243. end
  244. def test_keep_if
  245. h = {1=>2,3=>4,5=>6}
  246. assert_equal({3=>4,5=>6}, h.keep_if {|k, v| k + v >= 7 })
  247. h = {1=>2,3=>4,5=>6}
  248. assert_equal({1=>2,3=>4,5=>6}, h.keep_if{true})
  249. end
  250. def test_dup
  251. for taint in [ false, true ]
  252. for untrust in [ false, true ]
  253. for frozen in [ false, true ]
  254. a = @h.dup
  255. a.taint if taint
  256. a.freeze if frozen
  257. b = a.dup
  258. assert_equal(a, b)
  259. assert(a.__id__ != b.__id__)
  260. assert_equal(false, b.frozen?)
  261. assert_equal(a.tainted?, b.tainted?)
  262. assert_equal(a.untrusted?, b.untrusted?)
  263. end
  264. end
  265. end
  266. end
  267. def test_each
  268. count = 0
  269. @cls[].each { |k, v| count + 1 }
  270. assert_equal(0, count)
  271. h = @h
  272. h.each do |k, v|
  273. assert_equal(v, h.delete(k))
  274. end
  275. assert_equal(@cls[], h)
  276. end
  277. def test_each_key
  278. count = 0
  279. @cls[].each_key { |k| count + 1 }
  280. assert_equal(0, count)
  281. h = @h
  282. h.each_key do |k|
  283. h.delete(k)
  284. end
  285. assert_equal(@cls[], h)
  286. end
  287. def test_each_pair
  288. count = 0
  289. @cls[].each_pair { |k, v| count + 1 }
  290. assert_equal(0, count)
  291. h = @h
  292. h.each_pair do |k, v|
  293. assert_equal(v, h.delete(k))
  294. end
  295. assert_equal(@cls[], h)
  296. end
  297. def test_each_value
  298. res = []
  299. @cls[].each_value { |v| res << v }
  300. assert_equal(0, [].length)
  301. @h.each_value { |v| res << v }
  302. assert_equal(0, [].length)
  303. expected = []
  304. @h.each { |k, v| expected << v }
  305. assert_equal([], expected - res)
  306. assert_equal([], res - expected)
  307. end
  308. def test_empty?
  309. assert(@cls[].empty?)
  310. assert(!@h.empty?)
  311. end
  312. def test_fetch
  313. assert_equal('gumbygumby', @h.fetch('gumby') {|k| k * 2 })
  314. assert_equal('pokey', @h.fetch('gumby', 'pokey'))
  315. assert_equal('one', @h.fetch(1))
  316. assert_equal(nil, @h.fetch('nil'))
  317. assert_equal('nil', @h.fetch(nil))
  318. end
  319. def test_fetch_error
  320. assert_raise(KeyError) { @cls[].fetch(1) }
  321. assert_raise(KeyError) { @h.fetch('gumby') }
  322. e = assert_raise(KeyError) { @h.fetch('gumby'*20) }
  323. assert_match(/key not found: "gumbygumby/, e.message)
  324. assert_match(/\.\.\.\z/, e.message)
  325. end
  326. def test_key2?
  327. assert(!@cls[].key?(1))
  328. assert(!@cls[].key?(nil))
  329. assert(@h.key?(nil))
  330. assert(@h.key?(1))
  331. assert(!@h.key?('gumby'))
  332. end
  333. def test_value?
  334. assert(!@cls[].value?(1))
  335. assert(!@cls[].value?(nil))
  336. assert(@h.value?('one'))
  337. assert(@h.value?(nil))
  338. assert(!@h.value?('gumby'))
  339. end
  340. def test_include?
  341. assert(!@cls[].include?(1))
  342. assert(!@cls[].include?(nil))
  343. assert(@h.include?(nil))
  344. assert(@h.include?(1))
  345. assert(!@h.include?('gumby'))
  346. end
  347. def test_key
  348. assert_equal(1, @h.key('one'))
  349. assert_equal(nil, @h.key('nil'))
  350. assert_equal('nil', @h.key(nil))
  351. assert_equal(nil, @h.key('gumby'))
  352. assert_equal(nil, @cls[].key('gumby'))
  353. end
  354. def test_values_at
  355. res = @h.values_at('dog', 'cat', 'horse')
  356. assert(res.length == 3)
  357. assert_equal([nil, nil, nil], res)
  358. res = @h.values_at
  359. assert(res.length == 0)
  360. res = @h.values_at(3, 2, 1, nil)
  361. assert_equal 4, res.length
  362. assert_equal %w( three two one nil ), res
  363. res = @h.values_at(3, 99, 1, nil)
  364. assert_equal 4, res.length
  365. assert_equal ['three', nil, 'one', 'nil'], res
  366. end
  367. def test_invert
  368. h = @h.invert
  369. assert_equal(1, h['one'])
  370. assert_equal(true, h['true'])
  371. assert_equal(nil, h['nil'])
  372. h.each do |k, v|
  373. assert(@h.key?(v)) # not true in general, but works here
  374. end
  375. h = @cls[ 'a' => 1, 'b' => 2, 'c' => 1].invert
  376. assert_equal(2, h.length)
  377. assert(h[1] == 'a' || h[1] == 'c')
  378. assert_equal('b', h[2])
  379. end
  380. def test_key?
  381. assert(!@cls[].key?(1))
  382. assert(!@cls[].key?(nil))
  383. assert(@h.key?(nil))
  384. assert(@h.key?(1))
  385. assert(!@h.key?('gumby'))
  386. end
  387. def test_keys
  388. assert_equal([], @cls[].keys)
  389. keys = @h.keys
  390. expected = []
  391. @h.each { |k, v| expected << k }
  392. assert_equal([], keys - expected)
  393. assert_equal([], expected - keys)
  394. end
  395. def test_length
  396. assert_equal(0, @cls[].length)
  397. assert_equal(7, @h.length)
  398. end
  399. def test_member?
  400. assert(!@cls[].member?(1))
  401. assert(!@cls[].member?(nil))
  402. assert(@h.member?(nil))
  403. assert(@h.member?(1))
  404. assert(!@h.member?('gumby'))
  405. end
  406. def test_rehash
  407. a = [ "a", "b" ]
  408. c = [ "c", "d" ]
  409. h = @cls[ a => 100, c => 300 ]
  410. assert_equal(100, h[a])
  411. a[0] = "z"
  412. assert_nil(h[a])
  413. h.rehash
  414. assert_equal(100, h[a])
  415. end
  416. def test_reject
  417. base = @cls[ 1 => 'one', 2 => false, true => 'true', 'cat' => 99 ]
  418. h1 = @cls[ 1 => 'one', 2 => false, true => 'true' ]
  419. h2 = @cls[ 2 => false, 'cat' => 99 ]
  420. h3 = @cls[ 2 => false ]
  421. h = base.dup
  422. assert_equal(h, h.reject { false })
  423. assert_equal(@cls[], h.reject { true })
  424. h = base.dup
  425. assert_equal(h1, h.reject {|k,v| k.instance_of?(String) })
  426. assert_equal(h2, h.reject {|k,v| v.instance_of?(String) })
  427. assert_equal(h3, h.reject {|k,v| v })
  428. assert_equal(base, h)
  429. end
  430. def test_reject!
  431. base = @cls[ 1 => 'one', 2 => false, true => 'true', 'cat' => 99 ]
  432. h1 = @cls[ 1 => 'one', 2 => false, true => 'true' ]
  433. h2 = @cls[ 2 => false, 'cat' => 99 ]
  434. h3 = @cls[ 2 => false ]
  435. h = base.dup
  436. assert_equal(nil, h.reject! { false })
  437. assert_equal(@cls[], h.reject! { true })
  438. h = base.dup
  439. assert_equal(h1, h.reject! {|k,v| k.instance_of?(String) })
  440. assert_equal(h1, h)
  441. h = base.dup
  442. assert_equal(h2, h.reject! {|k,v| v.instance_of?(String) })
  443. assert_equal(h2, h)
  444. h = base.dup
  445. assert_equal(h3, h.reject! {|k,v| v })
  446. assert_equal(h3, h)
  447. end
  448. def test_replace
  449. h = @cls[ 1 => 2, 3 => 4 ]
  450. h1 = h.replace(@cls[ 9 => 8, 7 => 6 ])
  451. assert_equal(h, h1)
  452. assert_equal(8, h[9])
  453. assert_equal(6, h[7])
  454. assert_nil(h[1])
  455. assert_nil(h[2])
  456. end
  457. def test_shift
  458. h = @h.dup
  459. @h.length.times {
  460. k, v = h.shift
  461. assert(@h.key?(k))
  462. assert_equal(@h[k], v)
  463. }
  464. assert_equal(0, h.length)
  465. end
  466. def test_size
  467. assert_equal(0, @cls[].length)
  468. assert_equal(7, @h.length)
  469. end
  470. def test_sort
  471. h = @cls[].sort
  472. assert_equal([], h)
  473. h = @cls[ 1 => 1, 2 => 1 ].sort
  474. assert_equal([[1,1], [2,1]], h)
  475. h = @cls[ 'cat' => 'feline', 'ass' => 'asinine', 'bee' => 'beeline' ]
  476. h1 = h.sort
  477. assert_equal([ %w(ass asinine), %w(bee beeline), %w(cat feline)], h1)
  478. end
  479. def test_store
  480. t = Time.now
  481. h = @cls.new
  482. h.store(1, 'one')
  483. h.store(2, 'two')
  484. h.store(3, 'three')
  485. h.store(self, 'self')
  486. h.store(t, 'time')
  487. h.store(nil, 'nil')
  488. h.store('nil', nil)
  489. assert_equal('one', h[1])
  490. assert_equal('two', h[2])
  491. assert_equal('three', h[3])
  492. assert_equal('self', h[self])
  493. assert_equal('time', h[t])
  494. assert_equal('nil', h[nil])
  495. assert_equal(nil, h['nil'])
  496. assert_equal(nil, h['koala'])
  497. h.store(1, 1)
  498. h.store(nil, 99)
  499. h.store('nil', nil)
  500. assert_equal(1, h[1])
  501. assert_equal('two', h[2])
  502. assert_equal('three', h[3])
  503. assert_equal('self', h[self])
  504. assert_equal('time', h[t])
  505. assert_equal(99, h[nil])
  506. assert_equal(nil, h['nil'])
  507. assert_equal(nil, h['koala'])
  508. end
  509. def test_to_a
  510. assert_equal([], @cls[].to_a)
  511. assert_equal([[1,2]], @cls[ 1=>2 ].to_a)
  512. a = @cls[ 1=>2, 3=>4, 5=>6 ].to_a
  513. assert_equal([1,2], a.delete([1,2]))
  514. assert_equal([3,4], a.delete([3,4]))
  515. assert_equal([5,6], a.delete([5,6]))
  516. assert_equal(0, a.length)
  517. h = @cls[ 1=>2, 3=>4, 5=>6 ]
  518. h.taint
  519. h.untrust
  520. a = h.to_a
  521. assert_equal(true, a.tainted?)
  522. assert_equal(true, a.untrusted?)
  523. end
  524. def test_to_hash
  525. h = @h.to_hash
  526. assert_equal(@h, h)
  527. end
  528. def test_to_s
  529. h = @cls[ 1 => 2, "cat" => "dog", 1.5 => :fred ]
  530. assert_equal(h.inspect, h.to_s)
  531. $, = ":"
  532. assert_equal(h.inspect, h.to_s)
  533. h = @cls[]
  534. assert_equal(h.inspect, h.to_s)
  535. ensure
  536. $, = nil
  537. end
  538. def test_update
  539. h1 = @cls[ 1 => 2, 2 => 3, 3 => 4 ]
  540. h2 = @cls[ 2 => 'two', 4 => 'four' ]
  541. ha = @cls[ 1 => 2, 2 => 'two', 3 => 4, 4 => 'four' ]
  542. hb = @cls[ 1 => 2, 2 => 3, 3 => 4, 4 => 'four' ]
  543. assert_equal(ha, h1.update(h2))
  544. assert_equal(ha, h1)
  545. h1 = @cls[ 1 => 2, 2 => 3, 3 => 4 ]
  546. h2 = @cls[ 2 => 'two', 4 => 'four' ]
  547. assert_equal(hb, h2.update(h1))
  548. assert_equal(hb, h2)
  549. end
  550. def test_value2?
  551. assert(!@cls[].value?(1))
  552. assert(!@cls[].value?(nil))
  553. assert(@h.value?(nil))
  554. assert(@h.value?('one'))
  555. assert(!@h.value?('gumby'))
  556. end
  557. def test_values
  558. assert_equal([], @cls[].values)
  559. vals = @h.values
  560. expected = []
  561. @h.each { |k, v| expected << v }
  562. assert_equal([], vals - expected)
  563. assert_equal([], expected - vals)
  564. end
  565. def test_security_check
  566. h = {}
  567. assert_raise(SecurityError) do
  568. Thread.new do
  569. $SAFE = 4
  570. h[1] = 1
  571. end.join
  572. end
  573. end
  574. def test_intialize_wrong_arguments
  575. assert_raise(ArgumentError) do
  576. Hash.new(0) { }
  577. end
  578. end
  579. def test_create
  580. assert_equal({1=>2, 3=>4}, Hash[[[1,2],[3,4]]])
  581. assert_raise(ArgumentError) { Hash[0, 1, 2] }
  582. assert_equal({1=>2, 3=>4}, Hash[1,2,3,4])
  583. o = Object.new
  584. def o.to_hash() {1=>2} end
  585. assert_equal({1=>2}, Hash[o], "[ruby-dev:34555]")
  586. end
  587. def test_rehash2
  588. h = {1 => 2, 3 => 4}
  589. assert_equal(h.dup, h.rehash)
  590. assert_raise(RuntimeError) { h.each { h.rehash } }
  591. assert_equal({}, {}.rehash)
  592. end
  593. def test_fetch2
  594. assert_equal(:bar, @h.fetch(0, :foo) { :bar })
  595. end
  596. def test_default_proc
  597. h = Hash.new {|hh, k| hh + k + "baz" }
  598. assert_equal("foobarbaz", h.default_proc.call("foo", "bar"))
  599. h = {}
  600. assert_nil(h.default_proc)
  601. end
  602. def test_shift2
  603. h = Hash.new {|hh, k| :foo }
  604. h[1] = 2
  605. assert_equal([1, 2], h.shift)
  606. assert_equal(:foo, h.shift)
  607. assert_equal(:foo, h.shift)
  608. h = Hash.new(:foo)
  609. h[1] = 2
  610. assert_equal([1, 2], h.shift)
  611. assert_equal(:foo, h.shift)
  612. assert_equal(:foo, h.shift)
  613. h = {1=>2}
  614. h.each { assert_equal([1, 2], h.shift) }
  615. end
  616. def test_reject_bang2
  617. assert_equal({1=>2}, {1=>2,3=>4}.reject! {|k, v| k + v == 7 })
  618. assert_nil({1=>2,3=>4}.reject! {|k, v| k == 5 })
  619. assert_nil({}.reject! { })
  620. end
  621. def test_select
  622. assert_equal({3=>4,5=>6}, {1=>2,3=>4,5=>6}.select {|k, v| k + v >= 7 })
  623. end
  624. def test_select!
  625. h = {1=>2,3=>4,5=>6}
  626. assert_equal(h, h.select! {|k, v| k + v >= 7 })
  627. assert_equal({3=>4,5=>6}, h)
  628. h = {1=>2,3=>4,5=>6}
  629. assert_equal(nil, h.select!{true})
  630. end
  631. def test_clear2
  632. assert_equal({}, {1=>2,3=>4,5=>6}.clear)
  633. h = {1=>2,3=>4,5=>6}
  634. h.each { h.clear }
  635. assert_equal({}, h)
  636. end
  637. def test_replace2
  638. h1 = Hash.new { :foo }
  639. h2 = {}
  640. h2.replace h1
  641. assert_equal(:foo, h2[0])
  642. assert_raise(ArgumentError) { h2.replace() }
  643. assert_raise(TypeError) { h2.replace(1) }
  644. h2.freeze
  645. assert_raise(ArgumentError) { h2.replace() }
  646. assert_raise(RuntimeError) { h2.replace(h1) }
  647. assert_raise(RuntimeError) { h2.replace(42) }
  648. end
  649. def test_size2
  650. assert_equal(0, {}.size)
  651. end
  652. def test_equal2
  653. assert({} != 0)
  654. o = Object.new
  655. def o.to_hash; {}; end
  656. def o.==(x); true; end
  657. assert({} == o)
  658. def o.==(x); false; end
  659. assert({} != o)
  660. h1 = {1=>2}; h2 = {3=>4}
  661. assert(h1 != h2)
  662. h1 = {1=>2}; h2 = {1=>4}
  663. assert(h1 != h2)
  664. end
  665. def test_eql
  666. assert(!({}.eql?(0)))
  667. o = Object.new
  668. def o.to_hash; {}; end
  669. def o.eql?(x); true; end
  670. assert({}.eql?(o))
  671. def o.eql?(x); false; end
  672. assert(!({}.eql?(o)))
  673. end
  674. def test_hash2
  675. assert_kind_of(Integer, {}.hash)
  676. h = {1=>2}
  677. h.shift
  678. assert_equal({}.hash, h.hash, '[ruby-core:38650]')
  679. end
  680. def test_update2
  681. h1 = {1=>2, 3=>4}
  682. h2 = {1=>3, 5=>7}
  683. h1.update(h2) {|k, v1, v2| k + v1 + v2 }
  684. assert_equal({1=>6, 3=>4, 5=>7}, h1)
  685. end
  686. def test_merge
  687. h1 = {1=>2, 3=>4}
  688. h2 = {1=>3, 5=>7}
  689. assert_equal({1=>3, 3=>4, 5=>7}, h1.merge(h2))
  690. assert_equal({1=>6, 3=>4, 5=>7}, h1.merge(h2) {|k, v1, v2| k + v1 + v2 })
  691. end
  692. def test_assoc
  693. assert_equal([3,4], {1=>2, 3=>4, 5=>6}.assoc(3))
  694. assert_nil({1=>2, 3=>4, 5=>6}.assoc(4))
  695. end
  696. def test_rassoc
  697. assert_equal([3,4], {1=>2, 3=>4, 5=>6}.rassoc(4))
  698. assert_nil({1=>2, 3=>4, 5=>6}.rassoc(3))
  699. end
  700. def test_flatten
  701. assert_equal([[1], [2]], {[1] => [2]}.flatten)
  702. end
  703. def test_callcc
  704. h = {1=>2}
  705. c = nil
  706. f = false
  707. h.each { callcc {|c2| c = c2 } }
  708. unless f
  709. f = true
  710. c.call
  711. end
  712. assert_raise(RuntimeError) { h.each { h.rehash } }
  713. h = {1=>2}
  714. c = nil
  715. assert_raise(RuntimeError) do
  716. h.each { callcc {|c2| c = c2 } }
  717. h.clear
  718. c.call
  719. end
  720. end
  721. def test_compare_by_identity
  722. a = "foo"
  723. assert(!{}.compare_by_identity?)
  724. h = { a => "bar" }
  725. assert(!h.compare_by_identity?)
  726. h.compare_by_identity
  727. assert(h.compare_by_identity?)
  728. #assert_equal("bar", h[a])
  729. assert_nil(h["foo"])
  730. end
  731. class ObjWithHash
  732. def initialize(value, hash)
  733. @value = value
  734. @hash = hash
  735. end
  736. attr_reader :value, :hash
  737. def eql?(other)
  738. @value == other.value
  739. end
  740. end
  741. def test_hash_hash
  742. assert_equal({0=>2,11=>1}.hash, {11=>1,0=>2}.hash)
  743. o1 = ObjWithHash.new(0,1)
  744. o2 = ObjWithHash.new(11,1)
  745. assert_equal({o1=>1,o2=>2}.hash, {o2=>2,o1=>1}.hash)
  746. end
  747. def test_hash_bignum_hash
  748. x = 2<<(32-3)-1
  749. assert_equal({x=>1}.hash, {x=>1}.hash)
  750. x = 2<<(64-3)-1
  751. assert_equal({x=>1}.hash, {x=>1}.hash)
  752. o = Object.new
  753. def o.hash; 2 << 100; end
  754. assert_equal({x=>1}.hash, {x=>1}.hash)
  755. end
  756. def test_hash_poped
  757. assert_nothing_raised { eval("a = 1; {a => a}; a") }
  758. end
  759. def test_recursive_key
  760. h = {}
  761. assert_nothing_raised { h[h] = :foo }
  762. h.rehash
  763. assert_equal(:foo, h[h])
  764. end
  765. def test_inverse_hash
  766. feature4262 = '[ruby-core:34334]'
  767. [{1=>2}, {123=>"abc"}].each do |h|
  768. assert_not_equal(h.hash, h.invert.hash, feature4262)
  769. end
  770. end
  771. end