home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / rbemx144.zip / ruby-1.4.4 / sample / test.rb < prev    next >
Text File  |  1999-08-13  |  19KB  |  1,137 lines

  1. #! /usr/local/bin/ruby
  2.  
  3. $testnum=0
  4. $ntest=0
  5. $failed = 0
  6.  
  7. def check(what)
  8.   printf "%s\n", what
  9.   $what = what
  10.   $testnum = 0
  11. end
  12.  
  13. def ok(cond)
  14.   $testnum+=1
  15.   $ntest+=1
  16.   if cond
  17.     printf "ok %d\n", $testnum
  18.   else
  19.     where = caller[0]
  20.     printf "not ok %s %d -- %s\n", $what, $testnum, where
  21.     $failed+=1 
  22.   end
  23. end
  24.  
  25. # make sure conditional operators work
  26.  
  27. check "assignment"
  28.  
  29. a=[]; a[0] ||= "bar";
  30. ok(a[0] == "bar")
  31. h={}; h["foo"] ||= "bar";
  32. ok(h["foo"] == "bar")
  33.  
  34. aa = 5
  35. aa ||= 25
  36. ok(aa == 5)
  37. bb ||= 25
  38. ok(bb == 25)
  39. cc &&=33
  40. ok(cc == nil)
  41. cc = 5
  42. cc &&=44
  43. ok(cc == 44)
  44.  
  45. check "condition"
  46.  
  47. $x = '0';
  48.  
  49. $x == $x && ok(true)
  50. $x != $x && ok(false)
  51. $x == $x || ok(false)
  52. $x != $x || ok(true)
  53.  
  54. # first test to see if we can run the tests.
  55.  
  56. check "if/unless";
  57.  
  58. $x = 'test';
  59. ok(if $x == $x then true else false end)
  60. $bad = false
  61. unless $x == $x
  62.   $bad = true
  63. end
  64. ok(!$bad)
  65. ok(unless $x != $x then true else false end)
  66.  
  67. check "case"
  68.  
  69. case 5
  70. when 1, 2, 3, 4, 6, 7, 8
  71.   ok(false)
  72. when 5
  73.   ok(true)
  74. end
  75.  
  76. case 5
  77. when 5
  78.   ok(true)
  79. when 1..10
  80.   ok(false)
  81. end
  82.  
  83. case 5
  84. when 1..10
  85.   ok(true)
  86. else
  87.   ok(false)
  88. end
  89.  
  90. case 5
  91. when 5
  92.   ok(true)
  93. else
  94.   ok(false)
  95. end
  96.  
  97. case "foobar"
  98. when /^f.*r$/
  99.   ok(true)
  100. else
  101.   ok(false)
  102. end
  103.  
  104. check "while/until";
  105.  
  106. tmp = open("while_tmp", "w")
  107. tmp.print "tvi925\n";
  108. tmp.print "tvi920\n";
  109. tmp.print "vt100\n";
  110. tmp.print "Amiga\n";
  111. tmp.print "paper\n";
  112. tmp.close
  113.  
  114. # test break
  115.  
  116. tmp = open("while_tmp", "r")
  117. ok(tmp.kind_of?(File))
  118.  
  119. while tmp.gets()
  120.   break if /vt100/
  121. end
  122.  
  123. ok(!tmp.eof? && /vt100/)
  124. tmp.close
  125.  
  126. # test next
  127. $bad = false
  128. tmp = open("while_tmp", "r")
  129. while tmp.gets()
  130.   next if /vt100/;
  131.   $bad = 1 if /vt100/;
  132. end
  133. ok(!(!tmp.eof? || /vt100/ || $bad))
  134. tmp.close
  135.  
  136. # test redo
  137. $bad = false
  138. tmp = open("while_tmp", "r")
  139. while tmp.gets()
  140.   if gsub!('vt100', 'VT100')
  141.     gsub!('VT100', 'Vt100')
  142.     redo;
  143.   end
  144.   $bad = 1 if /vt100/;
  145.   $bad = 1 if /VT100/;
  146. end
  147. ok(tmp.eof? && !$bad)
  148. tmp.close
  149.  
  150. sum=0
  151. for i in 1..10
  152.   sum += i
  153.   i -= 1
  154.   if i > 0
  155.     redo
  156.   end
  157. end
  158. ok(sum == 220)
  159.  
  160. # test interval
  161. $bad = false
  162. tmp = open("while_tmp", "r")
  163. while tmp.gets()
  164.   break unless 1..2
  165.   if /vt100/ || /Amiga/ || /paper/
  166.     $bad = true
  167.     break
  168.   end
  169. end
  170. ok(!$bad)
  171. tmp.close
  172.  
  173. File.unlink "while_tmp" or `/bin/rm -f "while_tmp"`
  174. ok(!File.exist?("while_tmp"))
  175.  
  176. i = 0
  177. until i>4
  178.   i+=1
  179. end
  180. ok(i>4)
  181.  
  182. # exception handling
  183. check "exception";
  184.  
  185. begin
  186.   raise "this must be handled"
  187.   ok(false)
  188. rescue
  189.   ok(true)
  190. end
  191.  
  192. $bad = true
  193. begin
  194.   raise "this must be handled no.2"
  195. rescue
  196.   if $bad
  197.     $bad = false
  198.     retry
  199.     ok(false)
  200.   end
  201. end
  202. ok(true)
  203.  
  204. # exception in rescue clause
  205. $string = "this must be handled no.3"
  206. begin
  207.   begin
  208.     raise "exception in rescue clause"
  209.   rescue 
  210.     raise $string
  211.   end
  212.   ok(false)
  213. rescue
  214.   ok(true) if $! == $string
  215. end
  216.   
  217. # exception in ensure clause
  218. begin
  219.   begin
  220.     raise "this must be handled no.4"
  221.   ensure 
  222.     raise "exception in ensure clause"
  223.   end
  224.   ok(false)
  225. rescue
  226.   ok(true)
  227. end
  228.  
  229. $bad = true
  230. begin
  231.   begin
  232.     raise "this must be handled no.5"
  233.   ensure
  234.     $bad = false
  235.   end
  236. rescue
  237. end
  238. ok(!$bad)
  239.  
  240. $bad = true
  241. begin
  242.   begin
  243.     raise "this must be handled no.6"
  244.   ensure
  245.     $bad = false
  246.   end
  247. rescue
  248. end
  249. ok(!$bad)
  250.  
  251. $bad = true
  252. while true
  253.   begin
  254.     break
  255.   ensure
  256.     $bad = false
  257.   end
  258. end
  259. ok(!$bad)
  260.  
  261. ok(catch(:foo) {
  262.      loop do
  263.        loop do
  264.      throw :foo, true
  265.      break
  266.        end
  267.        break
  268.        ok(false)            # should no reach here
  269.      end
  270.      false
  271.    })
  272.  
  273. check "array"
  274. ok([1, 2] + [3, 4] == [1, 2, 3, 4])
  275. ok([1, 2] * 2 == [1, 2, 1, 2])
  276. ok([1, 2] * ":" == "1:2")
  277.  
  278. ok([1, 2].hash == [1, 2].hash)
  279.  
  280. ok([1,2,3] & [2,3,4] == [2,3])
  281. ok([1,2,3] | [2,3,4] == [1,2,3,4])
  282. ok([1,2,3] - [2,3] == [1])
  283.  
  284. $x = [0, 1, 2, 3, 4, 5]
  285. ok($x[2] == 2)
  286. ok($x[1..3] == [1, 2, 3])
  287. ok($x[1,3] == [1, 2, 3])
  288.  
  289. $x[0, 2] = 10
  290. ok($x[0] == 10 && $x[1] == 2)
  291.   
  292. $x[0, 0] = -1
  293. ok($x[0] == -1 && $x[1] == 10)
  294.  
  295. $x[-1, 1] = 20
  296. ok($x[-1] == 20 && $x.pop == 20)
  297.  
  298. # array and/or
  299. ok(([1,2,3]&[2,4,6]) == [2])
  300. ok(([1,2,3]|[2,4,6]) == [1,2,3,4,6])
  301.  
  302. # compact
  303. $x = [nil, 1, nil, nil, 5, nil, nil]
  304. $x.compact!
  305. ok($x == [1, 5])
  306.  
  307. # uniq
  308. $x = [1, 1, 4, 2, 5, 4, 5, 1, 2]
  309. $x.uniq!
  310. ok($x == [1, 4, 2, 5])
  311.  
  312. # empty?
  313. ok(!$x.empty?)
  314. $x = []
  315. ok($x.empty?)
  316.  
  317. # sort
  318. $x = ["it", "came", "to", "pass", "that", "..."]
  319. $x = $x.sort.join(" ")
  320. ok($x == "... came it pass that to")
  321. $x = [2,5,3,1,7]
  322. $x.sort!{|a,b| a<=>b}        # sort with condition
  323. ok($x == [1,2,3,5,7])
  324. $x.sort!{|a,b| b-a}        # reverse sort
  325. ok($x == [7,5,3,2,1])
  326.  
  327. # split test
  328. $x = "The Book of Mormon"
  329. ok($x.split(//).reverse!.join == $x.reverse)
  330. ok($x.reverse == $x.reverse!)
  331. ok("1 byte string".split(//).reverse.join(":") == "g:n:i:r:t:s: :e:t:y:b: :1")
  332. $x = "a b c  d"
  333. ok($x.split == ['a', 'b', 'c', 'd'])
  334. ok($x.split(' ') == ['a', 'b', 'c', 'd'])
  335. ok(defined? "a".chomp)
  336. ok("abc".scan(/./) == ["a", "b", "c"])
  337. ok("1a2b3c".scan(/(\d.)/) == [["1a"], ["2b"], ["3c"]])
  338. # non-greedy match
  339. ok("a=12;b=22".scan(/(.*?)=(\d*);?/) == [["a", "12"], ["b", "22"]])
  340.  
  341. $x = [1]
  342. ok(($x * 5).join(":") == '1:1:1:1:1')
  343. ok(($x * 1).join(":") == '1')
  344. ok(($x * 0).join(":") == '')
  345.  
  346. *$x = 1..7
  347. ok($x.size == 7)
  348. ok($x == [1, 2, 3, 4, 5, 6, 7])
  349.  
  350. check "hash"
  351. $x = {1=>2, 2=>4, 3=>6}
  352. $y = {1, 2, 2, 4, 3, 6}
  353.  
  354. ok($x[1] == 2)
  355.  
  356. ok(begin   
  357.      for k,v in $y
  358.        raise if k*2 != v
  359.      end
  360.      true
  361.    rescue
  362.      false
  363.    end)
  364.  
  365. ok($x.length == 3)
  366. ok($x.has_key?(1))
  367. ok($x.has_value?(4))
  368. ok($x.indexes(2,3) == [4,6])
  369. ok($x == {1=>2, 2=>4, 3=>6})
  370.  
  371. $z = $y.keys.join(":")
  372. ok($z == "1:2:3")
  373.  
  374. $z = $y.values.join(":")
  375. ok($z == "2:4:6")
  376. ok($x == $y)
  377.  
  378. $y.shift
  379. ok($y.length == 2)
  380.  
  381. $z = [1,2]
  382. $y[$z] = 256
  383. ok($y[$z] == 256)
  384.  
  385. check "iterator"
  386.  
  387. ok(!iterator?)
  388.  
  389. def ttt
  390.   ok(iterator?)
  391. end
  392. ttt{}
  393.  
  394. # yield at top level
  395. ok(!defined?(yield))
  396.  
  397. $x = [1, 2, 3, 4]
  398. $y = []
  399.  
  400. # iterator over array
  401. for i in $x
  402.   $y.push i
  403. end
  404. ok($x == $y)
  405.  
  406. # nested iterator
  407. def tt
  408.   1.upto(10) {|i|
  409.     yield i
  410.   }
  411. end
  412.  
  413. tt{|i| break if i == 5}
  414. ok(i == 5)
  415.  
  416. # iterator break/redo/next/retry
  417. done = true
  418. loop{
  419.   break
  420.   done = false            # should not reach here
  421. }
  422. ok(done)
  423.  
  424. done = false
  425. $bad = false
  426. loop {
  427.   break if done
  428.   done = true
  429.   next
  430.   $bad = true            # should not reach here
  431. }
  432. ok(!$bad)
  433.  
  434. done = false
  435. $bad = false
  436. loop {
  437.   break if done
  438.   done = true
  439.   redo
  440.   $bad = true            # should not reach here
  441. }
  442. ok(!$bad)
  443.  
  444. $x = []
  445. for i in 1 .. 7
  446.   $x.push i
  447. end
  448. ok($x.size == 7)
  449. ok($x == [1, 2, 3, 4, 5, 6, 7])
  450.  
  451. $done = false
  452. $x = []
  453. for i in 1 .. 7            # see how retry works in iterator loop
  454.   if i == 4 and not $done
  455.     $done = true
  456.     retry
  457.   end
  458.   $x.push(i)
  459. end
  460. ok($x.size == 10)
  461. ok($x == [1, 2, 3, 1, 2, 3, 4, 5, 6, 7])
  462.  
  463. # append method to built-in class
  464. class Array
  465.   def iter_test1
  466.     collect{|e| [e, yield(e)]}.sort{|a,b|a[1]<=>b[1]}
  467.   end
  468.   def iter_test2
  469.     a = collect{|e| [e, yield(e)]}
  470.     a.sort{|a,b|a[1]<=>b[1]}
  471.   end
  472. end
  473. $x = [[1,2],[3,4],[5,6]]
  474. ok($x.iter_test1{|x|x} == $x.iter_test2{|x|x})
  475.  
  476. class IterTest
  477.   def initialize(e); @body = e; end
  478.  
  479.   def each0(&block); @body.each(&block); end
  480.   def each1(&block); @body.each { |*x| block.call(*x) } end
  481.   def each2(&block); @body.each { |*x| block.call(x) } end
  482.   def each3(&block); @body.each { |x| block.call(*x) } end
  483.   def each4(&block); @body.each { |x| block.call(x) } end
  484.   def each5; @body.each { |*x| yield(*x) } end
  485.   def each6; @body.each { |*x| yield(x) } end
  486.   def each7; @body.each { |x| yield(*x) } end
  487.   def each8; @body.each { |x| yield(x) } end
  488. end
  489.  
  490. IterTest.new([0]).each0 { |x| $x = x }
  491. ok($x == 0)
  492. IterTest.new([1]).each1 { |x| $x = x }
  493. ok($x == 1)
  494. IterTest.new([2]).each2 { |x| $x = x }
  495. ok($x == [2])
  496. IterTest.new([3]).each3 { |x| $x = x }
  497. ok($x == 3)
  498. IterTest.new([4]).each4 { |x| $x = x }
  499. ok($x == 4)
  500. IterTest.new([5]).each5 { |x| $x = x }
  501. ok($x == 5)
  502. IterTest.new([6]).each6 { |x| $x = x }
  503. ok($x == [6])
  504. IterTest.new([7]).each7 { |x| $x = x }
  505. ok($x == 7)
  506. IterTest.new([8]).each8 { |x| $x = x }
  507. ok($x == 8)
  508.  
  509. IterTest.new([[0]]).each0 { |x| $x = x }
  510. ok($x == [0])
  511. IterTest.new([[1]]).each1 { |x| $x = x }
  512. ok($x == 1)
  513. IterTest.new([[2]]).each2 { |x| $x = x }
  514. ok($x == [2])
  515. IterTest.new([[3]]).each3 { |x| $x = x }
  516. ok($x == 3)
  517. IterTest.new([[4]]).each4 { |x| $x = x }
  518. ok($x == [4])
  519. IterTest.new([[5]]).each5 { |x| $x = x }
  520. ok($x == 5)
  521. IterTest.new([[6]]).each6 { |x| $x = x }
  522. ok($x == [6])
  523. IterTest.new([[7]]).each7 { |x| $x = x }
  524. ok($x == 7)
  525. IterTest.new([[8]]).each8 { |x| $x = x }
  526. ok($x == [8])
  527.  
  528. IterTest.new([[0,0]]).each0 { |x| $x = x }
  529. ok($x == [0,0])
  530. IterTest.new([[8,8]]).each8 { |x| $x = x }
  531. ok($x == [8,8])
  532.  
  533. check "bignum"
  534. def fact(n)
  535.   return 1 if n == 0
  536.   f = 1
  537.   while n>0
  538.     f *= n
  539.     n -= 1
  540.   end
  541.   return f
  542. end
  543. fact(3)
  544. $x = fact(40)
  545. ok($x == $x)
  546. ok($x == fact(40))
  547. ok($x < $x+2)
  548. ok($x > $x-2)
  549. ok($x == 815915283247897734345611269596115894272000000000)
  550. ok($x != 815915283247897734345611269596115894272000000001)
  551. ok($x+1 == 815915283247897734345611269596115894272000000001)
  552. ok($x/fact(20) == 335367096786357081410764800000)
  553. $x = -$x
  554. ok($x == -815915283247897734345611269596115894272000000000)
  555. ok(2-(2**32) == -(2**32-2))
  556. ok(2**32 - 5 == (2**32-3)-2)
  557.  
  558. $good = true;
  559. for i in 1000..1014
  560.   $good = false if ((1<<i) != (2**i))
  561. end
  562. ok($good)
  563.  
  564. $good = true;
  565. n1=1<<1000
  566. for i in 1000..1014
  567.   $good = false if ((1<<i) != n1)
  568.   n1 *= 2
  569. end
  570. ok($good)
  571.  
  572. $good = true;
  573. n2=n1
  574. for i in 1..10
  575.   n1 = n1 / 2
  576.   n2 = n2 >> 1
  577.   $good = false if (n1 != n2)
  578. end
  579. ok($good)
  580.  
  581. $good = true;
  582. for i in 4000..4096
  583.   n1 = 1 << i;
  584.   if (n1**2-1) / (n1+1) != (n1-1)
  585.     $good = false
  586.   end
  587. end
  588. ok($good)
  589.  
  590. check "string & char"
  591.  
  592. ok("abcd" == "abcd")
  593. ok("abcd" =~ "abcd")
  594. ok("abcd" === "abcd")
  595. ok("abc" !~ /^$/)
  596. ok("abc\n" !~ /^$/)
  597. ok("abc" !~ /^d*$/)
  598. ok(("abc" =~ /d*$/) == 3)
  599. ok("" =~ /^$/)
  600. ok("\n" =~ /^$/)
  601. ok("a\n\n" =~ /^$/)
  602. ok("abcabc" =~ /.*a/ && $& == "abca")
  603. ok("abcabc" =~ /.*c/ && $& == "abcabc")
  604. ok("abcabc" =~ /.*?a/ && $& == "a")
  605. ok("abcabc" =~ /.*?c/ && $& == "abc")
  606. ok(/(.|\n)*?\n(b|\n)/ =~ "a\nb\n\n" && $& == "a\nb")
  607. $x = <<END;
  608. ABCD
  609. ABCD
  610. END
  611.  
  612. ok(/^(ab+)+b/ =~ "ababb" && $& == "ababb")
  613. ok(/^(?:ab+)+b/ =~ "ababb" && $& == "ababb")
  614. ok(/^(ab+)+/ =~ "ababb" && $& == "ababb")
  615. ok(/^(?:ab+)+/ =~ "ababb" && $& == "ababb")
  616.  
  617. ok(/(\s+\d+){2}/ =~ " 1 2" && $& == " 1 2")
  618. ok(/(?:\s+\d+){2}/ =~ " 1 2" && $& == " 1 2")
  619.  
  620. $x.gsub!(/((.|\n)*?)B((.|\n)*?)D/){$1+$3}
  621. ok($x == "AC\nAC\n")
  622.  
  623. ok("foobar" =~ /foo(?=(bar)|(baz))/)
  624. ok("foobaz" =~ /foo(?=(bar)|(baz))/)
  625.  
  626. $foo = "abc"
  627. ok("#$foo = abc" == "abc = abc")
  628. ok("#{$foo} = abc" == "abc = abc")
  629.  
  630. foo = "abc"
  631. ok("#{foo} = abc" == "abc = abc")
  632.  
  633. ok('-' * 5 == '-----')
  634. ok('-' * 1 == '-')
  635. ok('-' * 0 == '')
  636.  
  637. foo = '-'
  638. ok(foo * 5 == '-----')
  639. ok(foo * 1 == '-')
  640. ok(foo * 0 == '')
  641.  
  642. $x = "a.gif"
  643. ok($x.sub(/.*\.([^\.]+)$/, '\1') == "gif")
  644. ok($x.sub(/.*\.([^\.]+)$/, 'b.\1') == "b.gif")
  645. ok($x.sub(/.*\.([^\.]+)$/, '\2') == "")
  646. ok($x.sub(/.*\.([^\.]+)$/, 'a\2b') == "ab")
  647. ok($x.sub(/.*\.([^\.]+)$/, '<\&>') == "<a.gif>")
  648.  
  649. # character constants(assumes ASCII)
  650. ok("a"[0] == ?a)
  651. ok(?a == ?a)
  652. ok(?\C-a == 1)
  653. ok(?\M-a == 225)
  654. ok(?\M-\C-a == 129)
  655. ok("a".upcase![0] == ?A)
  656. ok("A".downcase![0] == ?a)
  657. ok("abc".tr!("a-z", "A-Z") == "ABC")
  658. ok("aabbcccc".tr_s!("a-z", "A-Z") == "ABC")
  659. ok("abc".tr!("0-9", "A-Z") == nil)
  660. ok("abcc".squeeze!("a-z") == "abc")
  661. ok("abcd".delete!("bc") == "ad")
  662.  
  663. $x = "abcdef"
  664. $y = [ ?a, ?b, ?c, ?d, ?e, ?f ]
  665. $bad = false
  666. $x.each_byte {|i|
  667.   if i != $y.shift
  668.     $bad = true
  669.     break
  670.   end
  671. }
  672. ok(!$bad)
  673.  
  674. check "asignment"
  675. a = nil
  676. ok(defined?(a))
  677. ok(a == nil)
  678.  
  679. # multiple asignment
  680. a, b = 1, 2
  681. ok(a == 1 && b == 2)
  682.  
  683. a, b = b, a
  684. ok(a == 2 && b == 1)
  685.  
  686. a, = 1,2
  687. ok(a == 1)
  688.  
  689. a, *b = 1, 2, 3
  690. ok(a == 1 && b == [2, 3])
  691.  
  692. a, (b, c), d = 1, [2, 3], 4
  693. ok(a == 1 && b == 2 && c == 3 && d == 4)
  694.  
  695. *a = 1, 2, 3
  696. ok(a == [1, 2, 3])
  697.  
  698. *a = 1..3            # array conversion
  699. ok(a == [1, 2, 3])
  700.  
  701. check "call"
  702. def aaa(a, b=100, *rest)
  703.   res = [a, b]
  704.   res += rest if rest
  705.   return res
  706. end
  707.  
  708. # not enough argument
  709. begin
  710.   aaa()                # need at least 1 arg
  711.   ok(false)
  712. rescue
  713.   ok(true)
  714. end
  715.  
  716. begin
  717.   aaa                # no arg given (exception raised)
  718.   ok(false)
  719. rescue
  720.   ok(true)
  721. end
  722.  
  723. ok(aaa(1) == [1, 100])
  724. ok(aaa(1, 2) == [1, 2])
  725. ok(aaa(1, 2, 3, 4) == [1, 2, 3, 4])
  726. ok(aaa(1, *[2, 3, 4]) == [1, 2, 3, 4])
  727.  
  728. check "proc"
  729. $proc = proc{|i| i}
  730. ok($proc.call(2) == 2)
  731. ok($proc.call(3) == 3)
  732.  
  733. $proc = proc{|i| i*2}
  734. ok($proc.call(2) == 4)
  735. ok($proc.call(3) == 6)
  736.  
  737. proc{
  738.   iii=5                # nested local variable
  739.   $proc = proc{|i|
  740.     iii = i
  741.   }
  742.   $proc2 = proc {
  743.     $x = iii            # nested variables shared by procs
  744.   }
  745.   # scope of nested variables
  746.   ok(defined?(iii))
  747. }.call
  748. ok(!defined?(iii))        # out of scope
  749.  
  750. $x=0
  751. $proc.call(5)
  752. $proc2.call
  753. ok($x == 5)
  754.  
  755. if defined? Process.kill
  756.   check "signal"
  757.  
  758.   $x = 0
  759.   trap "SIGINT", proc{|sig| $x = 2}
  760.   Process.kill "SIGINT", $$
  761.   sleep 0.1
  762.   ok($x == 2)
  763.  
  764.   trap "SIGINT", proc{raise "Interrupt"}
  765.  
  766.   x = false
  767.   begin
  768.     Process.kill "SIGINT", $$
  769.     sleep 0.1
  770.   rescue
  771.     x = $!
  772.   end
  773.   ok(x && /Interrupt/ =~ x)
  774. end
  775.  
  776. check "eval"
  777. ok(eval("") == nil)
  778. $bad=false
  779. eval 'while false; $bad = true; print "foo\n" end'
  780. ok(!$bad)
  781.  
  782. ok(eval('TRUE'))
  783. ok(eval('true'))
  784. ok(!eval('NIL'))
  785. ok(!eval('nil'))
  786. ok(!eval('FALSE'))
  787. ok(!eval('false'))
  788.  
  789. $foo = 'ok(true)'
  790. begin
  791.   eval $foo
  792. rescue
  793.   ok(false)
  794. end
  795.  
  796. ok(eval("$foo") == 'ok(true)')
  797. ok(eval("true") == true)
  798. i = 5
  799. ok(eval("i == 5"))
  800. ok(eval("i") == 5)
  801. ok(eval("defined? i"))
  802.  
  803. # eval with binding
  804. def test_ev
  805.   local1 = "local1"
  806.   lambda {
  807.     local2 = "local2"
  808.     return binding
  809.   }.call
  810. end
  811.  
  812. $x = test_ev
  813. ok(eval("local1", $x) == "local1") # normal local var
  814. ok(eval("local2", $x) == "local2") # nested local var
  815. $bad = true
  816. begin
  817.   p eval("local1")
  818. rescue NameError        # must raise error
  819.   $bad = false
  820. end
  821. ok(!$bad)
  822.  
  823. module EvTest
  824.   EVTEST1 = 25
  825.   evtest2 = 125
  826.   $x = binding
  827. end
  828. ok(eval("EVTEST1", $x) == 25)    # constant in module
  829. ok(eval("evtest2", $x) == 125)    # local var in module
  830. $bad = true
  831. begin
  832.   eval("EVTEST1")
  833. rescue NameError        # must raise error
  834.   $bad = false
  835. end
  836. ok(!$bad)
  837.  
  838. x = proc{}
  839. eval "i4 = 1", x
  840. ok(eval("i4", x) == 1)
  841. x = proc{proc{}}.call
  842. eval "i4 = 22", x
  843. ok(eval("i4", x) == 22)
  844. $x = []
  845. x = proc{proc{}}.call
  846. eval "(0..9).each{|i5| $x[i5] = proc{i5*2}}", x
  847. ok($x[4].call == 8)
  848.  
  849. x = binding
  850. eval "i = 1", x
  851. ok(eval("i", x) == 1)
  852. x = proc{binding}.call
  853. eval "i = 22", x
  854. ok(eval("i", x) == 22)
  855. $x = []
  856. x = proc{binding}.call
  857. eval "(0..9).each{|i5| $x[i5] = proc{i5*2}}", x
  858. ok($x[4].call == 8)
  859. x = proc{binding}.call
  860. eval "for i6 in 1..1; j6=i6; end", x
  861. ok(eval("defined? i6", x))
  862. ok(eval("defined? j6", x))
  863.  
  864. proc {
  865.   p = binding
  866.   eval "foo11 = 1", p
  867.   foo22 = 5
  868.   proc{foo11=22}.call
  869.   proc{foo22=55}.call
  870.   ok(eval("foo11", p) == eval("foo11"))
  871.   ok(eval("foo11") == 1)
  872.   ok(eval("foo22", p) == eval("foo22"))
  873.   ok(eval("foo22") == 55)
  874. }.call
  875.  
  876. p1 = proc{i7 = 0; proc{i7}}.call
  877. ok(p1.call == 0)
  878. eval "i7=5", p1
  879. ok(p1.call == 5)
  880. ok(!defined?(i7))
  881.  
  882. p1 = proc{i7 = 0; proc{i7}}.call
  883. i7 = nil
  884. ok(p1.call == 0)
  885. eval "i7=1", p1
  886. ok(p1.call == 1)
  887. eval "i7=5", p1
  888. ok(p1.call == 5)
  889. ok(i7 == nil)
  890.  
  891. check "system"
  892. ok(`echo foobar` == "foobar\n")
  893. ok(`./miniruby -e 'print "foobar"'` == 'foobar')
  894.  
  895. tmp = open("script_tmp", "w")
  896. tmp.print "print $zzz\n";
  897. tmp.close
  898.  
  899. ok(`./miniruby -s script_tmp -zzz` == 'true')
  900. ok(`./miniruby -s script_tmp -zzz=555` == '555')
  901.  
  902. tmp = open("script_tmp", "w")
  903. tmp.print "#! /usr/local/bin/ruby -s\n";
  904. tmp.print "print $zzz\n";
  905. tmp.close
  906.  
  907. ok(`./miniruby script_tmp -zzz=678` == '678')
  908.  
  909. tmp = open("script_tmp", "w")
  910. tmp.print "this is a leading junk\n";
  911. tmp.print "#! /usr/local/bin/ruby -s\n";
  912. tmp.print "print $zzz\n";
  913. tmp.print "__END__\n";
  914. tmp.print "this is a trailing junk\n";
  915. tmp.close
  916.  
  917. ok(`./miniruby -x script_tmp` == 'nil')
  918. ok(`./miniruby -x script_tmp -zzz=555` == '555')
  919.  
  920. tmp = open("script_tmp", "w")
  921. for i in 1..5
  922.   tmp.print i, "\n"
  923. end
  924. tmp.close
  925.  
  926. `./miniruby -i.bak -pe 'sub(/^[0-9]+$/){$&.to_i * 5}' script_tmp`
  927. done = true
  928. tmp = open("script_tmp", "r")
  929. while tmp.gets
  930.   if $_.to_i % 5 != 0
  931.     done = false
  932.     break
  933.   end
  934. end
  935. tmp.close
  936. ok(done)
  937.   
  938. File.unlink "script_tmp" or `/bin/rm -f "script_tmp"`
  939. File.unlink "script_tmp.bak" or `/bin/rm -f "script_tmp.bak"`
  940.  
  941. $bad = false
  942. for script in Dir["{lib,sample}/*.rb"]
  943.   unless `./miniruby -c #{script}`.chomp == "Syntax OK"
  944.     $bad = true
  945.   end
  946. end
  947. ok(!$bad)
  948.  
  949. check "const"
  950. TEST1 = 1
  951. TEST2 = 2
  952.  
  953. module Const
  954.   TEST3 = 3
  955.   TEST4 = 4
  956. end
  957.  
  958. module Const2
  959.   TEST3 = 6
  960.   TEST4 = 8
  961. end
  962.  
  963. include Const
  964.  
  965. ok([TEST1,TEST2,TEST3,TEST4] == [1,2,3,4])
  966.  
  967. include Const2
  968. STDERR.print "intentionally redefines TEST3, TEST4\n" if $VERBOSE
  969. ok([TEST1,TEST2,TEST3,TEST4] == [1,2,6,8])
  970.  
  971. check "clone"
  972. foo = Object.new
  973. def foo.test
  974.   "test"
  975. end
  976. bar = foo.clone
  977. def bar.test2
  978.   "test2"
  979. end
  980.  
  981. ok(bar.test2 == "test2")
  982. ok(bar.test == "test")
  983. ok(foo.test == "test")  
  984.  
  985. begin
  986.   foo.test2
  987.   ok false
  988. rescue
  989.   ok true
  990. end
  991.  
  992. check "marshal"
  993. $x = [1,2,3,[4,5,"foo"],{1=>"bar"},2.5,fact(30)]
  994. $y = Marshal.dump($x)
  995. ok($x == Marshal.load($y))
  996.  
  997. check "pack"
  998.  
  999. $format = "c2x5CCxsdila6";
  1000. # Need the expression in here to force ary[5] to be numeric.  This avoids
  1001. # test2 failing because ary2 goes str->numeric->str and ary does not.
  1002. ary = [1,-100,127,128,32767,987.654321098 / 100.0,12345,123456,"abcdef"]
  1003. $x = ary.pack($format)
  1004. ary2 = $x.unpack($format)
  1005.  
  1006. ok(ary.length == ary2.length)
  1007. ok(ary.join(':') == ary2.join(':'))
  1008. ok($x =~ /def/)
  1009.  
  1010. check "math"
  1011. ok(Math.sqrt(4) == 2)
  1012.  
  1013. include Math
  1014. ok(sqrt(4) == 2)
  1015.  
  1016. check "struct"
  1017. struct_test = Struct.new("Test", :foo, :bar)
  1018. ok(struct_test == Struct::Test)
  1019.  
  1020. test = struct_test.new(1, 2)
  1021. ok(test.foo == 1 && test.bar == 2)
  1022. ok(test[0] == 1 && test[1] == 2)
  1023.  
  1024. a, b = test
  1025. ok(a == 1 && b == 2)
  1026.  
  1027. test[0] = 22
  1028. ok(test.foo == 22)
  1029.  
  1030. test.bar = 47
  1031. ok(test.bar == 47)
  1032.  
  1033. check "variable"
  1034. ok($$.instance_of?(Fixnum))
  1035.  
  1036. # read-only variable
  1037. begin
  1038.   $$ = 5
  1039.   ok false
  1040. rescue
  1041.   ok true
  1042. end
  1043.  
  1044. foobar = "foobar"
  1045. $_ = foobar
  1046. ok($_ == foobar)
  1047.  
  1048. check "trace"
  1049. $x = 1234
  1050. $y = 0
  1051. trace_var :$x, proc{$y = $x}
  1052. $x = 40414
  1053. ok($y == $x)
  1054.  
  1055. untrace_var :$x
  1056. $x = 19660208
  1057. ok($y != $x)
  1058.  
  1059. trace_var :$x, proc{$x *= 2}
  1060. $x = 5
  1061. ok($x == 10)
  1062.  
  1063. untrace_var :$x
  1064.  
  1065. check "defined?"
  1066.  
  1067. ok(defined?($x))        # global variable
  1068. ok(defined?($x) == 'global-variable')# returns description
  1069.  
  1070. foo=5
  1071. ok(defined?(foo))        # local variable
  1072.  
  1073. ok(defined?(Array))        # constant
  1074. ok(defined?(Object.new))    # method
  1075. ok(!defined?(Object.print))    # private method
  1076. ok(defined?(1 == 2))        # operator expression
  1077.  
  1078. def defined_test
  1079.   return !defined?(yield)
  1080. end
  1081.  
  1082. ok(defined_test)        # not iterator
  1083. ok(!defined_test{})        # called as iterator
  1084.  
  1085. check "alias"
  1086. class Alias0
  1087.   def foo; "foo" end
  1088. end
  1089. class Alias1<Alias0
  1090.   alias bar foo
  1091.   def foo; "foo+" + super end
  1092. end
  1093. class Alias2<Alias1
  1094.   alias baz foo
  1095.   undef foo
  1096. end
  1097.  
  1098. x = Alias2.new
  1099. ok(x.bar == "foo")
  1100. ok(x.baz == "foo+foo")
  1101.  
  1102. # check for cache
  1103. ok(x.baz == "foo+foo")
  1104.  
  1105. class Alias3<Alias2
  1106.   def foo
  1107.     defined? super
  1108.   end
  1109.   def bar
  1110.     defined? super
  1111.   end
  1112.   def quux
  1113.     defined? super
  1114.   end
  1115. end
  1116. x = Alias3.new
  1117. ok(!x.foo)
  1118. ok(x.bar)
  1119. ok(!x.quux)
  1120.  
  1121. check "gc"
  1122. begin
  1123.   1.upto(10000) {
  1124.     tmp = [0,1,2,3,4,5,6,7,8,9]
  1125.   }
  1126.   tmp = nil
  1127.   ok true
  1128. rescue
  1129.   ok false
  1130. end
  1131.  
  1132. if $failed > 0
  1133.   printf "test: %d failed %d\n", $ntest, $failed
  1134. else
  1135.   printf "end of test(test: %d)\n", $ntest
  1136. end
  1137.