summaryrefslogtreecommitdiffstats
path: root/library/msgcat/tests/lsearch.test
blob: f36e987b88bd16b19731ad11ca8b2c34a5d07ab3 (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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
# Commands covered:  lsearch
#
# This file contains a collection of tests for one or more of the Tcl built-in
# commands.  Sourcing this file into Tcl runs the tests and generates output
# for errors.  No output means no errors were found.
#
# Copyright (c) 1991-1993 The Regents of the University of California.
# Copyright (c) 1994 Sun Microsystems, Inc.
# Copyright (c) 1998-1999 by Scriptics Corporation.
#
# See the file "license.terms" for information on usage and redistribution of
# this file, and for a DISCLAIMER OF ALL WARRANTIES.

if {"::tcltest" ni [namespace children]} {
    package require tcltest 2
    namespace import -force ::tcltest::*
}

set x {abcd bbcd 123 234 345}
test lsearch-1.1 {lsearch command} {
    lsearch $x 123
} 2
test lsearch-1.2 {lsearch command} {
    lsearch $x 3456
} -1
test lsearch-1.3 {lsearch command} {
    lsearch $x *5
} 4
test lsearch-1.4 {lsearch command} {
    lsearch $x *bc*
} 0

test lsearch-2.1 {search modes} {
    lsearch -exact {xyz bbcc *bc*} *bc*
} 2
test lsearch-2.2 {search modes} {
    lsearch -exact {b.x ^bc xy bcx} ^bc
} 1
test lsearch-2.3 {search modes} {
    lsearch -exact {foo bar cat} ba
} -1
test lsearch-2.4 {search modes} {
    lsearch -exact {foo bar cat} bart
} -1
test lsearch-2.5 {search modes} {
    lsearch -exact {foo bar cat} bar
} 1
test lsearch-2.6 {search modes} -returnCodes error -body {
    lsearch -regexp {xyz bbcc *bc*} *bc*
} -result {couldn't compile regular expression pattern: quantifier operand invalid}
test lsearch-2.7 {search modes} {
    lsearch -regexp {b.x ^bc xy bcx} ^bc
} 3
test lsearch-2.8 {search modes} {
    lsearch -glob {xyz bbcc *bc*} *bc*
} 1
test lsearch-2.9 {search modes} {
    lsearch -glob {b.x ^bc xy bcx} ^bc
} 1
test lsearch-2.10 {search modes} -returnCodes error -body {
    lsearch -glib {b.x bx xy bcx} b.x
} -result {bad option "-glib": must be -all, -ascii, -bisect, -decreasing, -dictionary, -exact, -glob, -increasing, -index, -inline, -integer, -nocase, -not, -real, -regexp, -sorted, -start, or -subindices}
test lsearch-2.11 {search modes with -nocase} {
    lsearch -exact -nocase {a b c A B C} A
} 0
test lsearch-2.12 {search modes with -nocase} {
    lsearch -glob -nocase {a b c A B C} A*
} 0
test lsearch-2.13 {search modes with -nocase} {
    lsearch -regexp -nocase {a b c A B C} ^A\$
} 0
test lsearch-2.14 {search modes without -nocase} {
    lsearch -exact {a b c A B C} A
} 3
test lsearch-2.15 {search modes without -nocase} {
    lsearch -glob {a b c A B C} A*
} 3
test lsearch-2.16 {search modes without -nocase} {
    lsearch -regexp {a b c A B C} ^A\$
} 3

test lsearch-3.1 {lsearch errors} -returnCodes error -body {
    lsearch
} -result {wrong # args: should be "lsearch ?-option value ...? list pattern"}
test lsearch-3.2 {lsearch errors} -returnCodes error -body {
    lsearch a
} -result {wrong # args: should be "lsearch ?-option value ...? list pattern"}
test lsearch-3.3 {lsearch errors} -returnCodes error -body {
    lsearch a b c
} -result {bad option "a": must be -all, -ascii, -bisect, -decreasing, -dictionary, -exact, -glob, -increasing, -index, -inline, -integer, -nocase, -not, -real, -regexp, -sorted, -start, or -subindices}
test lsearch-3.4 {lsearch errors} -returnCodes error -body {
    lsearch a b c d
} -result {bad option "a": must be -all, -ascii, -bisect, -decreasing, -dictionary, -exact, -glob, -increasing, -index, -inline, -integer, -nocase, -not, -real, -regexp, -sorted, -start, or -subindices}
test lsearch-3.5 {lsearch errors} -returnCodes error -body {
    lsearch "\{" b
} -result {unmatched open brace in list}
test lsearch-3.6 {lsearch errors} -returnCodes error -body {
    lsearch -index a b
} -result {"-index" option must be followed by list index}
test lsearch-3.7 {lsearch errors} -returnCodes error -body {
    lsearch -subindices -exact a b
} -result {-subindices cannot be used without -index option}

test lsearch-4.1 {binary data} {
    lsearch -exact [list foo one\000two bar] bar
} 2
test lsearch-4.2 {binary data} {
    set x one
    append x \x00
    append x two
    lsearch -exact [list foo one\000two bar] $x
} 1

# Make a sorted list
set l {}
set l2 {}
for {set i 0} {$i < 100} {incr i} {
    lappend l $i
    lappend l2 [expr {double($i)/2}]
}
set increasingIntegers [lsort -integer $l]
set decreasingIntegers [lsort -decreasing -integer $l]
set increasingDoubles [lsort -real $l2]
set decreasingDoubles [lsort -decreasing -real $l2]
set increasingStrings [lsort {48 6a 18b 22a 21aa 35 36}]
set decreasingStrings [lsort -decreasing {48 6a 18b 22a 21aa 35 36}]
set increasingDictionary [lsort -dictionary {48 6a 18b 22a 21aa 35 36}]
set decreasingDictionary [lsort -dictionary -decreasing $increasingDictionary]

set l {}
for {set i 0} {$i < 10} {incr i} {
    lappend l $i $i $i $i $i
}
set repeatingIncreasingIntegers [lsort -integer $l]
set repeatingDecreasingIntegers [lsort -integer -decreasing $l]

test lsearch-5.1 {binary search} {
    set res {}
    for {set i 0} {$i < 100} {incr i} {
	lappend res [lsearch -integer -sorted $increasingIntegers $i]
    }
    set res
} $increasingIntegers
test lsearch-5.2 {binary search} {
    set res {}
    for {set i 0} {$i < 100} {incr i} {
	lappend res [lsearch -integer -decreasing -sorted \
		$decreasingIntegers $i]
    }
    set res
} $decreasingIntegers
test lsearch-5.3 {binary search finds leftmost occurances} {
    set res {}
    for {set i 0} {$i < 10} {incr i} {
	lappend res [lsearch -integer -sorted $repeatingIncreasingIntegers $i]
    }
    set res
} [list 0 5 10 15 20 25 30 35 40 45]
test lsearch-5.4 {binary search -decreasing finds leftmost occurances} {
    set res {}
    for {set i 9} {$i >= 0} {incr i -1} {
	lappend res [lsearch -sorted -integer -decreasing \
		$repeatingDecreasingIntegers $i]
    }
    set res
} [list 0 5 10 15 20 25 30 35 40 45]

test lsearch-6.1 {integer search} {
    set res {}
    for {set i 0} {$i < 100} {incr i} {
	lappend res [lsearch -exact -integer $increasingIntegers $i]
    }
    set res
} [lrange $increasingIntegers 0 99]
test lsearch-6.2 {decreasing integer search} {
    set res {}
    for {set i 0} {$i < 100} {incr i} {
	lappend res [lsearch -exact -integer -decreasing \
		$decreasingIntegers $i]
    }
    set res
} [lrange $decreasingIntegers 0 99]
test lsearch-6.3 {sorted integer search} {
    set res {}
    for {set i 0} {$i < 100} {incr i} {
	lappend res [lsearch -sorted -integer $increasingIntegers $i]
    }
    set res
} [lrange $increasingIntegers 0 99]
test lsearch-6.4 {sorted decreasing integer search} {
    set res {}
    for {set i 0} {$i < 100} {incr i} {
	lappend res [lsearch -integer -sorted -decreasing \
		$decreasingIntegers $i]
    }
    set res
} [lrange $decreasingIntegers 0 99]

test lsearch-7.1 {double search} {
    set res {}
    for {set i 0} {$i < 100} {incr i} {
	lappend res [lsearch -exact -real $increasingDoubles \
		[expr {double($i)/2}]]
    }
    set res
} [lrange $increasingIntegers 0 99]
test lsearch-7.2 {decreasing double search} {
    set res {}
    for {set i 0} {$i < 100} {incr i} {
	lappend res [lsearch -exact -real -decreasing \
		$decreasingDoubles [expr {double($i)/2}]]
    }
    set res
} [lrange $decreasingIntegers 0 99]
test lsearch-7.3 {sorted double search} {
    set res {}
    for {set i 0} {$i < 100} {incr i} {
	lappend res [lsearch -sorted -real \
		$increasingDoubles [expr {double($i)/2}]]
    }
    set res
} [lrange $increasingIntegers 0 99]
test lsearch-7.4 {sorted decreasing double search} {
    set res {}
    for {set i 0} {$i < 100} {incr i} {
	lappend res [lsearch -sorted -real -decreasing \
		$decreasingDoubles [expr {double($i)/2}]]
    }
    set res
} [lrange $decreasingIntegers 0 99]

test lsearch-8.1 {dictionary search} {
    set res {}
    foreach val {6a 18b 21aa 22a 35 36 48} {
	lappend res [lsearch -exact -dictionary $increasingDictionary $val]
    }
    set res
} [list 0 1 2 3 4 5 6]
test lsearch-8.2 {decreasing dictionary search} {
    set res {}
    foreach val {6a 18b 21aa 22a 35 36 48} {
	lappend res [lsearch -exact -dictionary $decreasingDictionary $val]
    }
    set res
} [list 6 5 4 3 2 1 0]
test lsearch-8.3 {sorted dictionary search} {
    set res {}
    foreach val {6a 18b 21aa 22a 35 36 48} {
	lappend res [lsearch -sorted -dictionary $increasingDictionary $val]
    }
    set res
} [list 0 1 2 3 4 5 6]
test lsearch-8.4 {decreasing sorted dictionary search} {
    set res {}
    foreach val {6a 18b 21aa 22a 35 36 48} {
	lappend res [lsearch -decreasing -sorted -dictionary \
		$decreasingDictionary $val]
    }
    set res
} [list 6 5 4 3 2 1 0]

test lsearch-9.1 {ascii search} {
    set res {}
    foreach val {18b 21aa 22a 35 36 48 6a} {
	lappend res [lsearch -exact -ascii $increasingStrings $val]
    }
    set res
} [list 0 1 2 3 4 5 6]
test lsearch-9.2 {decreasing ascii search} {
    set res {}
    foreach val {18b 21aa 22a 35 36 48 6a} {
	lappend res [lsearch -exact -ascii $decreasingStrings $val]
    }
    set res
} [list 6 5 4 3 2 1 0]
test lsearch-9.3 {sorted ascii search} {
    set res {}
    foreach val {18b 21aa 22a 35 36 48 6a} {
	lappend res [lsearch -sorted -ascii $increasingStrings $val]
    }
    set res
} [list 0 1 2 3 4 5 6]
test lsearch-9.4 {decreasing sorted ascii search} {
    set res {}
    foreach val {18b 21aa 22a 35 36 48 6a} {
	lappend res [lsearch -decreasing -sorted -ascii \
		$decreasingStrings $val]
    }
    set res
} [list 6 5 4 3 2 1 0]

test lsearch-10.1 {offset searching} {
    lsearch -start 2 {a b c a b c} a
} 3
test lsearch-10.2 {offset searching} {
    lsearch -start 2 {a b c d e f} a
} -1
test lsearch-10.3 {offset searching} {
    lsearch -start end-4 {a b c a b c} a
} 3
test lsearch-10.4 {offset searching} -returnCodes error -body {
    lsearch -start foobar {a b c a b c} a
} -result {bad index "foobar": must be integer?[+-]integer? or end?[+-]integer?}
test lsearch-10.5 {offset searching} -returnCodes error -body {
    lsearch -start 1 2
} -result {missing starting index}
test lsearch-10.6 {binary search with offset} {
    set res {}
    for {set i 0} {$i < 100} {incr i} {
	lappend res [lsearch -integer -start 2 -sorted $increasingIntegers $i]
    }
    set res
} [concat -1 -1 [lrange $increasingIntegers 2 end]]
test lsearch-10.7 {offset searching with an empty list} {
    # Stop bug #694232 from reocurring
    lsearch -start 0 {} x
} -1
test lsearch-10.8 {offset searching past the end of the list} {
    # Stop [Bug 1374778] from reoccurring
    lsearch -start 10 {a b c} c
} -1
test lsearch-10.9 {offset searching past the end of the list} {
    # Stop [Bug 1374778] from reoccurring
    lsearch -start 10 -all {a b c} c
} {}
test lsearch-10.10 {offset searching past the end of the list} {
    # Stop [Bug 1374778] from reoccurring
    lsearch -start 10 -inline {a b c} c
} {}

test lsearch-11.1 {negated searches} {
    lsearch -not {a a a b a a a} a
} 3
test lsearch-11.2 {negated searches} {
    lsearch -not {a a a a a a a} a
} -1

test lsearch-12.1 {return values instead of indices} {
    lsearch -glob -inline {a1 b2 c3 d4} c*
} c3
test lsearch-12.2 {return values instead of indices} {
    lsearch -glob -inline {a1 b2 c3 d4} e*
} {}

test lsearch-13.1 {search for all matches} {
    lsearch -all {a b a c a d} 1
} {}
test lsearch-13.2 {search for all matches} {
    lsearch -all {a b a c a d} a
} {0 2 4}
test lsearch-13.3 {search for all matches with -nocase} {
    lsearch -all -exact -nocase {a b c A B C} A
} {0 3}
test lsearch-13.4 {search for all matches with -nocase} {
    lsearch -all -glob -nocase {a b c A B C} A*
} {0 3}
test lsearch-13.5 {search for all matches with -nocase} {
    lsearch -all -regexp -nocase {a b c A B C} ^A\$
} {0 3}

test lsearch-14.1 {combinations: -all and -inline} {
    lsearch -all -inline -glob {a1 b2 a3 c4 a5 d6} a*
} {a1 a3 a5}
test lsearch-14.2 {combinations: -all, -inline and -not} {
    lsearch -all -inline -not -glob {a1 b2 a3 c4 a5 d6} a*
} {b2 c4 d6}
test lsearch-14.3 {combinations: -all and -not} {
    lsearch -all -not -glob {a1 b2 a3 c4 a5 d6} a*
} {1 3 5}
test lsearch-14.4 {combinations: -inline and -not} {
    lsearch -inline -not -glob {a1 b2 a3 c4 a5 d6} a*
} {b2}
test lsearch-14.5 {combinations: -start, -all and -inline} {
    lsearch -start 2 -all -inline -glob {a1 b2 a3 c4 a5 d6} a*
} {a3 a5}
test lsearch-14.6 {combinations: -start, -all, -inline and -not} {
    lsearch -start 2 -all -inline -not -glob {a1 b2 a3 c4 a5 d6} a*
} {c4 d6}
test lsearch-14.7 {combinations: -start, -all and -not} {
    lsearch -start 2 -all -not -glob {a1 b2 a3 c4 a5 d6} a*
} {3 5}
test lsearch-14.8 {combinations: -start, -inline and -not} {
    lsearch -start 2 -inline -not -glob {a1 b2 a3 c4 a5 d6} a*
} {c4}

test lsearch-15.1 {make sure no shimmering occurs} {
    set x [expr int(sin(0))]
    lsearch -start $x $x $x
} 0

test lsearch-16.1 {lsearch -regexp shared object} {
    set str a
    lsearch -regexp $str $str
} 0
# Bug 1366683
test lsearch-16.2 {lsearch -regexp allows internal backrefs} {
    lsearch -regexp {a aa b} {(.)\1}
} 1

test lsearch-17.1 {lsearch -index option, basic functionality} {
    lsearch -index 1 {{a c} {a b} {a a}} a
} 2
test lsearch-17.2 {lsearch -index option, basic functionality} {
    lsearch -index 1 -exact {{a c} {a b} {a a}} a
} 2
test lsearch-17.3 {lsearch -index option, basic functionality} {
    lsearch -index 1 -glob {{ab cb} {ab bb} {ab ab}} b* 
} 1
test lsearch-17.4 {lsearch -index option, basic functionality} {
    lsearch -index 1 -regexp {{ab cb} {ab bb} {ab ab}} {[cb]b}
} 0 
test lsearch-17.5 {lsearch -index option, basic functionality} {
    lsearch -all -index 0 -exact {{a c} {a b} {d a}} a
} {0 1}
test lsearch-17.6 {lsearch -index option, basic functionality} {
    lsearch -all -index 1 -glob {{ab cb} {ab bb} {db bx}} b* 
} {1 2}
test lsearch-17.7 {lsearch -index option, basic functionality} {
    lsearch -all -index 1 -regexp {{ab cb} {ab bb} {ab ab}} {[cb]b}
} {0 1}

test lsearch-18.1 {lsearch -index option, list as index basic functionality} {
    lsearch -index {0 0} {{{x x} {x b} {a d}} {{a c} {a b} {a a}}} a
} 1
test lsearch-18.2 {lsearch -index option, list as index basic functionality} {
    lsearch -index {2 0} -exact {{{x x} {x b} {a d}} {{a c} {a b} {a a}}} a
} 0
test lsearch-18.3 {lsearch -index option, list as index basic functionality} {
    lsearch -index {1 1} -glob {{{ab cb} {ab bb} {ab ab}} {{ab cb} {ab bb} {ab ab}}} b* 
} 0
test lsearch-18.4 {lsearch -index option, list as index basic functionality} {
    lsearch -index {0 1} -regexp {{{ab cb} {ab bb} {ab ab}} {{ab cb} {ab bb} {ab ab}}} {[cb]b}
} 0 
test lsearch-18.5 {lsearch -index option, list as index basic functionality} {
    lsearch -all -index {0 0} -exact {{{a c} {a b} {d a}} {{a c} {a b} {d a}}} a
} {0 1}

test lsearch-19.1 {lsearch -sunindices option} {
    lsearch -subindices -index {0 0} {{{x x} {x b} {a d}} {{a c} {a b} {a a}}} a
} {1 0 0}
test lsearch-19.2 {lsearch -sunindices option} {
    lsearch -subindices -index {2 0} -exact {{{x x} {x b} {a d}} {{a c} {a b} {a a}}} a
} {0 2 0}
test lsearch-19.3 {lsearch -sunindices option} {
    lsearch -subindices -index {1 1} -glob {{{ab cb} {ab bb} {ab ab}} {{ab cb} {ab bb} {ab ab}}} b* 
} {0 1 1}
test lsearch-19.4 {lsearch -sunindices option} {
    lsearch -subindices -index {0 1} -regexp {{{ab cb} {ab bb} {ab ab}} {{ab cb} {ab bb} {ab ab}}} {[cb]b}
} {0 0 1} 
test lsearch-19.5 {lsearch -sunindices option} {
    lsearch -subindices -all -index {0 0} -exact {{{a c} {a b} {d a}} {{a c} {a b} {d a}}} a
} {{0 0 0} {1 0 0}}

test lsearch-20.1 {lsearch -index option, index larger than sublists} -body {
    lsearch -index 2 {{a c} {a b} {a a}} a
} -returnCodes error -result {element 2 missing from sublist "a c"}
test lsearch-20.2 {lsearch -index option, malformed index} -body {
    lsearch -index foo {{a c} {a b} {a a}} a
} -returnCodes error -result {bad index "foo": must be integer?[+-]integer? or end?[+-]integer?}
test lsearch-20.3 {lsearch -index option, malformed index} -body {
    lsearch -index \{ {{a c} {a b} {a a}} a
} -returnCodes error -result {unmatched open brace in list}

test lsearch-21.1 {lsearch shimmering crash} {
    set x 0
    lsearch -exact -integer $x $x
} 0
test lsearch-21.2 {lsearch shimmering crash} {
    set x 0.5
    lsearch -exact -real $x $x
} 0

test lsearch-22.1 {lsearch -bisect} -setup {
    set res {}
} -body {
    foreach i {0 1 5 6 7 8 15 16} {
	lappend res [lsearch -bisect -integer {1 4 5 7 9 15} $i]
    }
    return $res
} -result {-1 0 2 2 3 3 5 5}
test lsearch-22.2 {lsearch -bisect, last of equals} -setup {
    set res {}
} -body {
    foreach i {0 1 2 3} {
	lappend res [lsearch -bisect -integer {0 0 1 1 1 2 2 2 3 3 3} $i]
    }
    return $res
} -result {1 4 7 10}
test lsearch-22.3 {lsearch -bisect decreasing order} -setup {
    set res {}
} -body {
    foreach i {0 1 5 6 7 8 15 16} {
	lappend res [lsearch -bisect -integer -decreasing {15 9 7 5 4 1} $i]
    }
    return $res
} -result {5 5 3 2 2 1 0 -1}
test lsearch-22.4 {lsearch -bisect, last of equals, decreasing} -setup {
    set res {}
} -body {
    foreach i {0 1 2 3} {
	lappend res [lsearch -bisect -integer -decreasing \
		{3 3 3 2 2 2 1 1 1 0 0} $i]
    }
    return $res
} -result {10 8 5 2}
test lsearch-22.5 {lsearch -bisect, all equal} {
    lsearch -bisect -integer {5 5 5 5} 5
} {3}
test lsearch-22.6 {lsearch -sorted, all equal} {
    lsearch -sorted -integer {5 5 5 5} 5
} {0}

# cleanup
catch {unset res}
catch {unset increasingIntegers}
catch {unset decreasingIntegers}
catch {unset increasingDoubles}
catch {unset decreasingDoubles}
catch {unset increasingStrings}
catch {unset decreasingStrings}
catch {unset increasingDictionary}
catch {unset decreasingDictionary}
::tcltest::cleanupTests
return

# Local Variables:
# mode: tcl
# End: