1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
|
require 'test/unit'
class TestLoopStuff < Test::Unit::TestCase
IS19 = RUBY_VERSION =~ /1\.9/
FILE = "while_tmp"
def setup
File.open(FILE, "w") do |tmp|
assert(tmp.kind_of?(File))
tmp.puts "tvi925"
tmp.puts "tvi920"
tmp.puts "vt100"
tmp.puts "Amiga"
tmp.puts "paper"
end
end
def teardown
File.unlink(FILE)
end
def testBreak
File.open(FILE) do |tmp|
while line = tmp.gets()
break if /vt100/ =~ line
end
assert(!tmp.eof?)
assert_not_nil(/vt100/ =~ line)
end
end
def testBreakWithInterval
File.open("while_tmp") do |tmp|
while tmp.gets()
break if 3
assert($_ !~ /[vt100|Amiga|paper]/)
end
end
end
def testNext
File.open(FILE) do |tmp|
while line = tmp.gets()
next if /vt100/ =~ line
assert(/vt100/ !~ line)
end
assert(tmp.eof?)
assert(/vt100/ !~ line)
end
end
unless IS19
def testRedoWithWhile
File.open(FILE) do |tmp|
while tmp.gets()
line = $_
$_ = gsub(/vt100/, 'VT100')
if $_ != line
gsub!('VT100', 'Vt100')
redo;
end
assert(/vt100/ !~ $_)
assert(/VT100/ !~ $_)
end
assert(tmp.eof?)
end
end
end
def testRedoWithFor
sum = 0
for i in 1..10
sum += i
i -= 1
if i > 0
redo
end
end
assert_equal(220, sum)
end
def testUntil
i = 0
until i>4
i+=1
end
assert(i>4)
end
##########
# Iterators
#
def testNoIterator
assert(!iterator?)
end
def iteratorHelper
iterator?
end
def testIterator
assert(iteratorHelper {})
end
def testTopLevelYield
assert(!defined?(yield))
end
def iterateOverArray
x = [1, 2, 3, 4]
y = []
for i in x
y.push i
end
assert_equal(x, y)
end
def nestedIteratorHelper
1.upto(10) do |i|
yield i
end
end
def testNestedIterator
i = 0
nestedIteratorHelper { |i2| i = i2; break if i == 5 }
assert_equal(5, i)
end
if false
# what is this actually testing???
def tt2(dummy)
yield 1
end
def tt3(&block)
tt2(raise(ArgumentError,""),&block)
end
$x = false
begin
tt3{}
rescue ArgumentError
$x = true
rescue Exception
end
test_ok($x)
end
# iterator break/redo/next/retry
def testIteratorBreak
done = true
loop do
break
done = false # should not reach here
end
assert(done)
end
def testIteratorNext
done = false
loop do
break if done
done = true
next
fail("shouldn't read here")
end
assert(true)
end
def testIteratorRedo
done = false
loop do
break if done
done = true
redo
fail("should not reach here")
end
assert(true)
end
# CON: I've disabled retry in methods and blocks because of JRUBY-1629 and
# JRUBY-1522 for now; unfortunately retry is looking like something we can't
# reasonably support.
# def testIteratorRetry
# done = false
# x = []
# for i in 1 .. 7 # see how retry works in iterator loop
# if i == 4 and not done
# done = true
# retry
# end
# x.push(i)
# end
# assert_equal(10, x.size)
# assert_equal([1, 2, 3, 1, 2, 3, 4, 5, 6, 7], x)
# end
class IterTest
def initialize(e); @body = e; end
def each0(&block); @body.each(&block); end
def each1(&block); @body.each { |*x| block.call(*x) } end
def each2(&block); @body.each { |*x| block.call(x) } end
def each3(&block); @body.each { |x| block.call(*x) } end
def each4(&block); @body.each { |x| block.call(x) } end
def each5; @body.each { |*x| yield(*x) } end
def each6; @body.each { |*x| yield(x) } end
def each7; @body.each { |x| yield(*x) } end
def each8; @body.each { |x| yield(x) } end
end
def testClassFullOfIterators
x = nil
IterTest.new([0]).each0 { |x2| x = x2 }
assert_equal(0, x)
IterTest.new([1]).each1 { |x2| x = x2 }
assert_equal(1, x)
IterTest.new([2]).each2 { |x2| x = x2 }
assert_equal([2], x)
IterTest.new([3]).each3 { |x2| x = x2 }
assert_equal(3, x)
IterTest.new([4]).each4 { |x2| x = x2 }
assert_equal(4, x)
IterTest.new([5]).each5 { |x2| x = x2 }
assert_equal(5, x)
IterTest.new([6]).each6 { |x2| x = x2 }
assert_equal([6], x)
IterTest.new([7]).each7 { |x2| x = x2 }
assert_equal(7, x)
IterTest.new([8]).each8 { |x2| x = x2 }
assert_equal(8, x)
IterTest.new([[0]]).each0 { |x2| x = x2 }
assert_equal([0], x)
IterTest.new([[1]]).each1 { |x2| x = x2 }
assert_equal([1], x)
IterTest.new([[2]]).each2 { |x2| x = x2 }
assert_equal([[2]], x)
IterTest.new([[3]]).each3 { |x2| x = x2 }
assert_equal(3, x)
IterTest.new([[4]]).each4 { |x2| x = x2 }
assert_equal([4], x)
IterTest.new([[5]]).each5 { |x2| x = x2 }
assert_equal([5], x)
IterTest.new([[6]]).each6 { |x2| x = x2 }
assert_equal([[6]], x)
IterTest.new([[7]]).each7 { |x2| x = x2 }
assert_equal(7, x)
IterTest.new([[8]]).each8 { |x2| x = x2 }
assert_equal([8], x)
IterTest.new([[0,0]]).each0 { |x2| x = x2 }
assert_equal([0, 0], x)
IterTest.new([[8,8]]).each8 { |x2| x = x2 }
assert_equal([8, 8], x)
end
=begin This fails in MRI...
def testAppendIteratorToBuiltin
x = [[1,2],[3,4],[5,6]]
assert_equal(x.iter_test1{|x|x}, x.iter_test2{|x|x})
end
=end
end
# append method to built-in class
class Array
def iter_test1
collect{|e| [e, yield(e)]}.sort{|a,b|a[1]<=>b[1]}
end
def iter_test2
a = collect{|e| [e, yield(e)]}
a.sort{|a,b|a[1]<=>b[1]}
end
end
|