aboutsummaryrefslogtreecommitdiff
path: root/tests/intexpr.test
blob: 9ad6632b8a90cc23add15f9b0912ca973acfa916 (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 [file dirname [info script]]/testing.tcl

needs constraint jim

# There are two kinds of commands that use (safe) integer expressions:
# direct: loop, range, incr, string repeat, lrepeat, pack, unpack, rand
# index: lindex, linsert, lreplace, lset, lrange, lsort, regexp, regsub, string index,first,last,range
#
# Since they are all identical under the covers, we only test one from each group here,
# string repeat and string index

test intexpr-1.1 {string repeat} {
	string repeat a 2+1
} {aaa}

test intexpr-1.2 {string repeat} {
	string repeat a 2-1
} {a}

test intexpr-1.3 {string repeat} {
	string repeat a 2*3
} {aaaaaa}

test intexpr-1.4 {string repeat - function calls} {
	string repeat a int(abs(-2))
} {aa}

test intexpr-1.4 {string repeat - expanded var} {
	set n 3
	string repeat a $n+1
} {aaaa}

test intexpr-1.5 {string repeat - no subst var} -body {
	set n 3
	string repeat a {$n+1}
} -returnCodes error -result {expected integer expression but got "$n+1"}

test intexpr-1.6 {string repeat - no subst cmd} -body {
	string repeat a {[string length xy]+1}
} -returnCodes error -result {expected integer expression but got "[string length xy]+1"}

test intexpr-1.6 {string repeat - no subst dictvar} -body {
	set b(3) 4
	string repeat a {$b(4)}
} -returnCodes error -result {expected integer expression but got "$b(4)"}

test intexpr-1.7 {string repeat - no subst dictvar} -body {
	set b(3) 4
	string repeat a {$b(4)+2}
} -returnCodes error -result {expected integer expression but got "$b(4)+2"}

set str abcdefghi
test intexpr-2.1 {string index} {
	string index $str 2+1
} {d}

test intexpr-2.2 {string index} {
	string index $str 2-1
} {b}

test intexpr-2.3 {string index} {
	string index $str 2*3
} {g}

test intexpr-2.4 {string index - function calls} {
	string index $str int(abs(-2))
} {c}

test intexpr-2.4 {string index - expanded var} {
	set n 3
	string index $str $n+1
} {e}

test intexpr-2.5 {string index - no subst var} -body {
	set n 3
	string index $str {$n+1}
} -returnCodes error -result {bad index "$n+1": must be intexpr or end?[+-]intexpr?}

test intexpr-2.6 {string index - no subst cmd} -body {
	string index $str {[string length xy]+1}
} -returnCodes error -result {bad index "[string length xy]+1": must be intexpr or end?[+-]intexpr?}

test intexpr-2.6 {string index - no subst dictvar} -body {
	set b(3) 4
	string index $str {$b(4)}
} -returnCodes error -result {bad index "$b(4)": must be intexpr or end?[+-]intexpr?}

test intexpr-2.7 {string index - no subst dictvar} -body {
	set b(3) 4
	string index $str {$b(4)+2}
} -returnCodes error -result {bad index "$b(4)+2": must be intexpr or end?[+-]intexpr?}

test intexpr-3.1 {string index} {
	string index $str end-2+1
} {h}

test intexpr-3.2 {string index} {
	string index $str end-2-1
} {f}

test intexpr-3.3 {string index} {
	string index $str end-2*3
} {c}

test intexpr-3.4 {string index - function calls} {
	string index $str end+int(-2)
} {g}

test intexpr-3.4 {string index - expanded var} {
	set n 3
	string index $str end-($n+1)
} {e}

test intexpr-3.5 {string index - no subst var} -body {
	set n 3
	string index $str {end-($n+1)}
} -returnCodes error -result {bad index "end-($n+1)": must be intexpr or end?[+-]intexpr?}

test intexpr-3.6 {string index - no subst cmd} -body {
	string index $str {end-[string length xy]+1}
} -returnCodes error -result {bad index "end-[string length xy]+1": must be intexpr or end?[+-]intexpr?}

test intexpr-3.6 {string index - no subst dictvar} -body {
	set b(3) 4
	string index $str {end-$b(4)}
} -returnCodes error -result {bad index "end-$b(4)": must be intexpr or end?[+-]intexpr?}

test intexpr-3.7 {string index - no subst dictvar} -body {
	set b(3) -4
	string index $str {end+$b(4)-2}
} -returnCodes error -result {bad index "end+$b(4)-2": must be intexpr or end?[+-]intexpr?}

testreport