|
|
1.1 root 1: # This file contains contains a collection of tests for the "expr"
2: # command in Tcl. If everything is OK then it finishes silently.
3: # If a problem is detected then it generates a Tcl error with a
4: # cryptic message. To trace the error you'll have to read through
5: # the commands in this file.
6: #
7: # $Header: /sprite/src/lib/tcl/tests/RCS/expr.test,v 1.1 90/03/22 15:24:40 ouster Exp $ (Berkeley)
8:
9: proc check {a b num} {
10: if {$a != $b} {
11: error [format {Expr error %s: wanted "%s", got "%s"} $num $b $a]}
12: }
13: proc checkstr {a b num} {
14: if {[string compare $a $b] != 0} {
15: error [format {Expr error %s: wanted "%s", got "%s"} $num $b $a]}
16: }
17:
18: # First, test all of the operators individually.
19:
20: check [expr -4] -4 1.1
21: check [expr -(1+4)] -5 1.2
22: check [expr ~3] 0xfffffffc 1.3
23: check [expr !2] 0 1.3
24: check [expr !0] 1 1.4
25: check [expr 4*6] 24 1.5
26: check [expr 36/12] 3 1.6
27: check [expr 27/4] 6 1.7
28: check [expr 27%4] 3 1.8
29: check [expr 2+2] 4 1.9
30: check [expr 2-6] -4 1.10
31: check [expr 1<<3] 8 1.11
32: check [expr 0xff>>2] 0x3f 1.12
33: check [expr -1>>2] -1 1.13
34: check [expr 3>2] 1 1.14
35: check [expr 2>2] 0 1.15
36: check [expr 1>2] 0 1.16
37: check [expr 3<2] 0 1.17
38: check [expr 2<2] 0 1.18
39: check [expr 1<2] 1 1.19
40: check [expr 3>=2] 1 1.20
41: check [expr 2>=2] 1 1.21
42: check [expr 1>=2] 0 1.22
43: check [expr 3<=2] 0 1.23
44: check [expr 2<=2] 1 1.24
45: check [expr 1<=2] 1 1.25
46: check [expr 3==2] 0 1.26
47: check [expr 2==2] 1 1.27
48: check [expr 3!=2] 1 1.28
49: check [expr 2!=2] 0 1.29
50: check [expr 7&0x13] 3 1.30
51: check [expr 7^0x13] 0x14 1.31
52: check [expr 7|0x13] 0x17 1.32
53: check [expr 0&&1] 0 1.33
54: check [expr 0&&0] 0 1.34
55: check [expr 1&&3] 1 1.35
56: check [expr 0||1] 1 1.36
57: check [expr 3||0] 1 1.37
58: check [expr 0||0] 0 1.38
59: check [expr 3>2?44:66] 44 1.39
60: check [expr 2>3?44:66] 66 1.40
61:
62: # Check precedence pairwise.
63:
64: check [expr -~3] 4 2.1
65: check [expr -!3] 0 2.2
66: check [expr -~0] 1 2.3
67:
68: check [expr 2*4/6] 1 3.1
69: check [expr 24/6*3] 12 3.2
70: check [expr 24/6/2] 2 3.3
71:
72: check [expr -2+4] 2 4.1
73: check [expr -2-4] -6 4.2
74:
75: check [expr 2*3+4] 10 5.1
76: check [expr 8/2+4] 8 5.2
77: check [expr 8%3+4] 6 5.3
78: check [expr 2*3-1] 5 5.4
79: check [expr 8/2-1] 3 5.5
80: check [expr 8%3-1] 1 5.6
81:
82: check [expr 6-3-2] 1 6.1
83:
84: check [expr 7+1>>2] 2 7.1
85: check [expr 7+1<<2] 32 7.2
86: check [expr 7>>3-2] 3 7.3
87: check [expr 7<<3-2] 14 7.4
88:
89: check [expr 6>>1>4] 0 8.1
90: check [expr 6>>1<2] 0 8.2
91: check [expr 6>>1>=3] 1 8.3
92: check [expr 6>>1<=2] 0 8.4
93: check [expr 6<<1>5] 1 8.5
94: check [expr 6<<1<5] 0 8.6
95: check [expr 5<=6<<1] 1 8.7
96: check [expr 5>=6<<1] 0 8.8
97:
98: check [expr 2<3<4] 1 9.1
99: check [expr 0<4>2] 0 9.2
100: check [expr 4>2<1] 0 9.3
101: check [expr 4>3>2] 0 9.4
102: check [expr 4>3>=2] 0 9.5
103: check [expr 4>=3>2] 0 9.6
104: check [expr 4>=3>=2] 0 9.7
105: check [expr 0<=4>=2] 0 9.8
106: check [expr 4>=2<=0] 0 9.9
107: check [expr 2<=3<=4] 1 9.10
108:
109: check [expr 1==4>3] 1 10.1
110: check [expr 0!=4>3] 1 10.2
111: check [expr 1==3<4] 1 10.3
112: check [expr 0!=3<4] 1 10.4
113: check [expr 1==4>=3] 1 10.5
114: check [expr 0!=4>=3] 1 10.6
115: check [expr 1==3<=4] 1 10.7
116: check [expr 0!=3<=4] 1 10.8
117:
118: check [expr 1==3==3] 0 11.1
119: check [expr 3==3!=2] 1 11.2
120: check [expr 2!=3==3] 0 11.3
121: check [expr 2!=1!=1] 0 11.3
122:
123: check [expr 2&3==2] 0 12.1
124: check [expr 1&3!=3] 0 12.2
125:
126: check [expr 7&3^0x10] 0x13 13.1
127: check [expr 7^0x10&3] 7 13.2
128:
129: check [expr 7^0x10|3] 0x17 14.1
130: check [expr 7|0x10^3] 0x17 14.2
131:
132: check [expr 7|3&&1] 1 15.1
133: check [expr 1&&3|7] 1 15.2
134:
135: check [expr 0&&1||1] 1 15.3
136: check [expr 1||1&&0] 1 15.4
137:
138: check [expr 1||0?3:4] 3 16.1
139: check [expr 1?0:4||1] 0 16.2
140:
141: # Parentheses.
142:
143: check [expr (2+4)*6] 36 17.1
144: check [expr (1?0:4)||1] 1 17.2
145:
146: # Embedded commands and variable names.
147:
148: set a 16
149: check [expr {2*$a}] 32 18.1
150: check [expr {[set a] - 14}] 2 18.2
151:
152: # Numbers in various bases.
153:
154: check [expr 0x20] 32 19.1
155: check [expr 015] 13 19.2
156:
157: # Various error conditions.
158:
159: check [catch {expr 2+a} msg] 1 20.1
160: checkstr $msg {syntax error in expression "2+a"} 20.2
161: check [catch {expr 2+4*} msg] 1 20.3
162: check [catch {expr 2+4*(} msg] 1 20.4
163: check [catch {expr 2+$_non_existent_} msg] 1 20.5
164: checkstr $msg {couldn't find variable "_non_existent_"} 20.6
165: set a xx
166: check [catch {expr {2+$a}} msg] 1 20.7
167: checkstr $msg {variable "$a" contained non-numeric value "xx"} 20.8
168: check [catch {expr {2+[set a]}} msg] 1 20.9
169: checkstr $msg {command "set a" returned non-numeric result "xx"} 20.10
170: check [catch {expr {2+(4}} msg] 1 20.11
171: checkstr $msg {unmatched parentheses in expression "2+(4"} 20.12
172: check [catch {expr 2/0} msg] 1 20.13
173: checkstr $msg {divide by zero} 20.14
174: check [catch {expr 2%0} msg] 1 20.15
175: checkstr $msg {divide by zero} 20.16
176: check [catch {expr 2#} msg] 1 20.17
177: checkstr $msg {syntax error in expression "2#"} 20.18
178: check [catch {expr 2?foo:1} msg] 1 20.19
179: check [catch {expr 0?foo:1} msg] 1 20.20
180: check [catch {expr 2?1:foo} msg] 1 20.21
181: check [catch {expr 0?1:foo} msg] 1 20.22
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.