File: test_hash.rb

package info (click to toggle)
jruby 1.5.1-1
  • links: PTS, VCS
  • area: non-free
  • in suites: squeeze
  • size: 46,252 kB
  • ctags: 72,039
  • sloc: ruby: 398,155; java: 169,482; yacc: 3,782; xml: 2,469; ansic: 415; sh: 279; makefile: 78; tcl: 40
file content (59 lines) | stat: -rw-r--r-- 1,300 bytes parent folder | download | duplicates (5)
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
require 'test/unit'

class TestHash < Test::Unit::TestCase
  def test_clone_copies_default_initializer
    hash = Hash.new { |h, k| h[k] = 0 }
    clone = hash.clone
    assert_equal 0, clone[:test]
  end

  class Eql
    def initialize(hash); @hash=hash; end
    def eql?(other); true; end
    def hash; @hash; end
  end

  class Ee
    def initialize(hash); @hash=hash; end
    def ==(other); true; end
    def hash; @hash; end
  end

  class Equal
    def initialize(hash); @hash=hash; end
    def equal?(other); true; end
    def hash; @hash; end
  end

  def test_lookup_with_eql_and_same_hash_should_work
    eql1 = Eql.new(5)
    eql2 = Eql.new(5)

    hash = {eql1 => "bar"}
    assert_equal("bar", hash[eql1])
    assert_equal("bar", hash[eql2])
  end

  def test_lookup_with_eql_and_different_hash_should_not_work
    eql1 = Eql.new(5)
    eql2 = Eql.new(6)

    hash = {eql1 => "bar"}
    assert_equal("bar", hash[eql1])
    assert_nil(hash[eql2])
  end

  def test_lookup_with_ee_and_same_hash_should_not_work
    ee1 = Ee.new(5)
    ee2 = Ee.new(5)
    hash = {ee1 => 'bar'}
    assert_nil(hash[ee2])
  end

  def test_lookup_with_equal_and_same_hash_should_not_work
    equal1 = Equal.new(5)
    equal2 = Equal.new(5)
    hash = {equal1 => 'bar'}
    assert_nil(hash[equal2])
  end
end