8 STDERR.print "\nsample/test.rb:#{what} "
16 where = (st = caller(n)) ? st[0] : "caller error! (n=#{n}, trace=#{caller(0).join(', ')}"
19 printf "ok %d (%s)\n", $testnum, where
22 printf "not ok %s %d -- %s\n", $what, $testnum, where
29 # make sure conditional operators work
31 test_check "assignment"
34 test_ok(a[0] == "bar")
35 h={}; h["foo"] ||= "bar";
36 test_ok(h["foo"] == "bar")
49 a = nil; test_ok(a == nil)
50 a = 1; test_ok(a == 1)
51 a = []; test_ok(a == [])
52 a = [1]; test_ok(a == [1])
53 a = [nil]; test_ok(a == [nil])
54 a = [[]]; test_ok(a == [[]])
55 a = [1,2]; test_ok(a == [1,2])
56 a = [*[]]; test_ok(a == [])
57 a = [*[1]]; test_ok(a == [1])
58 a = [*[1,2]]; test_ok(a == [1,2])
60 a = *[]; test_ok(a == [])
61 a = *[1]; test_ok(a == [1])
62 a = *[nil]; test_ok(a == [nil])
63 a = *[[]]; test_ok(a == [[]])
64 a = *[1,2]; test_ok(a == [1,2])
65 a = *[*[]]; test_ok(a == [])
66 a = *[*[1]]; test_ok(a == [1])
67 a = *[*[1,2]]; test_ok(a == [1,2])
69 a, = nil; test_ok(a == nil)
70 a, = 1; test_ok(a == 1)
71 a, = []; test_ok(a == nil)
72 a, = [1]; test_ok(a == 1)
73 a, = [nil]; test_ok(a == nil)
74 a, = [[]]; test_ok(a == [])
75 a, = 1,2; test_ok(a == 1)
76 a, = [1,2]; test_ok(a == 1)
77 a, = [*[]]; test_ok(a == nil)
78 a, = [*[1]]; test_ok(a == 1)
79 a, = *[1,2]; test_ok(a == 1)
80 a, = [*[1,2]]; test_ok(a == 1)
82 a, = *[]; test_ok(a == nil)
83 a, = *[1]; test_ok(a == 1)
84 a, = *[nil]; test_ok(a == nil)
85 a, = *[[]]; test_ok(a == [])
86 a, = *[1,2]; test_ok(a == 1)
87 a, = *[*[]]; test_ok(a == nil)
88 a, = *[*[1]]; test_ok(a == 1)
89 a, = *[*[1,2]]; test_ok(a == 1)
91 *a = nil; test_ok(a == [nil])
92 *a = 1; test_ok(a == [1])
93 *a = []; test_ok(a == [])
94 *a = [1]; test_ok(a == [1])
95 *a = [nil]; test_ok(a == [nil])
96 *a = [[]]; test_ok(a == [[]])
97 *a = [1,2]; test_ok(a == [1,2])
98 *a = [*[]]; test_ok(a == [])
99 *a = [*[1]]; test_ok(a == [1])
100 *a = [*[1,2]]; test_ok(a == [1,2])
102 *a = *[]; test_ok(a == [])
103 *a = *[1]; test_ok(a == [1])
104 *a = *[nil]; test_ok(a == [nil])
105 *a = *[[]]; test_ok(a == [[]])
106 *a = *[1,2]; test_ok(a == [1,2])
107 *a = *[*[]]; test_ok(a == [])
108 *a = *[*[1]]; test_ok(a == [1])
109 *a = *[*[1,2]]; test_ok(a == [1,2])
111 a,b,*c = nil; test_ok([a,b,c] == [nil,nil,[]])
112 a,b,*c = 1; test_ok([a,b,c] == [1,nil,[]])
113 a,b,*c = []; test_ok([a,b,c] == [nil,nil,[]])
114 a,b,*c = [1]; test_ok([a,b,c] == [1,nil,[]])
115 a,b,*c = [nil]; test_ok([a,b,c] == [nil,nil,[]])
116 a,b,*c = [[]]; test_ok([a,b,c] == [[],nil,[]])
117 a,b,*c = [1,2]; test_ok([a,b,c] == [1,2,[]])
118 a,b,*c = [*[]]; test_ok([a,b,c] == [nil,nil,[]])
119 a,b,*c = [*[1]]; test_ok([a,b,c] == [1,nil,[]])
120 a,b,*c = [*[1,2]]; test_ok([a,b,c] == [1,2,[]])
122 a,b,*c = *[]; test_ok([a,b,c] == [nil,nil,[]])
123 a,b,*c = *[1]; test_ok([a,b,c] == [1,nil,[]])
124 a,b,*c = *[nil]; test_ok([a,b,c] == [nil,nil,[]])
125 a,b,*c = *[[]]; test_ok([a,b,c] == [[],nil,[]])
126 a,b,*c = *[1,2]; test_ok([a,b,c] == [1,2,[]])
127 a,b,*c = *[*[]]; test_ok([a,b,c] == [nil,nil,[]])
128 a,b,*c = *[*[1]]; test_ok([a,b,c] == [1,nil,[]])
129 a,b,*c = *[*[1,2]]; test_ok([a,b,c] == [1,2,[]])
131 def f; yield nil; end; f {|a| test_ok(a == nil)}
132 def f; yield 1; end; f {|a| test_ok(a == 1)}
133 def f; yield []; end; f {|a| test_ok(a == [])}
134 def f; yield [1]; end; f {|a| test_ok(a == [1])}
135 def f; yield [nil]; end; f {|a| test_ok(a == [nil])}
136 def f; yield [[]]; end; f {|a| test_ok(a == [[]])}
137 def f; yield [*[]]; end; f {|a| test_ok(a == [])}
138 def f; yield [*[1]]; end; f {|a| test_ok(a == [1])}
139 def f; yield [*[1,2]]; end; f {|a| test_ok(a == [1,2])}
140 def f; yield *[]; end; f {|a| test_ok(a == nil)}
141 def f; yield *[1]; end; f {|a| test_ok(a == 1)}
142 def f; yield *[nil]; end; f {|a| test_ok(a == nil)}
143 def f; yield *[[]]; end; f {|a| test_ok(a == [])}
144 def f; yield *[*[]]; end; f {|a| test_ok(a == nil)}
145 def f; yield *[*[1]]; end; f {|a| test_ok(a == 1)}
146 def f; yield *[*[1,2]]; end; f {|a| test_ok(a == 1)}
148 def f; yield; end; f {|a,| test_ok(a == nil)}
149 def f; yield nil; end; f {|a,| test_ok(a == nil)}
150 def f; yield 1; end; f {|a,| test_ok(a == 1)}
151 def f; yield []; end; f {|a,| test_ok(a == nil)}
152 def f; yield [1]; end; f {|a,| test_ok(a == 1)}
153 def f; yield [nil]; end; f {|a,| test_ok(a == nil)}
154 def f; yield [[]]; end; f {|a,| test_ok(a == [])}
155 def f; yield [*[]]; end; f {|a,| test_ok(a == nil)}
156 def f; yield [*[1]]; end; f {|a,| test_ok(a == 1)}
157 def f; yield [*[1,2]]; end; f {|a,| test_ok(a == 1)}
159 def f; yield *[]; end; f {|a,| test_ok(a == nil)}
160 def f; yield *[1]; end; f {|a,| test_ok(a == 1)}
161 def f; yield *[nil]; end; f {|a,| test_ok(a == nil)}
162 def f; yield *[[]]; end; f {|a,| test_ok(a == nil)}
163 def f; yield *[*[]]; end; f {|a,| test_ok(a == nil)}
164 def f; yield *[*[1]]; end; f {|a,| test_ok(a == 1)}
165 def f; yield *[*[1,2]]; end; f {|a,| test_ok(a == 1)}
167 def f; yield; end; f {|*a| test_ok(a == [])}
168 def f; yield nil; end; f {|*a| test_ok(a == [nil])}
169 def f; yield 1; end; f {|*a| test_ok(a == [1])}
170 def f; yield []; end; f {|*a| test_ok(a == [[]])}
171 def f; yield [1]; end; f {|*a| test_ok(a == [[1]])}
172 def f; yield [nil]; end; f {|*a| test_ok(a == [[nil]])}
173 def f; yield [[]]; end; f {|*a| test_ok(a == [[[]]])}
174 def f; yield [1,2]; end; f {|*a| test_ok(a == [[1,2]])}
175 def f; yield [*[]]; end; f {|*a| test_ok(a == [[]])}
176 def f; yield [*[1]]; end; f {|*a| test_ok(a == [[1]])}
177 def f; yield [*[1,2]]; end; f {|*a| test_ok(a == [[1,2]])}
179 def f; yield *[]; end; f {|*a| test_ok(a == [])}
180 def f; yield *[1]; end; f {|*a| test_ok(a == [1])}
181 def f; yield *[nil]; end; f {|*a| test_ok(a == [nil])}
182 def f; yield *[[]]; end; f {|*a| test_ok(a == [[]])}
183 def f; yield *[*[]]; end; f {|*a| test_ok(a == [])}
184 def f; yield *[*[1]]; end; f {|*a| test_ok(a == [1])}
185 def f; yield *[*[1,2]]; end; f {|*a| test_ok(a == [1,2])}
187 def f; yield; end; f {|a,b,*c| test_ok([a,b,c] == [nil,nil,[]])}
188 def f; yield nil; end; f {|a,b,*c| test_ok([a,b,c] == [nil,nil,[]])}
189 def f; yield 1; end; f {|a,b,*c| test_ok([a,b,c] == [1,nil,[]])}
190 def f; yield []; end; f {|a,b,*c| test_ok([a,b,c] == [nil,nil,[]])}
191 def f; yield [1]; end; f {|a,b,*c| test_ok([a,b,c] == [1,nil,[]])}
192 def f; yield [nil]; end; f {|a,b,*c| test_ok([a,b,c] == [nil,nil,[]])}
193 def f; yield [[]]; end; f {|a,b,*c| test_ok([a,b,c] == [[],nil,[]])}
194 def f; yield [*[]]; end; f {|a,b,*c| test_ok([a,b,c] == [nil,nil,[]])}
195 def f; yield [*[1]]; end; f {|a,b,*c| test_ok([a,b,c] == [1,nil,[]])}
196 def f; yield [*[1,2]]; end; f {|a,b,*c| test_ok([a,b,c] == [1,2,[]])}
198 def f; yield *[]; end; f {|a,b,*c| test_ok([a,b,c] == [nil,nil,[]])}
199 def f; yield *[1]; end; f {|a,b,*c| test_ok([a,b,c] == [1,nil,[]])}
200 def f; yield *[nil]; end; f {|a,b,*c| test_ok([a,b,c] == [nil,nil,[]])}
201 def f; yield *[[]]; end; f {|a,b,*c| test_ok([a,b,c] == [nil,nil,[]])}
202 def f; yield *[*[]]; end; f {|a,b,*c| test_ok([a,b,c] == [nil,nil,[]])}
203 def f; yield *[*[1]]; end; f {|a,b,*c| test_ok([a,b,c] == [1,nil,[]])}
204 def f; yield *[*[1,2]]; end; f {|a,b,*c| test_ok([a,b,c] == [1,2,[]])}
206 def r; return; end; a = r(); test_ok(a == nil)
207 def r; return nil; end; a = r(); test_ok(a == nil)
208 def r; return 1; end; a = r(); test_ok(a == 1)
209 def r; return []; end; a = r(); test_ok(a == [])
210 def r; return [1]; end; a = r(); test_ok(a == [1])
211 def r; return [nil]; end; a = r(); test_ok(a == [nil])
212 def r; return [[]]; end; a = r(); test_ok(a == [[]])
213 def r; return [*[]]; end; a = r(); test_ok(a == [])
214 def r; return [*[1]]; end; a = r(); test_ok(a == [1])
215 def r; return [*[1,2]]; end; a = r(); test_ok(a == [1,2])
217 def r; return *[]; end; a = r(); test_ok(a == [])
218 def r; return *[1]; end; a = r(); test_ok(a == [1])
219 def r; return *[nil]; end; a = r(); test_ok(a == [nil])
220 def r; return *[[]]; end; a = r(); test_ok(a == [[]])
221 def r; return *[*[]]; end; a = r(); test_ok(a == [])
222 def r; return *[*[1]]; end; a = r(); test_ok(a == [1])
223 def r; return *[*[1,2]]; end; a = r(); test_ok(a == [1,2])
225 def r; return *[[]]; end; a = *r(); test_ok(a == [[]])
226 def r; return *[*[1,2]]; end; a = *r(); test_ok(a == [1,2])
228 def r; return; end; *a = r(); test_ok(a == [nil])
229 def r; return nil; end; *a = r(); test_ok(a == [nil])
230 def r; return 1; end; *a = r(); test_ok(a == [1])
231 def r; return []; end; *a = r(); test_ok(a == [])
232 def r; return [1]; end; *a = r(); test_ok(a == [1])
233 def r; return [nil]; end; *a = r(); test_ok(a == [nil])
234 def r; return [[]]; end; *a = r(); test_ok(a == [[]])
235 def r; return [1,2]; end; *a = r(); test_ok(a == [1,2])
236 def r; return [*[]]; end; *a = r(); test_ok(a == [])
237 def r; return [*[1]]; end; *a = r(); test_ok(a == [1])
238 def r; return [*[1,2]]; end; *a = r(); test_ok(a == [1,2])
240 def r; return *[]; end; *a = r(); test_ok(a == [])
241 def r; return *[1]; end; *a = r(); test_ok(a == [1])
242 def r; return *[nil]; end; *a = r(); test_ok(a == [nil])
243 def r; return *[[]]; end; *a = r(); test_ok(a == [[]])
244 def r; return *[1,2]; end; *a = r(); test_ok(a == [1,2])
245 def r; return *[*[]]; end; *a = r(); test_ok(a == [])
246 def r; return *[*[1]]; end; *a = r(); test_ok(a == [1])
247 def r; return *[*[1,2]]; end; *a = r(); test_ok(a == [1,2])
249 def r; return *[[]]; end; *a = *r(); test_ok(a == [[]])
250 def r; return *[1,2]; end; *a = *r(); test_ok(a == [1,2])
251 def r; return *[*[1,2]]; end; *a = *r(); test_ok(a == [1,2])
253 def r; return; end; a,b,*c = r(); test_ok([a,b,c] == [nil,nil,[]])
254 def r; return nil; end; a,b,*c = r(); test_ok([a,b,c] == [nil,nil,[]])
255 def r; return 1; end; a,b,*c = r(); test_ok([a,b,c] == [1,nil,[]])
256 def r; return []; end; a,b,*c = r(); test_ok([a,b,c] == [nil,nil,[]])
257 def r; return [1]; end; a,b,*c = r(); test_ok([a,b,c] == [1,nil,[]])
258 def r; return [nil]; end; a,b,*c = r(); test_ok([a,b,c] == [nil,nil,[]])
259 def r; return [[]]; end; a,b,*c = r(); test_ok([a,b,c] == [[],nil,[]])
260 def r; return [1,2]; end; a,b,*c = r(); test_ok([a,b,c] == [1,2,[]])
261 def r; return [*[]]; end; a,b,*c = r(); test_ok([a,b,c] == [nil,nil,[]])
262 def r; return [*[1]]; end; a,b,*c = r(); test_ok([a,b,c] == [1,nil,[]])
263 def r; return [*[1,2]]; end; a,b,*c = r(); test_ok([a,b,c] == [1,2,[]])
265 def r; return *[]; end; a,b,*c = r(); test_ok([a,b,c] == [nil,nil,[]])
266 def r; return *[1]; end; a,b,*c = r(); test_ok([a,b,c] == [1,nil,[]])
267 def r; return *[nil]; end; a,b,*c = r(); test_ok([a,b,c] == [nil,nil,[]])
268 def r; return *[[]]; end; a,b,*c = r(); test_ok([a,b,c] == [[],nil,[]])
269 def r; return *[1,2]; end; a,b,*c = r(); test_ok([a,b,c] == [1,2,[]])
270 def r; return *[*[]]; end; a,b,*c = r(); test_ok([a,b,c] == [nil,nil,[]])
271 def r; return *[*[1]]; end; a,b,*c = r(); test_ok([a,b,c] == [1,nil,[]])
272 def r; return *[*[1,2]]; end; a,b,*c = r(); test_ok([a,b,c] == [1,2,[]])
274 f = lambda {|r,| test_ok([] == r)}
277 f = lambda {|r,*l| test_ok([] == r); test_ok([1] == l)}
281 test_ok(f.call(42) == 42)
282 test_ok(f.call([42]) == [42])
283 test_ok(f.call([[42]]) == [[42]])
284 test_ok(f.call([42,55]) == [42,55])
287 test_ok(f.call(42) == 42)
288 test_ok(f.call([42]) == [42])
289 test_ok(f.call([[42]]) == [[42]])
290 test_ok(f.call([42,55]) == [42,55])
293 test_ok(f.call(42) == [42])
294 test_ok(f.call([42]) == [[42]])
295 test_ok(f.call([[42]]) == [[[42]]])
296 test_ok(f.call([42,55]) == [[42,55]])
297 test_ok(f.call(42,55) == [42,55])
307 test_ok([1,2,nil] == [x,y,z])
309 test_ok([1,2,3] == [x,y,z])
311 test_ok([1,2,nil] == [x,y,z])
313 a = loop do break; end; test_ok(a == nil)
314 a = loop do break nil; end; test_ok(a == nil)
315 a = loop do break 1; end; test_ok(a == 1)
316 a = loop do break []; end; test_ok(a == [])
317 a = loop do break [1]; end; test_ok(a == [1])
318 a = loop do break [nil]; end; test_ok(a == [nil])
319 a = loop do break [[]]; end; test_ok(a == [[]])
320 a = loop do break [*[]]; end; test_ok(a == [])
321 a = loop do break [*[1]]; end; test_ok(a == [1])
322 a = loop do break [*[1,2]]; end; test_ok(a == [1,2])
324 a = loop do break *[]; end; test_ok(a == [])
325 a = loop do break *[1]; end; test_ok(a == [1])
326 a = loop do break *[nil]; end; test_ok(a == [nil])
327 a = loop do break *[[]]; end; test_ok(a == [[]])
328 a = loop do break *[*[]]; end; test_ok(a == [])
329 a = loop do break *[*[1]]; end; test_ok(a == [1])
330 a = loop do break *[*[1,2]]; end; test_ok(a == [1,2])
332 *a = loop do break; end; test_ok(a == [nil])
333 *a = loop do break nil; end; test_ok(a == [nil])
334 *a = loop do break 1; end; test_ok(a == [1])
335 *a = loop do break []; end; test_ok(a == [])
336 *a = loop do break [1]; end; test_ok(a == [1])
337 *a = loop do break [nil]; end; test_ok(a == [nil])
338 *a = loop do break [[]]; end; test_ok(a == [[]])
339 *a = loop do break [1,2]; end; test_ok(a == [1,2])
340 *a = loop do break [*[]]; end; test_ok(a == [])
341 *a = loop do break [*[1]]; end; test_ok(a == [1])
342 *a = loop do break [*[1,2]]; end; test_ok(a == [1,2])
344 *a = loop do break *[]; end; test_ok(a == [])
345 *a = loop do break *[1]; end; test_ok(a == [1])
346 *a = loop do break *[nil]; end; test_ok(a == [nil])
347 *a = loop do break *[[]]; end; test_ok(a == [[]])
348 *a = loop do break *[1,2]; end; test_ok(a == [1,2])
349 *a = loop do break *[*[]]; end; test_ok(a == [])
350 *a = loop do break *[*[1]]; end; test_ok(a == [1])
351 *a = loop do break *[*[1,2]]; end; test_ok(a == [1,2])
353 *a = *loop do break *[[]]; end; test_ok(a == [[]])
354 *a = *loop do break *[1,2]; end; test_ok(a == [1,2])
355 *a = *loop do break *[*[1,2]]; end; test_ok(a == [1,2])
357 a,b,*c = loop do break; end; test_ok([a,b,c] == [nil,nil,[]])
358 a,b,*c = loop do break nil; end; test_ok([a,b,c] == [nil,nil,[]])
359 a,b,*c = loop do break 1; end; test_ok([a,b,c] == [1,nil,[]])
360 a,b,*c = loop do break []; end; test_ok([a,b,c] == [nil,nil,[]])
361 a,b,*c = loop do break [1]; end; test_ok([a,b,c] == [1,nil,[]])
362 a,b,*c = loop do break [nil]; end; test_ok([a,b,c] == [nil,nil,[]])
363 a,b,*c = loop do break [[]]; end; test_ok([a,b,c] == [[],nil,[]])
364 a,b,*c = loop do break [1,2]; end; test_ok([a,b,c] == [1,2,[]])
365 a,b,*c = loop do break [*[]]; end; test_ok([a,b,c] == [nil,nil,[]])
366 a,b,*c = loop do break [*[1]]; end; test_ok([a,b,c] == [1,nil,[]])
367 a,b,*c = loop do break [*[1,2]]; end; test_ok([a,b,c] == [1,2,[]])
369 a,b,*c = loop do break *[]; end; test_ok([a,b,c] == [nil,nil,[]])
370 a,b,*c = loop do break *[1]; end; test_ok([a,b,c] == [1,nil,[]])
371 a,b,*c = loop do break *[nil]; end; test_ok([a,b,c] == [nil,nil,[]])
372 a,b,*c = loop do break *[[]]; end; test_ok([a,b,c] == [[],nil,[]])
373 a,b,*c = loop do break *[1,2]; end; test_ok([a,b,c] == [1,2,[]])
374 a,b,*c = loop do break *[*[]]; end; test_ok([a,b,c] == [nil,nil,[]])
375 a,b,*c = loop do break *[*[1]]; end; test_ok([a,b,c] == [1,nil,[]])
376 a,b,*c = loop do break *[*[1,2]]; end; test_ok([a,b,c] == [1,2,[]])
378 def r(val); a = yield(); test_ok(a == val, 2); end
388 r([1,2]){next [*[1,2]]}
392 r([nil]){next *[nil]}
396 r([1,2]){next *[*[1,2]]}
398 def r(val); *a = yield(); test_ok(a == val, 2); end
409 r([1,2]){next [*[1,2]]}
411 def r(val); *a = *yield(); test_ok(a == val, 2); end
413 r([1,2]){next *[1,2]}
414 r([1,2]){next *[*[1,2]]}
416 def r(val); a,b,*c = yield(); test_ok([a,b,c] == val, 2); end
417 r([nil,nil,[]]){next}
418 r([nil,nil,[]]){next nil}
419 r([1,nil,[]]){next 1}
420 r([nil,nil,[]]){next []}
421 r([1,nil,[]]){next [1]}
422 r([nil,nil,[]]){next [nil]}
423 r([[],nil,[]]){next [[]]}
424 r([1,2,[]]){next [1,2]}
425 r([nil,nil,[]]){next [*[]]}
426 r([1,nil,[]]){next [*[1]]}
427 r([1,2,[]]){next [*[1,2]]}
429 def r(val); a,b,*c = *yield(); test_ok([a,b,c] == val, 2); end
430 r([[],nil,[]]){next *[[]]}
431 r([1,2,[]]){next *[1,2]}
432 r([1,2,[]]){next *[*[1,2]]}
434 test_check "condition"
438 $x == $x && test_ok(true)
439 $x != $x && test_ok(false)
440 $x == $x || test_ok(false)
441 $x != $x || test_ok(true)
443 # first test to see if we can run the tests.
445 test_check "if/unless";
448 test_ok(if $x == $x then true else false end)
454 test_ok(unless $x != $x then true else false end)
459 when 1, 2, 3, 4, 6, 7, 8
493 test_check "while/until";
495 tmp = open("while_tmp", "w")
496 tmp.print "tvi925\n";
497 tmp.print "tvi920\n";
505 tmp = open("while_tmp", "r")
506 test_ok(tmp.kind_of?(File))
508 while line = tmp.gets()
509 break if /vt100/ =~ line
512 test_ok(!tmp.eof? && /vt100/ =~ line)
517 tmp = open("while_tmp", "r")
518 while line = tmp.gets()
519 next if /vt100/ =~ line
520 $bad = 1 if /vt100/ =~ line
522 test_ok(!(!tmp.eof? || /vt100/ =~ line || $bad))
527 tmp = open("while_tmp", "r")
528 while line = tmp.gets()
530 line = line.gsub(/vt100/, 'VT100')
532 line.gsub!('VT100', 'Vt100')
535 $bad = 1 if /vt100/ =~ line
536 $bad = 1 if /VT100/ =~ line
538 test_ok(tmp.eof? && !$bad)
553 tmp = open("while_tmp", "r")
554 while line = tmp.gets()
557 when /vt100/, /Amiga/, /paper/
564 File.unlink "while_tmp" or `/bin/rm -f "while_tmp"`
565 test_ok(!File.exist?("while_tmp"))
575 test_check "exception";
578 raise "this must be handled"
586 raise "this must be handled no.2"
596 # exception in rescue clause
597 $string = "this must be handled no.3"
600 raise "exception in rescue clause"
606 test_ok(true) if $! == $string
609 # exception in ensure clause
612 raise "this must be handled no.4"
614 raise "exception in ensure clause"
624 raise "this must be handled no.5"
635 raise "this must be handled no.6"
653 test_ok(catch(:foo) {
660 test_ok(false) # should no reach here
666 test_ok([1, 2] + [3, 4] == [1, 2, 3, 4])
667 test_ok([1, 2] * 2 == [1, 2, 1, 2])
668 test_ok([1, 2] * ":" == "1:2")
670 test_ok([1, 2].hash == [1, 2].hash)
672 test_ok([1,2,3] & [2,3,4] == [2,3])
673 test_ok([1,2,3] | [2,3,4] == [1,2,3,4])
674 test_ok([1,2,3] - [2,3] == [1])
676 $x = [0, 1, 2, 3, 4, 5]
678 test_ok($x[1..3] == [1, 2, 3])
679 test_ok($x[1,3] == [1, 2, 3])
682 test_ok($x[0] == 10 && $x[1] == 2)
685 test_ok($x[0] == -1 && $x[1] == 10)
688 test_ok($x[-1] == 20 && $x.pop == 20)
691 test_ok(([1,2,3]&[2,4,6]) == [2])
692 test_ok(([1,2,3]|[2,4,6]) == [1,2,3,4,6])
695 $x = [nil, 1, nil, nil, 5, nil, nil]
697 test_ok($x == [1, 5])
700 $x = [1, 1, 4, 2, 5, 4, 5, 1, 2]
702 test_ok($x == [1, 4, 2, 5])
710 $x = ["it", "came", "to", "pass", "that", "..."]
711 $x = $x.sort.join(" ")
712 test_ok($x == "... came it pass that to")
714 $x.sort!{|a,b| a<=>b} # sort with condition
715 test_ok($x == [1,2,3,5,7])
716 $x.sort!{|a,b| b-a} # reverse sort
717 test_ok($x == [7,5,3,2,1])
720 $x = "The Book of Mormon"
721 test_ok($x.split(//).reverse!.join == $x.reverse)
722 test_ok($x.reverse == $x.reverse!)
723 test_ok("1 byte string".split(//).reverse.join(":") == "g:n:i:r:t:s: :e:t:y:b: :1")
725 test_ok($x.split == ['a', 'b', 'c', 'd'])
726 test_ok($x.split(' ') == ['a', 'b', 'c', 'd'])
727 test_ok(defined? "a".chomp)
728 test_ok("abc".scan(/./) == ["a", "b", "c"])
729 test_ok("1a2b3c".scan(/(\d.)/) == [["1a"], ["2b"], ["3c"]])
731 test_ok("a=12;b=22".scan(/(.*?)=(\d*);?/) == [["a", "12"], ["b", "22"]])
734 test_ok(($x * 5).join(":") == '1:1:1:1:1')
735 test_ok(($x * 1).join(":") == '1')
736 test_ok(($x * 0).join(":") == '')
739 test_ok($x.size == 7)
740 test_ok($x == [1, 2, 3, 4, 5, 6, 7])
744 test_ok($x == [1,1,2,3,2,3])
748 test_ok($x == [1,2,1,2,3,3])
752 test_ok($x == [1,2,3,1,2,3])
755 $x = {1=>2, 2=>4, 3=>6}
768 test_ok($x.length == 3)
769 test_ok($x.has_key?(1))
770 test_ok($x.has_value?(4))
771 test_ok($x.values_at(2,3) == [4,6])
772 test_ok($x == {1=>2, 2=>4, 3=>6})
774 $z = $x.keys.sort.join(":")
775 test_ok($z == "1:2:3")
777 $z = $x.values.sort.join(":")
778 test_ok($z == "2:4:6")
782 test_ok($x.length == 2)
786 test_ok($x[$z] == 256)
794 test_ok($x[22] == [])
795 test_ok($x[22].equal?($x[22]))
798 test_ok($x[22] == [])
799 test_ok(!$x[22].equal?($x[22]))
801 $x = Hash.new{|h,k| $z = k; h[k] = k*2}
803 test_ok($x[22] == 44)
806 test_ok($x[22] == 44)
817 test_ok($x[22] == 44)
820 test_ok($x[22] == 44)
823 test_check "iterator"
833 test_ok(!defined?(yield))
838 # iterator over array
852 tt{|i| break if i == 5}
860 tt2(raise(ArgumentError,""),&block)
873 tt2(raise(ArgumentError,""),&block)
884 # iterator break/redo/next/retry
888 done = false # should not reach here
898 $bad = true # should not reach here
908 $bad = true # should not reach here
916 test_ok($x.size == 7)
917 test_ok($x == [1, 2, 3, 4, 5, 6, 7])
919 # append method to built-in class
922 collect{|e| [e, yield(e)]}.sort{|a,b|a[1]<=>b[1]}
925 a = collect{|e| [e, yield(e)]}
926 a.sort{|a,b|a[1]<=>b[1]}
929 $x = [[1,2],[3,4],[5,6]]
930 test_ok($x.iter_test1{|x|x} == $x.iter_test2{|x|x})
933 def initialize(e); @body = e; end
935 def each0(&block); @body.each(&block); end
936 def each1(&block); @body.each {|*x| block.call(*x) } end
937 def each2(&block); @body.each {|*x| block.call(x) } end
938 def each3(&block); @body.each {|x| block.call(*x) } end
939 def each4(&block); @body.each {|x| block.call(x) } end
940 def each5; @body.each {|*x| yield(*x) } end
941 def each6; @body.each {|*x| yield(x) } end
942 def each7; @body.each {|x| yield(*x) } end
943 def each8; @body.each {|x| yield(x) } end
949 test_ok(IterTest.new(nil).method(:f).to_proc.call([1]) == [1])
950 m = /\w+/.match("abc")
951 test_ok(IterTest.new(nil).method(:f).to_proc.call([m]) == [m])
953 IterTest.new([0]).each0 {|x| test_ok(x == 0)}
954 IterTest.new([1]).each1 {|x| test_ok(x == 1)}
955 IterTest.new([2]).each2 {|x| test_ok(x == [2])}
956 #IterTest.new([3]).each3 {|x| test_ok(x == 3)}
957 IterTest.new([4]).each4 {|x| test_ok(x == 4)}
958 IterTest.new([5]).each5 {|x| test_ok(x == 5)}
959 IterTest.new([6]).each6 {|x| test_ok(x == [6])}
960 #IterTest.new([7]).each7 {|x| test_ok(x == 7)}
961 IterTest.new([8]).each8 {|x| test_ok(x == 8)}
963 IterTest.new([[0]]).each0 {|x| test_ok(x == [0])}
964 IterTest.new([[1]]).each1 {|x| test_ok(x == [1])}
965 IterTest.new([[2]]).each2 {|x| test_ok(x == [[2]])}
966 IterTest.new([[3]]).each3 {|x| test_ok(x == 3)}
967 IterTest.new([[4]]).each4 {|x| test_ok(x == [4])}
968 IterTest.new([[5]]).each5 {|x| test_ok(x == [5])}
969 IterTest.new([[6]]).each6 {|x| test_ok(x == [[6]])}
970 IterTest.new([[7]]).each7 {|x| test_ok(x == 7)}
971 IterTest.new([[8]]).each8 {|x| test_ok(x == [8])}
973 IterTest.new([[0,0]]).each0 {|*x| test_ok(x == [[0,0]])}
974 IterTest.new([[8,8]]).each8 {|*x| test_ok(x == [[8,8]])}
983 test_ok(m1{p 'test'})
987 m0(block_given?,&Proc.new{})
989 test_ok(m1{p 'test'})
1002 test_ok(C.new.collect{|n| n} == [1,2,3])
1004 test_ok(Proc == lambda{}.class)
1005 test_ok(Proc == Proc.new{}.class)
1006 lambda{|a|test_ok(a==1)}.call(1)
1007 def block_test(klass, &block)
1008 test_ok(klass === block)
1011 block_test(NilClass)
1014 def call_argument_test(state, proc, *args)
1018 rescue ArgumentError
1024 call_argument_test(true, lambda{||})
1025 call_argument_test(false, lambda{||}, 1)
1026 call_argument_test(true, lambda{|a,|}, 1)
1027 call_argument_test(false, lambda{|a,|})
1028 call_argument_test(false, lambda{|a,|}, 1,2)
1030 call_argument_test(true, Proc.new{||})
1031 call_argument_test(true, Proc.new{||}, 1)
1032 call_argument_test(true, Proc.new{|a,|}, 1)
1033 call_argument_test(true, Proc.new{|a,|})
1034 call_argument_test(true, Proc.new{|a,|}, 1,2)
1036 def block_get(&block)
1040 test_ok(Proc == block_get{}.class)
1041 call_argument_test(true, block_get{||})
1042 call_argument_test(true, block_get{||}, 1)
1043 call_argument_test(true, block_get{|a,|}, 1)
1044 call_argument_test(true, block_get{|a,|})
1045 call_argument_test(true, block_get{|a,|}, 1,2)
1047 call_argument_test(true, block_get(&lambda{||}))
1048 call_argument_test(false, block_get(&lambda{||}),1)
1049 call_argument_test(true, block_get(&lambda{|a,|}),1)
1050 call_argument_test(false, block_get(&lambda{|a,|}),1,2)
1053 test_ok(blk.class == Proc)
1054 test_ok(blk.to_proc.class == Proc)
1055 test_ok(blk.clone.call == 11)
1056 test_ok(block_get(&blk).class == Proc)
1059 test_ok(lmd.class == Proc)
1060 test_ok(lmd.to_proc.class == Proc)
1061 test_ok(lmd.clone.call == 44)
1062 test_ok(block_get(&lmd).class == Proc)
1064 test_ok(Proc.new{|a,| a}.yield(1,2,3) == 1)
1065 call_argument_test(true, Proc.new{|a,|}, 1,2)
1067 test_ok(Proc.new{|&b| b.call(10)}.call {|x| x} == 10)
1068 test_ok(Proc.new{|a,&b| b.call(a)}.call(12) {|x| x} == 12)
1075 test_ok(test_return1() == 55)
1081 test_ok(test_return2() == 60)
1090 lambda{return 42}.call+1
1092 test_ok(proc_return1() == 43)
1094 ->{return 42}.call+1
1096 test_ok(proc_return2() == 43)
1098 proc_call{return 42}+1
1100 test_ok(proc_return3() == 42)
1102 proc_yield{return 42}+1
1104 test_ok(proc_return4() == 42)
1106 def ljump_test(state, proc, *args)
1110 rescue LocalJumpError
1116 ljump_test(false, block_get{break})
1117 ljump_test(true, lambda{break})
1119 def exit_value_test(&block)
1121 rescue LocalJumpError
1125 test_ok(45 == exit_value_test{break 45})
1128 block_get{break 55}.call
1129 rescue LocalJumpError
1133 def block_call(&block)
1138 block_call{break 11}
1140 test_ok(test_b1() == 11)
1145 rescue LocalJumpError => e
1146 r if /from proc-closure/ =~ e.message
1152 block_get{break 21}.call
1155 test_ok(test_b2() == 22)
1159 Proc.new{break 31}.yield
1162 test_ok(test_b3() == 33)
1165 lambda{break 44}.call
1167 test_ok(test_b4() == 44)
1171 b = block_get{break 54}
1175 test_ok(test_b5() == 55)
1178 b = lambda{break 67}
1182 test_ok(test_b6() == 66)
1194 test_ok(test_b7() == 77)
1203 test_ok(test_b8() == 88)
1206 lambda{block.call; 98}.call
1212 test_ok(test_b9() == 99)
1221 test_ok(test_b10() == 100)
1224 ljump_rescue(111) do
1226 Proc.new{break 110}.yield
1231 test_ok(test_b11() == 111)
1235 break lambda{break 122}.call
1239 test_ok(test_b12() == 122)
1242 ljump_rescue(133) do
1244 Proc.new{break 130}.yield
1249 test_ok(test_b13() == 133)
1253 break lambda{break 144}.call
1257 test_ok(test_b14() == 144)
1260 [0].each {|c| yield 1 }
1263 test_ok(test_b15{|e| break 155 } == 155)
1267 test_ok(method.arity == method.to_proc.arity, 2)
1269 marity_test(:test_ok)
1270 marity_test(:marity_test)
1273 lambda(&method(:test_ok)).call(true)
1274 lambda(&block_get{|a,n| test_ok(a,n)}).call(true, 2)
1282 class ITER_TEST2 < ITER_TEST1
1288 test_ok(ITER_TEST2.new.a {})
1292 return yield if block_given?
1297 class ITER_TEST4 < ITER_TEST3
1299 test_ok(super == yield)
1300 test_ok(super(x, &nil) == x)
1304 ITER_TEST4.new.foo(44){55}
1313 define_method(:tt) do |sym|
1326 test_ok(a.tt(1) == 1)
1328 class ITER_TEST6 < ITER_TEST5
1335 test_ok(ITER_TEST6.new.xx([24]) == 2)
1338 test_ok(2.6.floor == 2)
1339 test_ok((-2.6).floor == -3)
1340 test_ok(2.6.ceil == 3)
1341 test_ok((-2.6).ceil == -2)
1342 test_ok(2.6.truncate == 2)
1343 test_ok((-2.6).truncate == -2)
1344 test_ok(2.6.round == 3)
1345 test_ok((-2.4).truncate == -2)
1346 test_ok((13.4 % 1 - 0.4).abs < 0.0001)
1350 test_ok((x < y) == false)
1351 test_ok((x > y) == false)
1352 test_ok((x <= y) == false)
1353 test_ok((x >= y) == false)
1360 nan_test(nan, -1000)
1361 nan_test(nan, 1_000_000_000_000)
1362 nan_test(nan, -1_000_000_000_000)
1363 nan_test(nan, 100.0);
1364 nan_test(nan, -100.0);
1365 nan_test(nan, 0.001);
1366 nan_test(nan, -0.001);
1367 nan_test(nan, 1.0/0);
1368 nan_test(nan, -1.0/0);
1370 #s = "3.7517675036461267e+17"
1371 #test_ok(s == sprintf("%.16e", s.to_f))
1372 f = 3.7517675036461267e+17
1373 test_ok(f == sprintf("%.16e", f).to_f)
1388 test_ok($x == fact(40))
1391 test_ok($x == 815915283247897734345611269596115894272000000000)
1392 test_ok($x != 815915283247897734345611269596115894272000000001)
1393 test_ok($x+1 == 815915283247897734345611269596115894272000000001)
1394 test_ok($x/fact(20) == 335367096786357081410764800000)
1396 test_ok($x == -815915283247897734345611269596115894272000000000)
1397 test_ok(2-(2**32) == -(2**32-2))
1398 test_ok(2**32 - 5 == (2**32-3)-2)
1402 $good = false if ((1 << i) != (2**i))
1409 $good = false if ((1 << i) != n1)
1419 $good = false if (n1 != n2)
1426 if (n1**2-1) / (n1+1) != (n1-1)
1434 test_ok(7 == a.modulo(b))
1435 test_ok(-b + 7 == a.modulo(-b))
1436 test_ok(b + -7 == (-a).modulo(b))
1437 test_ok(-7 == (-a).modulo(-b))
1438 test_ok(7 == a.remainder(b))
1439 test_ok(7 == a.remainder(-b))
1440 test_ok(-7 == (-a).remainder(b))
1441 test_ok(-7 == (-a).remainder(-b))
1443 test_ok(10**40+10**20 == 10000000000000000000100000000000000000000)
1444 test_ok(10**40/10**20 == 100000000000000000000)
1446 a = 677330545177305025495135714080
1447 b = 14269972710765292560
1449 test_ok(-a % b == 0)
1461 shift_test(-4518325415524767873)
1462 shift_test(-0xfffffffffffffffff)
1464 test_check "string & char"
1466 test_ok("abcd" == "abcd")
1467 test_ok("abcd" =~ /abcd/)
1468 test_ok("abcd" === "abcd")
1469 # compile time string concatenation
1470 test_ok("ab" "cd" == "abcd")
1471 test_ok("#{22}aa" "cd#{44}" == "22aacd44")
1472 test_ok("#{22}aa" "cd#{44}" "55" "#{66}" == "22aacd445566")
1473 test_ok("abc" !~ /^$/)
1474 test_ok("abc\n" !~ /^$/)
1475 test_ok("abc" !~ /^d*$/)
1476 test_ok(("abc" =~ /d*$/) == 3)
1478 test_ok("\n" =~ /^$/)
1479 test_ok("a\n\n" =~ /^$/)
1480 test_ok("abcabc" =~ /.*a/ && $& == "abca")
1481 test_ok("abcabc" =~ /.*c/ && $& == "abcabc")
1482 test_ok("abcabc" =~ /.*?a/ && $& == "a")
1483 test_ok("abcabc" =~ /.*?c/ && $& == "abc")
1484 test_ok(/(.|\n)*?\n(b|\n)/ =~ "a\nb\n\n" && $& == "a\nb")
1486 test_ok(/^(ab+)+b/ =~ "ababb" && $& == "ababb")
1487 test_ok(/^(?:ab+)+b/ =~ "ababb" && $& == "ababb")
1488 test_ok(/^(ab+)+/ =~ "ababb" && $& == "ababb")
1489 test_ok(/^(?:ab+)+/ =~ "ababb" && $& == "ababb")
1491 test_ok(/(\s+\d+){2}/ =~ " 1 2" && $& == " 1 2")
1492 test_ok(/(?:\s+\d+){2}/ =~ " 1 2" && $& == " 1 2")
1498 $x.gsub!(/((.|\n)*?)B((.|\n)*?)D/, '\1\3')
1499 test_ok($x == "AC\nAC\n")
1501 test_ok("foobar" =~ /foo(?=(bar)|(baz))/)
1502 test_ok("foobaz" =~ /foo(?=(bar)|(baz))/)
1505 test_ok("#$foo = abc" == "abc = abc")
1506 test_ok("#{$foo} = abc" == "abc = abc")
1509 test_ok("#{foo} = abc" == "abc = abc")
1511 test_ok('-' * 5 == '-----')
1512 test_ok('-' * 1 == '-')
1513 test_ok('-' * 0 == '')
1516 test_ok(foo * 5 == '-----')
1517 test_ok(foo * 1 == '-')
1518 test_ok(foo * 0 == '')
1521 test_ok($x.sub(/.*\.([^\.]+)$/, '\1') == "gif")
1522 test_ok($x.sub(/.*\.([^\.]+)$/, 'b.\1') == "b.gif")
1523 test_ok($x.sub(/.*\.([^\.]+)$/, '\2') == "")
1524 test_ok($x.sub(/.*\.([^\.]+)$/, 'a\2b') == "ab")
1525 test_ok($x.sub(/.*\.([^\.]+)$/, '<\&>') == "<a.gif>")
1527 # character constants(assumes ASCII)
1528 test_ok("a"[0] == ?a)
1530 test_ok(?\C-a == "\1")
1531 test_ok(?\M-a == "\341")
1532 test_ok(?\M-\C-a == "\201")
1533 test_ok("a".upcase![0] == ?A)
1534 test_ok("A".downcase![0] == ?a)
1535 test_ok("abc".tr!("a-z", "A-Z") == "ABC")
1536 test_ok("aabbcccc".tr_s!("a-z", "A-Z") == "ABC")
1537 test_ok("abcc".squeeze!("a-z") == "abc")
1538 test_ok("abcd".delete!("bc") == "ad")
1541 $y = [ ?a, ?b, ?c, ?d, ?e, ?f ]
1544 if i.chr != $y.shift
1552 s[0..s.size]="another string"
1553 test_ok(s == "another string")
1560 test_ok(s == "1,2,3\n")
1561 test_ok("Just".to_i(36) == 926381)
1562 test_ok("-another".to_i(36) == -23200231779)
1563 test_ok(1299022.to_s(36) == "ruby")
1564 test_ok(-1045307475.to_s(36) == "-hacker")
1565 test_ok("Just_another_Ruby_hacker".to_i(36) == 265419172580680477752431643787347)
1566 test_ok(-265419172580680477752431643787347.to_s(36) == "-justanotherrubyhacker")
1571 a.push ch if /a#{Regexp.quote ch}b/x =~ "ab"
1573 test_ok(a.size == 0)
1575 test_check "assignment"
1577 test_ok(defined?(a))
1580 # multiple asignment
1582 test_ok(a == 1 && b == 2)
1585 test_ok(a == 2 && b == 1)
1591 test_ok(a == 1 && b == [2, 3])
1593 a, (b, c), d = 1, [2, 3], 4
1594 test_ok(a == 1 && b == 2 && c == 3 && d == 4)
1597 test_ok(a == [1, 2, 3])
1606 def aaa(a, b=100, *rest)
1612 # not enough argument
1614 aaa() # need at least 1 arg
1621 aaa # no arg given (exception raised)
1627 test_ok(aaa(1) == [1, 100])
1628 test_ok(aaa(1, 2) == [1, 2])
1629 test_ok(aaa(1, 2, 3, 4) == [1, 2, 3, 4])
1630 test_ok(aaa(1, *[2, 3, 4]) == [1, 2, 3, 4])
1633 $proc = Proc.new{|i| i}
1634 test_ok($proc.call(2) == 2)
1635 test_ok($proc.call(3) == 3)
1637 $proc = Proc.new{|i| i*2}
1638 test_ok($proc.call(2) == 4)
1639 test_ok($proc.call(3) == 6)
1642 iii=5 # nested local variable
1643 $proc = Proc.new{|i|
1647 $x = iii # nested variables shared by procs
1649 # scope of nested variables
1650 test_ok(defined?(iii))
1652 test_ok(!defined?(iii)) # out of scope
1654 loop{iii=5; test_ok(eval("defined? iii")); break}
1659 test_ok(!defined?(iii))
1671 if defined? Process.kill
1675 trap "SIGINT", Proc.new{|sig| $x = 2}
1676 Process.kill "SIGINT", $$
1683 trap "SIGINT", Proc.new{raise "Interrupt"}
1687 Process.kill "SIGINT", $$
1692 test_ok(x && /Interrupt/ =~ x.message)
1696 test_ok(eval("") == nil)
1698 eval 'while false; $bad = true; print "foo\n" end'
1701 test_ok(eval('TRUE'))
1702 test_ok(eval('true'))
1703 test_ok(!eval('NIL'))
1704 test_ok(!eval('nil'))
1705 test_ok(!eval('FALSE'))
1706 test_ok(!eval('false'))
1708 $foo = 'test_ok(true)'
1715 test_ok(eval("$foo") == 'test_ok(true)')
1716 test_ok(eval("true") == true)
1718 test_ok(eval("i == 5"))
1719 test_ok(eval("i") == 5)
1720 test_ok(eval("defined? i"))
1732 test_ok(eval("local1", $x) == "local1") # normal local var
1733 test_ok(eval("local2", $x) == "local2") # nested local var
1737 rescue NameError # must raise error
1747 test_ok(eval("EVTEST1", $x) == 25) # constant in module
1748 test_ok(eval("evtest2", $x) == 125) # local var in module
1752 rescue NameError # must raise error
1757 x = binding #! YARV Limitation: Proc.new{}
1759 test_ok(eval("i4", x) == 1)
1760 x = Proc.new{binding}.call #! YARV Limitation: Proc.new{Proc.new{}}.call
1762 test_ok(eval("i4", x) == 22)
1764 x = Proc.new{binding}.call #! YARV Limitation: Proc.new{Proc.new{}}.call
1765 eval "(0..9).each{|i5| $x[i5] = Proc.new{i5*2}}", x
1766 test_ok($x[4].call == 8)
1770 test_ok(eval("i", x) == 1)
1771 x = Proc.new{binding}.call
1773 test_ok(eval("i", x) == 22)
1775 x = Proc.new{binding}.call
1776 eval "(0..9).each{|i5| $x[i5] = Proc.new{i5*2}}", x
1777 test_ok($x[4].call == 8)
1778 x = Proc.new{binding}.call
1779 eval "for i6 in 1..1; j6=i6; end", x
1780 test_ok(eval("defined? i6", x))
1781 test_ok(eval("defined? j6", x))
1787 Proc.new{foo11=22}.call
1788 Proc.new{foo22=55}.call
1789 test_ok(eval("foo11", p) == eval("foo11"))
1790 test_ok(eval("foo11") == 1)
1791 test_ok(eval("foo22", p) == eval("foo22"))
1792 test_ok(eval("foo22") == 55)
1793 }.call if false #! YARV Limitation
1795 #! YARV Limitation: p1 = Proc.new{i7 = 0; Proc.new{i7}}.call
1796 p1 = Proc.new{i7 = 0; binding}.call
1797 #! YARV Limitation: test_ok(p1.call == 0)
1799 #! YARV Limitation: test_ok(p1.call == 5)
1800 test_ok(!defined?(i7))
1802 if false #! YARV Limitation
1803 p1 = Proc.new{i7 = 0; Proc.new{i7}}.call
1805 test_ok(p1.call == 0)
1807 test_ok(p1.call == 1)
1809 test_ok(p1.call == 5)
1814 test_ok(`echo foobar` == "foobar\n")
1815 test_ok(`./miniruby -e 'print "foobar"'` == 'foobar')
1817 tmp = open("script_tmp", "w")
1818 tmp.print "print $zzz\n";
1821 test_ok(`./miniruby -s script_tmp -zzz` == 'true')
1822 test_ok(`./miniruby -s script_tmp -zzz=555` == '555')
1824 tmp = open("script_tmp", "w")
1825 tmp.print "#! /usr/local/bin/ruby -s\n";
1826 tmp.print "print $zzz\n";
1829 test_ok(`./miniruby script_tmp -zzz=678` == '678')
1831 tmp = open("script_tmp", "w")
1832 tmp.print "this is a leading junk\n";
1833 tmp.print "#! /usr/local/bin/ruby -s\n";
1834 tmp.print "print $zzz\n";
1835 tmp.print "__END__\n";
1836 tmp.print "this is a trailing junk\n";
1839 test_ok(`./miniruby -x script_tmp` == '')
1840 test_ok(`./miniruby -x script_tmp -zzz=555` == '555')
1842 tmp = open("script_tmp", "w")
1848 `./miniruby -i.bak -pe '$_.sub!(/^[0-9]+$/){$&.to_i * 5}' script_tmp`
1850 tmp = open("script_tmp", "r")
1860 File.unlink "script_tmp" or `/bin/rm -f "script_tmp"`
1861 File.unlink "script_tmp.bak" or `/bin/rm -f "script_tmp.bak"`
1864 if (dir = File.dirname(File.dirname(__FILE__))) == '.'
1870 def valid_syntax?(code, fname)
1872 code.force_encoding("ascii-8bit")
1873 code = code.sub(/\A(?:\s*\#.*$)*(\n)?/n) {
1874 "#$&#{"\n" if $1 && !$2}BEGIN{return true}\n"
1876 eval(code, nil, fname, 0)
1878 STDERR.puts $!.message
1882 for script in Dir["#{dir}{lib,sample,ext,test}/**/*.rb"]
1883 unless valid_syntax? IO::read(script), script
1906 test_ok([TEST1,TEST2,TEST3,TEST4] == [1,2,3,4])
1909 STDERR.print "intentionally redefines TEST3, TEST4\n" if $VERBOSE
1910 test_ok([TEST1,TEST2,TEST3,TEST4] == [1,2,6,8])
1913 test_ok((String <=> Object) == -1)
1914 test_ok((Object <=> String) == 1)
1915 test_ok((Array <=> String) == nil)
1927 test_ok(bar.test2 == "test2")
1928 test_ok(bar.test == "test")
1929 test_ok(foo.test == "test")
1934 rescue NoMethodError
1940 module M003; include M002; end
1941 module M002; include M001; end
1942 module M003; include M002; end
1944 test_ok(M003.ancestors == [M003, M002, M001])
1946 test_check "marshal"
1947 $x = [1,2,3,[4,5,"foo"],{1=>"bar"},2.5,fact(30)]
1948 $y = Marshal.dump($x)
1949 test_ok($x == Marshal.load($y))
1951 StrClone=String.clone;
1952 test_ok(Marshal.load(Marshal.dump(StrClone.new("abc"))).class == StrClone)
1954 [[1,2,3,4], [81, 2, 118, 3146]].each { |w,x,y,z|
1955 a = (x.to_f + y.to_f / z.to_f) * Math.exp(w.to_f / (x.to_f + y.to_f / z.to_f))
1956 ma = Marshal.dump(a)
1957 b = Marshal.load(ma)
1963 $format = "c2x5CCxsdils_l_a6";
1964 # Need the expression in here to force ary[5] to be numeric. This avoids
1965 # test2 failing because ary2 goes str->numeric->str and ary does not.
1966 ary = [1,-100,127,128,32767,987.654321098 / 100.0,12345,123456,-32767,-123456,"abcdef"]
1967 $x = ary.pack($format)
1968 ary2 = $x.unpack($format)
1970 test_ok(ary.length == ary2.length)
1971 test_ok(ary.join(':') == ary2.join(':'))
1972 test_ok($x =~ /def/)
1975 test_ok($x.pack("q").unpack("q") == $x)
1978 test_ok(Math.sqrt(4) == 2)
1981 test_ok(sqrt(4) == 2)
1984 struct_test = Struct.new("Test", :foo, :bar)
1985 test_ok(struct_test == Struct::Test)
1987 test = struct_test.new(1, 2)
1988 test_ok(test.foo == 1 && test.bar == 2)
1989 test_ok(test[0] == 1 && test[1] == 2)
1992 test_ok(a == 1 && b == 2)
1995 test_ok(test.foo == 22)
1998 test_ok(test.bar == 47)
2000 test_check "variable"
2001 test_ok($$.instance_of?(Fixnum))
2003 # read-only variable
2013 test_ok($_ == foobar)
2016 @@rule = "Uranus" # private to Gods
2021 def self.ruler1 # <= per method definition style
2024 class << self # <= multiple method definition style
2039 @@rule = "Cronus" # do not affect @@rule in Gods
2046 test_ok(Gods.new.ruler0 == "Cronus")
2047 test_ok(Gods.ruler1 == "Cronus")
2048 test_ok(Gods.ruler2 == "Cronus")
2049 test_ok(Titans.ruler1 == "Cronus")
2050 test_ok(Titans.ruler2 == "Cronus")
2052 test_ok(atlas.ruler0 == "Cronus")
2053 test_ok(atlas.ruler3 == "Zeus")
2054 test_ok(atlas.ruler4 == "Cronus")
2059 trace_var :$x, Proc.new{$y = $x}
2067 trace_var :$x, Proc.new{$x *= 2}
2073 test_check "defined?"
2075 test_ok(defined?($x)) # global variable
2076 test_ok(defined?($x) == 'global-variable')# returns description
2079 test_ok(defined?(foo)) # local variable
2081 test_ok(defined?(Array)) # constant
2082 test_ok(defined?(Object.new)) # method
2083 test_ok(!defined?(Object.print))# private method
2084 test_ok(defined?(1 == 2)) # operator expression
2092 test_ok(defined?(self.foo))
2093 test_ok(defined?(f.foo))
2097 test_ok(defined?(f.foo) == nil)
2101 return !defined?(yield)
2104 test_ok(defined_test) # not iterator
2105 test_ok(!defined_test{}) # called as iterator
2113 def foo; "foo+" + super end
2121 test_ok(x.bar == "foo")
2122 test_ok(x.baz == "foo+foo")
2124 # test_check for cache
2125 test_ok(x.baz == "foo+foo")
2144 test_ok(File.basename("a") == "a")
2145 test_ok(File.basename("a/b") == "b")
2146 test_ok(File.basename("a/b/") == "b")
2147 test_ok(File.basename("/") == "/")
2148 test_ok(File.basename("//") == "/")
2149 test_ok(File.basename("///") == "/")
2150 test_ok(File.basename("a/b////") == "b")
2151 test_ok(File.basename("a.rb", ".rb") == "a")
2152 test_ok(File.basename("a.rb///", ".rb") == "a")
2153 test_ok(File.basename("a.rb///", ".*") == "a")
2154 test_ok(File.basename("a.rb///", ".c") == "a.rb")
2155 test_ok(File.dirname("a") == ".")
2156 test_ok(File.dirname("/") == "/")
2157 test_ok(File.dirname("/a") == "/")
2158 test_ok(File.dirname("a/b") == "a")
2159 test_ok(File.dirname("a/b/c") == "a/b")
2160 test_ok(File.dirname("/a/b/c") == "/a/b")
2161 test_ok(File.dirname("/a/b/") == "/a")
2162 test_ok(File.dirname("/a/b///") == "/a")
2165 test_ok(/\A\w:\/\z/ =~ File.expand_path(".", "/"))
2166 test_ok(/\A\w:\/a\z/ =~ File.expand_path("a", "/"))
2169 test_ok(%r'\A//[^/]+/[^/]+\z' =~ File.expand_path(".", "/"))
2170 test_ok(%r'\A//[^/]+/[^/]+/a\z' =~ File.expand_path(".", "/"))
2173 test_ok(File.expand_path(".", "/") == "/")
2174 test_ok(File.expand_path("sub", "/") == "/sub")
2177 test_ok(File.expand_path("/", "//machine/share/sub") == "//machine/share")
2178 test_ok(File.expand_path("/dir", "//machine/share/sub") == "//machine/share/dir")
2179 test_ok(File.expand_path("/", "z:/sub") == "z:/")
2180 test_ok(File.expand_path("/dir", "z:/sub") == "z:/dir")
2182 test_ok(File.expand_path(".", "//") == "//")
2183 test_ok(File.expand_path("sub", "//") == "//sub")
2185 # test_check "Proc#binding"
2186 ObjectSpace.each_object(Proc){|o|
2190 rescue ArgumentError
2197 tmp = [0,1,2,3,4,5,6,7,8,9]
2214 test_ok true # reach here or dumps core
2220 test_ok true # reach here or dumps core
2222 ObjectSpace.each_object{|o|
2226 test_ok true # reach here or dumps core
2229 printf "not ok/test: %d failed %d\n", $ntest, $failed
2231 printf "end of test(test: %d)\n", $ntest