~ubuntu-branches/ubuntu/trusty/ruby1.9/trusty

« back to all changes in this revision

Viewing changes to test/ruby/test_hash.rb

  • Committer: Bazaar Package Importer
  • Author(s): Stephan Hermann
  • Date: 2008-05-16 12:37:06 UTC
  • mfrom: (1.1.10 upstream)
  • Revision ID: james.westby@ubuntu.com-20080516123706-r4llcdfd35aobrjv
Tags: 1.9.0.1-1ubuntu1
* Merge from debian unstable, remaining changes:
  - Robustify check for target_os, fixing build failure on lpia.
* debian/control:
  - ruby1.9 pkg: moved rdoc1.9 suggestion to depends. (LP: #228345)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
require 'test/unit'
 
2
require 'continuation'
2
3
 
3
4
class TestHash < Test::Unit::TestCase
4
5
 
82
83
      self => 'self', true => 'true', nil => 'nil',
83
84
      'nil' => nil
84
85
    ]
 
86
    @verbose = $VERBOSE
 
87
    $VERBOSE = nil
 
88
  end
 
89
 
 
90
  def teardown
 
91
    $VERBOSE = @verbose
85
92
  end
86
93
 
87
94
  def test_s_AREF
644
651
    assert_equal([], expected - vals)
645
652
  end
646
653
 
 
654
  def test_security_check
 
655
    h = {}
 
656
    assert_raise(SecurityError) do
 
657
      Thread.new do
 
658
        $SAFE = 4
 
659
        h[1] = 1
 
660
      end.join
 
661
    end
 
662
  end
 
663
 
 
664
  def test_intialize_wrong_arguments
 
665
    assert_raise(ArgumentError) do
 
666
      Hash.new(0) { }
 
667
    end
 
668
  end
 
669
 
 
670
  def test_create
 
671
    assert_equal({1=>2, 3=>4}, Hash[[[1,2],[3,4]]])
 
672
    assert_raise(ArgumentError) { Hash[0, 1, 2] }
 
673
    assert_equal({1=>2, 3=>4}, Hash[1,2,3,4])
 
674
  end
 
675
 
 
676
  def test_rehash2
 
677
    h = {1 => 2, 3 => 4}
 
678
    assert_equal(h.dup, h.rehash)
 
679
    assert_raise(RuntimeError) { h.each { h.rehash } }
 
680
    assert_equal({}, {}.rehash)
 
681
  end
 
682
 
 
683
  def test_fetch2
 
684
    assert_equal(:bar, @h.fetch(0, :foo) { :bar })
 
685
  end
 
686
 
 
687
  def test_default_proc
 
688
    h = Hash.new {|h, k| h + k + "baz" }
 
689
    assert_equal("foobarbaz", h.default_proc.call("foo", "bar"))
 
690
    h = {}
 
691
    assert_nil(h.default_proc)
 
692
  end
 
693
 
 
694
  def test_shift2
 
695
    h = Hash.new {|h, k| :foo }
 
696
    h[1] = 2
 
697
    assert_equal([1, 2], h.shift)
 
698
    assert_equal(:foo, h.shift)
 
699
    assert_equal(:foo, h.shift)
 
700
 
 
701
    h = Hash.new(:foo)
 
702
    h[1] = 2
 
703
    assert_equal([1, 2], h.shift)
 
704
    assert_equal(:foo, h.shift)
 
705
    assert_equal(:foo, h.shift)
 
706
 
 
707
    h = {1=>2}
 
708
    h.each { assert_equal([1, 2], h.shift) }
 
709
  end
 
710
 
 
711
  def test_reject_bang2
 
712
    assert_equal({1=>2}, {1=>2,3=>4}.reject! {|k, v| k + v == 7 })
 
713
    assert_nil({1=>2,3=>4}.reject! {|k, v| k == 5 })
 
714
    assert_nil({}.reject! { })
 
715
  end
 
716
 
 
717
  def test_select
 
718
    assert_equal({3=>4,5=>6}, {1=>2,3=>4,5=>6}.select {|k, v| k + v >= 7 })
 
719
  end
 
720
 
 
721
  def test_clear
 
722
    assert_equal({}, {1=>2,3=>4,5=>6}.clear)
 
723
    h = {1=>2,3=>4,5=>6}
 
724
    h.each { h.clear }
 
725
    assert_equal({}, h)
 
726
  end
 
727
 
 
728
  def test_replace2
 
729
    h1 = Hash.new { :foo }
 
730
    h2 = {}
 
731
    h2.replace h1
 
732
    assert_equal(:foo, h2[0])
 
733
  end
 
734
 
 
735
  def test_size2
 
736
    assert_equal(0, {}.size)
 
737
  end
 
738
 
 
739
  def test_equal2
 
740
    assert({} != 0)
 
741
    o = Object.new
 
742
    def o.to_hash; {}; end
 
743
    def o.==(x); true; end
 
744
    assert({} == o)
 
745
    def o.==(x); false; end
 
746
    assert({} != o)
 
747
 
 
748
    h1 = {1=>2}; h2 = {3=>4}
 
749
    assert(h1 != h2)
 
750
    h1 = {1=>2}; h2 = {1=>4}
 
751
    assert(h1 != h2)
 
752
 
 
753
    h1 = {}; h1[h1] = h1; h1.rehash
 
754
    h2 = {}; h2[h2] = h2; h2.rehash
 
755
    assert(h1 != h2)
 
756
  end
 
757
 
 
758
  def test_eql
 
759
    assert(!({}.eql?(0)))
 
760
    o = Object.new
 
761
    def o.to_hash; {}; end
 
762
    def o.eql?(x); true; end
 
763
    assert({}.eql?(o))
 
764
    def o.eql?(x); false; end
 
765
    assert(!({}.eql?(o)))
 
766
  end
 
767
 
 
768
  def test_hash2
 
769
    assert_kind_of(Integer, {}.hash)
 
770
  end
 
771
 
 
772
  def test_update2
 
773
    h1 = {1=>2, 3=>4}
 
774
    h2 = {1=>3, 5=>7}
 
775
    h1.update(h2) {|k, v1, v2| k + v1 + v2 }
 
776
    assert_equal({1=>6, 3=>4, 5=>7}, h1)
 
777
  end
 
778
 
 
779
  def test_merge
 
780
    h1 = {1=>2, 3=>4}
 
781
    h2 = {1=>3, 5=>7}
 
782
    assert_equal({1=>3, 3=>4, 5=>7}, h1.merge(h2))
 
783
    assert_equal({1=>6, 3=>4, 5=>7}, h1.merge(h2) {|k, v1, v2| k + v1 + v2 })
 
784
  end
 
785
 
 
786
  def test_assoc
 
787
    assert_equal([3,4], {1=>2, 3=>4, 5=>6}.assoc(3))
 
788
    assert_nil({1=>2, 3=>4, 5=>6}.assoc(4))
 
789
  end
 
790
 
 
791
  def test_rassoc
 
792
    assert_equal([3,4], {1=>2, 3=>4, 5=>6}.rassoc(4))
 
793
    assert_nil({1=>2, 3=>4, 5=>6}.rassoc(3))
 
794
  end
 
795
 
 
796
  def test_flatten
 
797
    assert_equal([[1], [2]], {[1] => [2]}.flatten)
 
798
  end
 
799
 
 
800
  def test_callcc
 
801
    h = {1=>2}
 
802
    c = nil
 
803
    f = false
 
804
    h.each { callcc {|c2| c = c2 } }
 
805
    unless f
 
806
      f = true
 
807
      c.call
 
808
    end
 
809
    assert_raise(RuntimeError) { h.each { h.rehash } }
 
810
 
 
811
    h = {1=>2}
 
812
    c = nil
 
813
    assert_raise(RuntimeError) do
 
814
      h.each { callcc {|c2| c = c2 } }
 
815
      h.clear
 
816
      c.call
 
817
    end
 
818
  end
 
819
 
 
820
  def test_compare_by_identity
 
821
    a = "foo"
 
822
    assert(!{}.compare_by_identity?)
 
823
    h = { a => "bar" }
 
824
    assert(!h.compare_by_identity?)
 
825
    h.compare_by_identity
 
826
    assert(h.compare_by_identity?)
 
827
    #assert_equal("bar", h[a])
 
828
    assert_nil(h["foo"])
 
829
  end
647
830
end