Skip to content
This repository
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 108 lines (98 sloc) 1.859 kb
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
# flip-flop 2 should preserve its state accross different calls to the block it is in
# Test from Tomáš Matoušek of IronRuby
assert "0", %{
def y *a; yield *a; end
$a = 0
def test
$p = proc do |b, e|
if b..e
$a += 1
else
$a -= 1
end
end

y false, &$p
y true, true, &$p
y false, &$p
y true, false, &$p
end
test
p $a
}

# flip-flop 3 should preserve its state accross different calls to the block it is in
# Test from Tomáš Matoušek of IronRuby
assert "2", %{
def y *a; yield *a; end
$a = 0
def test
$p = proc do |b, e|
if b...e
$a += 1
else
$a -= 1
end
end

y false, &$p
y true, true, &$p
y false, &$p
y true, false, &$p
end
test
p $a
}

# General flip-flop test:
# Not only does it test the general use of a flip-flop operator,
# it also tests the `begin` and `end` block are only evaluated if needed
# Test from Tomáš Matoušek of IronRuby
assert "bfbetetetbetbfbfbf", %{
F = false
T = true
x = X = '!'
B = [F,T,x,x,x,T,x,F,F]
E = [x,x,F,F,T,x,T,x,x]

def b; step('b',B); end
def e; step('e',E); end

def step name, value
r = value[$j]
putc name
$j += 1
$continue = !r.nil?
r == X ? raise : r
end

$j = 0
$continue = true
while $continue
putc (b..e ? 't' : 'f')
end
}

# Recursive flip-flop test
assert "dadbaddb", %{
def y *a; yield *a; end
def test
$p = proc { |b, e|
if b..e
if e..b
putc 'a'
else
putc 'b'
end
else
if e..b
putc 'c'
else
putc 'd'
end
end
}

y false, &$p
y true, true, &$p
y false, &$p
y true, false, &$p
y true, true, &$p
y false, &$p
y false, &$p
y true, false, &$p
puts
end
test
}
Something went wrong with that request. Please try again.