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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
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
|