yarv-diff:202
From: ko1 atdot.net
Date: 2 Feb 2006 14:21:23 -0000
Subject: [yarv-diff:202] r360 - in trunk: . test/ruby
Author: aamine
Date: 2006-02-02 23:21:23 +0900 (Thu, 02 Feb 2006)
New Revision: 360
Added:
trunk/test/ruby/envutil.rb
trunk/test/ruby/marshaltestlib.rb
trunk/test/ruby/test_array.rb
trunk/test/ruby/test_beginendblock.rb
trunk/test/ruby/test_clone.rb
trunk/test/ruby/test_dir.rb
trunk/test/ruby/test_env.rb
trunk/test/ruby/test_float.rb
trunk/test/ruby/test_fnmatch.rb
trunk/test/ruby/test_hash.rb
trunk/test/ruby/test_io.rb
trunk/test/ruby/test_marshal.rb
trunk/test/ruby/test_math.rb
trunk/test/ruby/test_pack.rb
trunk/test/ruby/test_path.rb
trunk/test/ruby/test_pipe.rb
trunk/test/ruby/test_rand.rb
trunk/test/ruby/test_range.rb
trunk/test/ruby/test_readpartial.rb
trunk/test/ruby/test_regexp.rb
trunk/test/ruby/test_settracefunc.rb
trunk/test/ruby/test_signal.rb
trunk/test/ruby/test_sprintf.rb
trunk/test/ruby/test_string.rb
trunk/test/ruby/test_stringchar.rb
trunk/test/ruby/test_struct.rb
trunk/test/ruby/test_symbol.rb
trunk/test/ruby/test_system.rb
trunk/test/ruby/test_time.rb
trunk/test/ruby/ut_eof.rb
Modified:
trunk/ChangeLog
Log:
* test/ruby/envutil.rb: new file (imported from ruby trunk CVS HEAD).
* test/ruby/marshaltestlib.rb: ditto.
* test/ruby/test_array.rb: ditto.
* test/ruby/test_beginendblock.rb: ditto.
* test/ruby/test_clone.rb: ditto.
* test/ruby/test_dir.rb: ditto.
* test/ruby/test_env.rb: ditto.
* test/ruby/test_file.rb: ditto.
* test/ruby/test_float.rb: ditto.
* test/ruby/test_fnmatch.rb: ditto.
* test/ruby/test_hash.rb: ditto.
* test/ruby/test_io.rb: ditto.
* test/ruby/test_marshal.rb: ditto.
* test/ruby/test_math.rb: ditto.
* test/ruby/test_pack.rb: ditto.
* test/ruby/test_path.rb: ditto.
* test/ruby/test_pipe.rb: ditto.
* test/ruby/test_rand.rb: ditto.
* test/ruby/test_range.rb: ditto.
* test/ruby/test_readpartial.rb: ditto.
* test/ruby/test_regexp.rb: ditto.
* test/ruby/test_settracefunc.rb: ditto.
* test/ruby/test_signal.rb: ditto.
* test/ruby/test_sprintf.rb: ditto.
* test/ruby/test_string.rb: ditto.
* test/ruby/test_stringchar.rb: ditto.
* test/ruby/test_struct.rb: ditto.
* test/ruby/test_symbol.rb: ditto.
* test/ruby/test_system.rb: ditto.
* test/ruby/test_time.rb: ditto.
* test/ruby/ut_eof.rb: ditto.
Modified: trunk/ChangeLog
===================================================================
--- trunk/ChangeLog 2006-02-02 13:54:06 UTC (rev 359)
+++ trunk/ChangeLog 2006-02-02 14:21:23 UTC (rev 360)
@@ -4,6 +4,71 @@
# from Mon, 03 May 2004 01:24:19 +0900
#
+2006-02-02(Thu) 23:20:13 +0900 Minero Aoki <aamine loveruby.net>
+
+ * test/ruby/envutil.rb: new file (imported from ruby trunk HEAD).
+
+ * test/ruby/marshaltestlib.rb: ditto.
+
+ * test/ruby/test_array.rb: ditto.
+
+ * test/ruby/test_beginendblock.rb: ditto.
+
+ * test/ruby/test_clone.rb: ditto.
+
+ * test/ruby/test_dir.rb: ditto.
+
+ * test/ruby/test_env.rb: ditto.
+
+ * test/ruby/test_file.rb: ditto.
+
+ * test/ruby/test_float.rb: ditto.
+
+ * test/ruby/test_fnmatch.rb: ditto.
+
+ * test/ruby/test_hash.rb: ditto.
+
+ * test/ruby/test_io.rb: ditto.
+
+ * test/ruby/test_marshal.rb: ditto.
+
+ * test/ruby/test_math.rb: ditto.
+
+ * test/ruby/test_pack.rb: ditto.
+
+ * test/ruby/test_path.rb: ditto.
+
+ * test/ruby/test_pipe.rb: ditto.
+
+ * test/ruby/test_rand.rb: ditto.
+
+ * test/ruby/test_range.rb: ditto.
+
+ * test/ruby/test_readpartial.rb: ditto.
+
+ * test/ruby/test_regexp.rb: ditto.
+
+ * test/ruby/test_settracefunc.rb: ditto.
+
+ * test/ruby/test_signal.rb: ditto.
+
+ * test/ruby/test_sprintf.rb: ditto.
+
+ * test/ruby/test_string.rb: ditto.
+
+ * test/ruby/test_stringchar.rb: ditto.
+
+ * test/ruby/test_struct.rb: ditto.
+
+ * test/ruby/test_symbol.rb: ditto.
+
+ * test/ruby/test_system.rb: ditto.
+
+ * test/ruby/test_time.rb: ditto.
+
+ * test/ruby/ut_eof.rb: ditto.
+
+
2006-02-02(Thu) 22:53:44 +0900 Minero Aoki <aamine loveruby.net>
* test/ruby/test_proc.rb: test [yarv-dev:777].
Added: trunk/test/ruby/envutil.rb
===================================================================
--- trunk/test/ruby/envutil.rb 2006-02-02 13:54:06 UTC (rev 359)
+++ trunk/test/ruby/envutil.rb 2006-02-02 14:21:23 UTC (rev 360)
@@ -0,0 +1,24 @@
+module EnvUtil
+ def rubybin
+ if ruby = ENV["RUBY"]
+ return ruby
+ end
+ ruby = "ruby"
+ 3.times do
+ if File.exist? ruby or File.exist? ruby+".exe"
+ return File.expand_path(ruby)
+ end
+ ruby = File.join("..", ruby)
+ end
+ begin
+ require "rbconfig"
+ File.join(
+ Config::CONFIG["bindir"],
+ Config::CONFIG["ruby_install_name"] + Config::CONFIG["EXEEXT"]
+ )
+ rescue LoadError
+ "ruby"
+ end
+ end
+ module_function :rubybin
+end
Added: trunk/test/ruby/marshaltestlib.rb
===================================================================
--- trunk/test/ruby/marshaltestlib.rb 2006-02-02 13:54:06 UTC (rev 359)
+++ trunk/test/ruby/marshaltestlib.rb 2006-02-02 14:21:23 UTC (rev 360)
@@ -0,0 +1,494 @@
+module MarshalTestLib
+ # include this module to a Test::Unit::TestCase and definde encode(o) and
+ # decode(s) methods. e.g.
+ #
+ # def encode(o)
+ # SOAPMarshal.dump(o)
+ # end
+ #
+ # def decode(s)
+ # SOAPMarshal.load(s)
+ # end
+
+ NegativeZero = (-1.0 / (1.0 / 0.0))
+
+ module Mod1; end
+ module Mod2; end
+
+ def marshaltest(o1)
+ str = encode(o1)
+ print str, "\n" if $DEBUG
+ o2 = decode(str)
+ o2
+ end
+
+ def marshal_equal(o1, msg = nil)
+ msg = msg ? msg + "(#{ caller[0] })" : caller[0]
+ o2 = marshaltest(o1)
+ assert_equal(o1.class, o2.class, msg)
+ iv1 = o1.instance_variables.sort
+ iv2 = o2.instance_variables.sort
+ assert_equal(iv1, iv2)
+ val1 = iv1.map {|var| o1.instance_eval {eval var}}
+ val2 = iv1.map {|var| o2.instance_eval {eval var}}
+ assert_equal(val1, val2, msg)
+ if block_given?
+ assert_equal(yield(o1), yield(o2), msg)
+ else
+ assert_equal(o1, o2, msg)
+ end
+ end
+
+ class MyObject; def initialize(v) @v = v end; attr_reader :v; end
+ def test_object
+ o1 = Object.new
+ o1.instance_eval { @iv = 1 }
+ marshal_equal(o1) {|o| o.instance_eval { @iv }}
+ end
+
+ def test_object_subclass
+ marshal_equal(MyObject.new(2)) {|o| o.v}
+ end
+
+ def test_object_extend
+ o1 = Object.new
+ o1.extend(Mod1)
+ marshal_equal(o1) { |o|
+ (class << self; self; end).ancestors
+ }
+ o1.extend(Mod2)
+ marshal_equal(o1) { |o|
+ (class << self; self; end).ancestors
+ }
+ end
+
+ def test_object_subclass_extend
+ o1 = MyObject.new(2)
+ o1.extend(Mod1)
+ marshal_equal(o1) { |o|
+ (class << self; self; end).ancestors
+ }
+ o1.extend(Mod2)
+ marshal_equal(o1) { |o|
+ (class << self; self; end).ancestors
+ }
+ end
+
+ class MyArray < Array
+ def initialize(v, *args)
+ super(args)
+ @v = v
+ end
+ end
+ def test_array
+ marshal_equal(5)
+ marshal_equal([1,2,3])
+ end
+
+ def test_array_subclass
+ marshal_equal(MyArray.new(0, 1, 2, 3))
+ end
+
+ def test_array_ivar
+ o1 = Array.new
+ o1.instance_eval { @iv = 1 }
+ marshal_equal(o1) {|o| o.instance_eval { @iv }}
+ end
+
+ class MyException < Exception; def initialize(v, *args) super(*args); @v = v; end; attr_reader :v; end
+ def test_exception
+ marshal_equal(Exception.new('foo')) {|o| o.message}
+ marshal_equal(assert_raise(NoMethodError) {no_such_method()}) {|o| o.message}
+ end
+
+ def test_exception_subclass
+ marshal_equal(MyException.new(20, "bar")) {|o| [o.message, o.v]}
+ end
+
+ def test_false
+ marshal_equal(false)
+ end
+
+ class MyHash < Hash; def initialize(v, *args) super(*args); @v = v; end end
+ def test_hash
+ marshal_equal({1=>2, 3=>4})
+ end
+
+ def test_hash_default
+ h = Hash.new(:default)
+ h[5] = 6
+ marshal_equal(h)
+ end
+
+ def test_hash_subclass
+ h = MyHash.new(7, 8)
+ h[4] = 5
+ marshal_equal(h)
+ end
+
+ def test_hash_default_proc
+ h = Hash.new {}
+ assert_raises(TypeError) { marshaltest(h) }
+ end
+
+ def test_hash_ivar
+ o1 = Hash.new
+ o1.instance_eval { @iv = 1 }
+ marshal_equal(o1) {|o| o.instance_eval { @iv }}
+ end
+
+ def test_hash_extend
+ o1 = Hash.new
+ o1.extend(Mod1)
+ marshal_equal(o1) { |o|
+ (class << self; self; end).ancestors
+ }
+ o1.extend(Mod2)
+ marshal_equal(o1) { |o|
+ (class << self; self; end).ancestors
+ }
+ end
+
+ def test_hash_subclass_extend
+ o1 = MyHash.new(2)
+ o1.extend(Mod1)
+ marshal_equal(o1) { |o|
+ (class << self; self; end).ancestors
+ }
+ o1.extend(Mod2)
+ marshal_equal(o1) { |o|
+ (class << self; self; end).ancestors
+ }
+ end
+
+ def test_bignum
+ marshal_equal(-0x4000_0000_0000_0001)
+ marshal_equal(-0x4000_0001)
+ marshal_equal(0x4000_0000)
+ marshal_equal(0x4000_0000_0000_0000)
+ end
+
+ def test_fixnum
+ marshal_equal(-0x4000_0000)
+ marshal_equal(-0x3fff_ffff)
+ marshal_equal(-1)
+ marshal_equal(0)
+ marshal_equal(1)
+ marshal_equal(0x3fff_ffff)
+ end
+
+ def test_fixnum_ivar
+ o1 = 1
+ o1.instance_eval { @iv = 2 }
+ marshal_equal(o1) {|o| o.instance_eval { @iv }}
+ ensure
+ 1.instance_eval { remove_instance_variable("@iv") }
+ end
+
+ def test_fixnum_ivar_self
+ o1 = 1
+ o1.instance_eval { @iv = 1 }
+ marshal_equal(o1) {|o| o.instance_eval { @iv }}
+ ensure
+ 1.instance_eval { remove_instance_variable("@iv") }
+ end
+
+ def test_float
+ marshal_equal(-1.0)
+ marshal_equal(0.0)
+ marshal_equal(1.0)
+ end
+
+ def test_float_inf_nan
+ marshal_equal(1.0/0.0)
+ marshal_equal(-1.0/0.0)
+ marshal_equal(0.0/0.0) {|o| o.nan?}
+ marshal_equal(NegativeZero) {|o| 1.0/o}
+ end
+
+ def test_float_ivar
+ o1 = 1.23
+ o1.instance_eval { @iv = 1 }
+ marshal_equal(o1) {|o| o.instance_eval { @iv }}
+ end
+
+ def test_float_ivar_self
+ o1 = 5.5
+ o1.instance_eval { @iv = o1 }
+ marshal_equal(o1) {|o| o.instance_eval { @iv }}
+ end
+
+ def test_float_extend
+ o1 = 0.0/0.0
+ o1.extend(Mod1)
+ marshal_equal(o1) { |o|
+ (class << self; self; end).ancestors
+ }
+ o1.extend(Mod2)
+ marshal_equal(o1) { |o|
+ (class << self; self; end).ancestors
+ }
+ end
+
+ class MyRange < Range; def initialize(v, *args) super(*args); @v = v; end end
+ def test_range
+ marshal_equal(1..2)
+ marshal_equal(1...3)
+ end
+
+ def test_range_subclass
+ marshal_equal(MyRange.new(4,5,8, false))
+ end
+
+ class MyRegexp < Regexp; def initialize(v, *args) super(*args); @v = v; end end
+ def test_regexp
+ marshal_equal(/a/)
+ marshal_equal(/A/i)
+ marshal_equal(/A/mx)
+ end
+
+ def test_regexp_subclass
+ marshal_equal(MyRegexp.new(10, "a"))
+ end
+
+ class MyString < String; def initialize(v, *args) super(*args); @v = v; end end
+ def test_string
+ marshal_equal("abc")
+ end
+
+ def test_string_ivar
+ o1 = ""
+ o1.instance_eval { @iv = 1 }
+ marshal_equal(o1) {|o| o.instance_eval { @iv }}
+ end
+
+ def test_string_subclass
+ marshal_equal(MyString.new(10, "a"))
+ end
+
+ def test_string_subclass_cycle
+ str = MyString.new(10, "b")
+ str.instance_eval { @v = str }
+ marshal_equal(str) { |o|
+ assert_equal(o.__id__, o.instance_eval { @v }.__id__)
+ o.instance_eval { @v }
+ }
+ end
+
+ def test_string_subclass_extend
+ o = "abc"
+ o.extend(Mod1)
+ str = MyString.new(o, "c")
+ marshal_equal(str) { |o|
+ assert(o.instance_eval { @v }).kind_of?(Mod1)
+ }
+ end
+
+ MyStruct = Struct.new("MyStruct", :a, :b)
+ if RUBY_VERSION < "1.8.0"
+ # Struct#== is not defined in ruby/1.6
+ class MyStruct
+ def ==(rhs)
+ return true if __id__ == rhs.__id__
+ return false unless rhs.is_a?(::Struct)
+ return false if self.class != rhs.class
+ members.each do |member|
+ return false if self.__send__(member) != rhs.__send__(member)
+ end
+ return true
+ end
+ end
+ end
+ class MySubStruct < MyStruct; def initialize(v, *args) super(*args); @v = v; end end
+ def test_struct
+ marshal_equal(MyStruct.new(1,2))
+ end
+
+ def test_struct_subclass
+ if RUBY_VERSION < "1.8.0"
+ # Substruct instance cannot be dumped in ruby/1.6
+ # ::Marshal.dump(MySubStruct.new(10, 1, 2)) #=> uninitialized struct
+ return false
+ end
+ marshal_equal(MySubStruct.new(10,1,2))
+ end
+
+ def test_struct_ivar
+ o1 = MyStruct.new
+ o1.instance_eval { @iv = 1 }
+ marshal_equal(o1) {|o| o.instance_eval { @iv }}
+ end
+
+ def test_struct_subclass_extend
+ o1 = MyStruct.new
+ o1.extend(Mod1)
+ marshal_equal(o1) { |o|
+ (class << self; self; end).ancestors
+ }
+ o1.extend(Mod2)
+ marshal_equal(o1) { |o|
+ (class << self; self; end).ancestors
+ }
+ end
+
+ def test_symbol
+ marshal_equal(:a)
+ marshal_equal(:a?)
+ marshal_equal(:a!)
+ marshal_equal(:a=)
+ marshal_equal(:|)
+ marshal_equal(:^)
+ marshal_equal(:&)
+ marshal_equal(:<=>)
+ marshal_equal(:==)
+ marshal_equal(:===)
+ marshal_equal(:=~)
+ marshal_equal(:>)
+ marshal_equal(:>=)
+ marshal_equal(:<)
+ marshal_equal(:<=)
+ marshal_equal(:<<)
+ marshal_equal(:>>)
+ marshal_equal(:+)
+ marshal_equal(:-)
+ marshal_equal(:*)
+ marshal_equal(:/)
+ marshal_equal(:%)
+ marshal_equal(:**)
+ marshal_equal(:~)
+ marshal_equal(:+@)
+ marshal_equal(:-@)
+ marshal_equal(:[])
+ marshal_equal(:[]=)
+ marshal_equal(:`) #`
+ marshal_equal("a b".intern)
+ end
+
+ class MyTime < Time; def initialize(v, *args) super(*args); @v = v; end end
+ def test_time
+ # once there was a bug caused by usec overflow. try a little harder.
+ 10.times do
+ t = Time.now
+ marshal_equal(t, t.usec.to_s)
+ end
+ end
+
+ def test_time_subclass
+ marshal_equal(MyTime.new(10))
+ end
+
+ def test_time_ivar
+ o1 = Time.now
+ o1.instance_eval { @iv = 1 }
+ marshal_equal(o1) {|o| o.instance_eval { @iv }}
+ end
+
+ def test_true
+ marshal_equal(true)
+ end
+
+ def test_nil
+ marshal_equal(nil)
+ end
+
+ def test_share
+ o = [:share]
+ o1 = [o, o]
+ o2 = marshaltest(o1)
+ assert_same(o2.first, o2.last)
+ end
+
+ class CyclicRange < Range
+ def <=>(other); true; end
+ end
+ def test_range_cyclic
+ return unless CyclicRange.respond_to?(:allocate) # test for 1.8
+ o1 = CyclicRange.allocate
+ o1.instance_eval { initialize(o1, o1) }
+ o2 = marshaltest(o1)
+ assert_same(o2, o2.begin)
+ assert_same(o2, o2.end)
+ end
+
+ def test_singleton
+ o = Object.new
+ def o.m() end
+ assert_raises(TypeError) { marshaltest(o) }
+ o = Object.new
+ c = class << o
+ @v = 1
+ class C; self; end
+ end
+ assert_raises(TypeError) { marshaltest(o) }
+ assert_raises(TypeError) { marshaltest(c) }
+ assert_raises(TypeError) { marshaltest(ARGF) }
+ assert_raises(TypeError) { marshaltest(ENV) }
+ end
+
+ def test_extend
+ o = Object.new
+ o.extend Mod1
+ marshal_equal(o) { |obj| obj.kind_of? Mod1 }
+ o = Object.new
+ o.extend Mod1
+ o.extend Mod2
+ marshal_equal(o) {|obj| class << obj; ancestors end}
+ o = Object.new
+ o.extend Module.new
+ assert_raises(TypeError) { marshaltest(o) }
+ end
+
+ def test_extend_string
+ o = ""
+ o.extend Mod1
+ marshal_equal(o) { |obj| obj.kind_of? Mod1 }
+ o = ""
+ o.extend Mod1
+ o.extend Mod2
+ marshal_equal(o) {|obj| class << obj; ancestors end}
+ o = ""
+ o.extend Module.new
+ assert_raises(TypeError) { marshaltest(o) }
+ end
+
+ def test_anonymous
+ c = Class.new
+ assert_raises(TypeError) { marshaltest(c) }
+ o = c.new
+ assert_raises(TypeError) { marshaltest(o) }
+ m = Module.new
+ assert_raises(TypeError) { marshaltest(m) }
+ end
+
+ def test_string_empty
+ marshal_equal("")
+ end
+
+ def test_string_crlf
+ marshal_equal("\r\n")
+ end
+
+ def test_string_escape
+ marshal_equal("\0<;;>\1;;")
+ end
+
+ MyStruct2 = Struct.new(:a, :b)
+ if RUBY_VERSION < "1.8.0"
+ # Struct#== is not defined in ruby/1.6
+ class MyStruct2
+ def ==(rhs)
+ return true if __id__ == rhs.__id__
+ return false unless rhs.is_a?(::Struct)
+ return false if self.class != rhs.class
+ members.each do |member|
+ return false if self.__send__(member) != rhs.__send__(member)
+ end
+ return true
+ end
+ end
+ end
+ def test_struct_toplevel
+ o = MyStruct2.new(1,2)
+ marshal_equal(o)
+ end
+end
Added: trunk/test/ruby/test_array.rb
===================================================================
--- trunk/test/ruby/test_array.rb 2006-02-02 13:54:06 UTC (rev 359)
+++ trunk/test/ruby/test_array.rb 2006-02-02 14:21:23 UTC (rev 360)
@@ -0,0 +1,173 @@
+require 'test/unit'
+
+class TestArray < Test::Unit::TestCase
+ def test_array
+ assert_equal([1, 2, 3, 4], [1, 2] + [3, 4])
+ assert_equal([1, 2, 1, 2], [1, 2] * 2)
+ assert_equal("1:2", [1, 2] * ":")
+
+ assert_equal([1, 2].hash, [1, 2].hash)
+
+ assert_equal([2,3], [1,2,3] & [2,3,4])
+ assert_equal([1,2,3,4], [1,2,3] | [2,3,4])
+ assert_equal([1,2,3] - [2,3], [1])
+
+ x = [0, 1, 2, 3, 4, 5]
+ assert_equal(2, x[2])
+ assert_equal([1, 2, 3], x[1..3])
+ assert_equal([1, 2, 3], x[1,3])
+
+ x[0, 2] = 10
+ assert(x[0] == 10 && x[1] == 2)
+
+ x[0, 0] = -1
+ assert(x[0] == -1 && x[1] == 10)
+
+ x[-1, 1] = 20
+ assert(x[-1] == 20 && x.pop == 20)
+ end
+
+ def test_array_andor
+ assert_equal([2], ([1,2,3]&[2,4,6]))
+ assert_equal([1,2,3,4,6], ([1,2,3]|[2,4,6]))
+ end
+
+ def test_compact
+ x = [nil, 1, nil, nil, 5, nil, nil]
+ x.compact!
+ assert_equal([1, 5], x)
+ end
+
+ def test_uniq
+ x = [1, 1, 4, 2, 5, 4, 5, 1, 2]
+ x.uniq!
+ assert_equal([1, 4, 2, 5], x)
+
+ # empty?
+ assert(!x.empty?)
+ x = []
+ assert(x.empty?)
+ end
+
+ def test_sort
+ x = ["it", "came", "to", "pass", "that", "..."]
+ x = x.sort.join(" ")
+ assert_equal("... came it pass that to", x)
+ x = [2,5,3,1,7]
+ x.sort!{|a,b| a<=>b} # sort with condition
+ assert_equal([1,2,3,5,7], x)
+ x.sort!{|a,b| b-a} # reverse sort
+ assert_equal([7,5,3,2,1], x)
+ end
+
+ def test_split
+ x = "The Boassert of Mormon"
+ assert_equal(x.reverse, x.split(//).reverse!.join)
+ assert_equal(x.reverse, x.reverse!)
+ assert_equal("g:n:i:r:t:s: :e:t:y:b: :1", "1 byte string".split(//).reverse.join(":"))
+ x = "a b c d"
+ assert_equal(['a', 'b', 'c', 'd'], x.split)
+ assert_equal(['a', 'b', 'c', 'd'], x.split(' '))
+ end
+
+ def test_misc
+ assert(defined? "a".chomp)
+ assert_equal(["a", "b", "c"], "abc".scan(/./))
+ assert_equal([["1a"], ["2b"], ["3c"]], "1a2b3c".scan(/(\d.)/))
+ # non-greedy match
+ assert_equal([["a", "12"], ["b", "22"]], "a=12;b=22".scan(/(.*?)=(\d*);?/))
+
+ x = [1]
+ assert_equal('1:1:1:1:1', (x * 5).join(":"))
+ assert_equal('1', (x * 1).join(":"))
+ assert_equal('', (x * 0).join(":"))
+
+ *x = *(1..7).to_a
+ assert_equal(7, x.size)
+ assert_equal([1, 2, 3, 4, 5, 6, 7], x)
+
+ x = [1,2,3]
+ x[1,0] = x
+ assert_equal([1,1,2,3,2,3], x)
+
+ x = [1,2,3]
+ x[-1,0] = x
+ assert_equal([1,2,1,2,3,3], x)
+
+ x = [1,2,3]
+ x.concat(x)
+ assert_equal([1,2,3,1,2,3], x)
+
+ x = [1,2,3]
+ x.clear
+ assert_equal([], x)
+
+ x = [1,2,3]
+ y = x.dup
+ x << 4
+ y << 5
+ assert_equal([1,2,3,4], x)
+ assert_equal([1,2,3,5], y)
+ end
+
+ def test_beg_end
+ x = [1, 2, 3, 4, 5]
+
+ assert_equal(1, x.first)
+ assert_equal([1], x.first(1))
+ assert_equal([1, 2, 3], x.first(3))
+
+ assert_equal(5, x.last)
+ assert_equal([5], x.last(1))
+ assert_equal([3, 4, 5], x.last(3))
+
+ assert_equal(1, x.shift)
+ assert_equal([2, 3, 4], x.shift(3))
+ assert_equal([5], x)
+
+ assert_equal([2, 3, 4, 5], x.unshift(2, 3, 4))
+ assert_equal([1, 2, 3, 4, 5], x.unshift(1))
+ assert_equal([1, 2, 3, 4, 5], x)
+
+ assert_equal(5, x.pop)
+ assert_equal([3, 4], x.pop(2))
+ assert_equal([1, 2], x)
+
+ assert_equal([1, 2, 3, 4], x.push(3, 4))
+ assert_equal([1, 2, 3, 4, 5], x.push(5))
+ assert_equal([1, 2, 3, 4, 5], x)
+ end
+
+ def test_find_all
+ assert_respond_to([], :find_all)
+ assert_respond_to([], :select) # Alias
+ assert_equal([], [].find_all{ |obj| obj == "foo"})
+
+ x = ["foo", "bar", "baz", "baz", 1, 2, 3, 3, 4]
+ assert_equal(["baz","baz"], x.find_all{ |obj| obj == "baz" })
+ assert_equal([3,3], x.find_all{ |obj| obj == 3 })
+ end
+
+ def test_fill
+ assert_equal([-1, -1, -1, -1, -1, -1], [0, 1, 2, 3, 4, 5].fill(-1))
+ assert_equal([0, 1, 2, -1, -1, -1], [0, 1, 2, 3, 4, 5].fill(-1, 3))
+ assert_equal([0, 1, 2, -1, -1, 5], [0, 1, 2, 3, 4, 5].fill(-1, 3, 2))
+ assert_equal([0, 1, 2, -1, -1, -1, -1, -1], [0, 1, 2, 3, 4, 5].fill(-1, 3, 5))
+ assert_equal([0, 1, -1, -1, 4, 5], [0, 1, 2, 3, 4, 5].fill(-1, 2, 2))
+ assert_equal([0, 1, -1, -1, -1, -1, -1], [0, 1, 2, 3, 4, 5].fill(-1, 2, 5))
+ assert_equal([0, 1, 2, 3, -1, 5], [0, 1, 2, 3, 4, 5].fill(-1, -2, 1))
+ assert_equal([0, 1, 2, 3, -1, -1, -1], [0, 1, 2, 3, 4, 5].fill(-1, -2, 3))
+ assert_equal([0, 1, 2, -1, -1, 5], [0, 1, 2, 3, 4, 5].fill(-1, 3..4))
+ assert_equal([0, 1, 2, -1, 4, 5], [0, 1, 2, 3, 4, 5].fill(-1, 3...4))
+ assert_equal([0, 1, -1, -1, -1, 5], [0, 1, 2, 3, 4, 5].fill(-1, 2..-2))
+ assert_equal([0, 1, -1, -1, 4, 5], [0, 1, 2, 3, 4, 5].fill(-1, 2...-2))
+ assert_equal([10, 11, 12, 13, 14, 15], [0, 1, 2, 3, 4, 5].fill{|i| i+10})
+ assert_equal([0, 1, 2, 13, 14, 15], [0, 1, 2, 3, 4, 5].fill(3){|i| i+10})
+ assert_equal([0, 1, 2, 13, 14, 5], [0, 1, 2, 3, 4, 5].fill(3, 2){|i| i+10})
+ assert_equal([0, 1, 2, 13, 14, 15, 16, 17], [0, 1, 2, 3, 4, 5].fill(3, 5){|i| i+10})
+ assert_equal([0, 1, 2, 13, 14, 5], [0, 1, 2, 3, 4, 5].fill(3..4){|i| i+10})
+ assert_equal([0, 1, 2, 13, 4, 5], [0, 1, 2, 3, 4, 5].fill(3...4){|i| i+10})
+ assert_equal([0, 1, 12, 13, 14, 5], [0, 1, 2, 3, 4, 5].fill(2..-2){|i| i+10})
+ assert_equal([0, 1, 12, 13, 4, 5], [0, 1, 2, 3, 4, 5].fill(2...-2){|i| i+10})
+ end
+end
Added: trunk/test/ruby/test_beginendblock.rb
===================================================================
--- trunk/test/ruby/test_beginendblock.rb 2006-02-02 13:54:06 UTC (rev 359)
+++ trunk/test/ruby/test_beginendblock.rb 2006-02-02 14:21:23 UTC (rev 360)
@@ -0,0 +1,60 @@
+# We cannot test these cases.
+=begin
+require 'test/unit'
+require 'tempfile'
+require 'envutil'
+
+class TestBeginEndBlock < Test::Unit::TestCase
+ DIR = File.dirname(File.expand_path(__FILE__))
+
+ def q(content)
+ "\"#{content}\""
+ end
+
+ def test_beginendblock
+ ruby = EnvUtil.rubybin
+ target = File.join(DIR, 'beginmainend.rb')
+ io = IO.popen("#{q(ruby)} #{q(target)}")
+ assert_equal(%w(b1 b2-1 b2 main b3-1 b3 b4 e1 e4 e3 e2 e4-2 e4-1 e1-1 e4-1-1), io.read.split)
+ io.close
+ end
+
+ def test_begininmethod
+ assert_raises(SyntaxError) do
+ eval("def foo; BEGIN {}; end")
+ end
+
+ assert_raises(SyntaxError) do
+ eval('eval("def foo; BEGIN {}; end")')
+ end
+ end
+
+ def test_endblockwarn
+ ruby = EnvUtil.rubybin
+ # Use Tempfile to create temporary file path.
+ launcher = Tempfile.new(self.class.name)
+ errout = Tempfile.new(self.class.name)
+
+ launcher << <<EOF
+errout = ARGV.shift
+STDERR.reopen(File.open(errout, "w"))
+STDERR.sync = true
+Dir.chdir(#{q(DIR)})
+cmd = "\\"#{ruby}\\" \\"endblockwarn.rb\\""
+system(cmd)
+EOF
+ launcher.close
+ launcherpath = launcher.path
+ errout.close
+ erroutpath = errout.path
+ system("#{q(ruby)} #{q(launcherpath)} #{q(erroutpath)}")
+ expected = <<EOW
+endblockwarn.rb:2: warning: END in method; use at_exit
+(eval):2: warning: END in method; use at_exit
+EOW
+ assert_equal(expected, File.read(erroutpath))
+ # expecting Tempfile to unlink launcher and errout file.
+ end
+
+end
+=end
Added: trunk/test/ruby/test_clone.rb
===================================================================
--- trunk/test/ruby/test_clone.rb 2006-02-02 13:54:06 UTC (rev 359)
+++ trunk/test/ruby/test_clone.rb 2006-02-02 14:21:23 UTC (rev 360)
@@ -0,0 +1,29 @@
+require 'test/unit'
+
+class TestClone < Test::Unit::TestCase
+ module M001; end
+ module M002; end
+ module M003; include M002; end
+ module M002; include M001; end
+ module M003; include M002; end
+
+ def test_clone
+assert false, "Class#clone not supported yet"
+ foo = Object.new
+ def foo.test
+ "test"
+ end
+ bar = foo.clone
+ def bar.test2
+ "test2"
+ end
+
+ assert_equal("test2", bar.test2)
+ assert_equal("test", bar.test)
+ assert_equal("test", foo.test)
+
+ assert_raises(NoMethodError) {foo.test2}
+
+ assert_equal([M003, M002, M001], M003.ancestors)
+ end
+end
Added: trunk/test/ruby/test_dir.rb
===================================================================
--- trunk/test/ruby/test_dir.rb 2006-02-02 13:54:06 UTC (rev 359)
+++ trunk/test/ruby/test_dir.rb 2006-02-02 14:21:23 UTC (rev 360)
@@ -0,0 +1,42 @@
+require 'test/unit'
+
+require 'tmpdir'
+require 'fileutils'
+
+class TestDir < Test::Unit::TestCase
+
+ ROOT = File.join(Dir.tmpdir, "__test_dir__#{$$}")
+
+ def setup
+ Dir.mkdir(ROOT)
+ for i in ?a..?z
+ if i % 2 == 0
+ FileUtils.touch(File.join(ROOT, i.chr))
+ else
+ FileUtils.mkdir(File.join(ROOT, i.chr))
+ end
+ end
+ end
+
+ def teardown
+ FileUtils.rm_rf ROOT if File.directory?(ROOT)
+ end
+
+ def test_seek
+ dir = Dir.open(ROOT)
+ begin
+ cache = []
+ loop do
+ pos = dir.tell
+ break unless name = dir.read
+ cache << [pos, name]
+ end
+ for x in cache.sort_by {|x| x[0] % 3 } # shuffle
+ dir.seek(x[0])
+ assert_equal(x[1], dir.read)
+ end
+ ensure
+ dir.close
+ end
+ end
+end
Added: trunk/test/ruby/test_env.rb
===================================================================
--- trunk/test/ruby/test_env.rb 2006-02-02 13:54:06 UTC (rev 359)
+++ trunk/test/ruby/test_env.rb 2006-02-02 14:21:23 UTC (rev 360)
@@ -0,0 +1,84 @@
+require 'test/unit'
+
+class TestEnv < Test::Unit::TestCase
+ IGNORE_CASE = /djgpp|bccwin|mswin|mingw/ =~ RUBY_PLATFORM
+
+ def setup
+ @backup = ENV['test']
+ @BACKUP = ENV['TEST']
+ ENV.delete('test')
+ ENV.delete('TEST')
+ end
+
+ def teardown
+ ENV['test'] = @backup if @backup
+ ENV['TEST'] = @BACKUP if @BACKUP
+ end
+
+ def test_bracket
+ assert_nil(ENV['test'])
+ assert_nil(ENV['TEST'])
+ ENV['test'] = 'foo'
+ assert_equal('foo', ENV['test'])
+ if IGNORE_CASE
+ assert_equal('foo', ENV['TEST'])
+ else
+ assert_nil(ENV['TEST'])
+ end
+ ENV['TEST'] = 'bar'
+ assert_equal('bar', ENV['TEST'])
+ if IGNORE_CASE
+ assert_equal('bar', ENV['test'])
+ else
+ assert_equal('foo', ENV['test'])
+ end
+
+ assert_raises(TypeError) {
+ tmp = ENV[1]
+ }
+ assert_raises(TypeError) {
+ ENV[1] = 'foo'
+ }
+ assert_raises(TypeError) {
+ ENV['test'] = 0
+ }
+ end
+
+ def test_has_value
+ val = 'a'
+ val.succ! while ENV.has_value?(val) && ENV.has_value?(val.upcase)
+ ENV['test'] = val[0...-1]
+
+ assert_equal(false, ENV.has_value?(val))
+ assert_equal(false, ENV.has_value?(val.upcase))
+ ENV['test'] = val
+ assert_equal(true, ENV.has_value?(val))
+ assert_equal(false, ENV.has_value?(val.upcase))
+ ENV['test'] = val.upcase
+ assert_equal(false, ENV.has_value?(val))
+ assert_equal(true, ENV.has_value?(val.upcase))
+ end
+
+ def test_key
+ val = 'a'
+ val.succ! while ENV.has_value?(val) && ENV.has_value?(val.upcase)
+ ENV['test'] = val[0...-1]
+
+ assert_nil(ENV.key(val))
+ assert_nil(ENV.key(val.upcase))
+ ENV['test'] = val
+ if IGNORE_CASE
+ assert_equal('TEST', ENV.key(val).upcase)
+ else
+ assert_equal('test', ENV.key(val))
+ end
+ assert_nil(ENV.key(val.upcase))
+ ENV['test'] = val.upcase
+ assert_nil(ENV.key(val))
+ if IGNORE_CASE
+ assert_equal('TEST', ENV.key(val.upcase).upcase)
+ else
+ assert_equal('test', ENV.key(val.upcase))
+ end
+ end
+end
Added: trunk/test/ruby/test_float.rb
===================================================================
--- trunk/test/ruby/test_float.rb 2006-02-02 13:54:06 UTC (rev 359)
+++ trunk/test/ruby/test_float.rb 2006-02-02 14:21:23 UTC (rev 360)
@@ -0,0 +1,89 @@
+require 'test/unit'
+
+class TestFloat < Test::Unit::TestCase
+ def test_float
+ assert_equal(2, 2.6.floor)
+ assert_equal(-3, (-2.6).floor)
+ assert_equal(3, 2.6.ceil)
+ assert_equal(-2, (-2.6).ceil)
+ assert_equal(2, 2.6.truncate)
+ assert_equal(-2, (-2.6).truncate)
+ assert_equal(3, 2.6.round)
+ assert_equal(-2, (-2.4).truncate)
+ assert((13.4 % 1 - 0.4).abs < 0.0001)
+ end
+
+ def nan_test(x,y)
+ extend Test::Unit::Assertions
+ assert(x != y)
+ assert_equal(false, (x < y))
+ assert_equal(false, (x > y))
+ assert_equal(false, (x <= y))
+ assert_equal(false, (x >= y))
+ end
+ def test_nan
+ nan = 0.0/0
+ nan_test(nan, nan)
+ nan_test(nan, 0)
+ nan_test(nan, 1)
+ nan_test(nan, -1)
+ nan_test(nan, 1000)
+ nan_test(nan, -1000)
+ nan_test(nan, 1_000_000_000_000)
+ nan_test(nan, -1_000_000_000_000)
+ nan_test(nan, 100.0);
+ nan_test(nan, -100.0);
+ nan_test(nan, 0.001);
+ nan_test(nan, -0.001);
+ nan_test(nan, 1.0/0);
+ nan_test(nan, -1.0/0);
+ end
+
+ def test_precision
+ u = 3.7517675036461267e+17
+ v = sprintf("%.16e", u).to_f
+ assert_in_delta(u, v, u.abs * Float::EPSILON)
+ assert_in_delta(u, v, v.abs * Float::EPSILON)
+ end
+
+ def test_symmetry_bignum # [ruby-bugs-ja:118]
+ a = 100000000000000000000000
+ b = 100000000000000000000000.0
+ assert_equal(a == b, b == a)
+ end
+
+ def test_strtod
+ a = Float("0")
+ assert(a.abs < Float::EPSILON)
+ a = Float("0.0")
+ assert(a.abs < Float::EPSILON)
+ a = Float("+0.0")
+ assert(a.abs < Float::EPSILON)
+ a = Float("-0.0")
+ assert(a.abs < Float::EPSILON)
+ a = Float("0.0000000000000000001")
+ assert(a != 0.0)
+ a = Float("+0.0000000000000000001")
+ assert(a != 0.0)
+ a = Float("-0.0000000000000000001")
+ assert(a != 0.0)
+ a = Float(".0")
+ assert(a.abs < Float::EPSILON)
+ a = Float("+.0")
+ assert(a.abs < Float::EPSILON)
+ a = Float("-.0")
+ assert(a.abs < Float::EPSILON)
+ a = Float("0.")
+ assert(a.abs < Float::EPSILON)
+ a = Float("+0.")
+ assert(a.abs < Float::EPSILON)
+ a = Float("-0.")
+ assert(a.abs < Float::EPSILON)
+ assert_raise(ArgumentError){Float(".")}
+ assert_raise(ArgumentError){Float("+")}
+ assert_raise(ArgumentError){Float("+.")}
+ assert_raise(ArgumentError){Float("-")}
+ assert_raise(ArgumentError){Float("-.")}
+ # add expected behaviour here.
+ end
+end
Added: trunk/test/ruby/test_fnmatch.rb
===================================================================
--- trunk/test/ruby/test_fnmatch.rb 2006-02-02 13:54:06 UTC (rev 359)
+++ trunk/test/ruby/test_fnmatch.rb 2006-02-02 14:21:23 UTC (rev 360)
@@ -0,0 +1,105 @@
+require 'test/unit'
+
+class TestFnmatch < Test::Unit::TestCase
+
+ def bracket_test(s, t) # `s' should start with neither '!' nor '^'
+ 0x21.upto(0x7E) do |i|
+ assert_equal(t.include?(i), File.fnmatch("[#{s}]", i.chr, File::FNM_DOTMATCH))
+ assert_equal(t.include?(i), !File.fnmatch("[^#{s}]", i.chr, File::FNM_DOTMATCH))
+ assert_equal(t.include?(i), !File.fnmatch("[!#{s}]", i.chr, File::FNM_DOTMATCH))
+ end
+ end
+ def test_fnmatch
+ # from [ruby-dev:22815] and [ruby-dev:22819]
+ assert(File.fnmatch('\[1\]' , '[1]'))
+ assert(File.fnmatch('*?', 'a'))
+ assert(File.fnmatch('*/', 'a/'))
+ assert(File.fnmatch('\[1\]' , '[1]', File::FNM_PATHNAME))
+ assert(File.fnmatch('*?', 'a', File::FNM_PATHNAME))
+ assert(File.fnmatch('*/', 'a/', File::FNM_PATHNAME))
+ # text
+ assert(File.fnmatch('cat', 'cat'))
+ assert(!File.fnmatch('cat', 'category'))
+ assert(!File.fnmatch('cat', 'wildcat'))
+ # '?' matches any one character
+ assert(File.fnmatch('?at', 'cat'))
+ assert(File.fnmatch('c?t', 'cat'))
+ assert(File.fnmatch('ca?', 'cat'))
+ assert(File.fnmatch('?a?', 'cat'))
+ assert(!File.fnmatch('c??t', 'cat'))
+ assert(!File.fnmatch('??at', 'cat'))
+ assert(!File.fnmatch('ca??', 'cat'))
+ # '*' matches any number (including 0) of any characters
+ assert(File.fnmatch('c*', 'cats'))
+ assert(File.fnmatch('c*ts', 'cats'))
+ assert(File.fnmatch('*ts', 'cats'))
+ assert(File.fnmatch('*c*a*t*s*', 'cats'))
+ assert(!File.fnmatch('c*t', 'cats'))
+ assert(!File.fnmatch('*abc', 'abcabz'))
+ assert(File.fnmatch('*abz', 'abcabz'))
+ assert(!File.fnmatch('a*abc', 'abc'))
+ assert(File.fnmatch('a*bc', 'abc'))
+ assert(!File.fnmatch('a*bc', 'abcd'))
+ # [seq] : matches any character listed between bracket
+ # [!seq] or [^seq] : matches any character except those listed between bracket
+ bracket_test("bd-gikl-mosv-x", "bdefgiklmosvwx")
+ # escaping character
+ assert(File.fnmatch('\?', '?'))
+ assert(!File.fnmatch('\?', '\?'))
+ assert(!File.fnmatch('\?', 'a'))
+ assert(!File.fnmatch('\?', '\a'))
+ assert(File.fnmatch('\*', '*'))
+ assert(!File.fnmatch('\*', '\*'))
+ assert(!File.fnmatch('\*', 'cats'))
+ assert(!File.fnmatch('\*', '\cats'))
+ assert(File.fnmatch('\a', 'a'))
+ assert(!File.fnmatch('\a', '\a'))
+ assert(File.fnmatch('[a\-c]', 'a'))
+ assert(File.fnmatch('[a\-c]', '-'))
+ assert(File.fnmatch('[a\-c]', 'c'))
+ assert(!File.fnmatch('[a\-c]', 'b'))
+ assert(!File.fnmatch('[a\-c]', '\\'))
+ # escaping character loses its meaning if FNM_NOESCAPE is set
+ assert(!File.fnmatch('\?', '?', File::FNM_NOESCAPE))
+ assert(File.fnmatch('\?', '\?', File::FNM_NOESCAPE))
+ assert(!File.fnmatch('\?', 'a', File::FNM_NOESCAPE))
+ assert(File.fnmatch('\?', '\a', File::FNM_NOESCAPE))
+ assert(!File.fnmatch('\*', '*', File::FNM_NOESCAPE))
+ assert(File.fnmatch('\*', '\*', File::FNM_NOESCAPE))
+ assert(!File.fnmatch('\*', 'cats', File::FNM_NOESCAPE))
+ assert(File.fnmatch('\*', '\cats', File::FNM_NOESCAPE))
+ assert(!File.fnmatch('\a', 'a', File::FNM_NOESCAPE))
+ assert(File.fnmatch('\a', '\a', File::FNM_NOESCAPE))
+ assert(File.fnmatch('[a\-c]', 'a', File::FNM_NOESCAPE))
+ assert(!File.fnmatch('[a\-c]', '-', File::FNM_NOESCAPE))
+ assert(File.fnmatch('[a\-c]', 'c', File::FNM_NOESCAPE))
+ assert(File.fnmatch('[a\-c]', 'b', File::FNM_NOESCAPE)) # '\\' < 'b' < 'c'
+ assert(File.fnmatch('[a\-c]', '\\', File::FNM_NOESCAPE))
+ # case is ignored if FNM_CASEFOLD is set
+ assert(!File.fnmatch('cat', 'CAT'))
+ assert(File.fnmatch('cat', 'CAT', File::FNM_CASEFOLD))
+ assert(!File.fnmatch('[a-z]', 'D'))
+ assert(File.fnmatch('[a-z]', 'D', File::FNM_CASEFOLD))
+ # wildcard doesn't match '/' if FNM_PATHNAME is set
+ assert(File.fnmatch('foo?boo', 'foo/boo'))
+ assert(File.fnmatch('foo*', 'foo/boo'))
+ assert(!File.fnmatch('foo?boo', 'foo/boo', File::FNM_PATHNAME))
+ assert(!File.fnmatch('foo*', 'foo/boo', File::FNM_PATHNAME))
+ # wildcard matches leading period if FNM_DOTMATCH is set
+ assert(!File.fnmatch('*', '.profile'))
+ assert(File.fnmatch('*', '.profile', File::FNM_DOTMATCH))
+ assert(File.fnmatch('.*', '.profile'))
+ assert(File.fnmatch('*', 'dave/.profile'))
+ assert(File.fnmatch('*/*', 'dave/.profile'))
+ assert(!File.fnmatch('*/*', 'dave/.profile', File::FNM_PATHNAME))
+ assert(File.fnmatch('*/*', 'dave/.profile', File::FNM_PATHNAME | File::FNM_DOTMATCH))
+ # recursive matching
+ assert(File.fnmatch('**/foo', 'a/b/c/foo', File::FNM_PATHNAME))
+ assert(File.fnmatch('**/foo', '/foo', File::FNM_PATHNAME))
+ assert(!File.fnmatch('**/foo', 'a/.b/c/foo', File::FNM_PATHNAME))
+ assert(File.fnmatch('**/foo', 'a/.b/c/foo', File::FNM_PATHNAME | File::FNM_DOTMATCH))
+ assert(File.fnmatch('**/foo', '/root/foo', File::FNM_PATHNAME))
+ assert(File.fnmatch('**/foo', 'c:/root/foo', File::FNM_PATHNAME))
+ end
+
+end
Added: trunk/test/ruby/test_hash.rb
===================================================================
--- trunk/test/ruby/test_hash.rb 2006-02-02 13:54:06 UTC (rev 359)
+++ trunk/test/ruby/test_hash.rb 2006-02-02 14:21:23 UTC (rev 360)
@@ -0,0 +1,74 @@
+require 'test/unit'
+
+class TestHash < Test::Unit::TestCase
+ def test_hash
+ x = {1=>2, 2=>4, 3=>6}
+ y = {1, 2, 2, 4, 3, 6}
+
+ assert_equal(2, x[1])
+
+ assert(begin
+ for k,v in y
+ raise if k*2 != v
+ end
+ true
+ rescue
+ false
+ end)
+
+ assert_equal(3, x.length)
+ assert(x.has_key?(1))
+ assert(x.has_value?(4))
+ assert_equal([4,6], x.values_at(2,3))
+ assert_equal({1=>2, 2=>4, 3=>6}, x)
+
+ z = y.keys.join(":")
+ assert_equal("1:2:3", z)
+
+ z = y.values.join(":")
+ assert_equal("2:4:6", z)
+ assert_equal(x, y)
+
+ y.shift
+ assert_equal(2, y.length)
+
+ z = [1,2]
+ y[z] = 256
+ assert_equal(256, y[z])
+
+ x = Hash.new(0)
+ x[1] = 1
+ assert_equal(1, x[1])
+ assert_equal(0, x[2])
+
+ x = Hash.new([])
+ assert_equal([], x[22])
+ assert_same(x[22], x[22])
+
+ x = Hash.new{[]}
+ assert_equal([], x[22])
+ assert_not_same(x[22], x[22])
+
+ x = Hash.new{|h,k| z = k; h[k] = k*2}
+ z = 0
+ assert_equal(44, x[22])
+ assert_equal(22, z)
+ z = 0
+ assert_equal(44, x[22])
+ assert_equal(0, z)
+ x.default = 5
+ assert_equal(5, x[23])
+
+ x = Hash.new
+ def x.default(k)
+ $z = k
+ self[k] = k*2
+ end
+ $z = 0
+ assert_equal(44, x[22])
+ assert_equal(22, $z)
+ $z = 0
+ assert_equal(44, x[22])
+ assert_equal(0, $z)
+ end
+end
Added: trunk/test/ruby/test_io.rb
===================================================================
--- trunk/test/ruby/test_io.rb 2006-02-02 13:54:06 UTC (rev 359)
+++ trunk/test/ruby/test_io.rb 2006-02-02 14:21:23 UTC (rev 360)
@@ -0,0 +1,38 @@
+require 'test/unit'
+
+class TestIO < Test::Unit::TestCase
+ def test_gets_rs
+ # default_rs
+ r, w = IO.pipe
+ w.print "aaa\nbbb\n"
+ w.close
+ assert_equal "aaa\n", r.gets
+ assert_equal "bbb\n", r.gets
+ assert_nil r.gets
+ r.close
+
+ # nil
+ r, w = IO.pipe
+ w.print "a\n\nb\n\n"
+ w.close
+ assert_equal "a\n\nb\n\n", r.gets(nil)
+ assert_nil r.gets("")
+ r.close
+
+ # "\377" [ruby-dev:24460]
+ r, w = IO.pipe
+ w.print "\377xyz"
+ w.close
+ assert_equal("\377", r.gets("\377"), "[ruby-dev:24460]")
+ r.close
+
+ # "" [ruby-core:03771]
+ r, w = IO.pipe
+ w.print "a\n\nb\n\n"
+ w.close
+ assert_equal "a\n\n", r.gets("")
+ assert_equal "b\n\n", r.gets("")
+ assert_nil r.gets("")
+ r.close
+ end
+end
Added: trunk/test/ruby/test_marshal.rb
===================================================================
--- trunk/test/ruby/test_marshal.rb 2006-02-02 13:54:06 UTC (rev 359)
+++ trunk/test/ruby/test_marshal.rb 2006-02-02 14:21:23 UTC (rev 360)
@@ -0,0 +1,40 @@
+require 'test/unit'
+require 'marshaltestlib'
+
+class TestMarshal < Test::Unit::TestCase
+ include MarshalTestLib
+
+ def encode(o)
+ Marshal.dump(o)
+ end
+
+ def decode(s)
+ Marshal.load(s)
+ end
+
+ def fact(n)
+ return 1 if n == 0
+ f = 1
+ while n>0
+ f *= n
+ n -= 1
+ end
+ return f
+ end
+
+ def test_marshal
+ x = [1, 2, 3, [4,5,"foo"], {1=>"bar"}, 2.5, fact(30)]
+ assert_equal x, Marshal.load(Marshal.dump(x))
+
+ [[1,2,3,4], [81, 2, 118, 3146]].each { |w,x,y,z|
+ obj = (x.to_f + y.to_f / z.to_f) * Math.exp(w.to_f / (x.to_f + y.to_f / z.to_f))
+ assert_equal obj, Marshal.load(Marshal.dump(obj))
+ }
+ end
+
+ #StrClone = String.clone
+ def test_marshal_cloned_class
+assert false, "Class#clone not supported yet"
+ assert_instance_of(StrClone, Marshal.load(Marshal.dump(StrClone.new("abc"))))
+ end
+end
Added: trunk/test/ruby/test_math.rb
===================================================================
--- trunk/test/ruby/test_math.rb 2006-02-02 13:54:06 UTC (rev 359)
+++ trunk/test/ruby/test_math.rb 2006-02-02 14:21:23 UTC (rev 360)
@@ -0,0 +1,12 @@
+require 'test/unit'
+
+class TestMath < Test::Unit::TestCase
+ def test_math
+ assert_equal(2, Math.sqrt(4))
+
+ self.class.class_eval {
+ include Math
+ }
+ assert_equal(2, sqrt(4))
+ end
+end
Added: trunk/test/ruby/test_pack.rb
===================================================================
--- trunk/test/ruby/test_pack.rb 2006-02-02 13:54:06 UTC (rev 359)
+++ trunk/test/ruby/test_pack.rb 2006-02-02 14:21:23 UTC (rev 360)
@@ -0,0 +1,57 @@
+require 'test/unit'
+
+class TestPack < Test::Unit::TestCase
+ def test_pack
+ $format = "c2x5CCxsdils_l_a6";
+ # Need the expression in here to force ary[5] to be numeric. This avoids
+ # test2 failing because ary2 goes str->numeric->str and ary does not.
+ ary = [1,-100,127,128,32767,987.654321098 / 100.0,12345,123456,-32767,-123456,"abcdef"]
+ $x = ary.pack($format)
+ ary2 = $x.unpack($format)
+
+ assert_equal(ary.length, ary2.length)
+ assert_equal(ary.join(':'), ary2.join(':'))
+ assert_match(/def/, $x)
+
+ $x = [-1073741825]
+ assert_equal($x, $x.pack("q").unpack("q"))
+ end
+
+ def test_pack_N
+ assert_equal "\000\000\000\000", [0].pack('N')
+ assert_equal "\000\000\000\001", [1].pack('N')
+ assert_equal "\000\000\000\002", [2].pack('N')
+ assert_equal "\000\000\000\003", [3].pack('N')
+ assert_equal "\377\377\377\376", [4294967294].pack('N')
+ assert_equal "\377\377\377\377", [4294967295].pack('N')
+
+ assert_equal "\200\000\000\000", [2**31].pack('N')
+ assert_equal "\177\377\377\377", [-2**31-1].pack('N')
+ assert_equal "\377\377\377\377", [-1].pack('N')
+
+ assert_equal "\000\000\000\001\000\000\000\001", [1,1].pack('N*')
+ assert_equal "\000\000\000\001\000\000\000\001\000\000\000\001", [1,1,1].pack('N*')
+ end
+
+ def test_unpack_N
+ assert_equal 1, "\000\000\000\001".unpack('N')[0]
+ assert_equal 2, "\000\000\000\002".unpack('N')[0]
+ assert_equal 3, "\000\000\000\003".unpack('N')[0]
+ assert_equal 3, "\000\000\000\003".unpack('N')[0]
+ assert_equal 4294967295, "\377\377\377\377".unpack('N')[0]
+ assert_equal [1,1], "\000\000\000\001\000\000\000\001".unpack('N*')
+ assert_equal [1,1,1], "\000\000\000\001\000\000\000\001\000\000\000\001".unpack('N*')
+ end
+
+ def test_pack_U
+ assert_raises(RangeError) { [-0x40000001].pack("U") }
+ assert_raises(RangeError) { [-0x40000000].pack("U") }
+ assert_raises(RangeError) { [-1].pack("U") }
+ assert_equal "\000", [0].pack("U")
+ assert_equal "\374\277\277\277\277\277", [0x3fffffff].pack("U")
+ assert_equal "\375\200\200\200\200\200", [0x40000000].pack("U")
+ assert_equal "\375\277\277\277\277\277", [0x7fffffff].pack("U")
+ assert_raises(RangeError) { [0x80000000].pack("U") }
+ assert_raises(RangeError) { [0x100000000].pack("U") }
+ end
+end
Added: trunk/test/ruby/test_path.rb
===================================================================
--- trunk/test/ruby/test_path.rb 2006-02-02 13:54:06 UTC (rev 359)
+++ trunk/test/ruby/test_path.rb 2006-02-02 14:21:23 UTC (rev 360)
@@ -0,0 +1,224 @@
+require 'test/unit'
+
+class TestPath < Test::Unit::TestCase
+ def test_path
+ assert_equal("a", File.basename("a"))
+ assert_equal("b", File.basename("a/b"))
+ assert_equal("b", File.basename("a/b/"))
+ assert_equal("/", File.basename("/"))
+ assert_equal("/", File.basename("//"))
+ assert_equal("/", File.basename("///"))
+ assert_equal("b", File.basename("a/b////"))
+ assert_equal("a", File.basename("a.rb", ".rb"))
+ assert_equal("a", File.basename("a.rb///", ".rb"))
+ assert_equal("a", File.basename("a.rb///", ".*"))
+ assert_equal("a.rb", File.basename("a.rb///", ".c"))
+ assert_equal(".", File.dirname("a"))
+ assert_equal("/", File.dirname("/"))
+ assert_equal("/", File.dirname("/a"))
+ assert_equal("a", File.dirname("a/b"))
+ assert_equal("a/b", File.dirname("a/b/c"))
+ assert_equal("/a/b", File.dirname("/a/b/c"))
+ assert_equal("/a", File.dirname("/a/b/"))
+ assert_equal("/a", File.dirname("/a/b///"))
+ case Dir.pwd
+ when %r'\A\w:'
+ assert_match(/\A\w:\/\z/, File.expand_path(".", "/"))
+ assert_match(/\A\w:\/a\z/, File.expand_path("a", "/"))
+ dosish = true
+ when %r'\A//'
+ assert_match(%r'\A//[^/]+/[^/]+\z', File.expand_path(".", "/"))
+ assert_match(%r'\A//[^/]+/[^/]+/a\z', File.expand_path(".", "/"))
+ dosish = true
+ else
+ assert_equal("/", File.expand_path(".", "/"))
+ assert_equal("/sub", File.expand_path("sub", "/"))
+ end
+ if dosish
+ assert_equal("//machine/share", File.expand_path("/", "//machine/share/sub"))
+ assert_equal("//machine/share/dir", File.expand_path("/dir", "//machine/share/sub"))
+ assert_equal("z:/", File.expand_path("/", "z:/sub"))
+ assert_equal("z:/dir", File.expand_path("/dir", "z:/sub"))
+ end
+ assert_equal("//", File.expand_path(".", "//"))
+ assert_equal("//sub", File.expand_path("sub", "//"))
+ end
+
+ def test_dirname # [ruby-dev:27738]
+ if /(bcc|ms)win\d|mingw|cygwin|djgpp|human|emx/ =~ RUBY_PLATFORM
+ # DOSISH_DRIVE_LETTER
+ assert_equal('C:.', File.dirname('C:'))
+ assert_equal('C:.', File.dirname('C:a'))
+ assert_equal('C:.', File.dirname('C:a/'))
+ assert_equal('C:a', File.dirname('C:a/b'))
+
+ assert_equal('C:/', File.dirname('C:/'))
+ assert_equal('C:/', File.dirname('C:/a'))
+ assert_equal('C:/', File.dirname('C:/a/'))
+ assert_equal('C:/a', File.dirname('C:/a/b'))
+
+ assert_equal('C:/', File.dirname('C://'))
+ assert_equal('C:/', File.dirname('C://a'))
+ assert_equal('C:/', File.dirname('C://a/'))
+ assert_equal('C:/a', File.dirname('C://a/b'))
+
+ assert_equal('C:/', File.dirname('C:///'))
+ assert_equal('C:/', File.dirname('C:///a'))
+ assert_equal('C:/', File.dirname('C:///a/'))
+ assert_equal('C:/a', File.dirname('C:///a/b'))
+ else
+ # others
+ assert_equal('.', File.dirname('C:'))
+ assert_equal('.', File.dirname('C:a'))
+ assert_equal('.', File.dirname('C:a/'))
+ assert_equal('C:a', File.dirname('C:a/b'))
+
+ assert_equal('.', File.dirname('C:/'))
+ assert_equal('C:', File.dirname('C:/a'))
+ assert_equal('C:', File.dirname('C:/a/'))
+ assert_equal('C:/a', File.dirname('C:/a/b'))
+
+ assert_equal('.', File.dirname('C://'))
+ assert_equal('C:', File.dirname('C://a'))
+ assert_equal('C:', File.dirname('C://a/'))
+ # not spec.
+ #assert_equal('C://a', File.dirname('C://a/b'))
+
+ assert_equal('.', File.dirname('C:///'))
+ assert_equal('C:', File.dirname('C:///a'))
+ assert_equal('C:', File.dirname('C:///a/'))
+ # not spec.
+ #assert_equal('C:///a', File.dirname('C:///a/b'))
+ end
+
+ assert_equal('.', File.dirname(''))
+ assert_equal('.', File.dirname('a'))
+ assert_equal('.', File.dirname('a/'))
+ assert_equal('a', File.dirname('a/b'))
+
+ assert_equal('/', File.dirname('/'))
+ assert_equal('/', File.dirname('/a'))
+ assert_equal('/', File.dirname('/a/'))
+ assert_equal('/a', File.dirname('/a/b'))
+
+ if /(bcc|ms|cyg)win|mingw|djgpp|human|emx/ =~ RUBY_PLATFORM
+ # DOSISH_UNC
+ assert_equal('//', File.dirname('//'))
+ assert_equal('//a', File.dirname('//a'))
+ assert_equal('//a', File.dirname('//a/'))
+ assert_equal('//a/b', File.dirname('//a/b'))
+ assert_equal('//a/b', File.dirname('//a/b/'))
+ assert_equal('//a/b', File.dirname('//a/b/c'))
+
+ assert_equal('//', File.dirname('///'))
+ assert_equal('//a', File.dirname('///a'))
+ assert_equal('//a', File.dirname('///a/'))
+ assert_equal('//a/b', File.dirname('///a/b'))
+ assert_equal('//a/b', File.dirname('///a/b/'))
+ assert_equal('//a/b', File.dirname('///a/b/c'))
+ else
+ # others
+ assert_equal('/', File.dirname('//'))
+ assert_equal('/', File.dirname('//a'))
+ assert_equal('/', File.dirname('//a/'))
+ assert_equal('/a', File.dirname('//a/b'))
+ assert_equal('/a', File.dirname('//a/b/'))
+ assert_equal('/a/b', File.dirname('//a/b/c'))
+
+ assert_equal('/', File.dirname('///'))
+ assert_equal('/', File.dirname('///a'))
+ assert_equal('/', File.dirname('///a/'))
+ assert_equal('/a', File.dirname('///a/b'))
+ assert_equal('/a', File.dirname('///a/b/'))
+ assert_equal('/a/b', File.dirname('///a/b/c'))
+ end
+ end
+
+ def test_basename # [ruby-dev:27766]
+ if /(bcc|ms)win\d|mingw|cygwin|djgpp|human|emx/ =~ RUBY_PLATFORM
+ # DOSISH_DRIVE_LETTER
+ assert_equal('', File.basename('C:'))
+ assert_equal('a', File.basename('C:a'))
+ assert_equal('a', File.basename('C:a/'))
+ assert_equal('b', File.basename('C:a/b'))
+
+ assert_equal('/', File.basename('C:/'))
+ assert_equal('a', File.basename('C:/a'))
+ assert_equal('a', File.basename('C:/a/'))
+ assert_equal('b', File.basename('C:/a/b'))
+
+ assert_equal('/', File.basename('C://'))
+ assert_equal('a', File.basename('C://a'))
+ assert_equal('a', File.basename('C://a/'))
+ assert_equal('b', File.basename('C://a/b'))
+
+ assert_equal('/', File.basename('C:///'))
+ assert_equal('a', File.basename('C:///a'))
+ assert_equal('a', File.basename('C:///a/'))
+ assert_equal('b', File.basename('C:///a/b'))
+ else
+ # others
+ assert_equal('C:', File.basename('C:'))
+ assert_equal('C:a', File.basename('C:a'))
+ assert_equal('C:a', File.basename('C:a/'))
+ assert_equal('b', File.basename('C:a/b'))
+
+ assert_equal('C:', File.basename('C:/'))
+ assert_equal('a', File.basename('C:/a'))
+ assert_equal('a', File.basename('C:/a/'))
+ assert_equal('b', File.basename('C:/a/b'))
+
+ assert_equal('C:', File.basename('C://'))
+ assert_equal('a', File.basename('C://a'))
+ assert_equal('a', File.basename('C://a/'))
+ assert_equal('b', File.basename('C://a/b'))
+
+ assert_equal('C:', File.basename('C:///'))
+ assert_equal('a', File.basename('C:///a'))
+ assert_equal('a', File.basename('C:///a/'))
+ assert_equal('b', File.basename('C:///a/b'))
+ end
+
+ assert_equal('', File.basename(''))
+ assert_equal('a', File.basename('a'))
+ assert_equal('a', File.basename('a/'))
+ assert_equal('b', File.basename('a/b'))
+
+ assert_equal('/', File.basename('/'))
+ assert_equal('a', File.basename('/a'))
+ assert_equal('a', File.basename('/a/'))
+ assert_equal('b', File.basename('/a/b'))
+
+ if /(bcc|ms|cyg)win|mingw|djgpp|human|emx/ =~ RUBY_PLATFORM
+ # DOSISH_UNC
+ assert_equal('/', File.basename('//'))
+ assert_equal('/', File.basename('//a'))
+ assert_equal('/', File.basename('//a/'))
+ assert_equal('/', File.basename('//a/b'))
+ assert_equal('/', File.basename('//a/b/'))
+ assert_equal('c', File.basename('//a/b/c'))
+
+ assert_equal('/', File.basename('///'))
+ assert_equal('/', File.basename('///a'))
+ assert_equal('/', File.basename('///a/'))
+ assert_equal('/', File.basename('///a/b'))
+ assert_equal('/', File.basename('///a/b/'))
+ assert_equal('c', File.basename('///a/b/c'))
+ else
+ # others
+ assert_equal('/', File.basename('//'))
+ assert_equal('a', File.basename('//a'))
+ assert_equal('a', File.basename('//a/'))
+ assert_equal('b', File.basename('//a/b'))
+ assert_equal('b', File.basename('//a/b/'))
+ assert_equal('c', File.basename('//a/b/c'))
+
+ assert_equal('/', File.basename('///'))
+ assert_equal('a', File.basename('///a'))
+ assert_equal('a', File.basename('///a/'))
+ assert_equal('b', File.basename('///a/b'))
+ assert_equal('b', File.basename('///a/b/'))
+ assert_equal('c', File.basename('///a/b/c'))
+ end
+ end
+end
Added: trunk/test/ruby/test_pipe.rb
===================================================================
--- trunk/test/ruby/test_pipe.rb 2006-02-02 13:54:06 UTC (rev 359)
+++ trunk/test/ruby/test_pipe.rb 2006-02-02 14:21:23 UTC (rev 360)
@@ -0,0 +1,17 @@
+require 'test/unit'
+require 'ut_eof'
+require 'envutil'
+
+class TestPipe < Test::Unit::TestCase
+ include TestEOF
+ def open_file(content)
+ r, w = IO.pipe
+ w << content
+ w.close
+ begin
+ yield r
+ ensure
+ r.close
+ end
+ end
+end
Added: trunk/test/ruby/test_rand.rb
===================================================================
--- trunk/test/ruby/test_rand.rb 2006-02-02 13:54:06 UTC (rev 359)
+++ trunk/test/ruby/test_rand.rb 2006-02-02 14:21:23 UTC (rev 360)
@@ -0,0 +1,131 @@
+require 'test/unit'
+
+class TestRand < Test::Unit::TestCase
+ def test_mt
+ srand(0x00000456_00000345_00000234_00000123)
+ %w(1067595299 955945823 477289528 4107218783 4228976476).each {|w|
+ assert_equal(w.to_i, rand(0x100000000))
+ }
+ end
+
+ def test_0x3fffffff
+ srand(0)
+ %w(209652396 398764591 924231285 404868288 441365315).each {|w|
+ assert_equal(w.to_i, rand(0x3fffffff))
+ }
+ end
+
+ def test_0x40000000
+ srand(0)
+ %w(209652396 398764591 924231285 404868288 441365315).each {|w|
+ assert_equal(w.to_i, rand(0x40000000))
+ }
+ end
+
+ def test_0x40000001
+ srand(0)
+ %w(209652396 398764591 924231285 441365315 192771779).each {|w|
+ assert_equal(w.to_i, rand(0x40000001))
+ }
+ end
+
+ def test_0xffffffff
+ srand(0)
+ %w(2357136044 2546248239 3071714933 3626093760 2588848963).each {|w|
+ assert_equal(w.to_i, rand(0xffffffff))
+ }
+ end
+
+ def test_0x100000000
+ srand(0)
+ %w(2357136044 2546248239 3071714933 3626093760 2588848963).each {|w|
+ assert_equal(w.to_i, rand(0x100000000))
+ }
+ end
+
+ def test_0x100000001
+ srand(0)
+ %w(2546248239 1277901399 243580376 1171049868 2051556033).each {|w|
+ assert_equal(w.to_i, rand(0x100000001))
+ }
+ end
+
+ def test_rand_0x100000000
+ srand(311702798)
+ %w(4119812344 3870378946 80324654 4294967296 410016213).each {|w|
+ assert_equal(w.to_i, rand(0x100000001))
+ }
+ end
+
+ def test_0x1000000000000
+ srand(0)
+ %w(11736396900911
+ 183025067478208
+ 197104029029115
+ 130583529618791
+ 180361239846611).each {|w|
+ assert_equal(w.to_i, rand(0x1000000000000))
+ }
+ end
+
+ def test_0x1000000000001
+ srand(0)
+ %w(187121911899765
+ 197104029029115
+ 180361239846611
+ 236336749852452
+ 208739549485656).each {|w|
+ assert_equal(w.to_i, rand(0x1000000000001))
+ }
+ end
+
+ def test_0x3fffffffffffffff
+ srand(0)
+ %w(900450186894289455
+ 3969543146641149120
+ 1895649597198586619
+ 827948490035658087
+ 3203365596207111891).each {|w|
+ assert_equal(w.to_i, rand(0x3fffffffffffffff))
+ }
+ end
+
+ def test_0x4000000000000000
+ srand(0)
+ %w(900450186894289455
+ 3969543146641149120
+ 1895649597198586619
+ 827948490035658087
+ 3203365596207111891).each {|w|
+ assert_equal(w.to_i, rand(0x4000000000000000))
+ }
+ end
+
+ def test_0x4000000000000001
+ srand(0)
+ %w(900450186894289455
+ 3969543146641149120
+ 1895649597198586619
+ 827948490035658087
+ 2279347887019741461).each {|w|
+ assert_equal(w.to_i, rand(0x4000000000000001))
+ }
+ end
+
+ def test_neg_0x10000000000
+ ws = %w(455570294424 1073054410371 790795084744 2445173525 1088503892627)
+ srand(3)
+ ws.each {|w| assert_equal(w.to_i, rand(0x10000000000)) }
+ srand(3)
+ ws.each {|w| assert_equal(w.to_i, rand(-0x10000000000)) }
+ end
+
+ def test_neg_0x10000
+ ws = %w(2732 43567 42613 52416 45891)
+ srand(0)
+ ws.each {|w| assert_equal(w.to_i, rand(0x10000)) }
+ srand(0)
+ ws.each {|w| assert_equal(w.to_i, rand(-0x10000)) }
+ end
+
+end
Added: trunk/test/ruby/test_range.rb
===================================================================
--- trunk/test/ruby/test_range.rb 2006-02-02 13:54:06 UTC (rev 359)
+++ trunk/test/ruby/test_range.rb 2006-02-02 14:21:23 UTC (rev 360)
@@ -0,0 +1,54 @@
+require 'test/unit'
+
+class TestRange < Test::Unit::TestCase
+ def test_range_string
+ # XXX: Is this really the test of Range?
+ assert_equal([], ("a" ... "a").to_a)
+ assert_equal(["a"], ("a" .. "a").to_a)
+ assert_equal(["a"], ("a" ... "b").to_a)
+ assert_equal(["a", "b"], ("a" .. "b").to_a)
+ end
+
+ def test_evaluation_order
+ arr = [1,2]
+ r = (arr.shift)..(arr.shift)
+ assert_equal(1..2, r, "[ruby-dev:26383]")
+ end
+
+ class DuckRange
+ def initialize(b,e,excl=false)
+ @begin = b
+ @end = e
+ @excl = excl
+ end
+ attr_reader :begin, :end
+
+ def exclude_end?
+ @excl
+ end
+ end
+
+ def test_duckrange
+ assert_equal("bc", "abcd"[DuckRange.new(1,2)])
+ end
+
+ def test_min
+ assert_equal(1, (1..2).min)
+ assert_equal(nil, (2..1).min)
+ assert_equal(1, (1...2).min)
+
+ assert_equal(1.0, (1.0..2.0).min)
+ assert_equal(nil, (2.0..1.0).min)
+ assert_equal(1, (1.0...2.0).min)
+ end
+
+ def test_max
+ assert_equal(2, (1..2).max)
+ assert_equal(nil, (2..1).max)
+ assert_equal(1, (1...2).max)
+
+ assert_equal(2.0, (1.0..2.0).max)
+ assert_equal(nil, (2.0..1.0).max)
+ assert_raise(TypeError) { (1.0...2.0).max }
+ end
+end
Added: trunk/test/ruby/test_readpartial.rb
===================================================================
--- trunk/test/ruby/test_readpartial.rb 2006-02-02 13:54:06 UTC (rev 359)
+++ trunk/test/ruby/test_readpartial.rb 2006-02-02 14:21:23 UTC (rev 360)
@@ -0,0 +1,72 @@
+require 'test/unit'
+require 'timeout'
+#require 'fcntl'
+
+class TestReadPartial < Test::Unit::TestCase
+ def make_pipe
+ r, w = IO.pipe
+ begin
+ yield r, w
+ ensure
+ r.close unless r.closed?
+ w.close unless w.closed?
+ end
+ end
+
+ def pipe
+ make_pipe {|r, w|
+ yield r, w
+ }
+ return unless defined?(Fcntl::F_SETFL)
+ return unless defined?(Fcntl::F_GETFL)
+ return unless defined?(Fcntl::O_NONBLOCK)
+ make_pipe {|r, w|
+ r.fcntl(Fcntl::F_SETFL, r.fcntl(Fcntl::F_GETFL) | Fcntl::O_NONBLOCK)
+ yield r, w
+ }
+ end
+
+ def test_length_zero
+ pipe {|r, w|
+ assert_equal('', r.readpartial(0))
+ }
+ end
+
+ def test_closed_pipe
+ pipe {|r, w|
+ w << 'abc'
+ w.close
+ assert_equal('ab', r.readpartial(2))
+ assert_equal('c', r.readpartial(2))
+ assert_raises(EOFError) { r.readpartial(2) }
+ assert_raises(EOFError) { r.readpartial(2) }
+ }
+ end
+
+ def test_open_pipe
+ pipe {|r, w|
+ w << 'abc'
+ assert_equal('ab', r.readpartial(2))
+ assert_equal('c', r.readpartial(2))
+ assert_raises(TimeoutError) {
+ timeout(0.1) { r.readpartial(2) }
+ }
+ }
+ end
+
+ def test_with_stdio
+ pipe {|r, w|
+ w << "abc\ndef\n"
+ assert_equal("abc\n", r.gets)
+ w << "ghi\n"
+ assert_equal("de", r.readpartial(2))
+ assert_equal("f\n", r.readpartial(4096))
+ assert_equal("ghi\n", r.readpartial(4096))
+ assert_raises(TimeoutError) {
+ timeout(0.1) { r.readpartial(2) }
+ }
+ }
+ end
+
+end
+
Added: trunk/test/ruby/test_regexp.rb
===================================================================
--- trunk/test/ruby/test_regexp.rb 2006-02-02 13:54:06 UTC (rev 359)
+++ trunk/test/ruby/test_regexp.rb 2006-02-02 14:21:23 UTC (rev 360)
@@ -0,0 +1,27 @@
+require 'test/unit'
+
+class TestRegexp < Test::Unit::TestCase
+ def test_ruby_dev_24643
+ assert_nothing_raised("[ruby-dev:24643]") { /(?:(?:[a]*[a])?b)*a*$/ =~ "aabaaca" }
+ end
+
+ def test_ruby_talk_116455
+ assert_match(/^(\w{2,}).* ([A-Za-z\xa2\xc0-\xff]{2,}?)$/, "Hallo Welt")
+ end
+
+ def test_ruby_dev_24887
+ assert_equal("a".gsub(/a\Z/, ""), "")
+ end
+
+ def test_yoshidam_net_20041111_1
+ s = "[\xC2\xA0-\xC3\xBE]"
+ assert_match(Regexp.new(s, nil, "u"), "\xC3\xBE")
+ end
+
+ def test_yoshidam_net_20041111_2
+ assert_raise(RegexpError) do
+ s = "[\xFF-\xFF]"
+ Regexp.new(s, nil, "u")
+ end
+ end
+end
Added: trunk/test/ruby/test_settracefunc.rb
===================================================================
--- trunk/test/ruby/test_settracefunc.rb 2006-02-02 13:54:06 UTC (rev 359)
+++ trunk/test/ruby/test_settracefunc.rb 2006-02-02 14:21:23 UTC (rev 360)
@@ -0,0 +1,137 @@
+require 'test/unit'
+
+class TestSetTraceFunc < Test::Unit::TestCase
+ def foo; end;
+
+ def bar
+ events = []
+ set_trace_func(Proc.new { |event, file, lineno, mid, bidning, klass|
+ events << [event, lineno, mid, klass]
+ })
+ return events
+ end
+
+ def test_event
+ events = []
+ set_trace_func(Proc.new { |event, file, lineno, mid, bidning, klass|
+ events << [event, lineno, mid, klass]
+ })
+ a = 1
+ foo
+ a
+ b = 1 + 2
+ if b == 3
+ case b
+ when 2
+ c = "b == 2"
+ when 3
+ c = "b == 3"
+ end
+ end
+ begin
+ raise "error"
+ rescue
+ end
+ eval("class Foo; end")
+ set_trace_func nil
+
+ assert_equal(["line", 19, :test_event, TestSetTraceFunc],
+ events.shift) # a = 1
+ assert_equal(["line", 20, :test_event, TestSetTraceFunc],
+ events.shift) # foo
+ assert_equal(["call", 4, :foo, TestSetTraceFunc],
+ events.shift) # foo
+ assert_equal(["return", 4, :foo, TestSetTraceFunc],
+ events.shift) # foo
+ assert_equal(["line", 21, :test_event, TestSetTraceFunc],
+ events.shift) # a
+ assert_equal(["line", 22, :test_event, TestSetTraceFunc],
+ events.shift) # b = 1 + 2
+ assert_equal(["c-call", 22, :+, Fixnum],
+ events.shift) # 1 + 2
+ assert_equal(["c-return", 22, :+, Fixnum],
+ events.shift) # 1 + 2
+ assert_equal(["line", 23, :test_event, TestSetTraceFunc],
+ events.shift) # if b == 3
+ assert_equal(["line", 23, :test_event, TestSetTraceFunc],
+ events.shift) # if b == 3
+ assert_equal(["c-call", 23, :==, Fixnum],
+ events.shift) # b == 3
+ assert_equal(["c-return", 23, :==, Fixnum],
+ events.shift) # b == 3
+ assert_equal(["line", 24, :test_event, TestSetTraceFunc],
+ events.shift) # case b
+ assert_equal(["line", 25, :test_event, TestSetTraceFunc],
+ events.shift) # when 2
+ assert_equal(["c-call", 25, :===, Kernel],
+ events.shift) # when 2
+ assert_equal(["c-call", 25, :==, Fixnum],
+ events.shift) # when 2
+ assert_equal(["c-return", 25, :==, Fixnum],
+ events.shift) # when 2
+ assert_equal(["c-return", 25, :===, Kernel],
+ events.shift) # when 2
+ assert_equal(["line", 27, :test_event, TestSetTraceFunc],
+ events.shift) # when 3
+ assert_equal(["c-call", 27, :===, Kernel],
+ events.shift) # when 3
+ assert_equal(["c-return", 27, :===, Kernel],
+ events.shift) # when 3
+ assert_equal(["line", 28, :test_event, TestSetTraceFunc],
+ events.shift) # c = "b == 3"
+ assert_equal(["line", 31, :test_event, TestSetTraceFunc],
+ events.shift) # begin
+ assert_equal(["line", 32, :test_event, TestSetTraceFunc],
+ events.shift) # raise "error"
+ assert_equal(["c-call", 32, :raise, Kernel],
+ events.shift) # raise "error"
+ assert_equal(["c-call", 32, :new, Class],
+ events.shift) # raise "error"
+ assert_equal(["c-call", 32, :initialize, Exception],
+ events.shift) # raise "error"
+ assert_equal(["c-return", 32, :initialize, Exception],
+ events.shift) # raise "error"
+ assert_equal(["c-return", 32, :new, Class],
+ events.shift) # raise "error"
+ assert_equal(["c-call", 32, :backtrace, Exception],
+ events.shift) # raise "error"
+ assert_equal(["c-return", 32, :backtrace, Exception],
+ events.shift) # raise "error"
+ assert_equal(["c-call", 32, :set_backtrace, Exception],
+ events.shift) # raise "error"
+ assert_equal(["c-return", 32, :set_backtrace, Exception],
+ events.shift) # raise "error"
+ assert_equal(["raise", 32, :test_event, TestSetTraceFunc],
+ events.shift) # raise "error"
+ assert_equal(["c-return", 32, :raise, Kernel],
+ events.shift) # raise "error"
+ assert_equal(["line", 35, :test_event, TestSetTraceFunc],
+ events.shift) # eval(<<EOF)
+ assert_equal(["c-call", 35, :eval, Kernel],
+ events.shift) # eval(<<EOF)
+ assert_equal(["line", 1, :test_event, TestSetTraceFunc],
+ events.shift) # class Foo
+ assert_equal(["c-call", 1, :inherited, Class],
+ events.shift) # class Foo
+ assert_equal(["c-return", 1, :inherited, Class],
+ events.shift) # class Foo
+ assert_equal(["class", 1, :test_event, TestSetTraceFunc],
+ events.shift) # class Foo
+ assert_equal(["end", 1, :test_event, TestSetTraceFunc],
+ events.shift) # class Foo
+ assert_equal(["c-return", 35, :eval, Kernel],
+ events.shift) # eval(<<EOF)
+ assert_equal(["line", 36, :test_event, TestSetTraceFunc],
+ events.shift) # set_trace_func nil
+ assert_equal(["c-call", 36, :set_trace_func, Kernel],
+ events.shift) # set_trace_func nil
+ assert_equal([], events)
+
+ events = bar
+ set_trace_func(nil)
+ assert_equal(["return", 7, :bar, TestSetTraceFunc], events.shift)
+ assert_equal(["line", 131, :test_event, TestSetTraceFunc], events.shift)
+ assert_equal(["c-call", 131, :set_trace_func, Kernel], events.shift)
+ assert_equal([], events)
+ end
+end
Added: trunk/test/ruby/test_signal.rb
===================================================================
--- trunk/test/ruby/test_signal.rb 2006-02-02 13:54:06 UTC (rev 359)
+++ trunk/test/ruby/test_signal.rb 2006-02-02 14:21:23 UTC (rev 360)
@@ -0,0 +1,68 @@
+require 'test/unit'
+require 'timeout'
+
+class TestSignal < Test::Unit::TestCase
+ def have_fork?
+ begin
+ fork{}
+ true
+ rescue NotImplementedError
+ false
+ end
+ end
+
+ def test_signal
+ defined?(Process.kill) or return
+ begin
+ $x = 0
+ oldtrap = trap "SIGINT", proc{|sig| $x = 2}
+ Process.kill "SIGINT", $$
+ sleep 0.1
+ assert_equal(2, $x)
+
+ trap "SIGINT", proc{raise "Interrupt"}
+
+ x = assert_raises(RuntimeError) do
+ Process.kill "SIGINT", $$
+ sleep 0.1
+ end
+ assert(x)
+ assert_match(/Interrupt/, x.message)
+ ensure
+ trap "SIGINT", oldtrap
+ end
+ end
+
+ def test_exit_action
+ return unless have_fork? # snip this test
+ begin
+ r, w = IO.pipe
+ r0, w0 = IO.pipe
+ pid = fork {
+ trap(:USR1, "EXIT")
+ w0.close
+ w.syswrite("a")
+ Thread.start { Thread.pass }
+ r0.sysread(4096)
+ }
+ r.sysread(1)
+ sleep 0.1
+ assert_nothing_raised("[ruby-dev:26128]") {
+ Process.kill(:USR1, pid)
+ begin
+ Timeout.timeout(1) {
+ Process.waitpid pid
+ }
+ rescue Timeout::Error
+ Process.kill(:TERM, pid)
+ raise
+ end
+ }
+ ensure
+ r.close
+ w.close
+ r0.close
+ w0.close
+ end
+ end
+end
Added: trunk/test/ruby/test_sprintf.rb
===================================================================
--- trunk/test/ruby/test_sprintf.rb 2006-02-02 13:54:06 UTC (rev 359)
+++ trunk/test/ruby/test_sprintf.rb 2006-02-02 14:21:23 UTC (rev 360)
@@ -0,0 +1,141 @@
+require 'test/unit'
+
+class TestSprintf < Test::Unit::TestCase
+ def test_binary
+ assert_equal("0", sprintf("%b", 0))
+ assert_equal("1", sprintf("%b", 1))
+ assert_equal("10", sprintf("%b", 2))
+ assert_equal("..1", sprintf("%b", -1))
+
+ assert_equal(" 0", sprintf("%4b", 0))
+ assert_equal(" 1", sprintf("%4b", 1))
+ assert_equal(" 10", sprintf("%4b", 2))
+ assert_equal(" ..1", sprintf("%4b", -1))
+
+ assert_equal("0000", sprintf("%04b", 0))
+ assert_equal("0001", sprintf("%04b", 1))
+ assert_equal("0010", sprintf("%04b", 2))
+ assert_equal("1111", sprintf("%04b", -1))
+
+ assert_equal("0000", sprintf("%.4b", 0))
+ assert_equal("0001", sprintf("%.4b", 1))
+ assert_equal("0010", sprintf("%.4b", 2))
+ assert_equal("1111", sprintf("%.4b", -1))
+
+ assert_equal(" 0000", sprintf("%6.4b", 0))
+ assert_equal(" 0001", sprintf("%6.4b", 1))
+ assert_equal(" 0010", sprintf("%6.4b", 2))
+ assert_equal(" 1111", sprintf("%6.4b", -1))
+
+ assert_equal(" 0b0", sprintf("%#4b", 0))
+ assert_equal(" 0b1", sprintf("%#4b", 1))
+ assert_equal("0b10", sprintf("%#4b", 2))
+ assert_equal("0b..1", sprintf("%#4b", -1))
+
+ assert_equal("0b00", sprintf("%#04b", 0))
+ assert_equal("0b01", sprintf("%#04b", 1))
+ assert_equal("0b10", sprintf("%#04b", 2))
+ assert_equal("0b11", sprintf("%#04b", -1))
+
+ assert_equal("0b0000", sprintf("%#.4b", 0))
+ assert_equal("0b0001", sprintf("%#.4b", 1))
+ assert_equal("0b0010", sprintf("%#.4b", 2))
+ assert_equal("0b1111", sprintf("%#.4b", -1))
+
+ assert_equal("0b0000", sprintf("%#6.4b", 0))
+ assert_equal("0b0001", sprintf("%#6.4b", 1))
+ assert_equal("0b0010", sprintf("%#6.4b", 2))
+ assert_equal("0b1111", sprintf("%#6.4b", -1))
+
+ assert_equal("+0", sprintf("%+b", 0))
+ assert_equal("+1", sprintf("%+b", 1))
+ assert_equal("+10", sprintf("%+b", 2))
+ assert_equal("-1", sprintf("%+b", -1))
+
+ assert_equal(" +0", sprintf("%+4b", 0))
+ assert_equal(" +1", sprintf("%+4b", 1))
+ assert_equal(" +10", sprintf("%+4b", 2))
+ assert_equal(" -1", sprintf("%+4b", -1))
+
+ assert_equal("+000", sprintf("%+04b", 0))
+ assert_equal("+001", sprintf("%+04b", 1))
+ assert_equal("+010", sprintf("%+04b", 2))
+ assert_equal("-001", sprintf("%+04b", -1))
+
+ assert_equal("+0000", sprintf("%+.4b", 0))
+ assert_equal("+0001", sprintf("%+.4b", 1))
+ assert_equal("+0010", sprintf("%+.4b", 2))
+ assert_equal("-0001", sprintf("%+.4b", -1))
+
+ assert_equal(" +0000", sprintf("%+6.4b", 0))
+ assert_equal(" +0001", sprintf("%+6.4b", 1))
+ assert_equal(" +0010", sprintf("%+6.4b", 2))
+ assert_equal(" -0001", sprintf("%+6.4b", -1))
+ end
+
+ def test_nan
+ nan = 0.0 / 0.0
+ assert_equal("NaN", sprintf("%f", nan))
+ assert_equal("NaN", sprintf("%-f", nan))
+ assert_equal("+NaN", sprintf("%+f", nan))
+
+ assert_equal(" NaN", sprintf("%8f", nan))
+ assert_equal("NaN ", sprintf("%-8f", nan))
+ assert_equal(" +NaN", sprintf("%+8f", nan))
+
+ assert_equal("00000NaN", sprintf("%08f", nan))
+ assert_equal("NaN ", sprintf("%-08f", nan))
+ assert_equal("+0000NaN", sprintf("%+08f", nan))
+
+ assert_equal(" NaN", sprintf("% 8f", nan))
+ assert_equal(" NaN ", sprintf("%- 8f", nan))
+ assert_equal(" +NaN", sprintf("%+ 8f", nan))
+
+ assert_equal(" 0000NaN", sprintf("% 08f", nan))
+ assert_equal(" NaN ", sprintf("%- 08f", nan))
+ assert_equal("+0000NaN", sprintf("%+ 08f", nan))
+ end
+
+ def test_inf
+ inf = 1.0 / 0.0
+ assert_equal("Inf", sprintf("%f", inf))
+ assert_equal("Inf", sprintf("%-f", inf))
+ assert_equal("+Inf", sprintf("%+f", inf))
+
+ assert_equal(" Inf", sprintf("%8f", inf))
+ assert_equal("Inf ", sprintf("%-8f", inf))
+ assert_equal(" +Inf", sprintf("%+8f", inf))
+
+ assert_equal("00000Inf", sprintf("%08f", inf))
+ assert_equal("Inf ", sprintf("%-08f", inf))
+ assert_equal("+0000Inf", sprintf("%+08f", inf))
+
+ assert_equal(" Inf", sprintf("% 8f", inf))
+ assert_equal(" Inf ", sprintf("%- 8f", inf))
+ assert_equal(" +Inf", sprintf("%+ 8f", inf))
+
+ assert_equal(" 0000Inf", sprintf("% 08f", inf))
+ assert_equal(" Inf ", sprintf("%- 08f", inf))
+ assert_equal("+0000Inf", sprintf("%+ 08f", inf))
+
+ assert_equal("-Inf", sprintf("%f", -inf))
+ assert_equal("-Inf", sprintf("%-f", -inf))
+ assert_equal("-Inf", sprintf("%+f", -inf))
+
+ assert_equal(" -Inf", sprintf("%8f", -inf))
+ assert_equal("-Inf ", sprintf("%-8f", -inf))
+ assert_equal(" -Inf", sprintf("%+8f", -inf))
+
+ assert_equal("-0000Inf", sprintf("%08f", -inf))
+ assert_equal("-Inf ", sprintf("%-08f", -inf))
+ assert_equal("-0000Inf", sprintf("%+08f", -inf))
+
+ assert_equal(" -Inf", sprintf("% 8f", -inf))
+ assert_equal("-Inf ", sprintf("%- 8f", -inf))
+ assert_equal(" -Inf", sprintf("%+ 8f", -inf))
+
+ assert_equal("-0000Inf", sprintf("% 08f", -inf))
+ assert_equal("-Inf ", sprintf("%- 08f", -inf))
+ assert_equal("-0000Inf", sprintf("%+ 08f", -inf))
+ end
+end
Added: trunk/test/ruby/test_string.rb
===================================================================
--- trunk/test/ruby/test_string.rb 2006-02-02 13:54:06 UTC (rev 359)
+++ trunk/test/ruby/test_string.rb 2006-02-02 14:21:23 UTC (rev 360)
@@ -0,0 +1,19 @@
+require 'test/unit'
+
+class TestString < Test::Unit::TestCase
+ def check_sum(str, bits=16)
+ sum = 0
+ str.each_byte {|c| sum += c}
+ sum = sum & ((1 << bits) - 1) if bits != 0
+ assert_equal(sum, str.sum(bits))
+ end
+ def test_sum
+ assert_equal(0, "".sum)
+ assert_equal(294, "abc".sum)
+ check_sum("abc")
+ check_sum("\x80")
+ 0.upto(70) {|bits|
+ check_sum("xyz", bits)
+ }
+ end
+end
Added: trunk/test/ruby/test_stringchar.rb
===================================================================
--- trunk/test/ruby/test_stringchar.rb 2006-02-02 13:54:06 UTC (rev 359)
+++ trunk/test/ruby/test_stringchar.rb 2006-02-02 14:21:23 UTC (rev 360)
@@ -0,0 +1,166 @@
+require 'test/unit'
+
+class TestStringchar < Test::Unit::TestCase
+ def test_string
+ assert_equal("abcd", "abcd")
+ assert_match(/abcd/, "abcd")
+ assert("abcd" === "abcd")
+ # compile time string concatenation
+ assert_equal("abcd", "ab" "cd")
+ assert_equal("22aacd44", "#{22}aa" "cd#{44}")
+ assert_equal("22aacd445566", "#{22}aa" "cd#{44}" "55" "#{66}")
+ assert("abc" !~ /^$/)
+ assert("abc\n" !~ /^$/)
+ assert("abc" !~ /^d*$/)
+ assert_equal(3, ("abc" =~ /d*$/))
+ assert("" =~ /^$/)
+ assert("\n" =~ /^$/)
+ assert("a\n\n" =~ /^$/)
+ assert("abcabc" =~ /.*a/); assert_equal("abca", $&)
+ assert("abcabc" =~ /.*c/); assert_equal("abcabc", $&)
+ assert("abcabc" =~ /.*?a/); assert_equal("a", $&)
+ assert("abcabc" =~ /.*?c/); assert_equal("abc", $&)
+ assert(/(.|\n)*?\n(b|\n)/ =~ "a\nb\n\n"); assert_equal("a\nb", $&)
+
+ assert(/^(ab+)+b/ =~ "ababb"); assert_equal("ababb", $&)
+ assert(/^(?:ab+)+b/ =~ "ababb"); assert_equal("ababb", $&)
+ assert(/^(ab+)+/ =~ "ababb"); assert_equal("ababb", $&)
+ assert(/^(?:ab+)+/ =~ "ababb"); assert_equal("ababb", $&)
+
+ assert(/(\s+\d+){2}/ =~ " 1 2"); assert_equal(" 1 2", $&)
+ assert(/(?:\s+\d+){2}/ =~ " 1 2"); assert_equal(" 1 2", $&)
+
+ $x = <<END;
+ABCD
+ABCD
+END
+ $x.gsub!(/((.|\n)*?)B((.|\n)*?)D/){$1+$3}
+ assert_equal("AC\nAC\n", $x)
+
+ assert("foobar" =~ /foo(?=(bar)|(baz))/)
+ assert("foobaz" =~ /foo(?=(bar)|(baz))/)
+
+ $foo = "abc"
+ assert_equal("abc = abc", "#$foo = abc")
+ assert_equal("abc = abc", "#{$foo} = abc")
+
+ foo = "abc"
+ assert_equal("abc = abc", "#{foo} = abc")
+
+ assert_equal('-----', '-' * 5)
+ assert_equal('-', '-' * 1)
+ assert_equal('', '-' * 0)
+
+ foo = '-'
+ assert_equal('-----', foo * 5)
+ assert_equal('-', foo * 1)
+ assert_equal('', foo * 0)
+
+ $x = "a.gif"
+ assert_equal("gif", $x.sub(/.*\.([^\.]+)$/, '\1'))
+ assert_equal("b.gif", $x.sub(/.*\.([^\.]+)$/, 'b.\1'))
+ assert_equal("", $x.sub(/.*\.([^\.]+)$/, '\2'))
+ assert_equal("ab", $x.sub(/.*\.([^\.]+)$/, 'a\2b'))
+ assert_equal("<a.gif>", $x.sub(/.*\.([^\.]+)$/, '<\&>'))
+ end
+
+ def test_char
+ # character constants(assumes ASCII)
+ assert_equal(?a, "a"[0])
+ assert_equal(?a, ?a)
+ assert_equal(1, ?\C-a)
+ assert_equal(225, ?\M-a)
+ assert_equal(129, ?\M-\C-a)
+ assert_equal(?A, "a".upcase![0])
+ assert_equal(?a, "A".downcase![0])
+ assert_equal("ABC", "abc".tr!("a-z", "A-Z"))
+ assert_equal("ABC", "aabbcccc".tr_s!("a-z", "A-Z"))
+ assert_equal("abc", "abcc".squeeze!("a-z"))
+ assert_equal("ad", "abcd".delete!("bc"))
+
+ $x = "abcdef"
+ $y = [ ?a, ?b, ?c, ?d, ?e, ?f ]
+ $bad = false
+ $x.each_byte {|i|
+ if i != $y.shift
+ $bad = true
+ break
+ end
+ }
+ assert(!$bad)
+
+ s = "a string"
+ s[0..s.size]="another string"
+ assert_equal("another string", s)
+
+ s = <<EOS
+#{
+[1,2,3].join(",")
+}
+EOS
+ assert_equal("1,2,3\n", s)
+ assert_equal(926381, "Just".to_i(36))
+ assert_equal(-23200231779, "-another".to_i(36))
+ assert_equal("ruby", 1299022.to_s(36))
+ assert_equal("-hacker", -1045307475.to_s(36))
+ assert_equal(265419172580680477752431643787347, "Just_another_Ruby_hacker".to_i(36))
+ assert_equal("-justanotherrubyhacker", -265419172580680477752431643787347.to_s(36))
+
+ a = []
+ (0..255).each {|n|
+ ch = [n].pack("C")
+ a.push ch if /a#{Regexp.quote ch}b/x =~ "ab"
+ }
+ assert_equal(0, a.size)
+ end
+
+ def test_bang
+ s = "aBc"
+ s.upcase
+ assert_equal("aBc", s)
+ s.upcase!
+ assert_equal("ABC", s)
+
+ s = "aBc"
+ s.downcase
+ assert_equal("aBc", s)
+ s.downcase!
+ assert_equal("abc", s)
+
+ s = "aBc"
+ s.swapcase
+ assert_equal("aBc", s)
+ s.swapcase!
+ assert_equal("AbC", s)
+
+ s = "aBc"
+ s.capitalize
+ assert_equal("aBc", s)
+ s.capitalize!
+ assert_equal("Abc", s)
+
+ s = "aBc"
+ s.tr("a-z", "A-Z")
+ assert_equal("aBc", s)
+ s.tr!("a-z", "A-Z")
+ assert_equal("ABC", s)
+
+ s = "aaBBcc"
+ s.tr_s("a-z", "A-Z")
+ assert_equal("aaBBcc", s)
+ s.tr_s!("a-z", "A-Z")
+ assert_equal("ABBC", s)
+
+ s = "aaBBcc"
+ s.squeeze("a-z")
+ assert_equal("aaBBcc", s)
+ s.squeeze!("a-z")
+ assert_equal("aBBc", s)
+
+ s = "aaBBcc"
+ s.delete("a-z")
+ assert_equal("aaBBcc", s)
+ s.delete!("a-z")
+ assert_equal("BB", s)
+ end
+end
Added: trunk/test/ruby/test_struct.rb
===================================================================
--- trunk/test/ruby/test_struct.rb 2006-02-02 13:54:06 UTC (rev 359)
+++ trunk/test/ruby/test_struct.rb 2006-02-02 14:21:23 UTC (rev 360)
@@ -0,0 +1,36 @@
+require 'test/unit'
+
+class TestStruct < Test::Unit::TestCase
+ def test_struct
+ struct_test = Struct.new("Test", :foo, :bar)
+ assert_equal(Struct::Test, struct_test)
+
+ test = struct_test.new(1, 2)
+ assert_equal(1, test.foo)
+ assert_equal(2, test.bar)
+ assert_equal(1, test[0])
+ assert_equal(2, test[1])
+
+ a, b = test.to_a
+ assert_equal(1, a)
+ assert_equal(2, b)
+
+ test[0] = 22
+ assert_equal(22, test.foo)
+
+ test.bar = 47
+ assert_equal(47, test.bar)
+ end
+
+ # [ruby-dev:26247] more than 10 struct members causes segmentation fault
+ def test_morethan10members
+ list = %w( a b c d e f g h i j k l m n o p )
+ until list.empty?
+ c = Struct.new(* list.map {|ch| ch.intern }).new
+ list.each do |ch|
+ c.__send__(ch)
+ end
+ list.pop
+ end
+ end
+end
Added: trunk/test/ruby/test_symbol.rb
===================================================================
--- trunk/test/ruby/test_symbol.rb 2006-02-02 13:54:06 UTC (rev 359)
+++ trunk/test/ruby/test_symbol.rb 2006-02-02 14:21:23 UTC (rev 360)
@@ -0,0 +1,77 @@
+require 'test/unit'
+
+class TestSymbol < Test::Unit::TestCase
+ # [ruby-core:3573]
+
+ def assert_eval_inspected(sym)
+ n = sym.inspect
+ assert_nothing_raised(SyntaxError) {assert_equal(sym, eval(n))}
+ end
+
+ def test_inspect_invalid
+ # 2) Symbol#inspect sometimes returns invalid symbol representations:
+ assert_eval_inspected(:"!")
+ assert_eval_inspected(:"=")
+ assert_eval_inspected(:"0")
+ assert_eval_inspected(:"$1")
+ assert_eval_inspected(:"@1")
+ assert_eval_inspected(:"@@1")
+ assert_eval_inspected(:"@")
+ assert_eval_inspected(:"@@")
+ end
+
+ def assert_inspect_evaled(n)
+ assert_nothing_raised(SyntaxError) {assert_equal(n, eval(n).inspect)}
+ end
+
+ def test_inspect_suboptimal
+ # 3) Symbol#inspect sometimes returns suboptimal symbol representations:
+ assert_inspect_evaled(':foo')
+ assert_inspect_evaled(':foo!')
+ assert_inspect_evaled(':bar?')
+ assert_inspect_evaled(':<<')
+ assert_inspect_evaled(':>>')
+ assert_inspect_evaled(':<=')
+ assert_inspect_evaled(':>=')
+ assert_inspect_evaled(':=~')
+ assert_inspect_evaled(':==')
+ assert_inspect_evaled(':===')
+ assert_raise(SyntaxError) {eval ':='}
+ assert_inspect_evaled(':*')
+ assert_inspect_evaled(':**')
+ assert_raise(SyntaxError) {eval ':***'}
+ assert_inspect_evaled(':+')
+ assert_inspect_evaled(':-')
+ assert_inspect_evaled(':+@')
+ assert_inspect_evaled(':-@')
+ assert_inspect_evaled(':|')
+ assert_inspect_evaled(':^')
+ assert_inspect_evaled(':&')
+ assert_inspect_evaled(':/')
+ assert_inspect_evaled(':%')
+ assert_inspect_evaled(':~')
+ assert_inspect_evaled(':`')
+ assert_inspect_evaled(':[]')
+ assert_inspect_evaled(':[]=')
+ assert_raise(SyntaxError) {eval ':||'}
+ assert_raise(SyntaxError) {eval ':&&'}
+ assert_raise(SyntaxError) {eval ':['}
+ end
+
+ def test_inspect_dollar
+ # 4) :$- always treats next character literally:
+ sym = "$-".intern
+ assert_nothing_raised(SyntaxError) {assert_equal(sym, eval(':$-'))}
+ assert_nothing_raised(SyntaxError) {assert_equal(sym, eval(":$-\n"))}
+ assert_nothing_raised(SyntaxError) {assert_equal(sym, eval(":$- "))}
+ assert_nothing_raised(SyntaxError) {assert_equal(sym, eval(":$-#"))}
+ assert_raise(SyntaxError) {eval ':$-('}
+ end
+
+ def test_inspect_number
+ # 5) Inconsistency between :$0 and :$1? The first one is valid, but the
+ # latter isn't.
+ assert_inspect_evaled(':$0')
+ assert_inspect_evaled(':$1')
+ end
+end
Added: trunk/test/ruby/test_system.rb
===================================================================
--- trunk/test/ruby/test_system.rb 2006-02-02 13:54:06 UTC (rev 359)
+++ trunk/test/ruby/test_system.rb 2006-02-02 14:21:23 UTC (rev 360)
@@ -0,0 +1,75 @@
+require 'test/unit'
+class TestSystem < Test::Unit::TestCase
+ def test_0
+ assert false, "BEGIN{} syntax not supported yet"
+ end
+end
+=begin
+require 'test/unit'
+require 'envutil'
+
+class TestSystem < Test::Unit::TestCase
+ def valid_syntax?(code, fname)
+ eval("BEGIN {return true}\n#{code}", nil, fname, 0)
+ end
+
+ def test_system
+ ruby = EnvUtil.rubybin
+ assert_equal("foobar\n", `echo foobar`)
+ assert_equal('foobar', `#{ruby} -e 'print "foobar"'`)
+
+ tmp = open("script_tmp", "w")
+ tmp.print "print $zzz\n";
+ tmp.close
+
+ assert_equal('true', `#{ruby} -s script_tmp -zzz`)
+ assert_equal('555', `#{ruby} -s script_tmp -zzz=555`)
+
+ tmp = open("script_tmp", "w")
+ tmp.print "#! /usr/local/bin/ruby -s\n";
+ tmp.print "print $zzz\n";
+ tmp.close
+
+ assert_equal('678', `#{ruby} script_tmp -zzz=678`)
+
+ tmp = open("script_tmp", "w")
+ tmp.print "this is a leading junk\n";
+ tmp.print "#! /usr/local/bin/ruby -s\n";
+ tmp.print "print $zzz\n";
+ tmp.print "__END__\n";
+ tmp.print "this is a trailing junk\n";
+ tmp.close
+
+ assert_equal('nil', `#{ruby} -x script_tmp`)
+ assert_equal('555', `#{ruby} -x script_tmp -zzz=555`)
+
+ tmp = open("script_tmp", "w")
+ for i in 1..5
+ tmp.print i, "\n"
+ end
+ tmp.close
+
+ `#{ruby} -i.bak -pe '$_.sub!(/^[0-9]+$/){$&.to_i * 5}' script_tmp`
+ tmp = open("script_tmp", "r")
+ while tmp.gets
+ assert_equal(0, $_.to_i % 5)
+ end
+ tmp.close
+
+ File.unlink "script_tmp" or `/bin/rm -f "script_tmp"`
+ File.unlink "script_tmp.bak" or `/bin/rm -f "script_tmp.bak"`
+ end
+
+ def test_syntax
+ assert_nothing_raised(Exception) do
+ for script in Dir[File.expand_path("../../../{lib,sample,ext}/**/*.rb", __FILE__)]
+ valid_syntax? IO::read(script), script
+ end
+ end
+ end
+
+ def test_empty_evstr # [ruby-dev:25113]
+ assert_equal("", eval('"#{}"', nil, __FILE__, __LINE__))
+ end
+end
+=end
Added: trunk/test/ruby/test_time.rb
===================================================================
--- trunk/test/ruby/test_time.rb 2006-02-02 13:54:06 UTC (rev 359)
+++ trunk/test/ruby/test_time.rb 2006-02-02 14:21:23 UTC (rev 360)
@@ -0,0 +1,74 @@
+require 'test/unit'
+
+class TestTime < Test::Unit::TestCase
+ def test_time_add()
+ assert_equal(Time.utc(2000, 3, 21, 3, 30) + 3 * 3600,
+ Time.utc(2000, 3, 21, 6, 30))
+ assert_equal(Time.utc(2000, 3, 21, 3, 30) + (-3 * 3600),
+ Time.utc(2000, 3, 21, 0, 30))
+ assert_equal(0, (Time.at(1.1) + 0.9).usec)
+ end
+
+ def test_time_subt()
+ assert_equal(Time.utc(2000, 3, 21, 3, 30) - 3 * 3600,
+ Time.utc(2000, 3, 21, 0, 30))
+ assert_equal(Time.utc(2000, 3, 21, 3, 30) - (-3 * 3600),
+ Time.utc(2000, 3, 21, 6, 30))
+ assert_equal(900000, (Time.at(1.1) - 0.2).usec)
+ end
+
+ def test_time_time()
+ assert_equal(Time.utc(2000, 3, 21, 3, 30) \
+ -Time.utc(2000, 3, 21, 0, 30), 3*3600)
+ assert_equal(Time.utc(2000, 3, 21, 0, 30) \
+ -Time.utc(2000, 3, 21, 3, 30), -3*3600)
+ end
+
+ def negative_time_t?
+ begin
+ Time.at(-1)
+ true
+ rescue ArgumentError
+ false
+ end
+ end
+
+ def test_timegm
+ if negative_time_t?
+ assert_equal(-0x80000000, Time.utc(1901, 12, 13, 20, 45, 52).tv_sec)
+ assert_equal(-2, Time.utc(1969, 12, 31, 23, 59, 58).tv_sec)
+ assert_equal(-1, Time.utc(1969, 12, 31, 23, 59, 59).tv_sec)
+ end
+
+ assert_equal(0, Time.utc(1970, 1, 1, 0, 0, 0).tv_sec) # the Epoch
+ assert_equal(1, Time.utc(1970, 1, 1, 0, 0, 1).tv_sec)
+ assert_equal(31535999, Time.utc(1970, 12, 31, 23, 59, 59).tv_sec)
+ assert_equal(31536000, Time.utc(1971, 1, 1, 0, 0, 0).tv_sec)
+ assert_equal(78796799, Time.utc(1972, 6, 30, 23, 59, 59).tv_sec)
+
+ # 1972-06-30T23:59:60Z is the first leap second.
+ if Time.utc(1972, 7, 1, 0, 0, 0) - Time.utc(1972, 6, 30, 23, 59, 59) == 1
+ # no leap second.
+ assert_equal(78796800, Time.utc(1972, 7, 1, 0, 0, 0).tv_sec)
+ assert_equal(78796801, Time.utc(1972, 7, 1, 0, 0, 1).tv_sec)
+ assert_equal(946684800, Time.utc(2000, 1, 1, 0, 0, 0).tv_sec)
+ assert_equal(0x7fffffff, Time.utc(2038, 1, 19, 3, 14, 7).tv_sec)
+ else
+ # leap seconds supported.
+ assert_equal(2, Time.utc(1972, 7, 1, 0, 0, 0) - Time.utc(1972, 6, 30, 23, 59, 59))
+ assert_equal(78796800, Time.utc(1972, 6, 30, 23, 59, 60).tv_sec)
+ assert_equal(78796801, Time.utc(1972, 7, 1, 0, 0, 0).tv_sec)
+ assert_equal(78796802, Time.utc(1972, 7, 1, 0, 0, 1).tv_sec)
+ assert_equal(946684822, Time.utc(2000, 1, 1, 0, 0, 0).tv_sec)
+ end
+ end
+
+ def test_huge_difference # [ruby-dev:22619]
+ if negative_time_t?
+ assert_equal(Time.at(-0x80000000), Time.at(0x7fffffff) - 0xffffffff)
+ assert_equal(Time.at(-0x80000000), Time.at(0x7fffffff) + (-0xffffffff))
+ assert_equal(Time.at(0x7fffffff), Time.at(-0x80000000) + 0xffffffff)
+ assert_equal(Time.at(0x7fffffff), Time.at(-0x80000000) - (-0xffffffff))
+ end
+ end
+end
Added: trunk/test/ruby/ut_eof.rb
===================================================================
--- trunk/test/ruby/ut_eof.rb 2006-02-02 13:54:06 UTC (rev 359)
+++ trunk/test/ruby/ut_eof.rb 2006-02-02 14:21:23 UTC (rev 360)
@@ -0,0 +1,128 @@
+require 'test/unit'
+
+module TestEOF
+ def test_eof_0
+ open_file("") {|f|
+ assert_equal("", f.read(0))
+ assert_equal("", f.read(0))
+ assert_equal("", f.read)
+ assert_equal("", f.read(0))
+ assert_equal("", f.read(0))
+ }
+ open_file("") {|f|
+ assert_nil(f.read(1))
+ assert_equal("", f.read)
+ assert_nil(f.read(1))
+ }
+ open_file("") {|f|
+ s = "x"
+ assert_equal("", f.read(nil, s))
+ assert_equal("", s)
+ }
+ open_file("") {|f|
+ s = "x"
+ assert_nil(f.read(10, s))
+ assert_equal("", s)
+ }
+ end
+
+ def test_eof_0_rw
+ return unless respond_to? :open_file_rw
+ open_file_rw("") {|f|
+ assert_equal("", f.read)
+ assert_equal("", f.read)
+ assert_equal(0, f.syswrite(""))
+ assert_equal("", f.read)
+ }
+ end
+
+ def test_eof_1
+ open_file("a") {|f|
+ assert_equal("", f.read(0))
+ assert_equal("a", f.read(1))
+ assert_equal("" , f.read(0))
+ assert_equal("" , f.read(0))
+ assert_equal("", f.read)
+ assert_equal("", f.read(0))
+ assert_equal("", f.read(0))
+ }
+ open_file("a") {|f|
+ assert_equal("a", f.read(1))
+ assert_nil(f.read(1))
+ }
+ open_file("a") {|f|
+ assert_equal("a", f.read(2))
+ assert_nil(f.read(1))
+ assert_equal("", f.read)
+ assert_nil(f.read(1))
+ }
+ open_file("a") {|f|
+ assert_equal("a", f.read)
+ assert_nil(f.read(1))
+ assert_equal("", f.read)
+ assert_nil(f.read(1))
+ }
+ open_file("a") {|f|
+ assert_equal("a", f.read(2))
+ assert_equal("", f.read)
+ assert_equal("", f.read)
+ }
+ open_file("a") {|f|
+ assert_equal("a", f.read)
+ assert_equal("", f.read(0))
+ }
+ open_file("a") {|f|
+ s = "x"
+ assert_equal("a", f.read(nil, s))
+ assert_equal("a", s)
+ }
+ open_file("a") {|f|
+ s = "x"
+ assert_equal("a", f.read(10, s))
+ assert_equal("a", s)
+ }
+ end
+
+ def test_eof_2
+ open_file("") {|f|
+ assert_equal("", f.read)
+ assert(f.eof?)
+ }
+ end
+
+ def test_eof_3
+ open_file("") {|f|
+ assert(f.eof?)
+ }
+ end
+
+ module Seek
+ def open_file_seek(content, pos)
+ open_file(content) do |f|
+ f.seek(pos)
+ yield f
+ end
+ end
+
+ def test_eof_0_seek
+ open_file_seek("", 10) {|f|
+ assert_equal(10, f.pos)
+ assert_equal("", f.read(0))
+ assert_equal("", f.read)
+ assert_equal("", f.read(0))
+ assert_equal("", f.read)
+ }
+ end
+
+ def test_eof_1_seek
+ open_file_seek("a", 10) {|f|
+ assert_equal("", f.read)
+ assert_equal("", f.read)
+ }
+ open_file_seek("a", 1) {|f|
+ assert_equal("", f.read)
+ assert_equal("", f.read)
+ }
+ end
+ end
+end
--
ML: yarv-diff quickml.atdot.net
Info: http://www.atdot.net/~ko1/quickml