regsub: fix substitution with a trailing backslash
[jimtcl.git] / tests / expr.test
blob682af89a825f4188e225dc9a5db8593164f6ae21
1 source [file dirname [info script]]/testing.tcl
3 test expr-1.1 "Compare strings lt" {
4         expr {"V000500" < "V000405"}
5 } {0}
7 test expr-1.2 "Compare strings with embedded nulls" {
8         set s1 [format abc%cdef 0]
9         set s2 [format abc%cghi 0]
10         expr {$s1 < $s2}
11 } {1}
13 test expr-1.3 "Hex values" {
14         set mask1 [expr 0x4050 & 0x0CCC]
15 } {64}
17 test expr-1.4 "Ternary operator - true" {
18         expr {1 ? 2 : 3}
19 } {2}
21 test expr-1.5 "Ternary operator - false" {
22         expr {0 ? 2 : 3}
23 } {3}
25 test expr-1.6 "Ternary operator - double check" {
26         expr {1.0 ? 2 : 3}
27 } {2}
29 test expr-1.7 "Ternary operator - string result" {
30         expr {1 ? "two" : 3}
31 } {two}
33 test expr-1.8 "Ternary operator - don't eval false path" {
34         set a 100
35         set b 200
36         set c [expr {20 ? [incr a] : [incr b]}]
37         list $a $b $c
38 } {101 200 101}
40 test expr-1.9 "Unary minus" {
41         set a 1
42         expr {-$a}
43 } {-1}
45 test expr-1.10 "Subtraction" {
46         set a 1
47         set b 10
48         expr {$b-$a}
49 } {9}
51 test expr-1.11 "Short circuit evaluation" {
52         set a 100
53         set c [expr {0 || [incr a]}]
54         list $a $c
55 } {101 1}
57 test expr-1.12 "Short circuit evaluation" {
58         set a 100
59         set c [expr {1 || [incr a]}]
60         list $a $c
61 } {100 1}
63 test expr-1.13 "Short circuit evaluation" {
64         set a 100
65         set c [expr {1 || [incr a] && [incr a]}]
66         list $a $c
67 } {100 1}
69 test expr-1.14 "Rotate left" jim {
70         expr {1 <<< 5}
71 } {32}
73 test expr-1.15 "Rotate left" jim {
74         expr {1 <<< 65}
75 } {2}
77 test expr-1.16 "Rotate right" jim {
78         expr {1 >>> 48}
79 } {65536}
81 test expr-1.17 "Rotate left" jim {
82         expr {1 >>> 63}
83 } {2}
85 # This crashes older jim
86 test expr-2.1 "bogus unarymin" {
87         catch {expr {unarymin 1}}
88         return 1
89 } {1}
91 test expr-2.2 "Ternary operator - missing colon" {
92         list [catch {expr {1 ? 2 3}} msg]
93 } {1}
95 test expr-2.3 "Ternary operator - missing third term" {
96         list [catch {expr {1 ? 2}} msg]
97 } {1}
99 test expr-2.4 "Ternary operator - missing question" {
100         list [catch {expr {1 : 2}} msg]
101 } {1}
103 test expr-3.1 "in, ni operators" {
104         set l {a b c d}
105         set c C
106         list [expr {"a" in $l}] [expr {$c in $l}] [expr {"b" ni $l}] [expr {$c ni $l}]
107 } {1 0 0 1}
109 test expr-3.2 "if: in, ni operators" {
110         set l {a b c d}
111         set a a
112         set c C
113         set result {}
114         if {$a in $l} {
115                 lappend result 1
116         }
117         if {$c in $l} {
118                 lappend result 2
119         }
120         if {$a ni $l} {
121                 lappend result 3
122         }
123         if {$c ni $l} {
124                 lappend result 4
125         }
126         if {"d" in $l} {
127                 lappend result 5
128         }
129 } {1 4 5}
131 # Don't want a to become 2.0
132 test expr-4.1 "Shimmering" {
133         set a 2
134         expr {$a < 3.0}
135         set a
136 } {2}
138 testreport