[前][次][番号順一覧][スレッド一覧]

ruby-changes:3891

From: ko1@a...
Date: Wed, 6 Feb 2008 00:50:39 +0900 (JST)
Subject: [ruby-changes:3891] mame - Ruby:r15380 (trunk): * test/ruby/test_hash.rb: add tests to achieve over 90% test coverage

mame	2008-02-06 00:50:25 +0900 (Wed, 06 Feb 2008)

  New Revision: 15380

  Modified files:
    trunk/ChangeLog
    trunk/test/ruby/test_env.rb
    trunk/test/ruby/test_hash.rb

  Log:
    * test/ruby/test_hash.rb: add tests to achieve over 90% test coverage
      of hash.c.
    * test/ruby/test_env.rb: ditto.


  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/test/ruby/test_hash.rb?r1=15380&r2=15379&diff_format=u
  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/ChangeLog?r1=15380&r2=15379&diff_format=u
  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/test/ruby/test_env.rb?r1=15380&r2=15379&diff_format=u

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 15379)
+++ ChangeLog	(revision 15380)
@@ -1,3 +1,9 @@
+Wed Feb 06 00:48:41 2008  Yusuke Endoh  <mame@t...>
+
+	* test/ruby/test_hash.rb: add tests to achieve over 90% test coverage
+	  of hash.c.
+	* test/ruby/test_env.rb: ditto.
+
 Wed Feb 06 00:24:49 2008  Yusuke Endoh  <mame@t...>
 
 	* hash.c (env_rassoc): remove access to free'd environment on mswin32.
Index: test/ruby/test_hash.rb
===================================================================
--- test/ruby/test_hash.rb	(revision 15379)
+++ test/ruby/test_hash.rb	(revision 15380)
@@ -1,4 +1,5 @@
 require 'test/unit'
+require 'continuation'
 
 class TestHash < Test::Unit::TestCase
 
@@ -82,8 +83,14 @@
       self => 'self', true => 'true', nil => 'nil',
       'nil' => nil
     ]
+    @verbose = $VERBOSE
+    $VERBOSE = nil
   end
 
+  def teardown
+    $VERBOSE = @verbose
+  end
+
   def test_s_AREF
     h = @cls["a" => 100, "b" => 200]
     assert_equal(100, h['a'])
@@ -644,4 +651,180 @@
     assert_equal([], expected - vals)
   end
 
+  def test_security_check
+    h = {}
+    assert_raise(SecurityError) do
+      Thread.new do
+        $SAFE = 4
+        h[1] = 1
+      end.join
+    end
+  end
+
+  def test_intialize_wrong_arguments
+    assert_raise(ArgumentError) do
+      Hash.new(0) { }
+    end
+  end
+
+  def test_create
+    assert_equal({1=>2, 3=>4}, Hash[[[1,2],[3,4]]])
+    assert_raise(ArgumentError) { Hash[0, 1, 2] }
+    assert_equal({1=>2, 3=>4}, Hash[1,2,3,4])
+  end
+
+  def test_rehash2
+    h = {1 => 2, 3 => 4}
+    assert_equal(h.dup, h.rehash)
+    assert_raise(RuntimeError) { h.each { h.rehash } }
+    assert_equal({}, {}.rehash)
+  end
+
+  def test_fetch2
+    assert_equal(:bar, @h.fetch(0, :foo) { :bar })
+  end
+
+  def test_default_proc
+    h = Hash.new {|h, k| h + k + "baz" }
+    assert_equal("foobarbaz", h.default_proc.call("foo", "bar"))
+    h = {}
+    assert_nil(h.default_proc)
+  end
+
+  def test_shift2
+    h = Hash.new {|h, k| :foo }
+    h[1] = 2
+    assert_equal([1, 2], h.shift)
+    assert_equal(:foo, h.shift)
+    assert_equal(:foo, h.shift)
+
+    h = Hash.new(:foo)
+    h[1] = 2
+    assert_equal([1, 2], h.shift)
+    assert_equal(:foo, h.shift)
+    assert_equal(:foo, h.shift)
+
+    h = {1=>2}
+    h.each { assert_equal([1, 2], h.shift) }
+  end
+
+  def test_reject_bang2
+    assert_equal({1=>2}, {1=>2,3=>4}.reject! {|k, v| k + v == 7 })
+    assert_nil({1=>2,3=>4}.reject! {|k, v| k == 5 })
+    assert_nil({}.reject! { })
+  end
+
+  def test_select
+    assert_equal({3=>4,5=>6}, {1=>2,3=>4,5=>6}.select {|k, v| k + v >= 7 })
+  end
+
+  def test_clear
+    assert_equal({}, {1=>2,3=>4,5=>6}.clear)
+    h = {1=>2,3=>4,5=>6}
+    h.each { h.clear }
+    assert_equal({}, h)
+  end
+
+  def test_replace2
+    h1 = Hash.new { :foo }
+    h2 = {}
+    h2.replace h1
+    assert_equal(:foo, h2[0])
+  end
+
+  def test_size2
+    assert_equal(0, {}.size)
+  end
+
+  def test_equal2
+    assert({} != 0)
+    o = Object.new
+    def o.to_hash; {}; end
+    def o.==(x); true; end
+    assert({} == o)
+    def o.==(x); false; end
+    assert({} != o)
+
+    h1 = {1=>2}; h2 = {3=>4}
+    assert(h1 != h2)
+    h1 = {1=>2}; h2 = {1=>4}
+    assert(h1 != h2)
+
+    h1 = {}; h1[h1] = h1; h1.rehash
+    h2 = {}; h2[h2] = h2; h2.rehash
+    assert(h1 != h2)
+  end
+
+  def test_eql
+    assert(!({}.eql?(0)))
+    o = Object.new
+    def o.to_hash; {}; end
+    def o.eql?(x); true; end
+    assert({}.eql?(o))
+    def o.eql?(x); false; end
+    assert(!({}.eql?(o)))
+  end
+
+  def test_hash2
+    assert_kind_of(Integer, {}.hash)
+  end
+
+  def test_update2
+    h1 = {1=>2, 3=>4}
+    h2 = {1=>3, 5=>7}
+    h1.update(h2) {|k, v1, v2| k + v1 + v2 }
+    assert_equal({1=>6, 3=>4, 5=>7}, h1)
+  end
+
+  def test_merge
+    h1 = {1=>2, 3=>4}
+    h2 = {1=>3, 5=>7}
+    assert_equal({1=>3, 3=>4, 5=>7}, h1.merge(h2))
+    assert_equal({1=>6, 3=>4, 5=>7}, h1.merge(h2) {|k, v1, v2| k + v1 + v2 })
+  end
+
+  def test_assoc
+    assert_equal([3,4], {1=>2, 3=>4, 5=>6}.assoc(3))
+    assert_nil({1=>2, 3=>4, 5=>6}.assoc(4))
+  end
+
+  def test_rassoc
+    assert_equal([3,4], {1=>2, 3=>4, 5=>6}.rassoc(4))
+    assert_nil({1=>2, 3=>4, 5=>6}.rassoc(3))
+  end
+
+  def test_flatten
+    assert_equal([1, 2], {[1] => [2]}.flatten)
+  end
+
+  def test_callcc
+    h = {1=>2}
+    c = nil
+    f = false
+    h.each { callcc {|c2| c = c2 } }
+    unless f
+      f = true
+      c.call
+    end
+    assert_raise(RuntimeError) { h.each { h.rehash } }
+
+    h = {1=>2}
+    c = nil
+    assert_raise(RuntimeError) do
+      h.each { callcc {|c2| c = c2 } }
+      h.clear
+      c.call
+    end
+  end
+
+  def test_compare_by_identity
+    a = "foo"
+    assert(!{}.compare_by_identity?)
+    h = { a => "bar" }
+    assert(!h.compare_by_identity?)
+    h.compare_by_identity
+    assert(h.compare_by_identity?)
+    #assert_equal("bar", h[a])
+    assert_nil(h["foo"])
+  end
 end
Index: test/ruby/test_env.rb
===================================================================
--- test/ruby/test_env.rb	(revision 15379)
+++ test/ruby/test_env.rb	(revision 15380)
@@ -2,17 +2,20 @@
 
 class TestEnv < Test::Unit::TestCase
   IGNORE_CASE = /djgpp|bccwin|mswin|mingw/ =~ RUBY_PLATFORM
+  PATH_ENV = /human68k/ =~ RUBY_PLATFORM ? "path" : "PATH"
 
   def setup
-    @backup = ENV['test']
-    @BACKUP = ENV['TEST']
+    @verbose = $VERBOSE
+    $VERBOSE = nil
+    @backup = ENV.to_hash
     ENV.delete('test')
     ENV.delete('TEST')
   end
 
   def teardown
-    ENV['test'] = @backup if @backup
-    ENV['TEST'] = @BACKUP if @BACKUP
+    $VERBOSE = @verbose
+    ENV.clear
+    @backup.each {|k, v| ENV[k] = v }
   end
 
   def test_bracket
@@ -81,4 +84,267 @@
       assert_equal('test', ENV.key(val.upcase))
     end
   end
+
+  def test_delete
+    assert_raise(ArgumentError) { ENV.delete("foo\0bar") }
+    assert_nil(ENV.delete("TEST"))
+    assert_nothing_raised { ENV.delete(PATH_ENV) }
+  end
+
+  def test_getenv
+    assert_raise(ArgumentError) { ENV["foo\0bar"] }
+    ENV[PATH_ENV] = ""
+    assert_equal("", ENV[PATH_ENV])
+  end
+
+  def test_fetch
+    ENV["test"] = "foo"
+    assert_equal("foo", ENV.fetch("test"))
+    ENV.delete("test")
+    assert_raise(KeyError) { ENV.fetch("test") }
+    assert_equal("foo", ENV.fetch("test", "foo"))
+    assert_equal("bar", ENV.fetch("test") { "bar" })
+    assert_equal("bar", ENV.fetch("test", "foo") { "bar" })
+    assert_raise(ArgumentError) { ENV.fetch("foo\0bar") }
+    assert_nothing_raised { ENV.fetch(PATH_ENV, "foo") }
+    ENV[PATH_ENV] = ""
+    assert_equal("", ENV.fetch(PATH_ENV))
+  end
+
+  def test_aset
+    assert_raise(SecurityError) do
+      Thread.new do
+        $SAFE = 4
+        ENV["test"] = "foo"
+      end.join
+    end
+    assert_raise(TypeError) { ENV["test"] = nil }
+    assert_raise(ArgumentError) { ENV["foo\0bar"] = "test" }
+    assert_raise(ArgumentError) { ENV["test"] = "foo\0bar" }
+    ENV[PATH_ENV] = "/tmp/".taint
+    assert_equal("/tmp/", ENV[PATH_ENV])
+  end
+
+  def test_keys
+    a = nil
+    assert_block { a = ENV.keys }
+    assert_kind_of(Array, a)
+    a.each {|k| assert_kind_of(String, k) }
+  end
+
+  def test_each_key
+    ENV.each_key {|k| assert_kind_of(String, k) }
+  end
+
+  def test_values
+    a = nil
+    assert_block { a = ENV.values }
+    assert_kind_of(Array, a)
+    a.each {|k| assert_kind_of(String, k) }
+  end
+
+  def test_each_value
+    ENV.each_value {|k| assert_kind_of(String, k) }
+  end
+
+  def test_each_pair
+    ENV.each_pair do |k, v|
+      assert_kind_of(String, k)
+      assert_kind_of(String, v)
+    end
+  end
+
+  def test_reject_bang
+    h1 = {}
+    ENV.each_pair {|k, v| h1[k] = v }
+    ENV["test"] = "foo"
+    ENV.reject! {|k, v| IGNORE_CASE ? k.upcase == "TEST" : k == "test" }
+    h2 = {}
+    ENV.each_pair {|k, v| h2[k] = v }
+    assert_equal(h1, h2)
+
+    h1 = {}
+    ENV.each_pair {|k, v| h1[k] = v }
+    ENV["test"] = "foo"
+    ENV.delete_if {|k, v| IGNORE_CASE ? k.upcase == "TEST" : k == "test" }
+    h2 = {}
+    ENV.each_pair {|k, v| h2[k] = v }
+    assert_equal(h1, h2)
+  end
+
+  def test_values_at
+    ENV["test"] = "foo"
+    assert_equal(["foo", "foo"], ENV.values_at("test", "test"))
+  end
+
+  def test_select
+    ENV["test"] = "foo"
+    h = ENV.select {|k| IGNORE_CASE ? k.upcase == "TEST" : k == "test" }
+    assert_equal(1, h.size)
+    k = h.keys.first
+    v = h.values.first
+    if IGNORE_CASE
+      assert_equal("TEST", k.upcase)
+      assert_equal("FOO", v.upcase)
+    else
+      assert_equal("test", k)
+      assert_equal("foo", v)
+    end
+  end
+
+  def test_clear
+    ENV.clear
+    assert_equal(0, ENV.size)
+  end
+
+  def test_to_s
+    assert_equal("ENV", ENV.to_s)
+  end
+
+  def test_inspect
+    ENV.clear
+    ENV["foo"] = "bar"
+    ENV["baz"] = "qux"
+    s = ENV.inspect
+    if IGNORE_CASE
+      s = s.upcase
+      assert(s == '{"FOO"=>"BAR", "BAZ"=>"QUX"}' || s == '{"BAZ"=>"QUX", "FOO"=>"BAR"}')
+    else
+      assert(s == '{"foo"=>"bar", "baz"=>"qux"}' || s == '{"baz"=>"qux", "foo"=>"bar"}')
+    end
+  end
+
+  def test_to_a
+    ENV.clear
+    ENV["foo"] = "bar"
+    ENV["baz"] = "qux"
+    a = ENV.to_a
+    assert_equal(2, a.size)
+    if IGNORE_CASE
+      a = a.map {|x| x.map {|y| y.upcase } }
+      assert(a == [%w(FOO BAR), %w(BAZ QUX)] || a == [%w(BAZ QUX), %w(FOO BAR)])
+    else
+      assert(a == [%w(foo bar), %w(baz qux)] || a == [%w(baz qux), %w(foo bar)])
+    end
+  end
+
+  def test_rehash
+    assert_nil(ENV.rehash)
+  end
+
+  def test_size
+    s = ENV.size
+    ENV["test"] = "foo"
+    assert_equal(s + 1, ENV.size)
+  end
+
+  def test_empty_p
+    ENV.clear
+    assert(ENV.empty?)
+    ENV["test"] = "foo"
+    assert(!ENV.empty?)
+  end
+
+  def test_has_key
+    assert(!ENV.has_key?("test"))
+    ENV["test"] = "foo"
+    assert(ENV.has_key?("test"))
+    assert_raise(ArgumentError) { ENV.has_key?("foo\0bar") }
+  end
+
+  def test_assoc
+    assert_nil(ENV.assoc("test"))
+    ENV["test"] = "foo"
+    k, v = ENV.assoc("test")
+    if IGNORE_CASE
+      assert_equal("TEST", k.upcase)
+      assert_equal("FOO", v.upcase)
+    else
+      assert_equal("test", k)
+      assert_equal("foo", v)
+    end
+    assert_raise(ArgumentError) { ENV.assoc("foo\0bar") }
+  end
+
+  def test_has_value
+    ENV.clear
+    assert(!ENV.has_value?("foo"))
+    ENV["test"] = "foo"
+    assert(ENV.has_value?("foo"))
+  end
+
+  def test_rassoc
+    ENV.clear
+    assert_nil(ENV.rassoc("foo"))
+    ENV["foo"] = "bar"
+    ENV["test"] = "foo"
+    ENV["baz"] = "qux"
+    k, v = ENV.rassoc("foo")
+    if IGNORE_CASE
+      assert_equal("TEST", k.upcase)
+      assert_equal("FOO", v.upcase)
+    else
+      assert_equal("test", k)
+      assert_equal("foo", v)
+    end
+  end
+
+  def test_to_hash
+    h = {}
+    ENV.each {|k, v| h[k] = v }
+    assert_equal(h, ENV.to_hash)
+  end
+
+  def test_reject
+    h1 = {}
+    ENV.each_pair {|k, v| h1[k] = v }
+    ENV["test"] = "foo"
+    h2 = ENV.reject {|k, v| IGNORE_CASE ? k.upcase == "TEST" : k == "test" }
+    assert_equal(h1, h2)
+  end
+
+  def check(as, bs)
+    if IGNORE_CASE
+      as = as.map {|xs| xs.map {|x| x.upcase } }
+      bs = bs.map {|xs| xs.map {|x| x.upcase } }
+    end
+    assert_equal(as.sort, bs.sort)
+  end
+
+  def test_shift
+    ENV.clear
+    ENV["foo"] = "bar"
+    ENV["baz"] = "qux"
+    a = ENV.shift
+    b = ENV.shift
+    check([a, b], [%w(foo bar), %w(baz qux)])
+    assert_nil(ENV.shift)
+  end
+
+  def test_invert
+    ENV.clear
+    ENV["foo"] = "bar"
+    ENV["baz"] = "qux"
+    check(ENV.invert.to_a, [%w(bar foo), %w(qux baz)])
+  end
+
+  def test_replace
+    ENV["foo"] = "xxx"
+    ENV.replace({"foo"=>"bar", "baz"=>"qux"})
+    check(ENV.to_hash.to_a, [%w(foo bar), %w(baz qux)])
+  end
+
+  def test_update
+    ENV.clear
+    ENV["foo"] = "bar"
+    ENV["baz"] = "qux"
+    ENV.update({"baz"=>"quux","a"=>"b"})
+    check(ENV.to_hash.to_a, [%w(foo bar), %w(baz quux), %w(a b)])
+
+    ENV.clear
+    ENV["foo"] = "bar"
+    ENV["baz"] = "qux"
+    ENV.update({"baz"=>"quux","a"=>"b"}) {|k, v1, v2| v1 ? k + "_" + v1 + "_" + v2 : v2 }
+    check(ENV.to_hash.to_a, [%w(foo bar), %w(baz baz_qux_quux), %w(a b)])
+  end
 end

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]