summaryrefslogtreecommitdiff
path: root/yarvtest/test_opts.rb
blob: 689257c78a63cfae502f168e478e29243d233756 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
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