Skip to content
This repository
Newer
Older
100644 107 lines (98 sloc) 1.859 kb
c437b620 » Thibault Martin-Lagardette
2010-02-03 - Adds the flip-flop operator (fixes #548)
1 # flip-flop 2 should preserve its state accross different calls to the block it is in
2 # Test from Tomáš Matoušek of IronRuby
3 assert "0", %{
4 def y *a; yield *a; end
5 $a = 0
6 def test
7 $p = proc do |b, e|
8 if b..e
9 $a += 1
10 else
11 $a -= 1
12 end
13 end
14
15 y false, &$p
16 y true, true, &$p
17 y false, &$p
18 y true, false, &$p
19 end
20 test
21 p $a
22 }
23
24 # flip-flop 3 should preserve its state accross different calls to the block it is in
25 # Test from Tomáš Matoušek of IronRuby
26 assert "2", %{
27 def y *a; yield *a; end
28 $a = 0
29 def test
30 $p = proc do |b, e|
31 if b...e
32 $a += 1
33 else
34 $a -= 1
35 end
36 end
37
38 y false, &$p
39 y true, true, &$p
40 y false, &$p
41 y true, false, &$p
42 end
43 test
44 p $a
45 }
46
47 # General flip-flop test:
48 # Not only does it test the general use of a flip-flop operator,
49 # it also tests the `begin` and `end` block are only evaluated if needed
50 # Test from Tomáš Matoušek of IronRuby
51 assert "bfbetetetbetbfbfbf", %{
52 F = false
53 T = true
54 x = X = '!'
55 B = [F,T,x,x,x,T,x,F,F]
56 E = [x,x,F,F,T,x,T,x,x]
57
58 def b; step('b',B); end
59 def e; step('e',E); end
60
61 def step name, value
62 r = value[$j]
63 putc name
64 $j += 1
65 $continue = !r.nil?
66 r == X ? raise : r
67 end
68
69 $j = 0
70 $continue = true
71 while $continue
72 putc (b..e ? 't' : 'f')
73 end
74 }
75
76 # Recursive flip-flop test
77 assert "dadbaddb", %{
78 def y *a; yield *a; end
79 def test
80 $p = proc { |b, e|
81 if b..e
82 if e..b
83 putc 'a'
84 else
85 putc 'b'
86 end
87 else
88 if e..b
89 putc 'c'
90 else
91 putc 'd'
92 end
93 end
94 }
95
96 y false, &$p
97 y true, true, &$p
98 y false, &$p
99 y true, false, &$p
100 y true, true, &$p
101 y false, &$p
102 y false, &$p
103 y true, false, &$p
104 puts
105 end
106 test
107 }
Something went wrong with that request. Please try again.