1#! /usr/bin/env ruby 2# -*- coding: us-ascii -*- 3 4$testnum=0 5$ntest=0 6$failed = 0 7class Progress 8 def initialize 9 @color = nil 10 @tty = nil 11 @quiet = nil 12 @verbose = nil 13 ARGV.each do |arg| 14 case arg 15 when /\A--color(?:=(?:always|(auto)|(never)|(.*)))?\z/ 16 warn "unknown --color argument: #$3" if $3 17 @color = $1 ? nil : !$2 18 when /\A--tty(=(?:yes|(no)|(.*)))?\z/ 19 warn "unknown --tty argument: #$3" if $3 20 @tty = !$1 || !$2 21 true 22 when /\A-(q|-quiet)\z/ 23 @quiet = true 24 when /\A-(v|-verbose)\z/ 25 @verbose = true 26 end 27 end 28 @tty = STDERR.tty? && !STDOUT.tty? && /dumb/ !~ ENV["TERM"] if @tty.nil? 29 @eol = @tty && !@verbose ? "\r\e[K\r" : "\n" 30 case @color 31 when nil 32 @color = @tty 33 end 34 if @color 35 # dircolors-like style 36 colors = (colors = ENV['TEST_COLORS']) ? Hash[colors.scan(/(\w+)=([^:]*)/)] : {} 37 @passed = "\e[#{colors["pass"] || "32"}m" 38 @failed = "\e[#{colors["fail"] || "31"}m" 39 @reset = "\e[m" 40 else 41 @passed = @failed = @reset = "" 42 end 43 extend(Rotator) if @tty 44 end 45 46 def passed_string 47 "." 48 end 49 def failed_string 50 "#{@failed}F#{@reset}" 51 end 52 def init_string 53 end 54 def finish_string 55 if @quiet 56 @eol 57 else 58 "#{@passed}#{@ok ? 'OK' : ''} #{$testnum}#{@reset}#{@eol}" 59 end 60 end 61 def pass 62 STDERR.print passed_string 63 end 64 def fail 65 @ok = false 66 STDERR.print failed_string 67 end 68 def init 69 @ok = true 70 STDERR.print init_string 71 end 72 def finish 73 STDERR.print finish_string 74 end 75 76 module Rotator 77 ROTATOR = %w[- \\ | /] 78 BS = "\b" * ROTATOR[0].size 79 def passed_string 80 "#{BS}#{ROTATOR[(@count += 1) % ROTATOR.size]}" 81 end 82 def failed_string 83 "#{BS}#{super}#{ROTATOR[@count % ROTATOR.size]}" 84 end 85 def init_string 86 @count = 0 87 " " 88 end 89 def finish_string 90 s = "#{BS}#{' ' * BS.size}#{BS}#{super}" 91 s.gsub!(/\n/, "\r\e[2K\r") if @quiet 92 s 93 end 94 end 95end 96PROGRESS = Progress.new 97 98def test_check(what) 99 unless $ntest.zero? 100 PROGRESS.finish 101 end 102 STDERR.print "sample/test.rb:#{what} " 103 PROGRESS.init 104 $what = what 105 $testnum = 0 106end 107 108def test_ok(cond,n=1) 109 $testnum+=1 110 $ntest+=1 111 where = (st = caller(n)) ? st[0] : "caller error! (n=#{n}, trace=#{caller(0).join(', ')}" 112 if cond 113 PROGRESS.pass 114 printf "ok %d (%s)\n", $testnum, where 115 else 116 PROGRESS.fail 117 printf "not ok %s %d -- %s\n", $what, $testnum, where 118 $failed+=1 119 end 120 STDOUT.flush 121 STDERR.flush 122end 123 124# make sure conditional operators work 125 126test_check "assignment" 127 128a=[]; a[0] ||= "bar"; 129test_ok(a[0] == "bar") 130h={}; h["foo"] ||= "bar"; 131test_ok(h["foo"] == "bar") 132 133aa = 5 134aa ||= 25 135test_ok(aa == 5) 136bb ||= 25 137test_ok(bb == 25) 138cc &&=33 139test_ok(cc == nil) 140cc = 5 141cc &&=44 142test_ok(cc == 44) 143 144a = nil; test_ok(a == nil) 145a = 1; test_ok(a == 1) 146a = []; test_ok(a == []) 147a = [1]; test_ok(a == [1]) 148a = [nil]; test_ok(a == [nil]) 149a = [[]]; test_ok(a == [[]]) 150a = [1,2]; test_ok(a == [1,2]) 151a = [*[]]; test_ok(a == []) 152a = [*[1]]; test_ok(a == [1]) 153a = [*[1,2]]; test_ok(a == [1,2]) 154 155a = *[]; test_ok(a == []) 156a = *[1]; test_ok(a == [1]) 157a = *[nil]; test_ok(a == [nil]) 158a = *[[]]; test_ok(a == [[]]) 159a = *[1,2]; test_ok(a == [1,2]) 160a = *[*[]]; test_ok(a == []) 161a = *[*[1]]; test_ok(a == [1]) 162a = *[*[1,2]]; test_ok(a == [1,2]) 163 164a, = nil; test_ok(a == nil) 165a, = 1; test_ok(a == 1) 166a, = []; test_ok(a == nil) 167a, = [1]; test_ok(a == 1) 168a, = [nil]; test_ok(a == nil) 169a, = [[]]; test_ok(a == []) 170a, = 1,2; test_ok(a == 1) 171a, = [1,2]; test_ok(a == 1) 172a, = [*[]]; test_ok(a == nil) 173a, = [*[1]]; test_ok(a == 1) 174a, = *[1,2]; test_ok(a == 1) 175a, = [*[1,2]]; test_ok(a == 1) 176 177a, = *[]; test_ok(a == nil) 178a, = *[1]; test_ok(a == 1) 179a, = *[nil]; test_ok(a == nil) 180a, = *[[]]; test_ok(a == []) 181a, = *[1,2]; test_ok(a == 1) 182a, = *[*[]]; test_ok(a == nil) 183a, = *[*[1]]; test_ok(a == 1) 184a, = *[*[1,2]]; test_ok(a == 1) 185 186*a = nil; test_ok(a == [nil]) 187*a = 1; test_ok(a == [1]) 188*a = []; test_ok(a == []) 189*a = [1]; test_ok(a == [1]) 190*a = [nil]; test_ok(a == [nil]) 191*a = [[]]; test_ok(a == [[]]) 192*a = [1,2]; test_ok(a == [1,2]) 193*a = [*[]]; test_ok(a == []) 194*a = [*[1]]; test_ok(a == [1]) 195*a = [*[1,2]]; test_ok(a == [1,2]) 196 197*a = *[]; test_ok(a == []) 198*a = *[1]; test_ok(a == [1]) 199*a = *[nil]; test_ok(a == [nil]) 200*a = *[[]]; test_ok(a == [[]]) 201*a = *[1,2]; test_ok(a == [1,2]) 202*a = *[*[]]; test_ok(a == []) 203*a = *[*[1]]; test_ok(a == [1]) 204*a = *[*[1,2]]; test_ok(a == [1,2]) 205 206a,b,*c = nil; test_ok([a,b,c] == [nil,nil,[]]) 207a,b,*c = 1; test_ok([a,b,c] == [1,nil,[]]) 208a,b,*c = []; test_ok([a,b,c] == [nil,nil,[]]) 209a,b,*c = [1]; test_ok([a,b,c] == [1,nil,[]]) 210a,b,*c = [nil]; test_ok([a,b,c] == [nil,nil,[]]) 211a,b,*c = [[]]; test_ok([a,b,c] == [[],nil,[]]) 212a,b,*c = [1,2]; test_ok([a,b,c] == [1,2,[]]) 213a,b,*c = [*[]]; test_ok([a,b,c] == [nil,nil,[]]) 214a,b,*c = [*[1]]; test_ok([a,b,c] == [1,nil,[]]) 215a,b,*c = [*[1,2]]; test_ok([a,b,c] == [1,2,[]]) 216 217a,b,*c = *[]; test_ok([a,b,c] == [nil,nil,[]]) 218a,b,*c = *[1]; test_ok([a,b,c] == [1,nil,[]]) 219a,b,*c = *[nil]; test_ok([a,b,c] == [nil,nil,[]]) 220a,b,*c = *[[]]; test_ok([a,b,c] == [[],nil,[]]) 221a,b,*c = *[1,2]; test_ok([a,b,c] == [1,2,[]]) 222a,b,*c = *[*[]]; test_ok([a,b,c] == [nil,nil,[]]) 223a,b,*c = *[*[1]]; test_ok([a,b,c] == [1,nil,[]]) 224a,b,*c = *[*[1,2]]; test_ok([a,b,c] == [1,2,[]]) 225 226def f; yield nil; end; f {|a| test_ok(a == nil)} 227def f; yield 1; end; f {|a| test_ok(a == 1)} 228def f; yield []; end; f {|a| test_ok(a == [])} 229def f; yield [1]; end; f {|a| test_ok(a == [1])} 230def f; yield [nil]; end; f {|a| test_ok(a == [nil])} 231def f; yield [[]]; end; f {|a| test_ok(a == [[]])} 232def f; yield [*[]]; end; f {|a| test_ok(a == [])} 233def f; yield [*[1]]; end; f {|a| test_ok(a == [1])} 234def f; yield [*[1,2]]; end; f {|a| test_ok(a == [1,2])} 235def f; yield *[]; end; f {|a| test_ok(a == nil)} 236def f; yield *[1]; end; f {|a| test_ok(a == 1)} 237def f; yield *[nil]; end; f {|a| test_ok(a == nil)} 238def f; yield *[[]]; end; f {|a| test_ok(a == [])} 239def f; yield *[*[]]; end; f {|a| test_ok(a == nil)} 240def f; yield *[*[1]]; end; f {|a| test_ok(a == 1)} 241def f; yield *[*[1,2]]; end; f {|a| test_ok(a == 1)} 242 243def f; yield; end; f {|a,| test_ok(a == nil)} 244def f; yield nil; end; f {|a,| test_ok(a == nil)} 245def f; yield 1; end; f {|a,| test_ok(a == 1)} 246def f; yield []; end; f {|a,| test_ok(a == nil)} 247def f; yield [1]; end; f {|a,| test_ok(a == 1)} 248def f; yield [nil]; end; f {|a,| test_ok(a == nil)} 249def f; yield [[]]; end; f {|a,| test_ok(a == [])} 250def f; yield [*[]]; end; f {|a,| test_ok(a == nil)} 251def f; yield [*[1]]; end; f {|a,| test_ok(a == 1)} 252def f; yield [*[1,2]]; end; f {|a,| test_ok(a == 1)} 253 254def f; yield *[]; end; f {|a,| test_ok(a == nil)} 255def f; yield *[1]; end; f {|a,| test_ok(a == 1)} 256def f; yield *[nil]; end; f {|a,| test_ok(a == nil)} 257def f; yield *[[]]; end; f {|a,| test_ok(a == nil)} 258def f; yield *[*[]]; end; f {|a,| test_ok(a == nil)} 259def f; yield *[*[1]]; end; f {|a,| test_ok(a == 1)} 260def f; yield *[*[1,2]]; end; f {|a,| test_ok(a == 1)} 261 262def f; yield; end; f {|*a| test_ok(a == [])} 263def f; yield nil; end; f {|*a| test_ok(a == [nil])} 264def f; yield 1; end; f {|*a| test_ok(a == [1])} 265def f; yield []; end; f {|*a| test_ok(a == [[]])} 266def f; yield [1]; end; f {|*a| test_ok(a == [[1]])} 267def f; yield [nil]; end; f {|*a| test_ok(a == [[nil]])} 268def f; yield [[]]; end; f {|*a| test_ok(a == [[[]]])} 269def f; yield [1,2]; end; f {|*a| test_ok(a == [[1,2]])} 270def f; yield [*[]]; end; f {|*a| test_ok(a == [[]])} 271def f; yield [*[1]]; end; f {|*a| test_ok(a == [[1]])} 272def f; yield [*[1,2]]; end; f {|*a| test_ok(a == [[1,2]])} 273 274def f; yield *[]; end; f {|*a| test_ok(a == [])} 275def f; yield *[1]; end; f {|*a| test_ok(a == [1])} 276def f; yield *[nil]; end; f {|*a| test_ok(a == [nil])} 277def f; yield *[[]]; end; f {|*a| test_ok(a == [[]])} 278def f; yield *[*[]]; end; f {|*a| test_ok(a == [])} 279def f; yield *[*[1]]; end; f {|*a| test_ok(a == [1])} 280def f; yield *[*[1,2]]; end; f {|*a| test_ok(a == [1,2])} 281 282def f; yield; end; f {|a,b,*c| test_ok([a,b,c] == [nil,nil,[]])} 283def f; yield nil; end; f {|a,b,*c| test_ok([a,b,c] == [nil,nil,[]])} 284def f; yield 1; end; f {|a,b,*c| test_ok([a,b,c] == [1,nil,[]])} 285def f; yield []; end; f {|a,b,*c| test_ok([a,b,c] == [nil,nil,[]])} 286def f; yield [1]; end; f {|a,b,*c| test_ok([a,b,c] == [1,nil,[]])} 287def f; yield [nil]; end; f {|a,b,*c| test_ok([a,b,c] == [nil,nil,[]])} 288def f; yield [[]]; end; f {|a,b,*c| test_ok([a,b,c] == [[],nil,[]])} 289def f; yield [*[]]; end; f {|a,b,*c| test_ok([a,b,c] == [nil,nil,[]])} 290def f; yield [*[1]]; end; f {|a,b,*c| test_ok([a,b,c] == [1,nil,[]])} 291def f; yield [*[1,2]]; end; f {|a,b,*c| test_ok([a,b,c] == [1,2,[]])} 292 293def f; yield *[]; end; f {|a,b,*c| test_ok([a,b,c] == [nil,nil,[]])} 294def f; yield *[1]; end; f {|a,b,*c| test_ok([a,b,c] == [1,nil,[]])} 295def f; yield *[nil]; end; f {|a,b,*c| test_ok([a,b,c] == [nil,nil,[]])} 296def f; yield *[[]]; end; f {|a,b,*c| test_ok([a,b,c] == [nil,nil,[]])} 297def f; yield *[*[]]; end; f {|a,b,*c| test_ok([a,b,c] == [nil,nil,[]])} 298def f; yield *[*[1]]; end; f {|a,b,*c| test_ok([a,b,c] == [1,nil,[]])} 299def f; yield *[*[1,2]]; end; f {|a,b,*c| test_ok([a,b,c] == [1,2,[]])} 300 301def r; return; end; a = r(); test_ok(a == nil) 302def r; return nil; end; a = r(); test_ok(a == nil) 303def r; return 1; end; a = r(); test_ok(a == 1) 304def r; return []; end; a = r(); test_ok(a == []) 305def r; return [1]; end; a = r(); test_ok(a == [1]) 306def r; return [nil]; end; a = r(); test_ok(a == [nil]) 307def r; return [[]]; end; a = r(); test_ok(a == [[]]) 308def r; return [*[]]; end; a = r(); test_ok(a == []) 309def r; return [*[1]]; end; a = r(); test_ok(a == [1]) 310def r; return [*[1,2]]; end; a = r(); test_ok(a == [1,2]) 311 312def r; return *[]; end; a = r(); test_ok(a == []) 313def r; return *[1]; end; a = r(); test_ok(a == [1]) 314def r; return *[nil]; end; a = r(); test_ok(a == [nil]) 315def r; return *[[]]; end; a = r(); test_ok(a == [[]]) 316def r; return *[*[]]; end; a = r(); test_ok(a == []) 317def r; return *[*[1]]; end; a = r(); test_ok(a == [1]) 318def r; return *[*[1,2]]; end; a = r(); test_ok(a == [1,2]) 319 320def r; return *[[]]; end; a = *r(); test_ok(a == [[]]) 321def r; return *[*[1,2]]; end; a = *r(); test_ok(a == [1,2]) 322 323def r; return; end; *a = r(); test_ok(a == [nil]) 324def r; return nil; end; *a = r(); test_ok(a == [nil]) 325def r; return 1; end; *a = r(); test_ok(a == [1]) 326def r; return []; end; *a = r(); test_ok(a == []) 327def r; return [1]; end; *a = r(); test_ok(a == [1]) 328def r; return [nil]; end; *a = r(); test_ok(a == [nil]) 329def r; return [[]]; end; *a = r(); test_ok(a == [[]]) 330def r; return [1,2]; end; *a = r(); test_ok(a == [1,2]) 331def r; return [*[]]; end; *a = r(); test_ok(a == []) 332def r; return [*[1]]; end; *a = r(); test_ok(a == [1]) 333def r; return [*[1,2]]; end; *a = r(); test_ok(a == [1,2]) 334 335def r; return *[]; end; *a = r(); test_ok(a == []) 336def r; return *[1]; end; *a = r(); test_ok(a == [1]) 337def r; return *[nil]; end; *a = r(); test_ok(a == [nil]) 338def r; return *[[]]; end; *a = r(); test_ok(a == [[]]) 339def r; return *[1,2]; end; *a = r(); test_ok(a == [1,2]) 340def r; return *[*[]]; end; *a = r(); test_ok(a == []) 341def r; return *[*[1]]; end; *a = r(); test_ok(a == [1]) 342def r; return *[*[1,2]]; end; *a = r(); test_ok(a == [1,2]) 343 344def r; return *[[]]; end; *a = *r(); test_ok(a == [[]]) 345def r; return *[1,2]; end; *a = *r(); test_ok(a == [1,2]) 346def r; return *[*[1,2]]; end; *a = *r(); test_ok(a == [1,2]) 347 348def r; return; end; a,b,*c = r(); test_ok([a,b,c] == [nil,nil,[]]) 349def r; return nil; end; a,b,*c = r(); test_ok([a,b,c] == [nil,nil,[]]) 350def r; return 1; end; a,b,*c = r(); test_ok([a,b,c] == [1,nil,[]]) 351def r; return []; end; a,b,*c = r(); test_ok([a,b,c] == [nil,nil,[]]) 352def r; return [1]; end; a,b,*c = r(); test_ok([a,b,c] == [1,nil,[]]) 353def r; return [nil]; end; a,b,*c = r(); test_ok([a,b,c] == [nil,nil,[]]) 354def r; return [[]]; end; a,b,*c = r(); test_ok([a,b,c] == [[],nil,[]]) 355def r; return [1,2]; end; a,b,*c = r(); test_ok([a,b,c] == [1,2,[]]) 356def r; return [*[]]; end; a,b,*c = r(); test_ok([a,b,c] == [nil,nil,[]]) 357def r; return [*[1]]; end; a,b,*c = r(); test_ok([a,b,c] == [1,nil,[]]) 358def r; return [*[1,2]]; end; a,b,*c = r(); test_ok([a,b,c] == [1,2,[]]) 359 360def r; return *[]; end; a,b,*c = r(); test_ok([a,b,c] == [nil,nil,[]]) 361def r; return *[1]; end; a,b,*c = r(); test_ok([a,b,c] == [1,nil,[]]) 362def r; return *[nil]; end; a,b,*c = r(); test_ok([a,b,c] == [nil,nil,[]]) 363def r; return *[[]]; end; a,b,*c = r(); test_ok([a,b,c] == [[],nil,[]]) 364def r; return *[1,2]; end; a,b,*c = r(); test_ok([a,b,c] == [1,2,[]]) 365def r; return *[*[]]; end; a,b,*c = r(); test_ok([a,b,c] == [nil,nil,[]]) 366def r; return *[*[1]]; end; a,b,*c = r(); test_ok([a,b,c] == [1,nil,[]]) 367def r; return *[*[1,2]]; end; a,b,*c = r(); test_ok([a,b,c] == [1,2,[]]) 368 369f = lambda {|r,| test_ok([] == r)} 370f.call([], *[]) 371 372f = lambda {|r,*l| test_ok([] == r); test_ok([1] == l)} 373f.call([], *[1]) 374 375f = lambda{|x| x} 376test_ok(f.call(42) == 42) 377test_ok(f.call([42]) == [42]) 378test_ok(f.call([[42]]) == [[42]]) 379test_ok(f.call([42,55]) == [42,55]) 380 381f = lambda{|x,| x} 382test_ok(f.call(42) == 42) 383test_ok(f.call([42]) == [42]) 384test_ok(f.call([[42]]) == [[42]]) 385test_ok(f.call([42,55]) == [42,55]) 386 387f = lambda{|*x| x} 388test_ok(f.call(42) == [42]) 389test_ok(f.call([42]) == [[42]]) 390test_ok(f.call([[42]]) == [[[42]]]) 391test_ok(f.call([42,55]) == [[42,55]]) 392test_ok(f.call(42,55) == [42,55]) 393 394f = lambda { |a, b=42, *c| [a,b,c] } 395test_ok(f.call(1 ) == [1,42,[ ]] ) 396test_ok(f.call(1,43 ) == [1,43,[ ]] ) 397test_ok(f.call(1,43,44) == [1,43,[44]] ) 398 399f = lambda { |a, b=(a|16), *c, &block| [a,b,c,block&&block[]] } 400test_ok(f.call(8 ) == [8,24,[ ],nil] ) 401test_ok(f.call(8,43 ) == [8,43,[ ],nil] ) 402test_ok(f.call(8,43,44) == [8,43,[44],nil] ) 403test_ok(f.call(8 ){45} == [8,24,[ ],45 ] ) 404test_ok(f.call(8,43 ){45} == [8,43,[ ],45 ] ) 405test_ok(f.call(8,43,44){45} == [8,43,[44],45 ] ) 406 407f = lambda { |a, b=42, *c, d| [a,b,c,d] } 408test_ok(f.call(1 ,99) == [1,42,[ ],99] ) 409test_ok(f.call(1,43 ,99) == [1,43,[ ],99] ) 410test_ok(f.call(1,43,44,99) == [1,43,[44],99] ) 411 412f = lambda { |a, b=(a|16), &block| [a,b,block&&block[]] } 413test_ok(f.call(8 ) == [8,24,nil] ) 414test_ok(f.call(8,43) == [8,43,nil] ) 415test_ok(f.call(8,43) == [8,43,nil] ) 416test_ok(f.call(8 ){45} == [8,24,45 ] ) 417test_ok(f.call(8,43){45} == [8,43,45 ] ) 418test_ok(f.call(8,43){45} == [8,43,45 ] ) 419 420f = lambda { |a, b=42, d| [a,b,d] } 421test_ok(f.call(1 ,99) == [1,42,99] ) 422test_ok(f.call(1,43,99) == [1,43,99] ) 423test_ok(f.call(1,43,99) == [1,43,99] ) 424 425f = lambda { |b=42, *c, &block| [b,c,block&&block[]] } 426test_ok(f.call( ) == [42,[ ],nil] ) 427test_ok(f.call(43 ) == [43,[ ],nil] ) 428test_ok(f.call(43,44) == [43,[44],nil] ) 429test_ok(f.call( ){45} == [42,[ ],45 ] ) 430test_ok(f.call(43 ){45} == [43,[ ],45 ] ) 431test_ok(f.call(43,44){45} == [43,[44],45 ] ) 432 433f = lambda { |b=42, *c, d| [b,c,d] } 434test_ok(f.call( 99) == [42,[ ],99] ) 435test_ok(f.call(43 ,99) == [43,[ ],99] ) 436test_ok(f.call(43,44,99) == [43,[44],99] ) 437 438f = lambda { |b=42, &block| [b,block&&block[]] } 439test_ok(f.call( ) == [42,nil] ) 440test_ok(f.call(43) == [43,nil] ) 441test_ok(f.call(43) == [43,nil] ) 442test_ok(f.call( ){45} == [42,45 ] ) 443test_ok(f.call(43){45} == [43,45 ] ) 444test_ok(f.call(43){45} == [43,45 ] ) 445 446f = lambda { |b=42, d| [b,d] } 447test_ok(f.call( 99) == [42,99] ) 448test_ok(f.call(43,99) == [43,99] ) 449test_ok(f.call(43,99) == [43,99] ) 450 451 452a,=*[1] 453test_ok(a == 1) 454a,=*[[1]] 455test_ok(a == [1]) 456a,=*[[[1]]] 457test_ok(a == [[1]]) 458 459x, (y, z) = 1, 2, 3 460test_ok([1,2,nil] == [x,y,z]) 461x, (y, z) = 1, [2,3] 462test_ok([1,2,3] == [x,y,z]) 463x, (y, z) = 1, [2] 464test_ok([1,2,nil] == [x,y,z]) 465 466a = loop do break; end; test_ok(a == nil) 467a = loop do break nil; end; test_ok(a == nil) 468a = loop do break 1; end; test_ok(a == 1) 469a = loop do break []; end; test_ok(a == []) 470a = loop do break [1]; end; test_ok(a == [1]) 471a = loop do break [nil]; end; test_ok(a == [nil]) 472a = loop do break [[]]; end; test_ok(a == [[]]) 473a = loop do break [*[]]; end; test_ok(a == []) 474a = loop do break [*[1]]; end; test_ok(a == [1]) 475a = loop do break [*[1,2]]; end; test_ok(a == [1,2]) 476 477a = loop do break *[]; end; test_ok(a == []) 478a = loop do break *[1]; end; test_ok(a == [1]) 479a = loop do break *[nil]; end; test_ok(a == [nil]) 480a = loop do break *[[]]; end; test_ok(a == [[]]) 481a = loop do break *[*[]]; end; test_ok(a == []) 482a = loop do break *[*[1]]; end; test_ok(a == [1]) 483a = loop do break *[*[1,2]]; end; test_ok(a == [1,2]) 484 485*a = loop do break; end; test_ok(a == [nil]) 486*a = loop do break nil; end; test_ok(a == [nil]) 487*a = loop do break 1; end; test_ok(a == [1]) 488*a = loop do break []; end; test_ok(a == []) 489*a = loop do break [1]; end; test_ok(a == [1]) 490*a = loop do break [nil]; end; test_ok(a == [nil]) 491*a = loop do break [[]]; end; test_ok(a == [[]]) 492*a = loop do break [1,2]; end; test_ok(a == [1,2]) 493*a = loop do break [*[]]; end; test_ok(a == []) 494*a = loop do break [*[1]]; end; test_ok(a == [1]) 495*a = loop do break [*[1,2]]; end; test_ok(a == [1,2]) 496 497*a = loop do break *[]; end; test_ok(a == []) 498*a = loop do break *[1]; end; test_ok(a == [1]) 499*a = loop do break *[nil]; end; test_ok(a == [nil]) 500*a = loop do break *[[]]; end; test_ok(a == [[]]) 501*a = loop do break *[1,2]; end; test_ok(a == [1,2]) 502*a = loop do break *[*[]]; end; test_ok(a == []) 503*a = loop do break *[*[1]]; end; test_ok(a == [1]) 504*a = loop do break *[*[1,2]]; end; test_ok(a == [1,2]) 505 506*a = *loop do break *[[]]; end; test_ok(a == [[]]) 507*a = *loop do break *[1,2]; end; test_ok(a == [1,2]) 508*a = *loop do break *[*[1,2]]; end; test_ok(a == [1,2]) 509 510a,b,*c = loop do break; end; test_ok([a,b,c] == [nil,nil,[]]) 511a,b,*c = loop do break nil; end; test_ok([a,b,c] == [nil,nil,[]]) 512a,b,*c = loop do break 1; end; test_ok([a,b,c] == [1,nil,[]]) 513a,b,*c = loop do break []; end; test_ok([a,b,c] == [nil,nil,[]]) 514a,b,*c = loop do break [1]; end; test_ok([a,b,c] == [1,nil,[]]) 515a,b,*c = loop do break [nil]; end; test_ok([a,b,c] == [nil,nil,[]]) 516a,b,*c = loop do break [[]]; end; test_ok([a,b,c] == [[],nil,[]]) 517a,b,*c = loop do break [1,2]; end; test_ok([a,b,c] == [1,2,[]]) 518a,b,*c = loop do break [*[]]; end; test_ok([a,b,c] == [nil,nil,[]]) 519a,b,*c = loop do break [*[1]]; end; test_ok([a,b,c] == [1,nil,[]]) 520a,b,*c = loop do break [*[1,2]]; end; test_ok([a,b,c] == [1,2,[]]) 521 522a,b,*c = loop do break *[]; end; test_ok([a,b,c] == [nil,nil,[]]) 523a,b,*c = loop do break *[1]; end; test_ok([a,b,c] == [1,nil,[]]) 524a,b,*c = loop do break *[nil]; end; test_ok([a,b,c] == [nil,nil,[]]) 525a,b,*c = loop do break *[[]]; end; test_ok([a,b,c] == [[],nil,[]]) 526a,b,*c = loop do break *[1,2]; end; test_ok([a,b,c] == [1,2,[]]) 527a,b,*c = loop do break *[*[]]; end; test_ok([a,b,c] == [nil,nil,[]]) 528a,b,*c = loop do break *[*[1]]; end; test_ok([a,b,c] == [1,nil,[]]) 529a,b,*c = loop do break *[*[1,2]]; end; test_ok([a,b,c] == [1,2,[]]) 530 531def r(val); a = yield(); test_ok(a == val, 2); end 532r(nil){next} 533r(nil){next nil} 534r(1){next 1} 535r([]){next []} 536r([1]){next [1]} 537r([nil]){next [nil]} 538r([[]]){next [[]]} 539r([]){next [*[]]} 540r([1]){next [*[1]]} 541r([1,2]){next [*[1,2]]} 542 543r([]){next *[]} 544r([1]){next *[1]} 545r([nil]){next *[nil]} 546r([[]]){next *[[]]} 547r([]){next *[*[]]} 548r([1]){next *[*[1]]} 549r([1,2]){next *[*[1,2]]} 550 551def r(val); *a = yield(); test_ok(a == val, 2); end 552r([nil]){next} 553r([nil]){next nil} 554r([1]){next 1} 555r([]){next []} 556r([1]){next [1]} 557r([nil]){next [nil]} 558r([[]]){next [[]]} 559r([1,2]){next [1,2]} 560r([]){next [*[]]} 561r([1]){next [*[1]]} 562r([1,2]){next [*[1,2]]} 563 564def r(val); *a = *yield(); test_ok(a == val, 2); end 565r([[]]){next *[[]]} 566r([1,2]){next *[1,2]} 567r([1,2]){next *[*[1,2]]} 568 569def r(val); a,b,*c = yield(); test_ok([a,b,c] == val, 2); end 570r([nil,nil,[]]){next} 571r([nil,nil,[]]){next nil} 572r([1,nil,[]]){next 1} 573r([nil,nil,[]]){next []} 574r([1,nil,[]]){next [1]} 575r([nil,nil,[]]){next [nil]} 576r([[],nil,[]]){next [[]]} 577r([1,2,[]]){next [1,2]} 578r([nil,nil,[]]){next [*[]]} 579r([1,nil,[]]){next [*[1]]} 580r([1,2,[]]){next [*[1,2]]} 581 582def r(val); a,b,*c = *yield(); test_ok([a,b,c] == val, 2); end 583r([[],nil,[]]){next *[[]]} 584r([1,2,[]]){next *[1,2]} 585r([1,2,[]]){next *[*[1,2]]} 586 587test_check "condition" 588 589$x = '0'; 590 591$x == $x && test_ok(true) 592$x != $x && test_ok(false) 593$x == $x || test_ok(false) 594$x != $x || test_ok(true) 595 596# first test to see if we can run the tests. 597 598test_check "if/unless"; 599 600$x = 'test'; 601test_ok(if $x == $x then true else false end) 602$bad = false 603unless $x == $x 604 $bad = true 605end 606test_ok(!$bad) 607test_ok(unless $x != $x then true else false end) 608 609test_check "case" 610 611case 5 612when 1, 2, 3, 4, 6, 7, 8 613 test_ok(false) 614when 5 615 test_ok(true) 616end 617 618case 5 619when 5 620 test_ok(true) 621when 1..10 622 test_ok(false) 623end 624 625case 5 626when 1..10 627 test_ok(true) 628else 629 test_ok(false) 630end 631 632case 5 633when 5 634 test_ok(true) 635else 636 test_ok(false) 637end 638 639case "foobar" 640when /^f.*r$/ 641 test_ok(true) 642else 643 test_ok(false) 644end 645 646test_check "while/until"; 647 648tmp = open("while_tmp", "w") 649tmp.print "tvi925\n"; 650tmp.print "tvi920\n"; 651tmp.print "vt100\n"; 652tmp.print "Amiga\n"; 653tmp.print "paper\n"; 654tmp.close 655 656# test break 657 658tmp = open("while_tmp", "r") 659test_ok(tmp.kind_of?(File)) 660 661while line = tmp.gets() 662 break if /vt100/ =~ line 663end 664 665test_ok(!tmp.eof? && /vt100/ =~ line) 666tmp.close 667 668# test next 669$bad = false 670tmp = open("while_tmp", "r") 671while line = tmp.gets() 672 next if /vt100/ =~ line 673 $bad = 1 if /vt100/ =~ line 674end 675test_ok(!(!tmp.eof? || /vt100/ =~ line || $bad)) 676tmp.close 677 678# test redo 679$bad = false 680tmp = open("while_tmp", "r") 681while line = tmp.gets() 682 lastline = line 683 line = line.gsub(/vt100/, 'VT100') 684 if lastline != line 685 line.gsub!('VT100', 'Vt100') 686 redo 687 end 688 $bad = 1 if /vt100/ =~ line 689 $bad = 1 if /VT100/ =~ line 690end 691test_ok(tmp.eof? && !$bad) 692tmp.close 693 694sum=0 695for i in 1..10 696 sum += i 697 i -= 1 698 if i > 0 699 redo 700 end 701end 702test_ok(sum == 220) 703 704# test interval 705$bad = false 706tmp = open("while_tmp", "r") 707while line = tmp.gets() 708 break if 3 709 case line 710 when /vt100/, /Amiga/, /paper/ 711 $bad = true 712 end 713end 714test_ok(!$bad) 715tmp.close 716 717File.unlink "while_tmp" or `/bin/rm -f "while_tmp"` 718test_ok(!File.exist?("while_tmp")) 719 720i = 0 721until i>4 722 i+=1 723end 724test_ok(i>4) 725 726 727# exception handling 728test_check "exception"; 729 730begin 731 raise "this must be handled" 732 test_ok(false) 733rescue 734 test_ok(true) 735end 736 737$bad = true 738begin 739 raise "this must be handled no.2" 740rescue 741 if $bad 742 $bad = false 743 retry 744 test_ok(false) 745 end 746end 747test_ok(true) 748 749# exception in rescue clause 750$string = "this must be handled no.3" 751begin 752 begin 753 raise "exception in rescue clause" 754 rescue 755 raise $string 756 end 757 test_ok(false) 758rescue => e 759 test_ok($! == e) 760 test_ok(e.message == $string) 761 test_ok(e != $string) 762end 763 764# exception in ensure clause 765begin 766 begin 767 raise "this must be handled no.4" 768 ensure 769 raise "exception in ensure clause" 770 end 771 test_ok(false) 772rescue 773 test_ok(true) 774end 775 776$bad = true 777begin 778 begin 779 raise "this must be handled no.5" 780 ensure 781 $bad = false 782 end 783rescue 784end 785test_ok(!$bad) 786 787$bad = true 788begin 789 begin 790 raise "this must be handled no.6" 791 ensure 792 $bad = false 793 end 794rescue 795end 796test_ok(!$bad) 797 798$bad = true 799while true 800 begin 801 break 802 ensure 803 $bad = false 804 end 805end 806test_ok(!$bad) 807 808test_ok(catch(:foo) { 809 loop do 810 loop do 811 throw :foo, true 812 break 813 end 814 break 815 test_ok(false) # should no reach here 816 end 817 false 818 }) 819 820test_check "array" 821test_ok([1, 2] + [3, 4] == [1, 2, 3, 4]) 822test_ok([1, 2] * 2 == [1, 2, 1, 2]) 823test_ok([1, 2] * ":" == "1:2") 824 825test_ok([1, 2].hash == [1, 2].hash) 826 827test_ok([1,2,3] & [2,3,4] == [2,3]) 828test_ok([1,2,3] | [2,3,4] == [1,2,3,4]) 829test_ok([1,2,3] - [2,3] == [1]) 830 831$x = [0, 1, 2, 3, 4, 5] 832test_ok($x[2] == 2) 833test_ok($x[1..3] == [1, 2, 3]) 834test_ok($x[1,3] == [1, 2, 3]) 835 836$x[0, 2] = 10 837test_ok($x[0] == 10 && $x[1] == 2) 838 839$x[0, 0] = -1 840test_ok($x[0] == -1 && $x[1] == 10) 841 842$x[-1, 1] = 20 843test_ok($x[-1] == 20 && $x.pop == 20) 844 845# array and/or 846test_ok(([1,2,3]&[2,4,6]) == [2]) 847test_ok(([1,2,3]|[2,4,6]) == [1,2,3,4,6]) 848 849# compact 850$x = [nil, 1, nil, nil, 5, nil, nil] 851$x.compact! 852test_ok($x == [1, 5]) 853 854# uniq 855$x = [1, 1, 4, 2, 5, 4, 5, 1, 2] 856$x.uniq! 857test_ok($x == [1, 4, 2, 5]) 858 859# empty? 860test_ok(!$x.empty?) 861$x = [] 862test_ok($x.empty?) 863 864# sort 865$x = ["it", "came", "to", "pass", "that", "..."] 866$x = $x.sort.join(" ") 867test_ok($x == "... came it pass that to") 868$x = [2,5,3,1,7] 869$x.sort!{|a,b| a<=>b} # sort with condition 870test_ok($x == [1,2,3,5,7]) 871$x.sort!{|a,b| b-a} # reverse sort 872test_ok($x == [7,5,3,2,1]) 873 874# split test 875$x = "The Book of Mormon" 876test_ok($x.split(//).reverse!.join == $x.reverse) 877test_ok($x.reverse == $x.reverse!) 878test_ok("1 byte string".split(//).reverse.join(":") == "g:n:i:r:t:s: :e:t:y:b: :1") 879$x = "a b c d" 880test_ok($x.split == ['a', 'b', 'c', 'd']) 881test_ok($x.split(' ') == ['a', 'b', 'c', 'd']) 882test_ok(defined? "a".chomp) 883test_ok("abc".scan(/./) == ["a", "b", "c"]) 884test_ok("1a2b3c".scan(/(\d.)/) == [["1a"], ["2b"], ["3c"]]) 885# non-greedy match 886test_ok("a=12;b=22".scan(/(.*?)=(\d*);?/) == [["a", "12"], ["b", "22"]]) 887 888$x = [1] 889test_ok(($x * 5).join(":") == '1:1:1:1:1') 890test_ok(($x * 1).join(":") == '1') 891test_ok(($x * 0).join(":") == '') 892 893*$x = *(1..7).to_a 894test_ok($x.size == 7) 895test_ok($x == [1, 2, 3, 4, 5, 6, 7]) 896 897$x = [1,2,3] 898$x[1,0] = $x 899test_ok($x == [1,1,2,3,2,3]) 900 901$x = [1,2,3] 902$x[-1,0] = $x 903test_ok($x == [1,2,1,2,3,3]) 904 905$x = [1,2,3] 906$x.concat($x) 907test_ok($x == [1,2,3,1,2,3]) 908 909test_check "hash" 910$x = {1=>2, 2=>4, 3=>6} 911 912test_ok($x[1] == 2) 913 914test_ok(begin 915 for k,v in $x 916 raise if k*2 != v 917 end 918 true 919 rescue 920 false 921 end) 922 923test_ok($x.length == 3) 924test_ok($x.has_key?(1)) 925test_ok($x.has_value?(4)) 926test_ok($x.values_at(2,3) == [4,6]) 927test_ok($x == {1=>2, 2=>4, 3=>6}) 928 929$z = $x.keys.sort.join(":") 930test_ok($z == "1:2:3") 931 932$z = $x.values.sort.join(":") 933test_ok($z == "2:4:6") 934test_ok($x == $x) 935 936$x.shift 937test_ok($x.length == 2) 938 939$z = [1,2] 940$x[$z] = 256 941test_ok($x[$z] == 256) 942 943$x = Hash.new(0) 944$x[1] = 1 945test_ok($x[1] == 1) 946test_ok($x[2] == 0) 947 948$x = Hash.new([]) 949test_ok($x[22] == []) 950test_ok($x[22].equal?($x[22])) 951 952$x = Hash.new{[]} 953test_ok($x[22] == []) 954test_ok(!$x[22].equal?($x[22])) 955 956$x = Hash.new{|h,k| $z = k; h[k] = k*2} 957$z = 0 958test_ok($x[22] == 44) 959test_ok($z == 22) 960$z = 0 961test_ok($x[22] == 44) 962test_ok($z == 0) 963$x.default = 5 964test_ok($x[23] == 5) 965 966$x = Hash.new 967def $x.default(k) 968 $z = k 969 self[k] = k*2 970end 971$z = 0 972test_ok($x[22] == 44) 973test_ok($z == 22) 974$z = 0 975test_ok($x[22] == 44) 976test_ok($z == 0) 977 978test_check "iterator" 979 980test_ok(!iterator?) 981 982def ttt 983 test_ok(iterator?) 984end 985ttt{} 986 987# yield at top level 988test_ok(!defined?(yield)) 989 990$x = [1, 2, 3, 4] 991$y = [] 992 993# iterator over array 994for i in $x 995 $y.push i 996end 997test_ok($x == $y) 998 999# nested iterator 1000def tt 1001 1.upto(10) {|i| 1002 yield i 1003 } 1004end 1005 1006i=0 1007tt{|i| break if i == 5} 1008test_ok(i == 0) 1009 1010def tt2(dummy) 1011 yield 1 1012end 1013 1014def tt3(&block) 1015 tt2(raise(ArgumentError,""),&block) 1016end 1017 1018$x = false 1019begin 1020 tt3{} 1021rescue ArgumentError 1022 $x = true 1023rescue Exception 1024end 1025test_ok($x) 1026 1027def tt4 &block 1028 tt2(raise(ArgumentError,""),&block) 1029end 1030$x = false 1031begin 1032 tt4{} 1033rescue ArgumentError 1034 $x = true 1035rescue Exception 1036end 1037test_ok($x) 1038 1039# iterator break/redo/next/retry 1040done = true 1041loop{ 1042 break 1043 done = false # should not reach here 1044} 1045test_ok(done) 1046 1047done = false 1048$bad = false 1049loop { 1050 break if done 1051 done = true 1052 next 1053 $bad = true # should not reach here 1054} 1055test_ok(!$bad) 1056 1057done = false 1058$bad = false 1059loop { 1060 break if done 1061 done = true 1062 redo 1063 $bad = true # should not reach here 1064} 1065test_ok(!$bad) 1066 1067$x = [] 1068for i in 1 .. 7 1069 $x.push i 1070end 1071test_ok($x.size == 7) 1072test_ok($x == [1, 2, 3, 4, 5, 6, 7]) 1073 1074# append method to built-in class 1075class Array 1076 def iter_test1 1077 collect{|e| [e, yield(e)]}.sort{|a,b|a[1]<=>b[1]} 1078 end 1079 def iter_test2 1080 a = collect{|e| [e, yield(e)]} 1081 a.sort{|a,b|a[1]<=>b[1]} 1082 end 1083end 1084$x = [[1,2],[3,4],[5,6]] 1085test_ok($x.iter_test1{|x|x} == $x.iter_test2{|x|x}) 1086 1087class IterTest 1088 def initialize(e); @body = e; end 1089 1090 def each0(&block); @body.each(&block); end 1091 def each1(&block); @body.each {|*x| block.call(*x) } end 1092 def each2(&block); @body.each {|*x| block.call(x) } end 1093 def each3(&block); @body.each {|x| block.call(*x) } end 1094 def each4(&block); @body.each {|x| block.call(x) } end 1095 def each5; @body.each {|*x| yield(*x) } end 1096 def each6; @body.each {|*x| yield(x) } end 1097 def each7; @body.each {|x| yield(*x) } end 1098 def each8; @body.each {|x| yield(x) } end 1099 1100 def f(a) 1101 a 1102 end 1103end 1104test_ok(IterTest.new(nil).method(:f).to_proc.call([1]) == [1]) 1105m = /\w+/.match("abc") 1106test_ok(IterTest.new(nil).method(:f).to_proc.call([m]) == [m]) 1107 1108IterTest.new([0]).each0 {|x| test_ok(x == 0)} 1109IterTest.new([1]).each1 {|x| test_ok(x == 1)} 1110IterTest.new([2]).each2 {|x| test_ok(x == [2])} 1111#IterTest.new([3]).each3 {|x| test_ok(x == 3)} 1112IterTest.new([4]).each4 {|x| test_ok(x == 4)} 1113IterTest.new([5]).each5 {|x| test_ok(x == 5)} 1114IterTest.new([6]).each6 {|x| test_ok(x == [6])} 1115#IterTest.new([7]).each7 {|x| test_ok(x == 7)} 1116IterTest.new([8]).each8 {|x| test_ok(x == 8)} 1117 1118IterTest.new([[0]]).each0 {|x| test_ok(x == [0])} 1119IterTest.new([[1]]).each1 {|x| test_ok(x == [1])} 1120IterTest.new([[2]]).each2 {|x| test_ok(x == [[2]])} 1121IterTest.new([[3]]).each3 {|x| test_ok(x == 3)} 1122IterTest.new([[4]]).each4 {|x| test_ok(x == [4])} 1123IterTest.new([[5]]).each5 {|x| test_ok(x == [5])} 1124IterTest.new([[6]]).each6 {|x| test_ok(x == [[6]])} 1125IterTest.new([[7]]).each7 {|x| test_ok(x == 7)} 1126IterTest.new([[8]]).each8 {|x| test_ok(x == [8])} 1127 1128IterTest.new([[0,0]]).each0 {|*x| test_ok(x == [[0,0]])} 1129IterTest.new([[8,8]]).each8 {|*x| test_ok(x == [[8,8]])} 1130 1131def m0(v) 1132 v 1133end 1134 1135def m1 1136 m0(block_given?) 1137end 1138test_ok(m1{p 'test'}) 1139test_ok(!m1) 1140 1141def m 1142 m0(block_given?,&Proc.new{}) 1143end 1144test_ok(m1{p 'test'}) 1145test_ok(!m1) 1146 1147class C 1148 include Enumerable 1149 def initialize 1150 @a = [1,2,3] 1151 end 1152 def each(&block) 1153 @a.each(&block) 1154 end 1155end 1156 1157test_ok(C.new.collect{|n| n} == [1,2,3]) 1158 1159test_ok(Proc == lambda{}.class) 1160test_ok(Proc == Proc.new{}.class) 1161lambda{|a|test_ok(a==1)}.call(1) 1162def block_test(klass, &block) 1163 test_ok(klass === block) 1164end 1165 1166block_test(NilClass) 1167block_test(Proc){} 1168 1169def call_argument_test(state, proc, *args) 1170 x = state 1171 begin 1172 proc.call(*args) 1173 rescue ArgumentError 1174 x = !x 1175 end 1176 test_ok(x,2) 1177end 1178 1179call_argument_test(true, lambda{||}) 1180call_argument_test(false, lambda{||}, 1) 1181call_argument_test(true, lambda{|a,|}, 1) 1182call_argument_test(false, lambda{|a,|}) 1183call_argument_test(false, lambda{|a,|}, 1,2) 1184 1185call_argument_test(true, Proc.new{||}) 1186call_argument_test(true, Proc.new{||}, 1) 1187call_argument_test(true, Proc.new{|a,|}, 1) 1188call_argument_test(true, Proc.new{|a,|}) 1189call_argument_test(true, Proc.new{|a,|}, 1,2) 1190 1191def block_get(&block) 1192 block 1193end 1194 1195test_ok(Proc == block_get{}.class) 1196call_argument_test(true, block_get{||}) 1197call_argument_test(true, block_get{||}, 1) 1198call_argument_test(true, block_get{|a,|}, 1) 1199call_argument_test(true, block_get{|a,|}) 1200call_argument_test(true, block_get{|a,|}, 1,2) 1201 1202call_argument_test(true, block_get(&lambda{||})) 1203call_argument_test(false, block_get(&lambda{||}),1) 1204call_argument_test(true, block_get(&lambda{|a,|}),1) 1205call_argument_test(false, block_get(&lambda{|a,|}),1,2) 1206 1207blk = block_get{11} 1208test_ok(blk.class == Proc) 1209test_ok(blk.to_proc.class == Proc) 1210test_ok(blk.clone.call == 11) 1211test_ok(block_get(&blk).class == Proc) 1212 1213lmd = lambda{44} 1214test_ok(lmd.class == Proc) 1215test_ok(lmd.to_proc.class == Proc) 1216test_ok(lmd.clone.call == 44) 1217test_ok(block_get(&lmd).class == Proc) 1218 1219test_ok(Proc.new{|a,| a}.yield(1,2,3) == 1) 1220call_argument_test(true, Proc.new{|a,|}, 1,2) 1221 1222test_ok(Proc.new{|&b| b.call(10)}.call {|x| x} == 10) 1223test_ok(Proc.new{|a,&b| b.call(a)}.call(12) {|x| x} == 12) 1224 1225def test_return1 1226 Proc.new { 1227 return 55 1228 }.yield + 5 1229end 1230test_ok(test_return1() == 55) 1231def test_return2 1232 lambda { 1233 return 55 1234 }.call + 5 1235end 1236test_ok(test_return2() == 60) 1237 1238def proc_call(&b) 1239 b.call 1240end 1241def proc_yield() 1242 yield 1243end 1244def proc_return1 1245 lambda{return 42}.call+1 1246end 1247test_ok(proc_return1() == 43) 1248def proc_return2 1249 ->{return 42}.call+1 1250end 1251test_ok(proc_return2() == 43) 1252def proc_return3 1253 proc_call{return 42}+1 1254end 1255test_ok(proc_return3() == 42) 1256def proc_return4 1257 proc_yield{return 42}+1 1258end 1259test_ok(proc_return4() == 42) 1260 1261def ljump_test(state, proc, *args) 1262 x = state 1263 begin 1264 proc.call(*args) 1265 rescue LocalJumpError 1266 x = !x 1267 end 1268 test_ok(x,2) 1269end 1270 1271ljump_test(false, block_get{break}) 1272ljump_test(true, lambda{break}) 1273 1274def exit_value_test(&block) 1275 block.call 1276rescue LocalJumpError 1277 $!.exit_value 1278end 1279 1280test_ok(45 == exit_value_test{break 45}) 1281 1282test_ok(55 == begin 1283 block_get{break 55}.call 1284 rescue LocalJumpError 1285 $!.exit_value 1286 end) 1287 1288def block_call(&block) 1289 block.call 1290end 1291 1292def test_b1 1293 block_call{break 11} 1294end 1295test_ok(test_b1() == 11) 1296 1297def ljump_rescue(r) 1298 begin 1299 yield 1300 rescue LocalJumpError => e 1301 r if /from proc-closure/ =~ e.message 1302 end 1303end 1304 1305def test_b2 1306 ljump_rescue(22) do 1307 block_get{break 21}.call 1308 end 1309end 1310test_ok(test_b2() == 22) 1311 1312def test_b3 1313 ljump_rescue(33) do 1314 Proc.new{break 31}.yield 1315 end 1316end 1317test_ok(test_b3() == 33) 1318 1319def test_b4 1320 lambda{break 44}.call 1321end 1322test_ok(test_b4() == 44) 1323 1324def test_b5 1325 ljump_rescue(55) do 1326 b = block_get{break 54} 1327 block_call(&b) 1328 end 1329end 1330test_ok(test_b5() == 55) 1331 1332def test_b6 1333 b = lambda{break 67} 1334 block_call(&b) 1335 66 1336end 1337test_ok(test_b6() == 66) 1338 1339def util_r7 1340 block_get{break 78} 1341end 1342 1343def test_b7 1344 b = util_r7() 1345 ljump_rescue(77) do 1346 block_call(&b) 1347 end 1348end 1349test_ok(test_b7() == 77) 1350 1351def util_b8(&block) 1352 block_call(&block) 1353end 1354 1355def test_b8 1356 util_b8{break 88} 1357end 1358test_ok(test_b8() == 88) 1359 1360def util_b9(&block) 1361 lambda{block.call; 98}.call 1362end 1363 1364def test_b9 1365 util_b9{break 99} 1366end 1367test_ok(test_b9() == 99) 1368 1369def util_b10 1370 util_b9{break 100} 1371end 1372 1373def test_b10 1374 util_b10() 1375end 1376test_ok(test_b10() == 100) 1377 1378def test_b11 1379 ljump_rescue(111) do 1380 loop do 1381 Proc.new{break 110}.yield 1382 break 112 1383 end 1384 end 1385end 1386test_ok(test_b11() == 111) 1387 1388def test_b12 1389 loop do 1390 break lambda{break 122}.call 1391 break 121 1392 end 1393end 1394test_ok(test_b12() == 122) 1395 1396def test_b13 1397 ljump_rescue(133) do 1398 while true 1399 Proc.new{break 130}.yield 1400 break 131 1401 end 1402 end 1403end 1404test_ok(test_b13() == 133) 1405 1406def test_b14 1407 while true 1408 break lambda{break 144}.call 1409 break 143 1410 end 1411end 1412test_ok(test_b14() == 144) 1413 1414def test_b15 1415 [0].each {|c| yield 1 } 1416 156 1417end 1418test_ok(test_b15{|e| break 155 } == 155) 1419 1420def marity_test(m) 1421 method = method(m) 1422 test_ok(method.arity == method.to_proc.arity, 2) 1423end 1424marity_test(:test_ok) 1425marity_test(:marity_test) 1426marity_test(:p) 1427 1428lambda(&method(:test_ok)).call(true) 1429lambda(&block_get{|a,n| test_ok(a,n)}).call(true, 2) 1430 1431class ITER_TEST1 1432 def a 1433 block_given? 1434 end 1435end 1436 1437class ITER_TEST2 < ITER_TEST1 1438 def a 1439 test_ok(super) 1440 super 1441 end 1442end 1443test_ok(ITER_TEST2.new.a {}) 1444 1445class ITER_TEST3 1446 def foo x 1447 return yield if block_given? 1448 x 1449 end 1450end 1451 1452class ITER_TEST4 < ITER_TEST3 1453 def foo x 1454 test_ok(super == yield) 1455 test_ok(super(x, &nil) == x) 1456 end 1457end 1458 1459ITER_TEST4.new.foo(44){55} 1460 1461class ITER_TEST5 1462 def tt(aa) 1463 aa 1464 end 1465 1466 def uu(a) 1467 class << self 1468 define_method(:tt) do |sym| 1469 super(sym) 1470 end 1471 end 1472 end 1473 1474 def xx(*x) 1475 x.size 1476 end 1477end 1478 1479a = ITER_TEST5.new 1480a.uu(12) 1481test_ok(a.tt(1) == 1) 1482 1483class ITER_TEST6 < ITER_TEST5 1484 def xx(*a) 1485 a << 12 1486 super 1487 end 1488end 1489 1490test_ok(ITER_TEST6.new.xx([24]) == 2) 1491 1492test_check "float" 1493test_ok(2.6.floor == 2) 1494test_ok((-2.6).floor == -3) 1495test_ok(2.6.ceil == 3) 1496test_ok((-2.6).ceil == -2) 1497test_ok(2.6.truncate == 2) 1498test_ok((-2.6).truncate == -2) 1499test_ok(2.6.round == 3) 1500test_ok((-2.4).truncate == -2) 1501test_ok((13.4 % 1 - 0.4).abs < 0.0001) 1502nan = 0.0/0 1503def nan_test(x,y) 1504 test_ok(x != y) 1505 test_ok((x < y) == false) 1506 test_ok((x > y) == false) 1507 test_ok((x <= y) == false) 1508 test_ok((x >= y) == false) 1509end 1510nan_test(nan, nan) 1511nan_test(nan, 0) 1512nan_test(nan, 1) 1513nan_test(nan, -1) 1514nan_test(nan, 1000) 1515nan_test(nan, -1000) 1516nan_test(nan, 1_000_000_000_000) 1517nan_test(nan, -1_000_000_000_000) 1518nan_test(nan, 100.0); 1519nan_test(nan, -100.0); 1520nan_test(nan, 0.001); 1521nan_test(nan, -0.001); 1522nan_test(nan, 1.0/0); 1523nan_test(nan, -1.0/0); 1524 1525#s = "3.7517675036461267e+17" 1526#test_ok(s == sprintf("%.16e", s.to_f)) 1527f = 3.7517675036461267e+17 1528test_ok(f == sprintf("%.16e", f).to_f) 1529 1530 1531test_check "bignum" 1532def fact(n) 1533 return 1 if n == 0 1534 f = 1 1535 while n>0 1536 f *= n 1537 n -= 1 1538 end 1539 return f 1540end 1541$x = fact(40) 1542test_ok($x == $x) 1543test_ok($x == fact(40)) 1544test_ok($x < $x+2) 1545test_ok($x > $x-2) 1546test_ok($x == 815915283247897734345611269596115894272000000000) 1547test_ok($x != 815915283247897734345611269596115894272000000001) 1548test_ok($x+1 == 815915283247897734345611269596115894272000000001) 1549test_ok($x/fact(20) == 335367096786357081410764800000) 1550$x = -$x 1551test_ok($x == -815915283247897734345611269596115894272000000000) 1552test_ok(2-(2**32) == -(2**32-2)) 1553test_ok(2**32 - 5 == (2**32-3)-2) 1554 1555$good = true; 1556for i in 1000..1014 1557 $good = false if ((1 << i) != (2**i)) 1558end 1559test_ok($good) 1560 1561$good = true; 1562n1= 1 << 1000 1563for i in 1000..1014 1564 $good = false if ((1 << i) != n1) 1565 n1 *= 2 1566end 1567test_ok($good) 1568 1569$good = true; 1570n2=n1 1571for i in 1..10 1572 n1 = n1 / 2 1573 n2 = n2 >> 1 1574 $good = false if (n1 != n2) 1575end 1576test_ok($good) 1577 1578$good = true; 1579for i in 4000..4096 1580 n1 = 1 << i; 1581 if (n1**2-1) / (n1+1) != (n1-1) 1582 $good = false 1583 end 1584end 1585test_ok($good) 1586 1587b = 10**80 1588a = b * 9 + 7 1589test_ok(7 == a.modulo(b)) 1590test_ok(-b + 7 == a.modulo(-b)) 1591test_ok(b + -7 == (-a).modulo(b)) 1592test_ok(-7 == (-a).modulo(-b)) 1593test_ok(7 == a.remainder(b)) 1594test_ok(7 == a.remainder(-b)) 1595test_ok(-7 == (-a).remainder(b)) 1596test_ok(-7 == (-a).remainder(-b)) 1597 1598test_ok(10**40+10**20 == 10000000000000000000100000000000000000000) 1599test_ok(10**40/10**20 == 100000000000000000000) 1600 1601a = 677330545177305025495135714080 1602b = 14269972710765292560 1603test_ok(a % b == 0) 1604test_ok(-a % b == 0) 1605 1606def shift_test(a) 1607 b = a / (2 ** 32) 1608 c = a >> 32 1609 test_ok(b == c) 1610 1611 b = a * (2 ** 32) 1612 c = a << 32 1613 test_ok(b == c) 1614end 1615 1616shift_test(-4518325415524767873) 1617shift_test(-0xfffffffffffffffff) 1618 1619test_check "string & char" 1620 1621test_ok("abcd" == "abcd") 1622test_ok("abcd" =~ /abcd/) 1623test_ok("abcd" === "abcd") 1624# compile time string concatenation 1625test_ok("ab" "cd" == "abcd") 1626test_ok("#{22}aa" "cd#{44}" == "22aacd44") 1627test_ok("#{22}aa" "cd#{44}" "55" "#{66}" == "22aacd445566") 1628test_ok("abc" !~ /^$/) 1629test_ok("abc\n" !~ /^$/) 1630test_ok("abc" !~ /^d*$/) 1631test_ok(("abc" =~ /d*$/) == 3) 1632test_ok("" =~ /^$/) 1633test_ok("\n" =~ /^$/) 1634test_ok("a\n\n" =~ /^$/) 1635test_ok("abcabc" =~ /.*a/ && $& == "abca") 1636test_ok("abcabc" =~ /.*c/ && $& == "abcabc") 1637test_ok("abcabc" =~ /.*?a/ && $& == "a") 1638test_ok("abcabc" =~ /.*?c/ && $& == "abc") 1639test_ok(/(.|\n)*?\n(b|\n)/ =~ "a\nb\n\n" && $& == "a\nb") 1640 1641test_ok(/^(ab+)+b/ =~ "ababb" && $& == "ababb") 1642test_ok(/^(?:ab+)+b/ =~ "ababb" && $& == "ababb") 1643test_ok(/^(ab+)+/ =~ "ababb" && $& == "ababb") 1644test_ok(/^(?:ab+)+/ =~ "ababb" && $& == "ababb") 1645 1646test_ok(/(\s+\d+){2}/ =~ " 1 2" && $& == " 1 2") 1647test_ok(/(?:\s+\d+){2}/ =~ " 1 2" && $& == " 1 2") 1648 1649$x = <<END; 1650ABCD 1651ABCD 1652END 1653$x.gsub!(/((.|\n)*?)B((.|\n)*?)D/, '\1\3') 1654test_ok($x == "AC\nAC\n") 1655 1656test_ok("foobar" =~ /foo(?=(bar)|(baz))/) 1657test_ok("foobaz" =~ /foo(?=(bar)|(baz))/) 1658 1659$foo = "abc" 1660test_ok("#$foo = abc" == "abc = abc") 1661test_ok("#{$foo} = abc" == "abc = abc") 1662 1663foo = "abc" 1664test_ok("#{foo} = abc" == "abc = abc") 1665 1666test_ok('-' * 5 == '-----') 1667test_ok('-' * 1 == '-') 1668test_ok('-' * 0 == '') 1669 1670foo = '-' 1671test_ok(foo * 5 == '-----') 1672test_ok(foo * 1 == '-') 1673test_ok(foo * 0 == '') 1674 1675$x = "a.gif" 1676test_ok($x.sub(/.*\.([^\.]+)$/, '\1') == "gif") 1677test_ok($x.sub(/.*\.([^\.]+)$/, 'b.\1') == "b.gif") 1678test_ok($x.sub(/.*\.([^\.]+)$/, '\2') == "") 1679test_ok($x.sub(/.*\.([^\.]+)$/, 'a\2b') == "ab") 1680test_ok($x.sub(/.*\.([^\.]+)$/, '<\&>') == "<a.gif>") 1681 1682# character constants(assumes ASCII) 1683test_ok("a"[0] == ?a) 1684test_ok(?a == ?a) 1685test_ok(?\C-a == "\1") 1686test_ok(?\M-a == "\341") 1687test_ok(?\M-\C-a == "\201") 1688test_ok("a".upcase![0] == ?A) 1689test_ok("A".downcase![0] == ?a) 1690test_ok("abc".tr!("a-z", "A-Z") == "ABC") 1691test_ok("aabbcccc".tr_s!("a-z", "A-Z") == "ABC") 1692test_ok("abcc".squeeze!("a-z") == "abc") 1693test_ok("abcd".delete!("bc") == "ad") 1694 1695$x = "abcdef" 1696$y = [ ?a, ?b, ?c, ?d, ?e, ?f ] 1697$bad = false 1698$x.each_byte {|i| 1699 if i.chr != $y.shift 1700 $bad = true 1701 break 1702 end 1703} 1704test_ok(!$bad) 1705 1706s = "a string" 1707s[0..s.size]="another string" 1708test_ok(s == "another string") 1709 1710s = <<EOS 1711#{ 1712[1,2,3].join(",") 1713} 1714EOS 1715test_ok(s == "1,2,3\n") 1716test_ok("Just".to_i(36) == 926381) 1717test_ok("-another".to_i(36) == -23200231779) 1718test_ok(1299022.to_s(36) == "ruby") 1719test_ok(-1045307475.to_s(36) == "-hacker") 1720test_ok("Just_another_Ruby_hacker".to_i(36) == 265419172580680477752431643787347) 1721test_ok(-265419172580680477752431643787347.to_s(36) == "-justanotherrubyhacker") 1722 1723a = [] 1724(0..255).each {|n| 1725 ch = [n].pack("C") 1726 a.push ch if /a#{Regexp.quote ch}b/x =~ "ab" 1727} 1728test_ok(a.size == 0) 1729 1730test_check "assignment" 1731a = nil 1732test_ok(defined?(a)) 1733test_ok(a == nil) 1734 1735# multiple asignment 1736a, b = 1, 2 1737test_ok(a == 1 && b == 2) 1738 1739a, b = b, a 1740test_ok(a == 2 && b == 1) 1741 1742a, = 1,2 1743test_ok(a == 1) 1744 1745a, *b = 1, 2, 3 1746test_ok(a == 1 && b == [2, 3]) 1747 1748a, (b, c), d = 1, [2, 3], 4 1749test_ok(a == 1 && b == 2 && c == 3 && d == 4) 1750 1751*a = 1, 2, 3 1752test_ok(a == [1, 2, 3]) 1753 1754*a = 4 1755test_ok(a == [4]) 1756 1757*a = nil 1758test_ok(a == [nil]) 1759 1760test_check "call" 1761def aaa(a, b=100, *rest) 1762 res = [a, b] 1763 res += rest if rest 1764 return res 1765end 1766 1767# not enough argument 1768begin 1769 aaa() # need at least 1 arg 1770 test_ok(false) 1771rescue 1772 test_ok(true) 1773end 1774 1775begin 1776 aaa # no arg given (exception raised) 1777 test_ok(false) 1778rescue 1779 test_ok(true) 1780end 1781 1782test_ok(aaa(1) == [1, 100]) 1783test_ok(aaa(1, 2) == [1, 2]) 1784test_ok(aaa(1, 2, 3, 4) == [1, 2, 3, 4]) 1785test_ok(aaa(1, *[2, 3, 4]) == [1, 2, 3, 4]) 1786 1787test_check "proc" 1788$proc = Proc.new{|i| i} 1789test_ok($proc.call(2) == 2) 1790test_ok($proc.call(3) == 3) 1791 1792$proc = Proc.new{|i| i*2} 1793test_ok($proc.call(2) == 4) 1794test_ok($proc.call(3) == 6) 1795 1796Proc.new{ 1797 iii=5 # nested local variable 1798 $proc = Proc.new{|i| 1799 iii = i 1800 } 1801 $proc2 = Proc.new { 1802 $x = iii # nested variables shared by procs 1803 } 1804 # scope of nested variables 1805 test_ok(defined?(iii)) 1806}.call 1807test_ok(!defined?(iii)) # out of scope 1808 1809loop{iii=5; test_ok(eval("defined? iii")); break} 1810loop { 1811 iii = 10 1812 def dyna_var_check 1813 loop { 1814 test_ok(!defined?(iii)) 1815 break 1816 } 1817 end 1818 dyna_var_check 1819 break 1820} 1821$x=0 1822$proc.call(5) 1823$proc2.call 1824test_ok($x == 5) 1825 1826if defined? Process.kill 1827 test_check "signal" 1828 1829 $x = 0 1830 trap "SIGINT", Proc.new{|sig| $x = 2} 1831 Process.kill "SIGINT", $$ 1832 100.times { 1833 sleep 0.1 1834 break if $x != 0 1835 } 1836 test_ok($x == 2) 1837 1838 trap "SIGINT", Proc.new{raise "Interrupt"} 1839 1840 x = false 1841 begin 1842 Process.kill "SIGINT", $$ 1843 sleep 0.1 1844 rescue 1845 x = $! 1846 end 1847 test_ok(x && /Interrupt/ =~ x.message) 1848end 1849 1850test_check "eval" 1851test_ok(eval("") == nil) 1852$bad=false 1853eval 'while false; $bad = true; print "foo\n" end' 1854test_ok(!$bad) 1855 1856test_ok(eval('TRUE')) 1857test_ok(eval('true')) 1858test_ok(!eval('NIL')) 1859test_ok(!eval('nil')) 1860test_ok(!eval('FALSE')) 1861test_ok(!eval('false')) 1862 1863$foo = 'test_ok(true)' 1864begin 1865 eval $foo 1866rescue 1867 test_ok(false) 1868end 1869 1870test_ok(eval("$foo") == 'test_ok(true)') 1871test_ok(eval("true") == true) 1872i = 5 1873test_ok(eval("i == 5")) 1874test_ok(eval("i") == 5) 1875test_ok(eval("defined? i")) 1876 1877# eval with binding 1878def test_ev 1879 local1 = "local1" 1880 lambda { 1881 local2 = "local2" 1882 return binding 1883 }.call 1884end 1885 1886$x = test_ev 1887test_ok(eval("local1", $x) == "local1") # normal local var 1888test_ok(eval("local2", $x) == "local2") # nested local var 1889$bad = true 1890begin 1891 p eval("local1") 1892rescue NameError # must raise error 1893 $bad = false 1894end 1895test_ok(!$bad) 1896 1897module EvTest 1898 EVTEST1 = 25 1899 evtest2 = 125 1900 $x = binding 1901end 1902test_ok(eval("EVTEST1", $x) == 25) # constant in module 1903test_ok(eval("evtest2", $x) == 125) # local var in module 1904$bad = true 1905begin 1906 eval("EVTEST1") 1907rescue NameError # must raise error 1908 $bad = false 1909end 1910test_ok(!$bad) 1911 1912x = binding #! YARV Limitation: Proc.new{} 1913eval "i4 = 1", x 1914test_ok(eval("i4", x) == 1) 1915x = Proc.new{binding}.call #! YARV Limitation: Proc.new{Proc.new{}}.call 1916eval "i4 = 22", x 1917test_ok(eval("i4", x) == 22) 1918$x = [] 1919x = Proc.new{binding}.call #! YARV Limitation: Proc.new{Proc.new{}}.call 1920eval "(0..9).each{|i5| $x[i5] = Proc.new{i5*2}}", x 1921test_ok($x[4].call == 8) 1922 1923x = binding 1924eval "i = 1", x 1925test_ok(eval("i", x) == 1) 1926x = Proc.new{binding}.call 1927eval "i = 22", x 1928test_ok(eval("i", x) == 22) 1929$x = [] 1930x = Proc.new{binding}.call 1931eval "(0..9).each{|i5| $x[i5] = Proc.new{i5*2}}", x 1932test_ok($x[4].call == 8) 1933x = Proc.new{binding}.call 1934eval "for i6 in 1..1; j6=i6; end", x 1935test_ok(eval("defined? i6", x)) 1936test_ok(eval("defined? j6", x)) 1937 1938Proc.new { 1939 p = binding 1940 eval "foo11 = 1", p 1941 foo22 = 5 1942 Proc.new{foo11=22}.call 1943 Proc.new{foo22=55}.call 1944 test_ok(eval("foo11", p) == eval("foo11")) 1945 test_ok(eval("foo11") == 1) 1946 test_ok(eval("foo22", p) == eval("foo22")) 1947 test_ok(eval("foo22") == 55) 1948}.call if false #! YARV Limitation 1949 1950#! YARV Limitation: p1 = Proc.new{i7 = 0; Proc.new{i7}}.call 1951p1 = Proc.new{i7 = 0; binding}.call 1952#! YARV Limitation: test_ok(p1.call == 0) 1953eval "i7=5", p1 1954#! YARV Limitation: test_ok(p1.call == 5) 1955test_ok(!defined?(i7)) 1956 1957if false #! YARV Limitation 1958p1 = Proc.new{i7 = 0; Proc.new{i7}}.call 1959i7 = nil 1960test_ok(p1.call == 0) 1961eval "i7=1", p1 1962test_ok(p1.call == 1) 1963eval "i7=5", p1 1964test_ok(p1.call == 5) 1965test_ok(i7 == nil) 1966end 1967 1968test_check "system" 1969test_ok(`echo foobar` == "foobar\n") 1970test_ok(`./miniruby -e 'print "foobar"'` == 'foobar') 1971 1972tmp = open("script_tmp", "w") 1973tmp.print "print $zzz\n"; 1974tmp.close 1975 1976test_ok(`./miniruby -s script_tmp -zzz` == 'true') 1977test_ok(`./miniruby -s script_tmp -zzz=555` == '555') 1978 1979tmp = open("script_tmp", "w") 1980tmp.print "#! /usr/local/bin/ruby -s\n"; 1981tmp.print "print $zzz\n"; 1982tmp.close 1983 1984test_ok(`./miniruby script_tmp -zzz=678` == '678') 1985 1986tmp = open("script_tmp", "w") 1987tmp.print "this is a leading junk\n"; 1988tmp.print "#! /usr/local/bin/ruby -s\n"; 1989tmp.print "print $zzz\n"; 1990tmp.print "__END__\n"; 1991tmp.print "this is a trailing junk\n"; 1992tmp.close 1993 1994test_ok(`./miniruby -x script_tmp` == '') 1995test_ok(`./miniruby -x script_tmp -zzz=555` == '555') 1996 1997tmp = open("script_tmp", "w") 1998for i in 1..5 1999 tmp.print i, "\n" 2000end 2001tmp.close 2002 2003`./miniruby -i.bak -pe '$_.sub!(/^[0-9]+$/){$&.to_i * 5}' script_tmp` 2004done = true 2005tmp = open("script_tmp", "r") 2006while tmp.gets 2007 if $_.to_i % 5 != 0 2008 done = false 2009 break 2010 end 2011end 2012tmp.close 2013test_ok(done) 2014 2015File.unlink "script_tmp" or `/bin/rm -f "script_tmp"` 2016File.unlink "script_tmp.bak" or `/bin/rm -f "script_tmp.bak"` 2017 2018test_check "const" 2019TEST1 = 1 2020TEST2 = 2 2021 2022module Const 2023 TEST3 = 3 2024 TEST4 = 4 2025end 2026 2027module Const2 2028 TEST3 = 6 2029 TEST4 = 8 2030end 2031 2032include Const 2033 2034test_ok([TEST1,TEST2,TEST3,TEST4] == [1,2,3,4]) 2035 2036include Const2 2037STDERR.print "intentionally redefines TEST3, TEST4\n" if $VERBOSE 2038test_ok([TEST1,TEST2,TEST3,TEST4] == [1,2,6,8]) 2039 2040 2041test_ok((String <=> Object) == -1) 2042test_ok((Object <=> String) == 1) 2043test_ok((Array <=> String) == nil) 2044 2045test_check "clone" 2046foo = Object.new 2047def foo.test 2048 "test" 2049end 2050bar = foo.clone 2051def bar.test2 2052 "test2" 2053end 2054 2055test_ok(bar.test2 == "test2") 2056test_ok(bar.test == "test") 2057test_ok(foo.test == "test") 2058 2059begin 2060 foo.test2 2061 test_ok false 2062rescue NoMethodError 2063 test_ok true 2064end 2065 2066module M001; end 2067module M002; end 2068module M003; include M002; end 2069module M002; include M001; end 2070module M003; include M002; end 2071 2072test_ok(M003.ancestors == [M003, M002, M001]) 2073 2074test_check "marshal" 2075$x = [1,2,3,[4,5,"foo"],{1=>"bar"},2.5,fact(30)] 2076$y = Marshal.dump($x) 2077test_ok($x == Marshal.load($y)) 2078 2079StrClone=String.clone; 2080test_ok(Marshal.load(Marshal.dump(StrClone.new("abc"))).class == StrClone) 2081 2082[[1,2,3,4], [81, 2, 118, 3146]].each { |w,x,y,z| 2083 a = (x.to_f + y.to_f / z.to_f) * Math.exp(w.to_f / (x.to_f + y.to_f / z.to_f)) 2084 ma = Marshal.dump(a) 2085 b = Marshal.load(ma) 2086 test_ok(a == b) 2087} 2088 2089test_check "pack" 2090 2091$format = "c2x5CCxsdils_l_a6"; 2092# Need the expression in here to force ary[5] to be numeric. This avoids 2093# test2 failing because ary2 goes str->numeric->str and ary does not. 2094ary = [1,-100,127,128,32767,987.654321098 / 100.0,12345,123456,-32767,-123456,"abcdef"] 2095$x = ary.pack($format) 2096ary2 = $x.unpack($format) 2097 2098test_ok(ary.length == ary2.length) 2099test_ok(ary.join(':') == ary2.join(':')) 2100test_ok($x =~ /def/) 2101 2102$x = [-1073741825] 2103test_ok($x.pack("q").unpack("q") == $x) 2104 2105test_check "math" 2106test_ok(Math.sqrt(4) == 2) 2107 2108include Math 2109test_ok(sqrt(4) == 2) 2110 2111test_check "struct" 2112struct_test = Struct.new("Test", :foo, :bar) 2113test_ok(struct_test == Struct::Test) 2114 2115test = struct_test.new(1, 2) 2116test_ok(test.foo == 1 && test.bar == 2) 2117test_ok(test[0] == 1 && test[1] == 2) 2118 2119a, b = test.to_a 2120test_ok(a == 1 && b == 2) 2121 2122test[0] = 22 2123test_ok(test.foo == 22) 2124 2125test.bar = 47 2126test_ok(test.bar == 47) 2127 2128test_check "variable" 2129test_ok($$.instance_of?(Fixnum)) 2130 2131# read-only variable 2132begin 2133 $$ = 5 2134 test_ok false 2135rescue NameError 2136 test_ok true 2137end 2138 2139foobar = "foobar" 2140$_ = foobar 2141test_ok($_ == foobar) 2142 2143class Gods 2144 @@rule = "Uranus" # private to Gods 2145 def ruler0 2146 @@rule 2147 end 2148 2149 def self.ruler1 # <= per method definition style 2150 @@rule 2151 end 2152 class << self # <= multiple method definition style 2153 def ruler2 2154 @@rule 2155 end 2156 end 2157end 2158 2159module Olympians 2160 @@rule ="Zeus" 2161 def ruler3 2162 @@rule 2163 end 2164end 2165 2166class Titans < Gods 2167 @@rule = "Cronus" # do not affect @@rule in Gods 2168 include Olympians 2169 def ruler4 2170 @@rule 2171 end 2172end 2173 2174test_ok(Gods.new.ruler0 == "Cronus") 2175test_ok(Gods.ruler1 == "Cronus") 2176test_ok(Gods.ruler2 == "Cronus") 2177test_ok(Titans.ruler1 == "Cronus") 2178test_ok(Titans.ruler2 == "Cronus") 2179atlas = Titans.new 2180test_ok(atlas.ruler0 == "Cronus") 2181test_ok(atlas.ruler3 == "Zeus") 2182test_ok(atlas.ruler4 == "Cronus") 2183 2184test_check "trace" 2185$x = 1234 2186$y = 0 2187trace_var :$x, Proc.new{$y = $x} 2188$x = 40414 2189test_ok($y == $x) 2190 2191untrace_var :$x 2192$x = 19660208 2193test_ok($y != $x) 2194 2195trace_var :$x, Proc.new{$x *= 2} 2196$x = 5 2197test_ok($x == 10) 2198 2199untrace_var :$x 2200 2201test_check "defined?" 2202 2203test_ok(defined?($x)) # global variable 2204test_ok(defined?($x) == 'global-variable')# returns description 2205 2206foo=5 2207test_ok(defined?(foo)) # local variable 2208 2209test_ok(defined?(Array)) # constant 2210test_ok(defined?(Object.new)) # method 2211test_ok(!defined?(Object.print))# private method 2212test_ok(defined?(1 == 2)) # operator expression 2213 2214class Foo 2215 def foo 2216 p :foo 2217 end 2218 protected :foo 2219 def bar(f) 2220 test_ok(defined?(self.foo)) 2221 test_ok(defined?(f.foo)) 2222 end 2223end 2224f = Foo.new 2225test_ok(defined?(f.foo) == nil) 2226f.bar(f) 2227 2228def defined_test 2229 return !defined?(yield) 2230end 2231 2232test_ok(defined_test) # not iterator 2233test_ok(!defined_test{}) # called as iterator 2234 2235test_check "alias" 2236class Alias0 2237 def foo; "foo" end 2238end 2239class Alias1<Alias0 2240 alias bar foo 2241 def foo; "foo+" + super end 2242end 2243class Alias2<Alias1 2244 alias baz foo 2245 undef foo 2246end 2247 2248x = Alias2.new 2249test_ok(x.bar == "foo") 2250test_ok(x.baz == "foo+foo") 2251 2252# test_check for cache 2253test_ok(x.baz == "foo+foo") 2254 2255class Alias3<Alias2 2256 def foo 2257 defined? super 2258 end 2259 def bar 2260 defined? super 2261 end 2262 def quux 2263 defined? super 2264 end 2265end 2266x = Alias3.new 2267test_ok(!x.foo) 2268test_ok(x.bar) 2269test_ok(!x.quux) 2270 2271test_check "path" 2272test_ok(File.basename("a") == "a") 2273test_ok(File.basename("a/b") == "b") 2274test_ok(File.basename("a/b/") == "b") 2275test_ok(File.basename("/") == "/") 2276test_ok(File.basename("//") == "/") 2277test_ok(File.basename("///") == "/") 2278test_ok(File.basename("a/b////") == "b") 2279test_ok(File.basename("a.rb", ".rb") == "a") 2280test_ok(File.basename("a.rb///", ".rb") == "a") 2281test_ok(File.basename("a.rb///", ".*") == "a") 2282test_ok(File.basename("a.rb///", ".c") == "a.rb") 2283test_ok(File.dirname("a") == ".") 2284test_ok(File.dirname("/") == "/") 2285test_ok(File.dirname("/a") == "/") 2286test_ok(File.dirname("a/b") == "a") 2287test_ok(File.dirname("a/b/c") == "a/b") 2288test_ok(File.dirname("/a/b/c") == "/a/b") 2289test_ok(File.dirname("/a/b/") == "/a") 2290test_ok(File.dirname("/a/b///") == "/a") 2291case Dir.pwd 2292when %r'\A\w:' 2293 test_ok(/\A\w:\/\z/ =~ File.expand_path(".", "/")) 2294 test_ok(/\A\w:\/a\z/ =~ File.expand_path("a", "/")) 2295 dosish = true 2296when %r'\A//' 2297 test_ok(%r'\A//[^/]+/[^/]+\z' =~ File.expand_path(".", "/")) 2298 test_ok(%r'\A//[^/]+/[^/]+/a\z' =~ File.expand_path(".", "/")) 2299 dosish = true 2300else 2301 test_ok(File.expand_path(".", "/") == "/") 2302 test_ok(File.expand_path("sub", "/") == "/sub") 2303end 2304if dosish 2305 test_ok(File.expand_path("/", "//machine/share/sub") == "//machine/share") 2306 test_ok(File.expand_path("/dir", "//machine/share/sub") == "//machine/share/dir") 2307 test_ok(File.expand_path("/", "z:/sub") == "z:/") 2308 test_ok(File.expand_path("/dir", "z:/sub") == "z:/dir") 2309end 2310test_ok(File.expand_path(".", "//") == "//") 2311test_ok(File.expand_path("sub", "//") == "//sub") 2312 2313# test_check "Proc#binding" 2314ObjectSpace.each_object(Proc){|o| 2315 begin 2316 b = o.binding 2317 eval 'self', b 2318 rescue ArgumentError 2319 end 2320} 2321 2322test_check "gc" 2323begin 2324 1.upto(10000) { 2325 tmp = [0,1,2,3,4,5,6,7,8,9] 2326 } 2327 tmp = nil 2328 test_ok true 2329rescue 2330 test_ok false 2331end 2332class S 2333 def initialize(a) 2334 @a = a 2335 end 2336end 2337l=nil 2338100000.times { 2339 l = S.new(l) 2340} 2341GC.start 2342test_ok true # reach here or dumps core 2343l = [] 2344100000.times { 2345 l.push([l]) 2346} 2347GC.start 2348test_ok true # reach here or dumps core 2349 2350ObjectSpace.each_object{|o| 2351 o.class.name 2352} 2353 2354test_ok true # reach here or dumps core 2355 2356PROGRESS.finish 2357if $failed > 0 2358 printf "not ok/test: %d failed %d\n", $ntest, $failed 2359else 2360 printf "end of test(test: %d)\n", $ntest 2361end 2362