ruby2c 1.0.0.6

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,328 @@
1
+ $TESTING = true
2
+
3
+ begin require 'rubygems'; rescue LoadError; end
4
+ require 'minitest/autorun' if $0 == __FILE__
5
+ require 'crewriter'
6
+ require 'r2ctestcase'
7
+
8
+ class TestCRewriter < R2CTestCase
9
+
10
+ def setup
11
+ @processor = CRewriter.new
12
+ @rewrite = CRewriter.new
13
+ Unique.reset
14
+ end
15
+
16
+ def test_process_call_rewritten
17
+
18
+ input = t(:call,
19
+ t(:str, "this", Type.str),
20
+ :+,
21
+ t(:array, t(:str, "that", Type.str)),
22
+ Type.str)
23
+ expected = t(:call,
24
+ nil,
25
+ :strcat,
26
+ t(:array,
27
+ t(:str, "this", Type.str),
28
+ t(:str, "that", Type.str)),
29
+ Type.str)
30
+
31
+ assert_equal expected, @rewrite.process(input)
32
+ end
33
+
34
+ def test_process_call_same
35
+
36
+ input = t(:call,
37
+ t(:lit, 1, Type.long),
38
+ :+,
39
+ t(:array, t(:lit, 2, Type.long)),
40
+ Type.long)
41
+ expected = input.deep_clone
42
+
43
+ assert_equal expected, @rewrite.process(input)
44
+ end
45
+
46
+ def test_process_class
47
+ input = t(:class,
48
+ :IterExample,
49
+ :Object,
50
+ t(:defn,
51
+ :example,
52
+ t(:args),
53
+ t(:scope,
54
+ t(:block,
55
+ t(:lasgn, :sum, t(:lit, 0, Type.long), Type.long),
56
+ t(:iter,
57
+ t(:call,
58
+ t(:lit, 0...10, Type.range),
59
+ :each,
60
+ nil, Type.void),
61
+ t(:dasgn_curr, :a, Type.long),
62
+ t(:iter,
63
+ t(:call,
64
+ t(:lit, 0...10, Type.range),
65
+ :each,
66
+ nil, Type.void),
67
+ t(:dasgn_curr, :b, Type.long),
68
+ t(:lasgn,
69
+ :sum,
70
+ t(:call,
71
+ t(:lvar, :sum, Type.long),
72
+ :+,
73
+ t(:arglist,
74
+ t(:call,
75
+ t(:dvar, :a, Type.long),
76
+ :+,
77
+ t(:arglist, t(:dvar, :b, Type.long)),
78
+ Type.void)),
79
+ Type.void),
80
+ Type.long))),
81
+ t(:return, t(:lvar, :sum, Type.long)))), Type.void))
82
+
83
+ expect = t(:class,
84
+ :IterExample,
85
+ :Object,
86
+ t(:static, "static VALUE static_temp_7;", Type.fucked),
87
+ t(:defx,
88
+ :temp_4,
89
+ t(:args,
90
+ t(:temp_5, Type.long),
91
+ t(:temp_6, Type.value)),
92
+ t(:scope,
93
+ t(:block,
94
+ t(:lasgn,
95
+ :sum,
96
+ t(:lvar, :static_temp_7, Type.long),
97
+ Type.long),
98
+ t(:lasgn,
99
+ :b,
100
+ t(:lvar, :temp_5, Type.long),
101
+ Type.long),
102
+ t(:lasgn,
103
+ :sum,
104
+ t(:call,
105
+ t(:lvar, :sum, Type.long),
106
+ :+,
107
+ t(:arglist,
108
+ t(:call,
109
+ t(:dvar, :a, Type.long),
110
+ :+,
111
+ t(:arglist, t(:dvar, :b, Type.long)), Type.void)),
112
+ Type.void), Type.long),
113
+ t(:lasgn,
114
+ :static_temp_7,
115
+ t(:lvar, :sum, Type.long),
116
+ Type.long),
117
+ t(:return, t(:nil, Type.value)))), Type.void),
118
+ t(:defx,
119
+ :temp_1,
120
+ t(:args,
121
+ t(:temp_2, Type.long),
122
+ t(:temp_3, Type.value)),
123
+ t(:scope,
124
+ t(:block,
125
+ t(:lasgn, :a, t(:lvar, :temp_2, Type.long), Type.long),
126
+ t(:iter,
127
+ t(:call,
128
+ t(:lit, 0...10, Type.range),
129
+ :each,
130
+ nil, Type.void),
131
+ t(:args,
132
+ t(:array, t(:lvar, :sum, Type.long), Type.void),
133
+ t(:array, t(:lvar, :static_temp_7, Type.long), Type.void),
134
+ Type.void),
135
+ :temp_4),
136
+ t(:return, t(:nil, Type.value)))), Type.void),
137
+ t(:defn,
138
+ :example,
139
+ t(:args),
140
+ t(:scope,
141
+ t(:block,
142
+ t(:lasgn, :sum, t(:lit, 0, Type.long), Type.long),
143
+ t(:iter,
144
+ t(:call,
145
+ t(:lit, 0...10, Type.range),
146
+ :each,
147
+ nil, Type.void),
148
+ t(:args,
149
+ t(:array, Type.void),
150
+ t(:array, Type.void),
151
+ Type.void),
152
+ :temp_1),
153
+ t(:return, t(:lvar, :sum, Type.long)))), Type.void),
154
+ Type.zclass)
155
+
156
+ assert_equal expect, @rewrite.process(input)
157
+ end
158
+
159
+ def test_process_lasgn
160
+ input = t(:lasgn, :variable, t(:nil), Type.long)
161
+ expect = t(:lasgn, :variable, t(:nil), Type.long)
162
+
163
+ assert_equal expect, @rewrite.process(input)
164
+ assert_equal [], @rewrite.free
165
+ assert_equal [:variable], @rewrite.env.all.keys
166
+ end
167
+
168
+ def test_process_lvar
169
+ input = t(:lvar, :variable, Type.long)
170
+ expect = t(:lvar, :variable, Type.long)
171
+
172
+ assert_equal expect, @rewrite.process(input)
173
+ assert_equal [[:variable, Type.value]], @rewrite.free # HACK
174
+ assert_equal [:variable], @rewrite.env.all.keys
175
+ end
176
+
177
+ def test_process_iter_each
178
+ # sum = 0; arr.each do |value| sum += value; end
179
+ input = t(:block,
180
+ t(:lasgn, :arr, t(:array, t(:lit, 1, Type.long)), Type.long_list),
181
+ t(:lasgn, :sum, t(:lit, 0, Type.long), Type.long),
182
+ t(:iter,
183
+ t(:call, t(:lvar, :arr, Type.long), :each, nil, Type.void),
184
+ t(:dasgn_curr, :value, Type.long),
185
+ t(:lasgn, # block guts
186
+ :sum,
187
+ t(:call,
188
+ t(:lvar, :sum, Type.long),
189
+ :+,
190
+ t(:arglist, t(:dvar, :value, Type.long)), Type.void),
191
+ Type.long), Type.void), Type.void)
192
+
193
+ # $sum = sum; arr.each do |value| temp_1(value) end
194
+ # def temp_1(value) sum = $sum; ...; $sum = sum; end
195
+
196
+ expect = t(:block,
197
+ t(:lasgn, :arr, t(:array, t(:lit, 1, Type.long)), Type.long_list),
198
+ t(:lasgn, :sum, t(:lit, 0, Type.long), Type.long),
199
+ t(:iter, # new iter
200
+ t(:call, t(:lvar, :arr, Type.long), :each, nil, Type.void),
201
+ t(:args,
202
+ t(:array, t(:lvar, :sum, Type.long), Type.void),
203
+ t(:array, t(:lvar, :static_temp_4, Type.long), Type.void),
204
+ Type.void),
205
+ :temp_1),
206
+ Type.void)
207
+
208
+ assert_equal expect, @rewrite.process(input)
209
+
210
+ static = t(:static, "static VALUE static_temp_4;", Type.fucked)
211
+
212
+ defx = t(:defx,
213
+ :temp_1,
214
+ t(:args, t(:temp_2, Type.long), t(:temp_3, Type.value)),
215
+ t(:scope,
216
+ t(:block,
217
+ t(:lasgn, :sum, t(:lvar, :static_temp_4, Type.long), Type.long),
218
+ t(:lasgn, :value, t(:lvar, :temp_2, Type.long), Type.long),
219
+ t(:lasgn, :sum, # sum =
220
+ t(:call,
221
+ t(:lvar, :sum, Type.long), # sum + value
222
+ :+,
223
+ t(:arglist, t(:dvar, :value, Type.long)), Type.void),
224
+ Type.long),
225
+ t(:lasgn, :static_temp_4, t(:lvar, :sum, Type.long), Type.long),
226
+ t(:return, t(:nil, Type.value)))),
227
+ Type.void)
228
+
229
+ assert_equal [static, defx], @rewrite.extra_methods
230
+ end
231
+
232
+ def test_process_iter_each_with_index
233
+ input = t(:block,
234
+ t(:lasgn, :arr, t(:array, t(:lit, 1, Type.long)), Type.long),
235
+ t(:lasgn, :sum, t(:lit, 0, Type.long), Type.long),
236
+ t(:iter,
237
+ t(:call,
238
+ t(:lvar, :arr, Type.long),
239
+ :each_with_index,
240
+ nil, Type.void),
241
+ t(:masgn,
242
+ t(:array,
243
+ t(:dasgn_curr, :value, Type.long),
244
+ t(:dasgn_curr, :i, Type.long))),
245
+ t(:lasgn, # block guts
246
+ :sum,
247
+ t(:call,
248
+ t(:lvar, :sum, Type.long),
249
+ :+,
250
+ t(:arglist, t(:dvar, :value, Type.long)), Type.void),
251
+ Type.long)))
252
+
253
+ expect = t(:block,
254
+ t(:lasgn, :arr, t(:array, t(:lit, 1, Type.long)), Type.long),
255
+ t(:lasgn, :sum, t(:lit, 0, Type.long), Type.long),
256
+ t(:iter, # new iter
257
+ t(:call,
258
+ t(:lvar, :arr, Type.long),
259
+ :each_with_index,
260
+ nil, Type.void),
261
+ t(:args,
262
+ t(:array, t(:lvar, :sum, Type.long), Type.void),
263
+ t(:array, t(:lvar, :static_temp_4, Type.long), Type.void),
264
+ Type.void),
265
+ :temp_1))
266
+
267
+ assert_equal expect, @rewrite.process(input)
268
+
269
+ static = t(:static, "static VALUE static_temp_4;", Type.fucked)
270
+
271
+ defx = t(:defx,
272
+ :temp_1,
273
+ t(:args,
274
+ t(:temp_2, Type.value),
275
+ t(:temp_3, Type.value)),
276
+ t(:scope,
277
+ t(:block,
278
+ t(:lasgn, :sum, t(:lvar, :static_temp_4, Type.long), Type.long),
279
+ t(:masgn,
280
+ t(:array,
281
+ t(:lasgn, :value, nil, Type.long),
282
+ t(:lasgn, :i, nil, Type.long)),
283
+ t(:to_ary, t(:lvar, :temp_2, Type.value))),
284
+ t(:lasgn, :sum, # sum =
285
+ t(:call,
286
+ t(:lvar, :sum, Type.long), # sum + value
287
+ :+,
288
+ t(:arglist, t(:dvar, :value, Type.long)), Type.void),
289
+ Type.long),
290
+ t(:lasgn, :static_temp_4, t(:lvar, :sum, Type.long), Type.long),
291
+ t(:return, t(:nil, Type.value)))), Type.void)
292
+
293
+ assert_equal [static, defx], @rewrite.extra_methods
294
+ end
295
+
296
+ def test_free
297
+ e = @rewrite.env
298
+
299
+ e.add :sum, Type.value
300
+ e.set_val :sum, true
301
+
302
+ e.extend
303
+
304
+ e.add :arr, Type.value
305
+ e.set_val :arr, true
306
+
307
+ # HACK this is a real bug, but not a priority for me right now
308
+ # expected = {:arr=>[Type.value, true], :sum=>[Type.value, true]}
309
+ # assert_equal expected, e.all
310
+ #
311
+ # expected = [{:arr=>[Type.value, true]}, {:sum=>[Type.value, true]}]
312
+ # assert_equal expected, e.env
313
+ #
314
+ # assert_equal [[:arr, Type.value]], @rewrite.free
315
+ end
316
+
317
+ def test_var_names_in
318
+ assert_equal [[:value, Type.long]], @rewrite.var_names_in(t(:dasgn_curr, :value, Type.long))
319
+
320
+ input = t(:masgn, t(:array,
321
+ t(:dasgn_curr, :value, Type.long),
322
+ t(:dasgn_curr, :i, Type.str)))
323
+ expect = [[:value, Type.long], [:i, Type.str]]
324
+
325
+ assert_equal expect, @rewrite.var_names_in(input)
326
+ end
327
+ end
328
+
@@ -0,0 +1,68 @@
1
+ #!/usr/local/bin/ruby -w
2
+
3
+ $TESTING = true
4
+
5
+ require 'minitest/autorun' if $0 == __FILE__
6
+ require 'minitest/unit'
7
+ require 'type_checker'
8
+
9
+ class RandomCode # ZenTest SKIP
10
+ def specific_method(x)
11
+ x = 0 # make x and y to be longs
12
+ return c.to_i > 0
13
+ end
14
+
15
+ def generic_method(x)
16
+ specific_method(x)
17
+ end
18
+
19
+ def meth_b(x)
20
+ # nothing to do so we don't hint what x is
21
+ end
22
+
23
+ def meth_a(x)
24
+ meth_b(x)
25
+ end
26
+
27
+ end
28
+
29
+ class TestExtraTypeChecker < MiniTest::Unit::TestCase # ZenTest SKIP
30
+
31
+ def setup
32
+ @rewriter = Rewriter.new
33
+ @type_checker = TypeChecker.new
34
+ end
35
+
36
+ def test_unify_function_args
37
+ act, bct = util_unify_function.map { |x| x.formal_types }
38
+ assert_equal act.first.list_type, bct.first.list_type
39
+ assert_equal act.first.list_type.object_id, bct.first.list_type.object_id
40
+ end
41
+
42
+ def test_unify_function_receiver
43
+ act, bct = util_unify_function
44
+ assert_equal act.receiver_type.list_type, bct.receiver_type.list_type
45
+ assert_equal act.receiver_type.list_type.object_id, bct.receiver_type.list_type.object_id
46
+ assert_equal act, bct
47
+ end
48
+
49
+ def test_unify_function_return
50
+ act, bct = util_unify_function
51
+ assert_equal act.return_type.list_type, bct.return_type.list_type
52
+ assert_equal act.return_type.list_type.object_id, bct.return_type.list_type.object_id
53
+ end
54
+
55
+ def test_unify_function_whole
56
+ act, bct = util_unify_function
57
+ assert_equal act, bct
58
+ end
59
+
60
+ def util_unify_function
61
+ a = Type.function(Type.unknown, [ Type.unknown ], Type.unknown)
62
+ b = Type.function(Type.long, [ Type.str ], Type.void)
63
+ a.unify b
64
+ act = a.list_type
65
+ bct = b.list_type
66
+ return act, bct
67
+ end
68
+ end
@@ -0,0 +1,90 @@
1
+ #!/usr/local/bin/ruby -w
2
+
3
+ $TESTING = true
4
+
5
+ require 'minitest/autorun' if $0 == __FILE__
6
+ require 'minitest/unit'
7
+ require 'function_table'
8
+ require 'type'
9
+
10
+ class TestFunctionTable < MiniTest::Unit::TestCase
11
+
12
+ def setup
13
+ @function_table = FunctionTable.new
14
+ end
15
+
16
+ def test_add_function
17
+ type = @function_table.add_function :func, Type.long
18
+
19
+ assert_equal Type.long, type
20
+ assert_equal Type.long, @function_table[:func]
21
+ end
22
+
23
+ def test_cheat
24
+ @function_table.add_function :func, Type.long
25
+ @function_table.add_function :func, Type.str
26
+
27
+ assert_equal [Type.long, Type.str], @function_table.cheat(:func)
28
+ end
29
+
30
+ def test_has_key?
31
+ @function_table.add_function :func, Type.long
32
+
33
+ assert_equal true, @function_table.has_key?(:func)
34
+ assert_equal false, @function_table.has_key?('no such func')
35
+ end
36
+
37
+ def test_index
38
+ @function_table.add_function :func, Type.long
39
+
40
+ assert_equal Type.long, @function_table[:func]
41
+
42
+ @function_table.add_function :func, Type.str
43
+
44
+ assert_equal Type.long, @function_table[:func]
45
+ end
46
+
47
+ def test_unify_one_type
48
+ @function_table.add_function :func, Type.unknown
49
+
50
+ @function_table.unify :func, Type.long do
51
+ flunk "Block should not have been called"
52
+ end
53
+
54
+ assert_equal Type.long, @function_table[:func]
55
+ end
56
+
57
+ def test_unify_two_type
58
+ @function_table.add_function :func, Type.unknown
59
+ @function_table.add_function :func, Type.str
60
+
61
+ @function_table.unify :func, Type.long do
62
+ flunk "Block should not have been called"
63
+ end
64
+
65
+ assert_equal Type.long, @function_table[:func]
66
+ end
67
+
68
+ def test_unify_block_called_no_type
69
+ @function_table.add_function :func, Type.str
70
+
71
+ test_var = false
72
+
73
+ @function_table.unify :func, Type.long do
74
+ test_var = true
75
+ end
76
+
77
+ assert test_var, "Block not called"
78
+ end
79
+
80
+ def test_unify_block_called_no_unify
81
+ test_var = false
82
+
83
+ @function_table.unify :func, Type.long do
84
+ test_var = true
85
+ end
86
+
87
+ assert test_var, "Block not called"
88
+ end
89
+
90
+ end