mirror of
				https://github.com/ruby/ruby.git
				synced 2022-11-09 12:17:21 -05:00 
			
		
		
		
	* test/ruby/test_optimization.rb: new test (merges test_opts.rb).
* yarvtest/test_opts.rb: removed. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@11831 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
		
							parent
							
								
									f52fd2165b
								
							
						
					
					
						commit
						bcd3345136
					
				
					 3 changed files with 201 additions and 118 deletions
				
			
		| 
						 | 
				
			
			@ -1,3 +1,9 @@
 | 
			
		|||
Fri Feb 23 18:13:22 2007  Minero Aoki  <aamine@loveruby.net>
 | 
			
		||||
 | 
			
		||||
	* test/ruby/test_optimization.rb: new test (merges test_opts.rb).
 | 
			
		||||
 | 
			
		||||
	* yarvtest/test_opts.rb: removed.
 | 
			
		||||
 | 
			
		||||
Fri Feb 23 16:59:39 2007  Minero Aoki  <aamine@loveruby.net>
 | 
			
		||||
 | 
			
		||||
	* test/ruby/test_assignment.rb: merge yarvtest/test_massign.
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										195
									
								
								test/ruby/test_optimization.rb
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										195
									
								
								test/ruby/test_optimization.rb
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,195 @@
 | 
			
		|||
require 'test/unit'
 | 
			
		||||
 | 
			
		||||
class TestRubyOptimization < Test::Unit::TestCase
 | 
			
		||||
 | 
			
		||||
  BIGNUM_POS_MIN_32 = 1073741824      # 2 ** 30
 | 
			
		||||
  if BIGNUM_POS_MIN_32.kind_of?(Fixnum)
 | 
			
		||||
    FIXNUM_MAX = 4611686018427387903  # 2 ** 62 - 1
 | 
			
		||||
  else
 | 
			
		||||
    FIXNUM_MAX = 1073741823           # 2 ** 30 - 1
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  BIGNUM_NEG_MAX_32 = -1073741825     # -2 ** 30 - 1
 | 
			
		||||
  if BIGNUM_NEG_MAX_32.kind_of?(Fixnum)
 | 
			
		||||
    FIXNUM_MIN = -4611686018427387904 # -2 ** 62
 | 
			
		||||
  else
 | 
			
		||||
    FIXNUM_MIN = -1073741824          # -2 ** 30
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  def test_fixnum_plus
 | 
			
		||||
    a, b = 1, 2
 | 
			
		||||
    assert_equal 3, a + b
 | 
			
		||||
    assert_instance_of Fixnum, FIXNUM_MAX
 | 
			
		||||
    assert_instance_of Bignum, FIXNUM_MAX + 1
 | 
			
		||||
 | 
			
		||||
    assert_equal 21, 10 + 11
 | 
			
		||||
    eval(<<-End, ::TOPLEVEL_BINDING)
 | 
			
		||||
      class Fixnum
 | 
			
		||||
        alias orig_plus +
 | 
			
		||||
        undef +
 | 
			
		||||
        def +(other)
 | 
			
		||||
          other
 | 
			
		||||
        end
 | 
			
		||||
      end
 | 
			
		||||
    End
 | 
			
		||||
    assert_equal 11, 10 + 11
 | 
			
		||||
  ensure
 | 
			
		||||
    eval(<<-End, ::TOPLEVEL_BINDING)
 | 
			
		||||
      class Fixnum
 | 
			
		||||
        undef +
 | 
			
		||||
        alias + orig_plus
 | 
			
		||||
      end
 | 
			
		||||
    End
 | 
			
		||||
    assert_equal 21, 10 + 11
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  def test_fixnum_minus
 | 
			
		||||
    assert_equal 5, 8 - 3
 | 
			
		||||
    assert_instance_of Fixnum, FIXNUM_MIN
 | 
			
		||||
    assert_instance_of Bignum, FIXNUM_MIN - 1
 | 
			
		||||
 | 
			
		||||
    assert_equal 5, 8 - 3
 | 
			
		||||
    eval(<<-End, ::TOPLEVEL_BINDING)
 | 
			
		||||
      class Fixnum
 | 
			
		||||
        alias orig_minus -
 | 
			
		||||
        undef -
 | 
			
		||||
        def -(other)
 | 
			
		||||
          other
 | 
			
		||||
        end
 | 
			
		||||
      end
 | 
			
		||||
    End
 | 
			
		||||
    assert_equal 3, 8 - 3
 | 
			
		||||
  ensure
 | 
			
		||||
    eval(<<-End, ::TOPLEVEL_BINDING)
 | 
			
		||||
      class Fixnum
 | 
			
		||||
        undef -
 | 
			
		||||
        alias - orig_minus
 | 
			
		||||
      end
 | 
			
		||||
    End
 | 
			
		||||
    assert_equal 5, 8 - 3
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  def test_fixnum_mul
 | 
			
		||||
    assert_equal 15, 3 * 5
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  def test_fixnum_div
 | 
			
		||||
    assert_equal 3, 15 / 5
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  def test_fixnum_mod
 | 
			
		||||
    assert_equal 1, 8 % 7
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  def test_float_plus
 | 
			
		||||
    assert_equal 4.0, 2.0 + 2.0
 | 
			
		||||
    eval(<<-End, ::TOPLEVEL_BINDING)
 | 
			
		||||
      class Float
 | 
			
		||||
        alias orig_plus +
 | 
			
		||||
        undef +
 | 
			
		||||
        def +(other)
 | 
			
		||||
          other
 | 
			
		||||
        end
 | 
			
		||||
      end
 | 
			
		||||
    End
 | 
			
		||||
    assert_equal 2.0, 2.0 + 2.0
 | 
			
		||||
    eval(<<-End, ::TOPLEVEL_BINDING)
 | 
			
		||||
      class Float
 | 
			
		||||
        undef +
 | 
			
		||||
        alias + orig_plus
 | 
			
		||||
      end
 | 
			
		||||
    End
 | 
			
		||||
    assert_equal 4.0, 2.0 + 2.0
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  def test_string_length
 | 
			
		||||
    assert_equal 6, "string".length
 | 
			
		||||
    eval(<<-End, ::TOPLEVEL_BINDING)
 | 
			
		||||
      class String
 | 
			
		||||
        alias orig_length length
 | 
			
		||||
        undef length
 | 
			
		||||
        def length
 | 
			
		||||
          99
 | 
			
		||||
        end
 | 
			
		||||
      end
 | 
			
		||||
    End
 | 
			
		||||
    assert_equal 99, "string".length
 | 
			
		||||
  ensure
 | 
			
		||||
    eval(<<-End, ::TOPLEVEL_BINDING)
 | 
			
		||||
      class String
 | 
			
		||||
        undef length
 | 
			
		||||
        alias length orig_length
 | 
			
		||||
      end
 | 
			
		||||
    End
 | 
			
		||||
    assert_equal 6, "string".length
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  def test_string_plus
 | 
			
		||||
    assert_equal "", "" + ""
 | 
			
		||||
    assert_equal "x", "x" + ""
 | 
			
		||||
    assert_equal "x", "" + "x"
 | 
			
		||||
    assert_equal "ab", "a" + "b"
 | 
			
		||||
    eval(<<-End, ::TOPLEVEL_BINDING)
 | 
			
		||||
      class String
 | 
			
		||||
        alias orig_plus +
 | 
			
		||||
        undef +
 | 
			
		||||
        def +(other)
 | 
			
		||||
          'OK'
 | 
			
		||||
        end
 | 
			
		||||
      end
 | 
			
		||||
    End
 | 
			
		||||
    assert_equal 'OK', "a" + "b"
 | 
			
		||||
  ensure
 | 
			
		||||
    eval(<<-End, ::TOPLEVEL_BINDING)
 | 
			
		||||
      class String
 | 
			
		||||
        undef +
 | 
			
		||||
        alias + orig_plus
 | 
			
		||||
      end
 | 
			
		||||
    End
 | 
			
		||||
    assert_equal "ab", "a" + "b"
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  def test_string_succ
 | 
			
		||||
    assert_equal 'b', 'a'.succ
 | 
			
		||||
    assert_equal 'B', 'A'.succ
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  def test_string_format
 | 
			
		||||
    assert_equal '2', '%d' % 2
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  def test_array_plus
 | 
			
		||||
    assert_equal [1,2], [1]+[2]
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  def test_array_minus
 | 
			
		||||
    assert_equal [2], [1,2] - [1]
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  def test_array_length
 | 
			
		||||
    assert_equal 0, [].length
 | 
			
		||||
    assert_equal 3, [1,2,3].length
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  def test_hash_length
 | 
			
		||||
    assert_equal 0, {}.length
 | 
			
		||||
    assert_equal 1, {1=>1}.length
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  class MyObj
 | 
			
		||||
    def ==(other)
 | 
			
		||||
      true
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  def test_eq
 | 
			
		||||
    assert_equal true, nil == nil
 | 
			
		||||
    assert_equal true, 1 == 1
 | 
			
		||||
    assert_equal true, 'string' == 'string'
 | 
			
		||||
    assert_equal true, 1 == MyObj.new
 | 
			
		||||
    assert_equal false, nil == MyObj.new
 | 
			
		||||
    assert_equal true, MyObj.new == 1
 | 
			
		||||
    assert_equal true, MyObj.new == nil
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
end
 | 
			
		||||
| 
						 | 
				
			
			@ -1,118 +0,0 @@
 | 
			
		|||
require 'yarvtest/yarvtest'
 | 
			
		||||
 | 
			
		||||
class TestOpt < YarvTestBase
 | 
			
		||||
  def test_plus
 | 
			
		||||
    ae %q{
 | 
			
		||||
      a, b = 1, 2
 | 
			
		||||
      a+b
 | 
			
		||||
    }
 | 
			
		||||
    ae %q{
 | 
			
		||||
      class Fixnum
 | 
			
		||||
        def +(*o)
 | 
			
		||||
          o
 | 
			
		||||
        end
 | 
			
		||||
        def -(*o)
 | 
			
		||||
          o
 | 
			
		||||
        end
 | 
			
		||||
      end
 | 
			
		||||
      [10+11, 100-101]
 | 
			
		||||
    }
 | 
			
		||||
    ae %q{
 | 
			
		||||
      class Float
 | 
			
		||||
        def +(o)
 | 
			
		||||
          self * o
 | 
			
		||||
        end
 | 
			
		||||
      end
 | 
			
		||||
      
 | 
			
		||||
      a, b = 1, 2
 | 
			
		||||
      a+b
 | 
			
		||||
    }
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  def test_opt_methdos
 | 
			
		||||
    klasses = [[Fixnum, 2, 3], [Float, 1.1, 2.2],
 | 
			
		||||
    [String, "abc", "def"], [Array, [1,2,3], [4, 5]],
 | 
			
		||||
    [Hash, {:a=>1, :b=>2}, {:x=>"foo", :y=>"bar"}]]
 | 
			
		||||
    
 | 
			
		||||
    bin_methods = [:+, :-, :*, :/, :%, ]
 | 
			
		||||
    one_methods = [:length, :succ, ]
 | 
			
		||||
    ary = []
 | 
			
		||||
    
 | 
			
		||||
    bin_methods.each{|m|
 | 
			
		||||
      klasses.each{|klass, obj, arg|
 | 
			
		||||
        str = %{
 | 
			
		||||
          ary = []
 | 
			
		||||
          if (#{obj.inspect}).respond_to? #{m.inspect}
 | 
			
		||||
            begin
 | 
			
		||||
              ary << (#{obj.inspect}).#{m.to_s}(#{arg.inspect})
 | 
			
		||||
            rescue Exception => e
 | 
			
		||||
              ary << :error
 | 
			
		||||
            end
 | 
			
		||||
          end
 | 
			
		||||
          
 | 
			
		||||
          class #{klass}
 | 
			
		||||
            def #{m}(o)
 | 
			
		||||
              [#{m.inspect}, :bin, #{klass}].inspect
 | 
			
		||||
            end
 | 
			
		||||
          end
 | 
			
		||||
          ary << (#{obj.inspect}).#{m.to_s}(#{arg.inspect})
 | 
			
		||||
          ary
 | 
			
		||||
        }
 | 
			
		||||
        ae str
 | 
			
		||||
      }
 | 
			
		||||
    }
 | 
			
		||||
    one_methods.each{|m|
 | 
			
		||||
      klasses.each{|klass, obj|
 | 
			
		||||
        str = %{
 | 
			
		||||
          ary = []
 | 
			
		||||
          if (#{obj.inspect}).respond_to? #{m.inspect}
 | 
			
		||||
            ary << (#{obj.inspect}).#{m.to_s}()
 | 
			
		||||
          end
 | 
			
		||||
          
 | 
			
		||||
          class #{klass}
 | 
			
		||||
            def #{m}()
 | 
			
		||||
              [#{m.inspect}, self, #{klass}].inspect
 | 
			
		||||
            end
 | 
			
		||||
          end
 | 
			
		||||
          ary << (#{obj.inspect}).#{m.to_s}()
 | 
			
		||||
          ary
 | 
			
		||||
        }
 | 
			
		||||
        ae str
 | 
			
		||||
      }
 | 
			
		||||
    }
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  def test_opt_plus
 | 
			
		||||
    ae %q{
 | 
			
		||||
      temp = 2**30 - 5
 | 
			
		||||
      (1..5).map do
 | 
			
		||||
        temp += 1
 | 
			
		||||
        [temp, temp.class]
 | 
			
		||||
      end
 | 
			
		||||
    }
 | 
			
		||||
    ae %q{
 | 
			
		||||
      temp = -(2**30 - 5)
 | 
			
		||||
      (1..10).map do
 | 
			
		||||
        temp += 1
 | 
			
		||||
        [temp, temp.class]
 | 
			
		||||
      end
 | 
			
		||||
    }
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  def test_eq
 | 
			
		||||
    ae %q{
 | 
			
		||||
      class Foo
 | 
			
		||||
        def ==(other)
 | 
			
		||||
          true
 | 
			
		||||
        end
 | 
			
		||||
      end
 | 
			
		||||
      foo = Foo.new
 | 
			
		||||
      [1.0 == foo,
 | 
			
		||||
       1 == foo,
 | 
			
		||||
       "abc" == foo,
 | 
			
		||||
       ]
 | 
			
		||||
    }
 | 
			
		||||
  end
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue