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