-
Notifications
You must be signed in to change notification settings - Fork 2
/
test.fiz
125 lines (97 loc) · 2.32 KB
/
test.fiz
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
# Test the looping behaviour
set a 0
while {expr $a < 100} {
incr a;
# Test the continue statement
if {expr $a = 4} continue;
puts $a;
# Test the break statement
if {expr $a=8} break
}
# The following should work:
set x pu
set y ts
$x$y "hello world"
# Test recursion, through a factorial function
proc fac {x} {
if {expr $x<2} {return 1};
return [expr $x*[fac [expr $x-1]]]
}
puts "5! = [fac 5]"
dict numbers put alice "FAIL"
dict numbers put alice 1111222 # Replace alice's number with something sane
dict numbers put bob 2222333
dict numbers put carol 3333444
dict numbers put dwayne 5555666
dict numbers put edgar 6666777
dict numbers foreach k v do {puts "$k's number is $v"}
dict numbers remove alice
puts "** After removing alice:"
dict numbers foreach k v do {puts "$k's number is $v"}
set a hello
set b world
if {eq $a $b} {puts FAIL} else {puts WIN}
if {ne $a $b} {puts WIN} else {puts FAIL}
if {eq $a $a} {puts WIN} else {puts FAIL}
if {ne $b $b} {puts FAIL} else {puts WIN}
set i 1
proc set_i_to_2 {} {
# puts "local $i" --- this would be an error
set i 2
puts "local $i"
}
proc set_i_to_3 {} {
global i
puts "local $i"
set i 3
puts "local $i"
}
puts "top $i"
assert { eq $i 1 }
# prints "top 1"
set_i_to_2
# prints "local 2"
puts "top $i"
assert { eq $i 1 }
# prints "top 1"
# note: was not set to "2" - it was a local variable
set_i_to_3
# prints "local 1"
# prints "local 3"
puts "top $i"
assert { eq $i 3 }
# prints "top 3"
# note: was not set to "3" - it was a global variable
set i 0
proc increment_i {} {
global i
set i [expr $i + 1]
}
increment_i
increment_i
increment_i
increment_i
increment_i
puts "i = $i"
assert { eq $i 5 }
# prints "i = 5"
proc set_never_used_before_global {} {
global vvv
set vvv 123
}
set_never_used_before_global
puts $vvv
assert { eq $vvv 123 }
set messageVar "(none)"
puts "cought=[catch { assert { eq 1 1 } } messageVar] $messageVar"
# prints "cought=0 (none)"
puts "cought=[catch { assert { eq 1 2 } } messageVar] $messageVar"
# prints "cought=1 Assertion failed: eq 1 2"
if { expr [catch { assert { eq 1 2 } } messageVar]} {
puts "Cought error: $messageVar"
}
# prints "Cought error 1: Assertion failed: eq 1 2"
if { expr [catch { assert { eq 2 2 } } messageVar]} {
puts "should not happen"
}
# doesn't print anything, because the `eq 2 2` assertion succeeded