Trim bootstrap jimsh
[jimtcl.git] / tests / expr.test
blobdbe84f5d9383b6c5fa7c378fff2944752a550fe8
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" -body {
87         expr {unarymin 1}
88 } -returnCodes error -match glob -result *
90 test expr-2.2 "Ternary operator - missing colon" -body {
91         expr {1 ? 2 3}
92 } -returnCodes error -match glob -result {missing operator*}
94 test expr-2.3 "Ternary operator - missing third term" -body {
95         expr {1 ? 2}
96 } -returnCodes error -match glob -result *
98 test expr-2.4 "Ternary operator - missing question" -body {
99         expr {1 : 2}
100 } -returnCodes error -match glob -result *
102 test expr-3.1 "in, ni operators" {
103         set l {a b c d}
104         set c C
105         list [expr {"a" in $l}] [expr {$c in $l}] [expr {"b" ni $l}] [expr {$c ni $l}]
106 } {1 0 0 1}
108 test expr-3.2 "if: in, ni operators" {
109         set l {a b c d}
110         set a a
111         set c C
112         set result {}
113         if {$a in $l} {
114                 lappend result 1
115         }
116         if {$c in $l} {
117                 lappend result 2
118         }
119         if {$a ni $l} {
120                 lappend result 3
121         }
122         if {$c ni $l} {
123                 lappend result 4
124         }
125         if {"d" in $l} {
126                 lappend result 5
127         }
128 } {1 4 5}
130 # Don't want a to become 2.0
131 test expr-4.1 "Shimmering" {
132         set a 2
133         expr {$a < 3.0}
134         set a
135 } {2}
137 testreport