diff options
author | shyouhei <shyouhei@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2007-08-15 19:08:43 +0000 |
---|---|---|
committer | shyouhei <shyouhei@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2007-08-15 19:08:43 +0000 |
commit | d464704f111d211c1f1ff9ef23ef1d755054be00 (patch) | |
tree | b58b17b645dc463322e5fca57fe282360db659c9 /ruby_1_8_5/test/ruby | |
parent | e4f06b3f2dec4b5d6334c5e9907e1cecbf649fc4 (diff) |
add tag v1_8_5_54
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/tags/v1_8_5_54@12952 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'ruby_1_8_5/test/ruby')
51 files changed, 4710 insertions, 0 deletions
diff --git a/ruby_1_8_5/test/ruby/beginmainend.rb b/ruby_1_8_5/test/ruby/beginmainend.rb new file mode 100644 index 0000000000..646140dd22 --- /dev/null +++ b/ruby_1_8_5/test/ruby/beginmainend.rb @@ -0,0 +1,80 @@ +errout = ARGV.shift + +BEGIN { + puts "b1" + local_begin1 = "local_begin1" + $global_begin1 = "global_begin1" + ConstBegin1 = "ConstBegin1" +} + +BEGIN { + puts "b2" + + BEGIN { + puts "b2-1" + } +} + +# for scope check +raise if defined?(local_begin1) +raise unless defined?($global_begin1) +raise unless defined?(::ConstBegin1) +local_for_end2 = "e2" +$global_for_end1 = "e1" + +puts "main" + +END { + puts local_for_end2 # e2 +} + +eval <<EOE + BEGIN { + puts "b3" + + BEGIN { + puts "b3-1" + } + } + + BEGIN { + puts "b4" + } + + END { + puts "e3" + } + + END { + puts "e4" + + END { + puts "e4-1" + + END { + puts "e4-1-1" + } + } + + END { + puts "e4-2" + } + } +EOE + +END { + exit + puts "should not be dumped" + + END { + puts "not reached" + } +} + +END { + puts $global_for_end1 # e1 + + END { + puts "e1-1" + } +} diff --git a/ruby_1_8_5/test/ruby/endblockwarn.rb b/ruby_1_8_5/test/ruby/endblockwarn.rb new file mode 100644 index 0000000000..7b7f97f597 --- /dev/null +++ b/ruby_1_8_5/test/ruby/endblockwarn.rb @@ -0,0 +1,12 @@ +def end1 + END {} +end + +end1 + +eval <<EOE + def end2 + END {} + end +EOE + diff --git a/ruby_1_8_5/test/ruby/envutil.rb b/ruby_1_8_5/test/ruby/envutil.rb new file mode 100644 index 0000000000..c481326288 --- /dev/null +++ b/ruby_1_8_5/test/ruby/envutil.rb @@ -0,0 +1,28 @@ +module EnvUtil + def rubybin + if ruby = ENV["RUBY"] + return ruby + end + ruby = "ruby" + rubyexe = ruby+".exe" + 3.times do + if File.exist? ruby and File.executable? ruby and !File.directory? ruby + return File.expand_path(ruby) + end + if File.exist? rubyexe and File.executable? rubyexe + 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 diff --git a/ruby_1_8_5/test/ruby/marshaltestlib.rb b/ruby_1_8_5/test/ruby/marshaltestlib.rb new file mode 100644 index 0000000000..891f43b1f7 --- /dev/null +++ b/ruby_1_8_5/test/ruby/marshaltestlib.rb @@ -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 diff --git a/ruby_1_8_5/test/ruby/test_alias.rb b/ruby_1_8_5/test/ruby/test_alias.rb new file mode 100644 index 0000000000..83f897fb00 --- /dev/null +++ b/ruby_1_8_5/test/ruby/test_alias.rb @@ -0,0 +1,40 @@ +require 'test/unit' + +class TestAlias < Test::Unit::TestCase + class Alias0 + def foo; "foo" end + end + class Alias1<Alias0 + alias bar foo + def foo; "foo+" + super end + end + class Alias2<Alias1 + alias baz foo + undef foo + end + class Alias3<Alias2 + def foo + defined? super + end + def bar + defined? super + end + def quux + defined? super + end + end + + def test_alias + x = Alias2.new + assert_equal("foo", x.bar) + assert_equal("foo+foo", x.baz) + + # test_check for cache + assert_equal("foo+foo", x.baz) + + x = Alias3.new + assert(!x.foo) + assert(x.bar) + assert(!x.quux) + end +end diff --git a/ruby_1_8_5/test/ruby/test_array.rb b/ruby_1_8_5/test/ruby/test_array.rb new file mode 100644 index 0000000000..c56f06c3b2 --- /dev/null +++ b/ruby_1_8_5/test/ruby/test_array.rb @@ -0,0 +1,145 @@ +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_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 diff --git a/ruby_1_8_5/test/ruby/test_assignment.rb b/ruby_1_8_5/test/ruby/test_assignment.rb new file mode 100644 index 0000000000..63f37a9d73 --- /dev/null +++ b/ruby_1_8_5/test/ruby/test_assignment.rb @@ -0,0 +1,467 @@ +require 'test/unit' + +class TestAssignment < Test::Unit::TestCase + def test_assign + a=[]; a[0] ||= "bar"; + assert_equal("bar", a[0]) + h={}; h["foo"] ||= "bar"; + assert_equal("bar", h["foo"]) + + aa = 5 + aa ||= 25 + assert_equal(5, aa) + bb ||= 25 + assert_equal(25, bb) + cc &&=33 + assert_nil(cc) + cc = 5 + cc &&=44 + assert_equal(44, cc) + + a = nil; assert_nil(a) + a = 1; assert_equal(1, a) + a = []; assert_equal([], a) + a = [1]; assert_equal([1], a) + a = [nil]; assert_equal([nil], a) + a = [[]]; assert_equal([[]], a) + a = [1,2]; assert_equal([1,2], a) + a = [*[]]; assert_equal([], a) + a = [*[1]]; assert_equal([1], a) + a = [*[1,2]]; assert_equal([1,2], a) + + a = *nil; assert_nil(a) + a = *1; assert_equal(1, a) + a = *[]; assert_nil(a) + a = *[1]; assert_equal(1, a) + a = *[nil]; assert_nil(a) + a = *[[]]; assert_equal([], a) + a = *[1,2]; assert_equal([1,2], a) + a = *[*[]]; assert_nil(a) + a = *[*[1]]; assert_equal(1, a) + a = *[*[1,2]]; assert_equal([1,2], a) + + *a = nil; assert_equal([nil], a) + *a = 1; assert_equal([1], a) + *a = []; assert_equal([[]], a) + *a = [1]; assert_equal([[1]], a) + *a = [nil]; assert_equal([[nil]], a) + *a = [[]]; assert_equal([[[]]], a) + *a = [1,2]; assert_equal([[1,2]], a) + *a = [*[]]; assert_equal([[]], a) + *a = [*[1]]; assert_equal([[1]], a) + *a = [*[1,2]]; assert_equal([[1,2]], a) + + *a = *nil; assert_equal([nil], a) + *a = *1; assert_equal([1], a) + *a = *[]; assert_equal([], a) + *a = *[1]; assert_equal([1], a) + *a = *[nil]; assert_equal([nil], a) + *a = *[[]]; assert_equal([[]], a) + *a = *[1,2]; assert_equal([1,2], a) + *a = *[*[]]; assert_equal([], a) + *a = *[*[1]]; assert_equal([1], a) + *a = *[*[1,2]]; assert_equal([1,2], a) + + a,b,*c = nil; assert_equal([nil,nil,[]], [a,b,c]) + a,b,*c = 1; assert_equal([1,nil,[]], [a,b,c]) + a,b,*c = []; assert_equal([nil,nil,[]], [a,b,c]) + a,b,*c = [1]; assert_equal([1,nil,[]], [a,b,c]) + a,b,*c = [nil]; assert_equal([nil,nil,[]], [a,b,c]) + a,b,*c = [[]]; assert_equal([[],nil,[]], [a,b,c]) + a,b,*c = [1,2]; assert_equal([1,2,[]], [a,b,c]) + a,b,*c = [*[]]; assert_equal([nil,nil,[]], [a,b,c]) + a,b,*c = [*[1]]; assert_equal([1,nil,[]], [a,b,c]) + a,b,*c = [*[1,2]]; assert_equal([1,2,[]], [a,b,c]) + + a,b,*c = *nil; assert_equal([nil,nil,[]], [a,b,c]) + a,b,*c = *1; assert_equal([1,nil,[]], [a,b,c]) + a,b,*c = *[]; assert_equal([nil,nil,[]], [a,b,c]) + a,b,*c = *[1]; assert_equal([1,nil,[]], [a,b,c]) + a,b,*c = *[nil]; assert_equal([nil,nil,[]], [a,b,c]) + a,b,*c = *[[]]; assert_equal([[],nil,[]], [a,b,c]) + a,b,*c = *[1,2]; assert_equal([1,2,[]], [a,b,c]) + a,b,*c = *[*[]]; assert_equal([nil,nil,[]], [a,b,c]) + a,b,*c = *[*[1]]; assert_equal([1,nil,[]], [a,b,c]) + a,b,*c = *[*[1,2]]; assert_equal([1,2,[]], [a,b,c]) + end + + def test_yield + def f; yield nil; end; f {|a| assert_nil(a)} + def f; yield 1; end; f {|a| assert_equal(1, a)} + def f; yield []; end; f {|a| assert_equal([], a)} + def f; yield [1]; end; f {|a| assert_equal([1], a)} + def f; yield [nil]; end; f {|a| assert_equal([nil], a)} + def f; yield [[]]; end; f {|a| assert_equal([[]], a)} + def f; yield [*[]]; end; f {|a| assert_equal([], a)} + def f; yield [*[1]]; end; f {|a| assert_equal([1], a)} + def f; yield [*[1,2]]; end; f {|a| assert_equal([1,2], a)} + + def f; yield *nil; end; f {|a| assert_nil(a)} + def f; yield *1; end; f {|a| assert_equal(1, a)} + def f; yield *[1]; end; f {|a| assert_equal(1, a)} + def f; yield *[nil]; end; f {|a| assert_nil(a)} + def f; yield *[[]]; end; f {|a| assert_equal([], a)} + def f; yield *[*[1]]; end; f {|a| assert_equal(1, a)} + + def f; yield; end; f {|*a| assert_equal([], a)} + def f; yield nil; end; f {|*a| assert_equal([nil], a)} + def f; yield 1; end; f {|*a| assert_equal([1], a)} + def f; yield []; end; f {|*a| assert_equal([[]], a)} + def f; yield [1]; end; f {|*a| assert_equal([[1]], a)} + def f; yield [nil]; end; f {|*a| assert_equal([[nil]], a)} + def f; yield [[]]; end; f {|*a| assert_equal([[[]]], a)} + def f; yield [1,2]; end; f {|*a| assert_equal([[1,2]], a)} + def f; yield [*[]]; end; f {|*a| assert_equal([[]], a)} + def f; yield [*[1]]; end; f {|*a| assert_equal([[1]], a)} + def f; yield [*[1,2]]; end; f {|*a| assert_equal([[1,2]], a)} + + def f; yield *nil; end; f {|*a| assert_equal([nil], a)} + def f; yield *1; end; f {|*a| assert_equal([1], a)} + def f; yield *[]; end; f {|*a| assert_equal([], a)} + def f; yield *[1]; end; f {|*a| assert_equal([1], a)} + def f; yield *[nil]; end; f {|*a| assert_equal([nil], a)} + def f; yield *[[]]; end; f {|*a| assert_equal([[]], a)} + def f; yield *[*[]]; end; f {|*a| assert_equal([], a)} + def f; yield *[*[1]]; end; f {|*a| assert_equal([1], a)} + def f; yield *[*[1,2]]; end; f {|*a| assert_equal([1,2], a)} + + def f; yield; end; f {|a,b,*c| assert_equal([nil,nil,[]], [a,b,c])} + def f; yield nil; end; f {|a,b,*c| assert_equal([nil,nil,[]], [a,b,c])} + def f; yield 1; end; f {|a,b,*c| assert_equal([1,nil,[]], [a,b,c])} + def f; yield []; end; f {|a,b,*c| assert_equal([nil,nil,[]], [a,b,c])} + def f; yield [1]; end; f {|a,b,*c| assert_equal([1,nil,[]], [a,b,c])} + def f; yield [nil]; end; f {|a,b,*c| assert_equal([nil,nil,[]], [a,b,c])} + def f; yield [[]]; end; f {|a,b,*c| assert_equal([[],nil,[]], [a,b,c])} + def f; yield [*[]]; end; f {|a,b,*c| assert_equal([nil,nil,[]], [a,b,c])} + def f; yield [*[1]]; end; f {|a,b,*c| assert_equal([1,nil,[]], [a,b,c])} + def f; yield [*[1,2]]; end; f {|a,b,*c| assert_equal([1,2,[]], [a,b,c])} + + def f; yield *nil; end; f {|a,b,*c| assert_equal([nil,nil,[]], [a,b,c])} + def f; yield *1; end; f {|a,b,*c| assert_equal([1,nil,[]], [a,b,c])} + def f; yield *[]; end; f {|a,b,*c| assert_equal([nil,nil,[]], [a,b,c])} + def f; yield *[1]; end; f {|a,b,*c| assert_equal([1,nil,[]], [a,b,c])} + def f; yield *[nil]; end; f {|a,b,*c| assert_equal([nil,nil,[]], [a,b,c])} + def f; yield *[[]]; end; f {|a,b,*c| assert_equal([[],nil,[]], [a,b,c])} + def f; yield *[*[]]; end; f {|a,b,*c| assert_equal([nil,nil,[]], [a,b,c])} + def f; yield *[*[1]]; end; f {|a,b,*c| assert_equal([1,nil,[]], [a,b,c])} + def f; yield *[*[1,2]]; end; f {|a,b,*c| assert_equal([1,2,[]], [a,b,c])} + end + + def test_return + def r; return; end; a = r(); assert_nil(a) + def r; return nil; end; a = r(); assert_nil(a) + def r; return 1; end; a = r(); assert_equal(1, a) + def r; return []; end; a = r(); assert_equal([], a) + def r; return [1]; end; a = r(); assert_equal([1], a) + def r; return [nil]; end; a = r(); assert_equal([nil], a) + def r; return [[]]; end; a = r(); assert_equal([[]], a) + def r; return [*[]]; end; a = r(); assert_equal([], a) + def r; return [*[1]]; end; a = r(); assert_equal([1], a) + def r; return [*[1,2]]; end; a = r(); assert_equal([1,2], a) + + def r; return *nil; end; a = r(); assert_nil(a) + def r; return *1; end; a = r(); assert_equal(1, a) + def r; return *[]; end; a = r(); assert_nil(a) + def r; return *[1]; end; a = r(); assert_equal(1, a) + def r; return *[nil]; end; a = r(); assert_nil(a) + def r; return *[[]]; end; a = r(); assert_equal([], a) + def r; return *[*[]]; end; a = r(); assert_nil(a) + def r; return *[*[1]]; end; a = r(); assert_equal(1, a) + def r; return *[*[1,2]]; end; a = r(); assert_equal([1,2], a) + + def r; return *nil; end; a = *r(); assert_nil(a) + def r; return *1; end; a = *r(); assert_equal(1, a) + def r; return *[]; end; a = *r(); assert_nil(a) + def r; return *[1]; end; a = *r(); assert_equal(1, a) + def r; return *[nil]; end; a = *r(); assert_nil(a) + def r; return *[[]]; end; a = *r(); assert_nil(a) + def r; return *[*[]]; end; a = *r(); assert_nil(a) + def r; return *[*[1]]; end; a = *r(); assert_equal(1, a) + def r; return *[*[1,2]]; end; a = *r(); assert_equal([1,2], a) + + def r; return; end; *a = r(); assert_equal([nil], a) + def r; return nil; end; *a = r(); assert_equal([nil], a) + def r; return 1; end; *a = r(); assert_equal([1], a) + def r; return []; end; *a = r(); assert_equal([[]], a) + def r; return [1]; end; *a = r(); assert_equal([[1]], a) + def r; return [nil]; end; *a = r(); assert_equal([[nil]], a) + def r; return [[]]; end; *a = r(); assert_equal([[[]]], a) + def r; return [1,2]; end; *a = r(); assert_equal([[1,2]], a) + def r; return [*[]]; end; *a = r(); assert_equal([[]], a) + def r; return [*[1]]; end; *a = r(); assert_equal([[1]], a) + def r; return [*[1,2]]; end; *a = r(); assert_equal([[1,2]], a) + + def r; return *nil; end; *a = r(); assert_equal([nil], a) + def r; return *1; end; *a = r(); assert_equal([1], a) + def r; return *[]; end; *a = r(); assert_equal([nil], a) + def r; return *[1]; end; *a = r(); assert_equal([1], a) + def r; return *[nil]; end; *a = r(); assert_equal([nil], a) + def r; return *[[]]; end; *a = r(); assert_equal([[]], a) + def r; return *[1,2]; end; *a = r(); assert_equal([[1,2]], a) + def r; return *[*[]]; end; *a = r(); assert_equal([nil], a) + def r; return *[*[1]]; end; *a = r(); assert_equal([1], a) + def r; return *[*[1,2]]; end; *a = r(); assert_equal([[1,2]], a) + + def r; return *nil; end; *a = *r(); assert_equal([nil], a) + def r; return *1; end; *a = *r(); assert_equal([1], a) + def r; return *[]; end; *a = *r(); assert_equal([nil], a) + def r; return *[1]; end; *a = *r(); assert_equal([1], a) + def r; return *[nil]; end; *a = *r(); assert_equal([nil], a) + def r; return *[[]]; end; *a = *r(); assert_equal([], a) + def r; return *[1,2]; end; *a = *r(); assert_equal([1,2], a) + def r; return *[*[]]; end; *a = *r(); assert_equal([nil], a) + def r; return *[*[1]]; end; *a = *r(); assert_equal([1], a) + def r; return *[*[1,2]]; end; *a = *r(); assert_equal([1,2], a) + + def r; return; end; a,b,*c = r(); assert_equal([nil,nil,[]], [a,b,c]) + def r; return nil; end; a,b,*c = r(); assert_equal([nil,nil,[]], [a,b,c]) + def r; return 1; end; a,b,*c = r(); assert_equal([1,nil,[]], [a,b,c]) + def r; return []; end; a,b,*c = r(); assert_equal([nil,nil,[]], [a,b,c]) + def r; return [1]; end; a,b,*c = r(); assert_equal([1,nil,[]], [a,b,c]) + def r; return [nil]; end; a,b,*c = r(); assert_equal([nil,nil,[]], [a,b,c]) + def r; return [[]]; end; a,b,*c = r(); assert_equal([[],nil,[]], [a,b,c]) + def r; return [1,2]; end; a,b,*c = r(); assert_equal([1,2,[]], [a,b,c]) + def r; return [*[]]; end; a,b,*c = r(); assert_equal([nil,nil,[]], [a,b,c]) + def r; return [*[1]]; end; a,b,*c = r(); assert_equal([1,nil,[]], [a,b,c]) + def r; return [*[1,2]]; end; a,b,*c = r(); assert_equal([1,2,[]], [a,b,c]) + + def r; return *nil; end; a,b,*c = r(); assert_equal([nil,nil,[]], [a,b,c]) + def r; return *1; end; a,b,*c = r(); assert_equal([1,nil,[]], [a,b,c]) + def r; return *[]; end; a,b,*c = r(); assert_equal([nil,nil,[]], [a,b,c]) + def r; return *[1]; end; a,b,*c = r(); assert_equal([1,nil,[]], [a,b,c]) + def r; return *[nil]; end; a,b,*c = r(); assert_equal([nil,nil,[]], [a,b,c]) + def r; return *[[]]; end; a,b,*c = r(); assert_equal([nil,nil,[]], [a,b,c]) + def r; return *[1,2]; end; a,b,*c = r(); assert_equal([1,2,[]], [a,b,c]) + def r; return *[*[]]; end; a,b,*c = r(); assert_equal([nil,nil,[]], [a,b,c]) + def r; return *[*[1]]; end; a,b,*c = r(); assert_equal([1,nil,[]], [a,b,c]) + def r; return *[*[1,2]]; end; a,b,*c = r(); assert_equal([1,2,[]], [a,b,c]) + end + + def test_lambda + f = lambda {|r,| assert_equal([], r)} + f.call([], *[]) + + f = lambda {|r,*l| assert_equal([], r); assert_equal([1], l)} + f.call([], *[1]) + + f = lambda{|x| x} + assert_equal(42, f.call(42)) + assert_equal([42], f.call([42])) + assert_equal([[42]], f.call([[42]])) + assert_equal([42,55], f.call([42,55])) + + f = lambda{|x,| x} + assert_equal(42, f.call(42)) + assert_equal([42], f.call([42])) + assert_equal([[42]], f.call([[42]])) + assert_equal([42,55], f.call([42,55])) + + f = lambda{|*x| x} + assert_equal([42], f.call(42)) + assert_equal([[42]], f.call([42])) + assert_equal([[[42]]], f.call([[42]])) + assert_equal([[42,55]], f.call([42,55])) + assert_equal([42,55], f.call(42,55)) + end + + def test_multi + a,=*[1] + assert_equal(1, a) + a,=*[[1]] + assert_equal([1], a) + a,=*[[[1]]] + assert_equal([[1]], a) + + x, (y, z) = 1, 2, 3 + assert_equal([1,2,nil], [x,y,z]) + x, (y, z) = 1, [2,3] + assert_equal([1,2,3], [x,y,z]) + x, (y, z) = 1, [2] + assert_equal([1,2,nil], [x,y,z]) + end + + def test_break + a = loop do break; end; assert_nil(a) + a = loop do break nil; end; assert_nil(a) + a = loop do break 1; end; assert_equal(1, a) + a = loop do break []; end; assert_equal([], a) + a = loop do break [1]; end; assert_equal([1], a) + a = loop do break [nil]; end; assert_equal([nil], a) + a = loop do break [[]]; end; assert_equal([[]], a) + a = loop do break [*[]]; end; assert_equal([], a) + a = loop do break [*[1]]; end; assert_equal([1], a) + a = loop do break [*[1,2]]; end; assert_equal([1,2], a) + + a = loop do break *nil; end; assert_nil(a) + a = loop do break *1; end; assert_equal(1, a) + a = loop do break *[]; end; assert_nil(a) + a = loop do break *[1]; end; assert_equal(1, a) + a = loop do break *[nil]; end; assert_nil(a) + a = loop do break *[[]]; end; assert_equal([], a) + a = loop do break *[*[]]; end; assert_nil(a) + a = loop do break *[*[1]]; end; assert_equal(1, a) + a = loop do break *[*[1,2]]; end; assert_equal([1,2], a) + + *a = loop do break; end; assert_equal([nil], a) + *a = loop do break nil; end; assert_equal([nil], a) + *a = loop do break 1; end; assert_equal([1], a) + *a = loop do break []; end; assert_equal([[]], a) + *a = loop do break [1]; end; assert_equal([[1]], a) + *a = loop do break [nil]; end; assert_equal([[nil]], a) + *a = loop do break [[]]; end; assert_equal([[[]]], a) + *a = loop do break [1,2]; end; assert_equal([[1,2]], a) + *a = loop do break [*[]]; end; assert_equal([[]], a) + *a = loop do break [*[1]]; end; assert_equal([[1]], a) + *a = loop do break [*[1,2]]; end; assert_equal([[1,2]], a) + + *a = loop do break *nil; end; assert_equal([nil], a) + *a = loop do break *1; end; assert_equal([1], a) + *a = loop do break *[]; end; assert_equal([nil], a) + *a = loop do break *[1]; end; assert_equal([1], a) + *a = loop do break *[nil]; end; assert_equal([nil], a) + *a = loop do break *[[]]; end; assert_equal([[]], a) + *a = loop do break *[1,2]; end; assert_equal([[1,2]], a) + *a = loop do break *[*[]]; end; assert_equal([nil], a) + *a = loop do break *[*[1]]; end; assert_equal([1], a) + *a = loop do break *[*[1,2]]; end; assert_equal([[1,2]], a) + + *a = *loop do break *nil; end; assert_equal([nil], a) + *a = *loop do break *1; end; assert_equal([1], a) + *a = *loop do break *[]; end; assert_equal([nil], a) + *a = *loop do break *[1]; end; assert_equal([1], a) + *a = *loop do break *[nil]; end; assert_equal([nil], a) + *a = *loop do break *[[]]; end; assert_equal([], a) + *a = *loop do break *[1,2]; end; assert_equal([1,2], a) + *a = *loop do break *[*[]]; end; assert_equal([nil], a) + *a = *loop do break *[*[1]]; end; assert_equal([1], a) + *a = *loop do break *[*[1,2]]; end; assert_equal([1,2], a) + + a,b,*c = loop do break; end; assert_equal([nil,nil,[]], [a,b,c]) + a,b,*c = loop do break nil; end; assert_equal([nil,nil,[]], [a,b,c]) + a,b,*c = loop do break 1; end; assert_equal([1,nil,[]], [a,b,c]) + a,b,*c = loop do break []; end; assert_equal([nil,nil,[]], [a,b,c]) + a,b,*c = loop do break [1]; end; assert_equal([1,nil,[]], [a,b,c]) + a,b,*c = loop do break [nil]; end; assert_equal([nil,nil,[]], [a,b,c]) + a,b,*c = loop do break [[]]; end; assert_equal([[],nil,[]], [a,b,c]) + a,b,*c = loop do break [1,2]; end; assert_equal([1,2,[]], [a,b,c]) + a,b,*c = loop do break [*[]]; end; assert_equal([nil,nil,[]], [a,b,c]) + a,b,*c = loop do break [*[1]]; end; assert_equal([1,nil,[]], [a,b,c]) + a,b,*c = loop do break [*[1,2]]; end; assert_equal([1,2,[]], [a,b,c]) + + a,b,*c = loop do break *nil; end; assert_equal([nil,nil,[]], [a,b,c]) + a,b,*c = loop do break *1; end; assert_equal([1,nil,[]], [a,b,c]) + a,b,*c = loop do break *[]; end; assert_equal([nil,nil,[]], [a,b,c]) + a,b,*c = loop do break *[1]; end; assert_equal([1,nil,[]], [a,b,c]) + a,b,*c = loop do break *[nil]; end; assert_equal([nil,nil,[]], [a,b,c]) + a,b,*c = loop do break *[[]]; end; assert_equal([nil,nil,[]], [a,b,c]) + a,b,*c = loop do break *[1,2]; end; assert_equal([1,2,[]], [a,b,c]) + a,b,*c = loop do break *[*[]]; end; assert_equal([nil,nil,[]], [a,b,c]) + a,b,*c = loop do break *[*[1]]; end; assert_equal([1,nil,[]], [a,b,c]) + a,b,*c = loop do break *[*[1,2]]; end; assert_equal([1,2,[]], [a,b,c]) + end + + def test_next + def r(val); a = yield(); assert_equal(val, a); end + r(nil){next} + r(nil){next nil} + r(1){next 1} + r([]){next []} + r([1]){next [1]} + r([nil]){next [nil]} + r([[]]){next [[]]} + r([]){next [*[]]} + r([1]){next [*[1]]} + r([1,2]){next [*[1,2]]} + + r(nil){next *nil} + r(1){next *1} + r(nil){next *[]} + r(1){next *[1]} + r(nil){next *[nil]} + r([]){next *[[]]} + r(nil){next *[*[]]} + r(1){next *[*[1]]} + r([1,2]){next *[*[1,2]]} + + def r(val); *a = yield(); assert_equal(val, a); end + r([nil]){next} + r([nil]){next nil} + r([1]){next 1} + r([[]]){next []} + r([[1]]){next [1]} + r([[nil]]){next [nil]} + r([[[]]]){next [[]]} + r([[1,2]]){next [1,2]} + r([[]]){next [*[]]} + r([[1]]){next [*[1]]} + r([[1,2]]){next [*[1,2]]} + + def r(val); *a = *yield(); assert_equal(val, a); end + r([nil]){next *nil} + r([1]){next *1} + r([nil]){next *[]} + r([1]){next *[1]} + r([nil]){next *[nil]} + r([]){next *[[]]} + r([1,2]){next *[1,2]} + r([nil]){next *[*[]]} + r([1]){next *[*[1]]} + r([1,2]){next *[*[1,2]]} + + def r(val); a,b,*c = yield(); assert_equal(val, [a,b,c]); end + r([nil,nil,[]]){next} + r([nil,nil,[]]){next nil} + r([1,nil,[]]){next 1} + r([nil,nil,[]]){next []} + r([1,nil,[]]){next [1]} + r([nil,nil,[]]){next [nil]} + r([[],nil,[]]){next [[]]} + r([1,2,[]]){next [1,2]} + r([nil,nil,[]]){next [*[]]} + r([1,nil,[]]){next [*[1]]} + r([1,2,[]]){next [*[1,2]]} + + def r(val); a,b,*c = *yield(); assert_equal(val, [a,b,c]); end + r([nil,nil,[]]){next *nil} + r([1,nil,[]]){next *1} + r([nil,nil,[]]){next *[]} + r([1,nil,[]]){next *[1]} + r([nil,nil,[]]){next *[nil]} + r([nil,nil,[]]){next *[[]]} + r([1,2,[]]){next *[1,2]} + r([nil,nil,[]]){next *[*[]]} + r([1,nil,[]]){next *[*[1]]} + r([1,2,[]]){next *[*[1,2]]} + end + + def test_assign2 + a = nil + assert(defined?(a)) + assert_nil(a) + + # multiple asignment + a, b = 1, 2 + assert(a == 1 && b == 2) + + a, b = b, a + assert(a == 2 && b == 1) + + a, = 1,2 + assert_equal(1, a) + + a, *b = 1, 2, 3 + assert(a == 1 && b == [2, 3]) + + a, (b, c), d = 1, [2, 3], 4 + assert(a == 1 && b == 2 && c == 3 && d == 4) + + *a = 1, 2, 3 + assert_equal([1, 2, 3], a) + + *a = 4 + assert_equal([4], a) + + *a = nil + assert_equal([nil], a) + end +end diff --git a/ruby_1_8_5/test/ruby/test_beginendblock.rb b/ruby_1_8_5/test/ruby/test_beginendblock.rb new file mode 100644 index 0000000000..b56b596a65 --- /dev/null +++ b/ruby_1_8_5/test/ruby/test_beginendblock.rb @@ -0,0 +1,57 @@ +require 'test/unit' +require 'tempfile' +$:.replace([File.dirname(File.expand_path(__FILE__))] | $:) +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 diff --git a/ruby_1_8_5/test/ruby/test_bignum.rb b/ruby_1_8_5/test/ruby/test_bignum.rb new file mode 100644 index 0000000000..c238337db5 --- /dev/null +++ b/ruby_1_8_5/test/ruby/test_bignum.rb @@ -0,0 +1,87 @@ +require 'test/unit' + +class TestBignum < Test::Unit::TestCase + def fact(n) + return 1 if n == 0 + f = 1 + while n>0 + f *= n + n -= 1 + end + return f + end + + def test_bignum + $x = fact(40) + assert_equal($x, $x) + assert_equal($x, fact(40)) + assert($x < $x+2) + assert($x > $x-2) + assert_equal(815915283247897734345611269596115894272000000000, $x) + assert_not_equal(815915283247897734345611269596115894272000000001, $x) + assert_equal(815915283247897734345611269596115894272000000001, $x+1) + assert_equal(335367096786357081410764800000, $x/fact(20)) + $x = -$x + assert_equal(-815915283247897734345611269596115894272000000000, $x) + assert_equal(2-(2**32), -(2**32-2)) + assert_equal(2**32 - 5, (2**32-3)-2) + + for i in 1000..1014 + assert_equal(2 ** i, 1 << i) + end + + n1 = 1 << 1000 + for i in 1000..1014 + assert_equal(n1, 1 << i) + n1 *= 2 + end + + n2=n1 + for i in 1..10 + n1 = n1 / 2 + n2 = n2 >> 1 + assert_equal(n1, n2) + end + + for i in 4000..4096 + n1 = 1 << i; + assert_equal(n1-1, (n1**2-1) / (n1+1)) + end + end + + def test_calc + b = 10**80 + a = b * 9 + 7 + assert_equal(7, a.modulo(b)) + assert_equal(-b + 7, a.modulo(-b)) + assert_equal(b + -7, (-a).modulo(b)) + assert_equal(-7, (-a).modulo(-b)) + assert_equal(7, a.remainder(b)) + assert_equal(7, a.remainder(-b)) + assert_equal(-7, (-a).remainder(b)) + assert_equal(-7, (-a).remainder(-b)) + + assert_equal(10000000000000000000100000000000000000000, 10**40+10**20) + assert_equal(100000000000000000000, 10**40/10**20) + + a = 677330545177305025495135714080 + b = 14269972710765292560 + assert_equal(0, a % b) + assert_equal(0, -a % b) + end + + def shift_test(a) + b = a / (2 ** 32) + c = a >> 32 + assert_equal(b, c) + + b = a * (2 ** 32) + c = a << 32 + assert_equal(b, c) + end + + def test_shift + shift_test(-4518325415524767873) + shift_test(-0xfffffffffffffffff) + end +end diff --git a/ruby_1_8_5/test/ruby/test_call.rb b/ruby_1_8_5/test/ruby/test_call.rb new file mode 100644 index 0000000000..da7ee93c73 --- /dev/null +++ b/ruby_1_8_5/test/ruby/test_call.rb @@ -0,0 +1,19 @@ +require 'test/unit' + +class TestCall < Test::Unit::TestCase + def aaa(a, b=100, *rest) + res = [a, b] + res += rest if rest + return res + end + + def test_call + assert_raises(ArgumentError) {aaa()} + assert_raises(ArgumentError) {aaa} + + assert_equal([1, 100], aaa(1)) + assert_equal([1, 2], aaa(1, 2)) + assert_equal([1, 2, 3, 4], aaa(1, 2, 3, 4)) + assert_equal([1, 2, 3, 4], aaa(1, *[2, 3, 4])) + end +end diff --git a/ruby_1_8_5/test/ruby/test_case.rb b/ruby_1_8_5/test/ruby/test_case.rb new file mode 100644 index 0000000000..41a22038a0 --- /dev/null +++ b/ruby_1_8_5/test/ruby/test_case.rb @@ -0,0 +1,49 @@ +require 'test/unit' + +class TestCase < Test::Unit::TestCase + def test_case + case 5 + when 1, 2, 3, 4, 6, 7, 8 + assert(false) + when 5 + assert(true) + end + + case 5 + when 5 + assert(true) + when 1..10 + assert(false) + end + + case 5 + when 1..10 + assert(true) + else + assert(false) + end + + case 5 + when 5 + assert(true) + else + assert(false) + end + + case "foobar" + when /^f.*r$/ + assert(true) + else + assert(false) + end + + case + when true + assert(true) + when false, nil + assert(false) + else + assert(false) + end + end +end diff --git a/ruby_1_8_5/test/ruby/test_clone.rb b/ruby_1_8_5/test/ruby/test_clone.rb new file mode 100644 index 0000000000..43c0cffa1d --- /dev/null +++ b/ruby_1_8_5/test/ruby/test_clone.rb @@ -0,0 +1,28 @@ +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 + 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 diff --git a/ruby_1_8_5/test/ruby/test_condition.rb b/ruby_1_8_5/test/ruby/test_condition.rb new file mode 100644 index 0000000000..ba2e0688f3 --- /dev/null +++ b/ruby_1_8_5/test/ruby/test_condition.rb @@ -0,0 +1,16 @@ +require 'test/unit' + +class TestCondition < Test::Unit::TestCase + + # [should] first test to see if we can run the tests. + + def test_condition + $x = '0'; + + $x == $x && assert(true) + $x != $x && assert(false) + $x == $x || assert(false) + $x != $x || assert(true) + + end +end diff --git a/ruby_1_8_5/test/ruby/test_const.rb b/ruby_1_8_5/test/ruby/test_const.rb new file mode 100644 index 0000000000..8d01379dbd --- /dev/null +++ b/ruby_1_8_5/test/ruby/test_const.rb @@ -0,0 +1,33 @@ +require 'test/unit' + +class TestConst < Test::Unit::TestCase + TEST1 = 1 + TEST2 = 2 + + module Const + TEST3 = 3 + TEST4 = 4 + end + + module Const2 + TEST3 = 6 + TEST4 = 8 + end + + def test_const + self.class.class_eval { + include Const + } + assert_equal([1,2,3,4], [TEST1,TEST2,TEST3,TEST4]) + + self.class.class_eval { + include Const2 + } + STDERR.print "intentionally redefines TEST3, TEST4\n" if $VERBOSE + assert_equal([1,2,6,8], [TEST1,TEST2,TEST3,TEST4]) + + assert_equal(-1, (String <=> Object)) + assert_equal(1, (Object <=> String)) + assert_equal(nil, (Array <=> String)) + end +end diff --git a/ruby_1_8_5/test/ruby/test_defined.rb b/ruby_1_8_5/test/ruby/test_defined.rb new file mode 100644 index 0000000000..8a7fcf45a9 --- /dev/null +++ b/ruby_1_8_5/test/ruby/test_defined.rb @@ -0,0 +1,43 @@ +require 'test/unit' + +class TestDefined < Test::Unit::TestCase + class Foo + def foo + p :foo + end + protected :foo + def bar(f) + yield(defined?(self.foo)) + yield(defined?(f.foo)) + end + end + + def defined_test + return !defined?(yield) + end + + def test_defined + $x = nil + + assert(defined?($x)) # global variable + assert_equal('global-variable', defined?($x))# returns description + + assert_nil(defined?(foo)) # undefined + foo=5 + assert(defined?(foo)) # local variable + + assert(defined?(Array)) # constant + assert(defined?(::Array)) # toplevel constant + assert(defined?(File::Constants)) # nested constant + assert(defined?(Object.new)) # method + assert(!defined?(Object.print)) # private method + assert(defined?(1 == 2)) # operator expression + + f = Foo.new + assert_nil(defined?(f.foo)) + f.bar(f) { |v| assert(v) } + + assert(defined_test) # not iterator + assert(!defined_test{}) # called as iterator + end +end diff --git a/ruby_1_8_5/test/ruby/test_dir.rb b/ruby_1_8_5/test/ruby/test_dir.rb new file mode 100644 index 0000000000..09685bee8f --- /dev/null +++ b/ruby_1_8_5/test/ruby/test_dir.rb @@ -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 diff --git a/ruby_1_8_5/test/ruby/test_env.rb b/ruby_1_8_5/test/ruby/test_env.rb new file mode 100644 index 0000000000..3a65f91fa4 --- /dev/null +++ b/ruby_1_8_5/test/ruby/test_env.rb @@ -0,0 +1,82 @@ +require 'test/unit' + +class TestEnv < Test::Unit::TestCase + IGNORE_CASE = /djgpp|bccwin|mswin|mingw/ =~ RUBY_PLATFORM + + def setup + @backup = ENV.delete('test') + @BACKUP = 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_index + val = 'a' + val.succ! while ENV.has_value?(val) && ENV.has_value?(val.upcase) + ENV['test'] = val[0...-1] + + assert_nil(ENV.index(val)) + assert_nil(ENV.index(val.upcase)) + ENV['test'] = val + if IGNORE_CASE + assert_equal('TEST', ENV.index(val).upcase) + else + assert_equal('test', ENV.index(val)) + end + assert_nil(ENV.index(val.upcase)) + ENV['test'] = val.upcase + assert_nil(ENV.index(val)) + if IGNORE_CASE + assert_equal('TEST', ENV.index(val.upcase).upcase) + else + assert_equal('test', ENV.index(val.upcase)) + end + end +end diff --git a/ruby_1_8_5/test/ruby/test_eval.rb b/ruby_1_8_5/test/ruby/test_eval.rb new file mode 100644 index 0000000000..6b3fea7bb4 --- /dev/null +++ b/ruby_1_8_5/test/ruby/test_eval.rb @@ -0,0 +1,157 @@ +require 'test/unit' + +class TestEval < Test::Unit::TestCase + # eval with binding + def test_ev + local1 = "local1" + lambda { + local2 = "local2" + return binding + }.call + end + + def test_eval + assert_nil(eval("")) + $bad=false + eval 'while false; $bad = true; print "foo\n" end' + assert(!$bad) + + assert(eval('TRUE')) + assert(eval('true')) + assert(!eval('NIL')) + assert(!eval('nil')) + assert(!eval('FALSE')) + assert(!eval('false')) + + $foo = 'assert(true)' + begin + eval $foo + rescue + assert(false) + end + + assert_equal('assert(true)', eval("$foo")) + assert_equal(true, eval("true")) + i = 5 + assert(eval("i == 5")) + assert_equal(5, eval("i")) + assert(eval("defined? i")) + + $x = test_ev + assert_equal("local1", eval("local1", $x)) # normal local var + assert_equal("local2", eval("local2", $x)) # nested local var + $bad = true + begin + p eval("local1") + rescue NameError # must raise error + $bad = false + end + assert(!$bad) + + # !! use class_eval to avoid nested definition + self.class.class_eval %q( + module EvTest + EVTEST1 = 25 + evtest2 = 125 + $x = binding + end + ) + assert_equal(25, eval("EVTEST1", $x)) # constant in module + assert_equal(125, eval("evtest2", $x)) # local var in module + $bad = true + begin + eval("EVTEST1") + rescue NameError # must raise error + $bad = false + end + assert(!$bad) + + x = proc{} + eval "i4 = 1", x + assert_equal(1, eval("i4", x)) + x = proc{proc{}}.call + eval "i4 = 22", x + assert_equal(22, eval("i4", x)) + $x = [] + x = proc{proc{}}.call + eval "(0..9).each{|i5| $x[i5] = proc{i5*2}}", x + assert_equal(8, $x[4].call) + + x = binding + eval "i = 1", x + assert_equal(1, eval("i", x)) + x = proc{binding}.call + eval "i = 22", x + assert_equal(22, eval("i", x)) + $x = [] + x = proc{binding}.call + eval "(0..9).each{|i5| $x[i5] = proc{i5*2}}", x + assert_equal(8, $x[4].call) + x = proc{binding}.call + eval "for i6 in 1..1; j6=i6; end", x + assert(eval("defined? i6", x)) + assert(eval("defined? j6", x)) + + proc { + p = binding + eval "foo11 = 1", p + foo22 = 5 + proc{foo11=22}.call + proc{foo22=55}.call + assert_equal(eval("foo11"), eval("foo11", p)) + assert_equal(1, eval("foo11")) + assert_equal(eval("foo22"), eval("foo22", p)) + assert_equal(55, eval("foo22")) + }.call + + p1 = proc{i7 = 0; proc{i7}}.call + assert_equal(0, p1.call) + eval "i7=5", p1 + assert_equal(5, p1.call) + assert(!defined?(i7)) + + p1 = proc{i7 = 0; proc{i7}}.call + i7 = nil + assert_equal(0, p1.call) + eval "i7=1", p1 + assert_equal(1, p1.call) + eval "i7=5", p1 + assert_equal(5, p1.call) + assert_nil(i7) + end + + def test_nil_instance_eval_cvar # [ruby-dev:24103] + def nil.test_binding + binding + end + bb = eval("nil.instance_eval \"binding\"", nil.test_binding) + assert_raise(NameError) { eval("@@a", bb) } + class << nil + remove_method :test_binding + end + end + + def test_fixnum_instance_eval_cvar # [ruby-dev:24213] + assert_raise(NameError) { 1.instance_eval "@@a" } + end + + def test_cvar_scope_with_instance_eval # [ruby-dev:24223] + Fixnum.class_eval "@@test_cvar_scope_with_instance_eval = 1" # depends on [ruby-dev:24229] + @@test_cvar_scope_with_instance_eval = 4 + assert_equal(4, 1.instance_eval("@@test_cvar_scope_with_instance_eval")) + Fixnum.__send__(:remove_class_variable, :@@test_cvar_scope_with_instance_eval) + end + + def test_eval_and_define_method # [ruby-dev:24228] + assert_nothing_raised { + def temporally_method_for_test_eval_and_define_method(&block) + lambda { + class << Object.new; self end.__send__(:define_method, :zzz, &block) + } + end + v = eval("temporally_method_for_test_eval_and_define_method {}") + {}[0] = {} + v.call + } + end +end diff --git a/ruby_1_8_5/test/ruby/test_exception.rb b/ruby_1_8_5/test/ruby/test_exception.rb new file mode 100644 index 0000000000..4c27c52f3c --- /dev/null +++ b/ruby_1_8_5/test/ruby/test_exception.rb @@ -0,0 +1,187 @@ +require 'test/unit' + +class TestException < Test::Unit::TestCase + def test_exception + begin + raise "this must be handled" + assert(false) + rescue + assert(true) + end + + $bad = true + begin + raise "this must be handled no.2" + rescue + if $bad + $bad = false + retry + assert(false) + end + end + assert(true) + + # exception in rescue clause + $string = "this must be handled no.3" + e = assert_raises(RuntimeError) do + begin + raise "exception in rescue clause" + rescue + raise $string + end + assert(false) + end + assert_equal($string, e.message) + + # exception in ensure clause + $string = "exception in ensure clause" + e = assert_raises(RuntimeError) do + begin + raise "this must be handled no.4" + ensure + assert_instance_of(RuntimeError, $!) + assert_equal("this must be handled no.4", $!.message) + raise "exception in ensure clause" + end + assert(false) + end + assert_equal($string, e.message) + + $bad = true + begin + begin + raise "this must be handled no.5" + ensure + $bad = false + end + rescue + end + assert(!$bad) + + $bad = true + begin + begin + raise "this must be handled no.6" + ensure + $bad = false + end + rescue + end + assert(!$bad) + + $bad = true + while true + begin + break + ensure + $bad = false + end + end + assert(!$bad) + + assert(catch(:foo) { + loop do + loop do + throw :foo, true + break + end + break + assert(false) # should no reach here + end + false + }) + + end + + def test_else + begin + assert(true) + rescue + assert(false) + else + assert(true) + end + + begin + assert(true) + raise + assert(false) + rescue + assert(true) + else + assert(false) + end + + begin + assert(true) + begin + assert(true) + rescue + assert(false) + else + assert(true) + end + assert(true) + rescue + assert(false) + else + assert(true) + end + + begin + assert(true) + begin + assert(true) + raise + assert(false) + rescue + assert(true) + else + assert(false) + end + assert(true) + rescue + assert(false) + else + assert(true) + end + + begin + assert(true) + begin + assert(true) + rescue + assert(false) + else + assert(true) + end + assert(true) + raise + assert(false) + rescue + assert(true) + else + assert(false) + end + + begin + assert(true) + begin + assert(true) + raise + assert(false) + rescue + assert(true) + else + assert(false) + end + assert(true) + raise + assert(false) + rescue + assert(true) + else + assert(false) + end + end +end diff --git a/ruby_1_8_5/test/ruby/test_file.rb b/ruby_1_8_5/test/ruby/test_file.rb new file mode 100644 index 0000000000..2458dde347 --- /dev/null +++ b/ruby_1_8_5/test/ruby/test_file.rb @@ -0,0 +1,108 @@ +require 'test/unit' +require 'tempfile' +$:.replace([File.dirname(File.expand_path(__FILE__))] | $:) +require 'ut_eof' + +class TestFile < Test::Unit::TestCase + + # I don't know Ruby's spec about "unlink-before-close" exactly. + # This test asserts current behaviour. + def test_unlink_before_close + filename = File.basename(__FILE__) + ".#{$$}" + w = File.open(filename, "w") + w << "foo" + w.close + r = File.open(filename, "r") + begin + if /(mswin|bccwin|mingw|emx)/ =~ RUBY_PLATFORM + begin + File.unlink(filename) + assert(false) + rescue Errno::EACCES + assert(true) + end + else + File.unlink(filename) + assert(true) + end + ensure + r.close + File.unlink(filename) if File.exist?(filename) + end + end + + include TestEOF + def open_file(content) + f = Tempfile.new("test-eof") + f << content + f.rewind + yield f + end + alias open_file_rw open_file + + include TestEOF::Seek + + def test_fnmatch + # from [ruby-dev:22815] and [ruby-dev:22819] + assert(true, File.fnmatch('\[1\]' , '[1]')) + assert(true, File.fnmatch('*?', 'a')) + end + + def test_truncate_wbuf # [ruby-dev:24191] + f = Tempfile.new("test-truncate") + f.print "abc" + f.truncate(0) + f.print "def" + f.close + assert_equal("\0\0\0def", File.read(f.path)) + end + + def test_truncate_rbuf # [ruby-dev:24197] + f = Tempfile.new("test-truncate") + f.puts "abc" + f.puts "def" + f.close + f.open + assert_equal("abc\n", f.gets) + f.truncate(3) + assert_equal(nil, f.gets) + end + + def test_read_all_extended_file + f = Tempfile.new("test-extended-file") + assert_nil(f.getc) + open(f.path, "w") {|g| g.print "a" } + assert_equal("a", f.read) + end + + def test_gets_extended_file + f = Tempfile.new("test-extended-file") + assert_nil(f.getc) + open(f.path, "w") {|g| g.print "a" } + assert_equal("a", f.gets("a")) + end + + def test_gets_para_extended_file + f = Tempfile.new("test-extended-file") + assert_nil(f.getc) + open(f.path, "w") {|g| g.print "\na" } + assert_equal("a", f.gets("")) + end + + def test_each_byte_extended_file + f = Tempfile.new("test-extended-file") + assert_nil(f.getc) + open(f.path, "w") {|g| g.print "a" } + result = [] + f.each_byte {|b| result << b } + assert_equal([?a], result) + end + + def test_getc_extended_file + f = Tempfile.new("test-extended-file") + assert_nil(f.getc) + open(f.path, "w") {|g| g.print "a" } + assert_equal(?a, f.getc) + end + +end diff --git a/ruby_1_8_5/test/ruby/test_float.rb b/ruby_1_8_5/test/ruby/test_float.rb new file mode 100644 index 0000000000..d559ce5cab --- /dev/null +++ b/ruby_1_8_5/test/ruby/test_float.rb @@ -0,0 +1,113 @@ +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) + 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("-.")} + assert_raise(ArgumentError){Float("1e")} + # add expected behaviour here. + end + + def test_divmod + assert_equal([2, 3.5], 11.5.divmod(4)) + assert_equal([-3, -0.5], 11.5.divmod(-4)) + assert_equal([-3, 0.5], (-11.5).divmod(4)) + assert_equal([2, -3.5], (-11.5).divmod(-4)) + end + + def test_div + assert_equal(2, 11.5.div(4)) + assert_equal(-3, 11.5.div(-4)) + assert_equal(-3, (-11.5).div(4)) + assert_equal(2, (-11.5).div(-4)) + end + + def test_modulo + assert_equal(3.5, 11.5.modulo(4)) + assert_equal(-0.5, 11.5.modulo(-4)) + assert_equal(0.5, (-11.5).modulo(4)) + assert_equal(-3.5, (-11.5).modulo(-4)) + end + + def test_remainder + assert_equal(3.5, 11.5.remainder(4)) + assert_equal(3.5, 11.5.remainder(-4)) + assert_equal(-3.5, (-11.5).remainder(4)) + assert_equal(-3.5, (-11.5).remainder(-4)) + end +end diff --git a/ruby_1_8_5/test/ruby/test_gc.rb b/ruby_1_8_5/test/ruby/test_gc.rb new file mode 100644 index 0000000000..d0b4e3df77 --- /dev/null +++ b/ruby_1_8_5/test/ruby/test_gc.rb @@ -0,0 +1,30 @@ +require 'test/unit' + +class TestGc < Test::Unit::TestCase + class S + def initialize(a) + @a = a + end + end + + def test_gc + assert_nothing_raised do + 1.upto(10000) { + tmp = [0,1,2,3,4,5,6,7,8,9] + } + tmp = nil + end + l=nil + 100000.times { + l = S.new(l) + } + GC.start + assert true # reach here or dumps core + l = [] + 100000.times { + l.push([l]) + } + GC.start + assert true # reach here or dumps core + end +end diff --git a/ruby_1_8_5/test/ruby/test_hash.rb b/ruby_1_8_5/test/ruby/test_hash.rb new file mode 100644 index 0000000000..5bec012bf8 --- /dev/null +++ b/ruby_1_8_5/test/ruby/test_hash.rb @@ -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 diff --git a/ruby_1_8_5/test/ruby/test_ifunless.rb b/ruby_1_8_5/test/ruby/test_ifunless.rb new file mode 100644 index 0000000000..bffc794512 --- /dev/null +++ b/ruby_1_8_5/test/ruby/test_ifunless.rb @@ -0,0 +1,14 @@ +require 'test/unit' + +class TestIfunless < Test::Unit::TestCase + def test_if_unless + $x = 'test'; + assert(if $x == $x then true else false end) + $bad = false + unless $x == $x + $bad = true + end + assert(!$bad) + assert(unless $x != $x then true else false end) + end +end diff --git a/ruby_1_8_5/test/ruby/test_io.rb b/ruby_1_8_5/test/ruby/test_io.rb new file mode 100644 index 0000000000..642c8f4430 --- /dev/null +++ b/ruby_1_8_5/test/ruby/test_io.rb @@ -0,0 +1,11 @@ +require 'test/unit' + +class TestIO < Test::Unit::TestCase + def test_gets_rs + r, w = IO.pipe + w.print "\377xyz" + w.close + assert_equal("\377", r.gets("\377"), "[ruby-dev:24460]") + r.close + end +end diff --git a/ruby_1_8_5/test/ruby/test_iterator.rb b/ruby_1_8_5/test/ruby/test_iterator.rb new file mode 100644 index 0000000000..638916309e --- /dev/null +++ b/ruby_1_8_5/test/ruby/test_iterator.rb @@ -0,0 +1,465 @@ +require 'test/unit' + +class Array + def iter_test1 + collect{|e| [e, yield(e)]}.sort{|a,b|a[1]<=>b[1]} + end + def iter_test2 + a = collect{|e| [e, yield(e)]} + a.sort{|a,b|a[1]<=>b[1]} + end +end + +class TestIterator < Test::Unit::TestCase + def ttt + assert(iterator?) + end + + def test_iterator + assert(!iterator?) + + ttt{} + + # yield at top level !! here's not toplevel + assert(!defined?(yield)) + end + + def test_array + $x = [1, 2, 3, 4] + $y = [] + + # iterator over array + for i in $x + $y.push i + end + assert_equal($x, $y) + end + + def tt + 1.upto(10) {|i| + yield i + } + end + + def tt2(dummy) + yield 1 + end + + def tt3(&block) + tt2(raise(ArgumentError,""),&block) + end + + def test_nested_iterator + i = 0 + tt{|i| break if i == 5} + assert_equal(5, i) + + assert_raises(ArgumentError) do + tt3{} + end + end + + def tt4 &block + tt2(raise(ArgumentError,""),&block) + end + + def test_block_argument_without_paren + assert_raises(ArgumentError) do + tt4{} + end + end + + # iterator break/redo/next/retry + def test_break + done = true + loop{ + break + done = false # should not reach here + } + assert(done) + + done = false + $bad = false + loop { + break if done + done = true + next + $bad = true # should not reach here + } + assert(!$bad) + + done = false + $bad = false + loop { + break if done + done = true + redo + $bad = true # should not reach here + } + assert(!$bad) + + $x = [] + for i in 1 .. 7 + $x.push i + end + assert_equal(7, $x.size) + assert_equal([1, 2, 3, 4, 5, 6, 7], $x) + + $done = false + $x = [] + for i in 1 .. 7 # see how retry works in iterator loop + if i == 4 and not $done + $done = true + retry + end + $x.push(i) + end + assert_equal(10, $x.size) + assert_equal([1, 2, 3, 1, 2, 3, 4, 5, 6, 7], $x) + end + + def test_append_method_to_built_in_class + $x = [[1,2],[3,4],[5,6]] + assert_equal($x.iter_test1{|x|x}, $x.iter_test2{|x|x}) + end + + class IterTest + def initialize(e); @body = e; end + + def each0(&block); @body.each(&block); end + def each1(&block); @body.each {|*x| block.call(*x) } end + def each2(&block); @body.each {|*x| block.call(x) } end + def each3(&block); @body.each {|x| block.call(*x) } end + def each4(&block); @body.each {|x| block.call(x) } end + def each5; @body.each {|*x| yield(*x) } end + def each6; @body.each {|*x| yield(x) } end + def each7; @body.each {|x| yield(*x) } end + def each8; @body.each {|x| yield(x) } end + + def f(a) + a + end + end + + def test_itertest + assert_equal([1], IterTest.new(nil).method(:f).to_proc.call([1])) + m = /\w+/.match("abc") + assert_equal([m], IterTest.new(nil).method(:f).to_proc.call([m])) + + IterTest.new([0]).each0 {|x| assert_equal(0, x)} + IterTest.new([1]).each1 {|x| assert_equal(1, x)} + IterTest.new([2]).each2 {|x| assert_equal([2], x)} + IterTest.new([3]).each3 {|x| assert_equal(3, x)} + IterTest.new([4]).each4 {|x| assert_equal(4, x)} + IterTest.new([5]).each5 {|x| assert_equal(5, x)} + IterTest.new([6]).each6 {|x| assert_equal([6], x)} + IterTest.new([7]).each7 {|x| assert_equal(7, x)} + IterTest.new([8]).each8 {|x| assert_equal(8, x)} + + IterTest.new([[0]]).each0 {|x| assert_equal([0], x)} + IterTest.new([[1]]).each1 {|x| assert_equal([1], x)} + IterTest.new([[2]]).each2 {|x| assert_equal([[2]], x)} + IterTest.new([[3]]).each3 {|x| assert_equal(3, x)} + IterTest.new([[4]]).each4 {|x| assert_equal([4], x)} + IterTest.new([[5]]).each5 {|x| assert_equal([5], x)} + IterTest.new([[6]]).each6 {|x| assert_equal([[6]], x)} + IterTest.new([[7]]).each7 {|x| assert_equal(7, x)} + IterTest.new([[8]]).each8 {|x| assert_equal([8], x)} + + IterTest.new([[0,0]]).each0 {|x| assert_equal([0,0], x)} + IterTest.new([[8,8]]).each8 {|x| assert_equal([8,8], x)} + end + + def m(var) + var + end + + def m1 + m(block_given?) + end + + def m2 + m(block_given?,&proc{}) + end + + def test_block_given + assert(m1{p 'test'}) + assert(m2{p 'test'}) + assert(!m1()) + assert(!m2()) + end + + def m3(var, &block) + m(yield(var), &block) + end + + def m4(&block) + m(m1(), &block) + end + + def test_block_passing + assert(!m4()) + assert(!m4 {}) + assert_equal(100, m3(10) {|x|x*x}) + end + + class C + include Enumerable + def initialize + @a = [1,2,3] + end + def each(&block) + @a.each(&block) + end + end + + def test_collect + assert_equal([1,2,3], C.new.collect{|n| n}) + end + + def test_proc + assert_instance_of(Proc, lambda{}) + assert_instance_of(Proc, Proc.new{}) + lambda{|a|assert_equal(a, 1)}.call(1) + end + + def test_block + assert_instance_of(NilClass, get_block) + assert_instance_of(Proc, get_block{}) + end + + def test_argument + assert_nothing_raised {lambda{||}.call} + assert_raises(ArgumentError) {lambda{||}.call(1)} + assert_nothing_raised {lambda{|a,|}.call(1)} + assert_raises(ArgumentError) {lambda{|a,|}.call()} + assert_raises(ArgumentError) {lambda{|a,|}.call(1,2)} + end + + def get_block(&block) + block + end + + def test_get_block + assert_instance_of(Proc, get_block{}) + assert_nothing_raised {get_block{||}.call()} + assert_nothing_raised {get_block{||}.call(1)} + assert_nothing_raised {get_block{|a,|}.call(1)} + assert_nothing_raised {get_block{|a,|}.call()} + assert_nothing_raised {get_block{|a,|}.call(1,2)} + + assert_nothing_raised {get_block(&lambda{||}).call()} + assert_raises(ArgumentError) {get_block(&lambda{||}).call(1)} + assert_nothing_raised {get_block(&lambda{|a,|}).call(1)} + assert_raises(ArgumentError) {get_block(&lambda{|a,|}).call(1,2)} + + block = get_block{11} + assert_instance_of(Proc, block) + assert_instance_of(Proc, block.to_proc) + assert_equal(block.clone.call, 11) + assert_instance_of(Proc, get_block(&block)) + + lambda = lambda{44} + assert_instance_of(Proc, lambda) + assert_instance_of(Proc, lambda.to_proc) + assert_equal(lambda.clone.call, 44) + assert_instance_of(Proc, get_block(&lambda)) + + assert_equal(1, Proc.new{|a,| a}.call(1,2,3)) + assert_nothing_raised {Proc.new{|a,|}.call(1,2)} + end + + def return1_test + Proc.new { + return 55 + }.call + 5 + end + + def test_return1 + assert_equal(55, return1_test()) + end + + def return2_test + lambda { + return 55 + }.call + 5 + end + + def test_return2 + assert_equal(60, return2_test()) + end + + def proc_call(&b) + b.call + end + def proc_yield() + yield + end + def proc_return1 + proc_call{return 42}+1 + end + + def test_proc_return1 + assert_equal(42, proc_return1()) + end + + def proc_return2 + proc_yield{return 42}+1 + end + + def test_proc_return2 + assert_equal(42, proc_return2()) + end + + def test_ljump + block = get_block{11} + lambda = lambda{44} + assert_raises(LocalJumpError) {get_block{break}.call} + assert_nothing_raised {lambda{break}.call} + assert_instance_of(LocalJumpError, (get_block{break}.call rescue $!)) + + assert_equal(-1, block.arity) + assert_equal(-1, lambda.arity) + assert_equal(0, lambda{||}.arity) + assert_equal(1, lambda{|a|}.arity) + assert_equal(1, lambda{|a,|}.arity) + assert_equal(2, lambda{|a,b|}.arity) + end + + def marity_test(m) + method = method(m) + assert_equal(method.arity, method.to_proc.arity) + end + + def test_marity + marity_test(:assert) + marity_test(:marity_test) + marity_test(:p) + + lambda(&method(:assert)).call(true) + lambda(&get_block{|a,n| assert(a,n)}).call(true, "marity") + end + + def foo + yield([:key, :value]) + end + def bar(&blk) + blk.call([:key, :value]) + end + + def test_yield_vs_call + foo{|k,v| assert_equal([:key, :value], [k,v])} + bar{|k,v| assert_equal([:key, :value], [k,v])} + end + + class H + def each + yield [:key, :value] + end + end + + def test_assoc_yield + [{:key=>:value}, H.new].each {|h| + h.each{|a| assert_equal([:key, :value], a)} + h.each{|*a| assert_equal([[:key, :value]], a)} + h.each{|k,v| assert_equal([:key, :value], [k,v])} + } + end + + class ITER_TEST1 + def a + block_given? + end + end + + class ITER_TEST2 < ITER_TEST1 + include Test::Unit::Assertions + def a + assert(super) + super + end + end + + def test_iter_test2 + assert(ITER_TEST2.new.a {}) + end + + class ITER_TEST3 + def foo x + return yield if block_given? + x + end + end + + class ITER_TEST4 < ITER_TEST3 + include Test::Unit::Assertions + def foo x + assert_equal(super, yield) + assert_equal(x, super(x, &nil)) + end + end + + def test_iter4 + ITER_TEST4.new.foo(44){55} + end + + def test_break__nested_loop1 + _test_break__nested_loop1 do + break + end + end + + def _test_break__nested_loop1 + while true + yield + end + assert(false, "must not reach here") + end + + def test_break__nested_loop2 + _test_break__nested_loop2 do + break + end + end + + def _test_break__nested_loop2 + until false + yield + end + assert(false, "must not reach here") + end + + def test_break__nested_loop3 + _test_break__nested_loop3 do + break + end + end + + def _test_break__nested_loop3 + loop do + yield + end + assert(false, "must not reach here") + end + + def test_break_from_enum + result = ["a"].inject("ng") {|x,y| break "ok"} + assert_equal("ok", result) + end + + def _test_return_trace_func(x) + set_trace_func(proc {}) + [].fetch(2) {return x} + ensure + set_trace_func(nil) + end + + def test_return_trace_func + ok = "returned gracefully" + result = "skipped" + result = _test_return_trace_func(ok) + ensure + assert_equal(ok, result) + return + end +end diff --git a/ruby_1_8_5/test/ruby/test_marshal.rb b/ruby_1_8_5/test/ruby/test_marshal.rb new file mode 100644 index 0000000000..9c9fd9470b --- /dev/null +++ b/ruby_1_8_5/test/ruby/test_marshal.rb @@ -0,0 +1,48 @@ +require 'test/unit' +dir = File.dirname(File.expand_path(__FILE__)) +orgpath = $:.dup +begin + $:.push(dir) + require 'marshaltestlib' +ensure + $:.replace(orgpath) +end + +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 + + StrClone=String.clone; + + def test_marshal + $x = [1,2,3,[4,5,"foo"],{1=>"bar"},2.5,fact(30)] + $y = Marshal.dump($x) + assert_equal($x, Marshal.load($y)) + + assert_instance_of(StrClone, Marshal.load(Marshal.dump(StrClone.new("abc")))) + + [[1,2,3,4], [81, 2, 118, 3146]].each { |w,x,y,z| + a = (x.to_f + y.to_f / z.to_f) * Math.exp(w.to_f / (x.to_f + y.to_f / z.to_f)) + ma = Marshal.dump(a) + b = Marshal.load(ma) + assert_equal(a, b) + } + end +end diff --git a/ruby_1_8_5/test/ruby/test_math.rb b/ruby_1_8_5/test/ruby/test_math.rb new file mode 100644 index 0000000000..e1e49dba01 --- /dev/null +++ b/ruby_1_8_5/test/ruby/test_math.rb @@ -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 diff --git a/ruby_1_8_5/test/ruby/test_method.rb b/ruby_1_8_5/test/ruby/test_method.rb new file mode 100644 index 0000000000..ef28098dce --- /dev/null +++ b/ruby_1_8_5/test/ruby/test_method.rb @@ -0,0 +1,53 @@ +require 'test/unit' + +class TestMethod < Test::Unit::TestCase + def m0() end + def m1(a) end + def m2(a, b) end + def mo1(a = nil, &b) end + def mo2(a, b = nil) end + def mo3(*a) end + def mo4(a, *b, &c) end + + class Base + def foo() :base end + def bar() :bar end + end + module SuperBar + def bar() super end + end + class Derived < Base + include SuperBar + def foo() :derived end + end + + def test_arity + assert_equal(0, method(:m0).arity) + assert_equal(1, method(:m1).arity) + assert_equal(2, method(:m2).arity) + assert_equal(-1, method(:mo1).arity) + assert_equal(-2, method(:mo2).arity) + assert_equal(-1, method(:mo3).arity) + assert_equal(-2, method(:mo4).arity) + end + + def test_unbind + assert_equal(:derived, Derived.new.foo) + um = Derived.new.method(:foo).unbind + assert_instance_of(UnboundMethod, um) + Derived.class_eval do + def foo() :changed end + end + assert_equal(:changed, Derived.new.foo) + assert_equal(:derived, um.bind(Derived.new).call) + assert_raise(TypeError) do + um.bind(Base.new) + end + end + + def test_method_super + assert_nothing_raised do + assert_equal(:bar, Derived.new.method(:bar).call) + end + end +end diff --git a/ruby_1_8_5/test/ruby/test_objectspace.rb b/ruby_1_8_5/test/ruby/test_objectspace.rb new file mode 100644 index 0000000000..14d44f0b39 --- /dev/null +++ b/ruby_1_8_5/test/ruby/test_objectspace.rb @@ -0,0 +1,36 @@ +require 'test/unit' + +class TestObjectSpace < Test::Unit::TestCase + def self.deftest_id2ref(obj) + /:(\d+)/ =~ caller[0] + file = $` + line = $1.to_i + code = <<"End" + define_method("test_id2ref_#{line}") {\ + o = ObjectSpace._id2ref(obj.object_id);\ + assert_equal(obj, o, "didn't round trip: \#{obj.inspect}");\ + } +End + eval code, binding, file, line + end + + deftest_id2ref(-0x4000000000000001) + deftest_id2ref(-0x4000000000000000) + deftest_id2ref(-0x40000001) + deftest_id2ref(-0x40000000) + deftest_id2ref(-1) + deftest_id2ref(0) + deftest_id2ref(1) + deftest_id2ref(0x3fffffff) + deftest_id2ref(0x40000000) + deftest_id2ref(0x3fffffffffffffff) + deftest_id2ref(0x4000000000000000) + deftest_id2ref(:a) + deftest_id2ref(:abcdefghijilkjl) + deftest_id2ref(:==) + deftest_id2ref(Object.new) + deftest_id2ref(self) + deftest_id2ref(true) + deftest_id2ref(false) + deftest_id2ref(nil) +end diff --git a/ruby_1_8_5/test/ruby/test_pack.rb b/ruby_1_8_5/test/ruby/test_pack.rb new file mode 100644 index 0000000000..e67465b33a --- /dev/null +++ b/ruby_1_8_5/test/ruby/test_pack.rb @@ -0,0 +1,60 @@ +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")) + + $x = [-1] + assert_equal($x, $x.pack("l").unpack("l")) + 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 diff --git a/ruby_1_8_5/test/ruby/test_path.rb b/ruby_1_8_5/test/ruby/test_path.rb new file mode 100644 index 0000000000..63dbd07346 --- /dev/null +++ b/ruby_1_8_5/test/ruby/test_path.rb @@ -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 diff --git a/ruby_1_8_5/test/ruby/test_pipe.rb b/ruby_1_8_5/test/ruby/test_pipe.rb new file mode 100644 index 0000000000..c3b4d29c0a --- /dev/null +++ b/ruby_1_8_5/test/ruby/test_pipe.rb @@ -0,0 +1,18 @@ +require 'test/unit' +$:.replace([File.dirname(File.expand_path(__FILE__))] | $:) +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 diff --git a/ruby_1_8_5/test/ruby/test_proc.rb b/ruby_1_8_5/test/ruby/test_proc.rb new file mode 100644 index 0000000000..f0b78ffb23 --- /dev/null +++ b/ruby_1_8_5/test/ruby/test_proc.rb @@ -0,0 +1,89 @@ +require 'test/unit' + +class TestProc < Test::Unit::TestCase + def test_proc + p1 = proc{|i| i} + assert_equal(2, p1.call(2)) + assert_equal(3, p1.call(3)) + + p1 = proc{|i| i*2} + assert_equal(4, p1.call(2)) + assert_equal(6, p1.call(3)) + + p2 = nil + x=0 + + proc{ + iii=5 # nested local variable + p1 = proc{|i| + iii = i + } + p2 = proc { + x = iii # nested variables shared by procs + } + # scope of nested variables + assert(defined?(iii)) + }.call + assert(!defined?(iii)) # out of scope + + loop{iii=5; assert(eval("defined? iii")); break} + loop { + iii = 10 + def self.dyna_var_check + loop { + assert(!defined?(iii)) + break + } + end + dyna_var_check + break + } + p1.call(5) + p2.call + assert_equal(5, x) + end + + def assert_arity(n) + meta = class << self; self; end + meta.class_eval {define_method(:foo, Proc.new)} + assert_equal(n, method(:foo).arity) + end + + def test_arity + assert_equal(-1, proc{}.arity) + assert_equal(0, proc{||}.arity) + assert_equal(1, proc{|x|}.arity) + assert_equal(2, proc{|x, y|}.arity) + assert_equal(-2, proc{|x, *y|}.arity) + assert_equal(-1, proc{|*x|}.arity) + assert_equal(-1, proc{|*|}.arity) + + assert_arity(-1) {} + assert_arity(0) {||} + assert_arity(1) {|x|} + assert_arity(2) {|x, y|} + assert_arity(-2) {|x, *y|} + assert_arity(-1) {|*x|} + assert_arity(-1) {|*|} + end + + # [ruby-dev:22592] + def m(x) + lambda { x } + end + def test_eq + # [ruby-dev:22592] + a = m(1) + b = m(2) + assert_not_equal(a, b) + assert_not_equal(a.call, b.call) + + # [ruby-dev:22599] + assert_not_equal(proc {||}, proc {|x,y|}) + + # [ruby-dev:22601] + a = lambda {|x| lambda {} }.call(1) + b = lambda {} + assert_not_equal(a, b) + end +end diff --git a/ruby_1_8_5/test/ruby/test_process.rb b/ruby_1_8_5/test/ruby/test_process.rb new file mode 100644 index 0000000000..93eb871edc --- /dev/null +++ b/ruby_1_8_5/test/ruby/test_process.rb @@ -0,0 +1,41 @@ +require 'test/unit' + +class TestProcess < Test::Unit::TestCase + def test_rlimit_availability + begin + Process.getrlimit(nil) + rescue NotImplementedError + assert_raise(NotImplementedError) { Process.setrlimit } + rescue TypeError + assert_raise(ArgumentError) { Process.setrlimit } + end + end + + def rlimit_exist? + Process.getrlimit(nil) + rescue NotImplementedError + return false + rescue TypeError + return true + end + + def test_rlimit_nofile + return unless rlimit_exist? + pid = fork { + cur_nofile, max_nofile = Process.getrlimit(Process::RLIMIT_NOFILE) + begin + Process.setrlimit(Process::RLIMIT_NOFILE, 0, max_nofile) + rescue Errno::EINVAL + exit 0 + end + begin + IO.pipe + rescue Errno::EMFILE + exit 0 + end + exit 1 + } + Process.wait pid + assert_equal(0, $?.to_i) + end +end diff --git a/ruby_1_8_5/test/ruby/test_rand.rb b/ruby_1_8_5/test/ruby/test_rand.rb new file mode 100644 index 0000000000..e2fd127f9c --- /dev/null +++ b/ruby_1_8_5/test/ruby/test_rand.rb @@ -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 diff --git a/ruby_1_8_5/test/ruby/test_range.rb b/ruby_1_8_5/test/ruby/test_range.rb new file mode 100644 index 0000000000..455175087d --- /dev/null +++ b/ruby_1_8_5/test/ruby/test_range.rb @@ -0,0 +1,17 @@ +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 +end diff --git a/ruby_1_8_5/test/ruby/test_readpartial.rb b/ruby_1_8_5/test/ruby/test_readpartial.rb new file mode 100644 index 0000000000..3877e21f85 --- /dev/null +++ b/ruby_1_8_5/test/ruby/test_readpartial.rb @@ -0,0 +1,74 @@ +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 + + if !File::ALT_SEPARATOR # read on pipe cannot timeout on Windows. + 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 + +end + diff --git a/ruby_1_8_5/test/ruby/test_settracefunc.rb b/ruby_1_8_5/test/ruby/test_settracefunc.rb new file mode 100644 index 0000000000..39e1b035d8 --- /dev/null +++ b/ruby_1_8_5/test/ruby/test_settracefunc.rb @@ -0,0 +1,138 @@ +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(["line", 11, :bar, TestSetTraceFunc], events.shift) + 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 diff --git a/ruby_1_8_5/test/ruby/test_signal.rb b/ruby_1_8_5/test/ruby/test_signal.rb new file mode 100644 index 0000000000..43e16b8c79 --- /dev/null +++ b/ruby_1_8_5/test/ruby/test_signal.rb @@ -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(10) { + Process.waitpid pid + } + rescue Timeout::Error + Process.kill(:TERM, pid) + raise + end + } + ensure + r.close + w.close + r0.close + w0.close + end + end +end diff --git a/ruby_1_8_5/test/ruby/test_string.rb b/ruby_1_8_5/test/ruby/test_string.rb new file mode 100644 index 0000000000..f8938cad84 --- /dev/null +++ b/ruby_1_8_5/test/ruby/test_string.rb @@ -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 diff --git a/ruby_1_8_5/test/ruby/test_stringchar.rb b/ruby_1_8_5/test/ruby/test_stringchar.rb new file mode 100644 index 0000000000..34934e87bd --- /dev/null +++ b/ruby_1_8_5/test/ruby/test_stringchar.rb @@ -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 diff --git a/ruby_1_8_5/test/ruby/test_struct.rb b/ruby_1_8_5/test/ruby/test_struct.rb new file mode 100644 index 0000000000..fa1bb1549a --- /dev/null +++ b/ruby_1_8_5/test/ruby/test_struct.rb @@ -0,0 +1,24 @@ +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 +end diff --git a/ruby_1_8_5/test/ruby/test_super.rb b/ruby_1_8_5/test/ruby/test_super.rb new file mode 100644 index 0000000000..cf2e241fdd --- /dev/null +++ b/ruby_1_8_5/test/ruby/test_super.rb @@ -0,0 +1,117 @@ +require 'test/unit' + +class TestSuper < Test::Unit::TestCase + class Base + def single(a) a end + def double(a, b) [a,b] end + def array(*a) a end + def optional(a = 0) a end + end + class Single1 < Base + def single(*) super end + end + class Single2 < Base + def single(a,*) super end + end + class Double1 < Base + def double(*) super end + end + class Double2 < Base + def double(a,*) super end + end + class Double3 < Base + def double(a,b,*) super end + end + class Array1 < Base + def array(*) super end + end + class Array2 < Base + def array(a,*) super end + end + class Array3 < Base + def array(a,b,*) super end + end + class Array4 < Base + def array(a,b,c,*) super end + end + class Optional1 < Base + def optional(a = 1) super end + end + class Optional2 < Base + def optional(a, b = 1) super end + end + class Optional3 < Base + def single(a = 1) super end + end + + def test_single1 + assert_equal(1, Single1.new.single(1)) + end + def test_single2 + assert_equal(1, Single2.new.single(1)) + end + def test_double1 + assert_equal([1, 2], Double1.new.double(1, 2)) + end + def test_double2 + assert_equal([1, 2], Double2.new.double(1, 2)) + end + def test_double3 + assert_equal([1, 2], Double3.new.double(1, 2)) + end + def test_array1 + assert_equal([], Array1.new.array()) + assert_equal([1], Array1.new.array(1)) + end + def test_array2 + assert_equal([1], Array2.new.array(1)) + assert_equal([1,2], Array2.new.array(1, 2)) + end + def test_array3 + assert_equal([1,2], Array3.new.array(1, 2)) + assert_equal([1,2,3], Array3.new.array(1, 2, 3)) + end + def test_array4 + assert_equal([1,2,3], Array4.new.array(1, 2, 3)) + assert_equal([1,2,3,4], Array4.new.array(1, 2, 3, 4)) + end + def test_optional1 + assert_equal(9, Optional1.new.optional(9)) + assert_equal(1, Optional1.new.optional) + end + def test_optional2 + assert_raise(ArgumentError) do + # call Base#optional with 2 arguments; the 2nd arg is supplied + assert_equal(9, Optional2.new.optional(9)) + end + assert_raise(ArgumentError) do + # call Base#optional with 2 arguments + assert_equal(9, Optional2.new.optional(9, 2)) + end + end + def test_optional3 + assert_equal(9, Optional3.new.single(9)) + # call Base#single with 1 argument; the arg is supplied + assert_equal(1, Optional3.new.single) + end + + class A + def tt(aa) + "A#tt" + end + + def uu(a) + class << self + define_method(:tt) do |sym| + super + end + end + end + end + + def test_define_method # [ruby-core:03856] + a = A.new + a.uu(12) + assert_equal("A#tt", a.tt(12)) + end +end diff --git a/ruby_1_8_5/test/ruby/test_symbol.rb b/ruby_1_8_5/test/ruby/test_symbol.rb new file mode 100644 index 0000000000..2ccfe64c92 --- /dev/null +++ b/ruby_1_8_5/test/ruby/test_symbol.rb @@ -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 diff --git a/ruby_1_8_5/test/ruby/test_system.rb b/ruby_1_8_5/test/ruby/test_system.rb new file mode 100644 index 0000000000..f3a71598f7 --- /dev/null +++ b/ruby_1_8_5/test/ruby/test_system.rb @@ -0,0 +1,64 @@ +require 'test/unit' +$:.replace([File.dirname(File.expand_path(__FILE__))] | $:) +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 +end diff --git a/ruby_1_8_5/test/ruby/test_time.rb b/ruby_1_8_5/test/ruby/test_time.rb new file mode 100644 index 0000000000..ad9b64ae0e --- /dev/null +++ b/ruby_1_8_5/test/ruby/test_time.rb @@ -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 diff --git a/ruby_1_8_5/test/ruby/test_trace.rb b/ruby_1_8_5/test/ruby/test_trace.rb new file mode 100644 index 0000000000..6adfe0bf64 --- /dev/null +++ b/ruby_1_8_5/test/ruby/test_trace.rb @@ -0,0 +1,21 @@ +require 'test/unit' + +class TestTrace < Test::Unit::TestCase + def test_trace + $x = 1234 + $y = 0 + trace_var :$x, proc{$y = $x} + $x = 40414 + assert_equal($x, $y) + + untrace_var :$x + $x = 19660208 + assert_not_equal($x, $y) + + trace_var :$x, proc{$x *= 2} + $x = 5 + assert_equal(10, $x) + + untrace_var :$x + end +end diff --git a/ruby_1_8_5/test/ruby/test_variable.rb b/ruby_1_8_5/test/ruby/test_variable.rb new file mode 100644 index 0000000000..8726160826 --- /dev/null +++ b/ruby_1_8_5/test/ruby/test_variable.rb @@ -0,0 +1,53 @@ +require 'test/unit' + +class TestVariable < Test::Unit::TestCase + class Gods + @@rule = "Uranus" + def ruler0 + @@rule + end + + def self.ruler1 # <= per method definition style + @@rule + end + class << self # <= multiple method definition style + def ruler2 + @@rule + end + end + end + + module Olympians + @@rule ="Zeus" + def ruler3 + @@rule + end + end + + class Titans < Gods + @@rule = "Cronus" + include Olympians # OK to cause warning (intentional) + end + + def test_variable + assert_instance_of(Fixnum, $$) + + # read-only variable + assert_raises(NameError) do + $$ = 5 + end + + foobar = "foobar" + $_ = foobar + assert_equal(foobar, $_) + + assert_equal("Cronus", Gods.new.ruler0) + assert_equal("Cronus", Gods.ruler1) + assert_equal("Cronus", Gods.ruler2) + assert_equal("Cronus", Titans.ruler1) + assert_equal("Cronus", Titans.ruler2) + atlas = Titans.new + assert_equal("Cronus", atlas.ruler0) + assert_equal("Zeus", atlas.ruler3) + end +end diff --git a/ruby_1_8_5/test/ruby/test_whileuntil.rb b/ruby_1_8_5/test/ruby/test_whileuntil.rb new file mode 100644 index 0000000000..6faed31160 --- /dev/null +++ b/ruby_1_8_5/test/ruby/test_whileuntil.rb @@ -0,0 +1,77 @@ +require 'test/unit' + +class TestWhileuntil < Test::Unit::TestCase + def test_while + tmp = open("while_tmp", "w") + tmp.print "tvi925\n"; + tmp.print "tvi920\n"; + tmp.print "vt100\n"; + tmp.print "Amiga\n"; + tmp.print "paper\n"; + tmp.close + + tmp = open("while_tmp", "r") + assert_instance_of(File, tmp) + + while line = tmp.gets() + break if /vt100/ =~ line + end + + assert(!tmp.eof?) + assert_match(/vt100/, line) + tmp.close + + tmp = open("while_tmp", "r") + while line = tmp.gets() + next if /vt100/ =~ line + assert_no_match(/vt100/, line) + end + assert(tmp.eof?) + assert_no_match(/vt100/, line) + tmp.close + + tmp = open("while_tmp", "r") + while tmp.gets() + line = $_ + gsub(/vt100/, 'VT100') + if $_ != line + $_.gsub!('VT100', 'Vt100') + redo + end + assert_no_match(/vt100/, $_) + assert_no_match(/VT100/, $_) + end + assert(tmp.eof?) + tmp.close + + sum=0 + for i in 1..10 + sum += i + i -= 1 + if i > 0 + redo + end + end + assert_equal(220, sum) + + tmp = open("while_tmp", "r") + while line = tmp.gets() + break if 3 + assert_no_match(/vt100/, line) + assert_no_match(/Amiga/, line) + assert_no_match(/paper/, line) + end + tmp.close + + File.unlink "while_tmp" or `/bin/rm -f "while_tmp"` + assert(!File.exist?("while_tmp")) + end + + def test_until + i = 0 + until i>4 + i+=1 + end + assert(i>4) + end +end diff --git a/ruby_1_8_5/test/ruby/ut_eof.rb b/ruby_1_8_5/test/ruby/ut_eof.rb new file mode 100644 index 0000000000..e6f6eddd6f --- /dev/null +++ b/ruby_1_8_5/test/ruby/ut_eof.rb @@ -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_nil(f.read(0)) + assert_nil(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_nil(f.read(0)) + assert_nil(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_nil(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_nil(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 |