aboutsummaryrefslogtreecommitdiff
path: root/tests/expr.test
blob: d7c7b5efa17bb42fa732a6d8c08c0f4dd191d785 (plain)
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
126
127
128
129
130
131
132
133
source testing.tcl

section "String comparison"

test expr-1.1 "Compare strings lt" {
	expr {"V000500" < "V000405"}
} {0}

test expr-1.2 "Compare strings with embedded nulls" {
	set s1 [format abc%cdef 0]
	set s2 [format abc%cghi 0]
	expr {$s1 < $s2}
} {1}

test expr-1.3 "Hex values" {
	set mask1 [expr 0x4050 & 0x0CCC]
} {64}

test expr-1.4 "Ternary operator - true" {
	expr {1 ? 2 : 3}
} {2}

test expr-1.5 "Ternary operator - false" {
	expr {0 ? 2 : 3}
} {3}

test expr-1.6 "Ternary operator - double check" {
	expr {1.0 ? 2 : 3}
} {2}

test expr-1.7 "Ternary operator - string result" {
	expr {1 ? "two" : 3}
} {two}

test expr-1.8 "Ternary operator - don't eval false path" {
	set a 100
	set b 200
	set c [expr {20 ? [incr a] : [incr b]}]
	list $a $b $c
} {101 200 101}

test expr-1.9 "Unary minus" {
	set a 1
	expr {-$a}
} {-1}

test expr-1.10 "Subtraction" {
	set a 1
	set b 10
	expr {$b-$a}
} {9}

test expr-1.11 "Short circuit evaluation" {
	set a 100
	set c [expr {0 || [incr a]}]
	list $a $c
} {101 1}

test expr-1.12 "Short circuit evaluation" {
	set a 100
	set c [expr {1 || [incr a]}]
	list $a $c
} {100 1}

test expr-1.13 "Short circuit evaluation" {
	set a 100
	set c [expr {1 || [incr a] && [incr a]}]
	list $a $c
} {100 1}

test expr-1.14 "Rotate left" {
	expr {1 <<< 5}
} {32}

test expr-1.15 "Rotate left" {
	expr {1 <<< 65}
} {2}

test expr-1.16 "Rotate right" {
	expr {1 >>> 48}
} {65536}

test expr-1.17 "Rotate left" {
	expr {1 >>> 63}
} {2}

# This crashes older jim
test expr-2.1 "bogus unarymin" {
	catch {expr {unarymin 1}}
	return 1
} {1}

test expr-2.2 "Ternary operator - missing colon" {
	list [catch {expr {1 ? 2 3}} msg]
} {1}

test expr-2.3 "Ternary operator - missing third term" {
	list [catch {expr {1 ? 2}} msg]
} {1}

test expr-2.4 "Ternary operator - missing question" {
	list [catch {expr {1 : 2}} msg]
} {1}

test expr-3.1 "in, ni operators" {
	set l {a b c d}
	set c C
	list [expr {"a" in $l}] [expr {$c in $l}] [expr {"b" ni $l}] [expr {$c ni $l}]
} {1 0 0 1}

test expr-3.2 "if: in, ni operators" {
	set l {a b c d}
	set a a
	set c C
	set result {}
	if {$a in $l} {
		lappend result 1
	}
	if {$c in $l} {
		lappend result 2
	}
	if {$a ni $l} {
		lappend result 3
	}
	if {$c ni $l} {
		lappend result 4
	}
	if {"d" in $l} {
		lappend result 5
	}
} {1 4 5}

testreport