/
control_structures_test.rb
executable file
·89 lines (75 loc) · 1.29 KB
/
control_structures_test.rb
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
#!/usr/bin/env ruby
require 'test/unit'
class ControlStructuresTest < Test::Unit::TestCase
def test_while
x = 0
while x < 10
x += 1
end
assert_equal(10, x)
end
def test_while_modifier
x = 0
x += 1 while x < 10
assert_equal(10, x)
end
def test_until
x = 0
until x == 10
x += 1
end
assert_equal(10, x)
end
def test_until_modifier
x = 0
x += 1 until x == 10
assert_equal(10, x)
end
def test_for
numbers = [1, 2, 3]
x = 0
for i in numbers
x += i
end
assert_equal(6, x)
end
# test above translates into...
def test_for_iterator
numbers = [1, 2, 3]
x = 0
numbers.each { |i| x += i }
assert_equal(6, x)
end
def test_for_upto
x = 0
1.upto(3) { |i| x += i }
assert_equal(6, x)
end
def test_loop
x = 0
loop do
x += 1
break if x > 4
end
assert_equal(5, x)
end
def test_loop_next
numbers = []
x = 0
loop do
x += 1
next if x < 3
numbers << x
break if x > 4
end
assert_equal([3, 4, 5], numbers)
end
def test_retry
numbers = []
for i in 1..3
numbers << i
retry if i == 3 and numbers.length < 6
end
assert_equal([1, 2, 3, 1, 2, 3], numbers)
end
end