summaryrefslogtreecommitdiffstats
path: root/tests/string.test
blob: 3809ba9a2095b0900f395fb6544426624f913c00 (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
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
# Commands covered:  string
#
# 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.
#
# RCS: @(#) $Id: string.test,v 1.7 1999/05/04 02:57:55 stanton Exp $

if {[lsearch [namespace children] ::tcltest] == -1} {
    source [file join [pwd] [file dirname [info script]] defs.tcl]
}

test string-1.1 {string compare} {
    string compare abcde abdef
} -1
test string-1.2 {string compare, shortest method name} {
    string c abcde ABCDE
} 1
test string-1.3 {string compare} {
    string compare abcde abcde
} 0
test string-1.4 {string compare too few args} {
    list [catch {string compare a} msg] $msg
} {1 {wrong # args: should be "string compare string1 string2 ?length?"}}
test string-1.5 {string compare bad args} {
    list [catch {string compare a b c} msg] $msg
} {1 {expected integer but got "c"}}
test string-1.6 {string compare too many args} {
    list [catch {string compare a b 1 c} msg] $msg
} {1 {wrong # args: should be "string compare string1 string2 ?length?"}}
test string-1.7 {string compare with length} {
    string compare abcde abxyz 2
} 0
test string-1.8 {string compare with special index} {
    list [catch {string compare abcde abxyz end-3} msg] $msg
} {1 {expected integer but got "end-3"}}

test string-2.1 {string first} {
    string first bq abcdefgbcefgbqrs
} 12
test string-2.2 {string first} {
    string fir bcd abcdefgbcefgbqrs
} 1
test string-2.3 {string first} {
    string f b abcdefgbcefgbqrs
} 1
test string-2.4 {string first} {
    string first xxx x123xx345xxx789xxx012
} 9
test string-2.5 {string first} {
    string first "" x123xx345xxx789xxx012
} -1
test string-2.6 {string first} {
    list [catch {string first a} msg] $msg
} {1 {wrong # args: should be "string first string1 string2"}}
test string-2.7 {string first} {
    list [catch {string first a b c} msg] $msg
} {1 {wrong # args: should be "string first string1 string2"}}

test string-3.1 {string index} {
    string index abcde 0
} a
test string-3.2 {string index} {
    string in abcde 4
} e
test string-3.3 {string index} {
    string index abcde 5
} {}
test string-3.4 {string index} {
    list [catch {string index abcde -10} msg] $msg
} {0 {}}
test string-3.5 {string index} {
    list [catch {string index} msg] $msg
} {1 {wrong # args: should be "string index string charIndex"}}
test string-3.6 {string index} {
    list [catch {string index a b c} msg] $msg
} {1 {wrong # args: should be "string index string charIndex"}}
test string-3.7 {string index} {
    list [catch {string index a xyz} msg] $msg
} {1 {syntax error in expression "xyz"}}
test string-3.8 {string index} {
    string index abc end
} c
test string-3.9 {string index} {
    string index abc end-1
} b

test string-4.1 {string last} {
    string la xxx xxxx123xx345x678
} 1
test string-4.2 {string last} {
    string last xx xxxx123xx345x678
} 7
test string-4.3 {string last} {
    string las x xxxx123xx345x678
} 12
test string-4.4 {string last} {
    list [catch {string last a} msg] $msg
} {1 {wrong # args: should be "string last string1 string2"}}
test string-4.5 {string last} {
    list [catch {string last a b c} msg] $msg
} {1 {wrong # args: should be "string last string1 string2"}}

test string-5.1 {string length} {
    string length "a little string"
} 15
test string-5.2 {string length} {
    string le ""
} 0
test string-5.3 {string length} {
    list [catch {string length} msg] $msg
} {1 {wrong # args: should be "string length string"}}
test string-5.4 {string length} {
    list [catch {string length a b} msg] $msg
} {1 {wrong # args: should be "string length string"}}

test string-6.1 {string match} {
    string match abc abc
} 1
test string-6.2 {string match} {
    string mat abc abd
} 0
test string-6.3 {string match} {
    string match ab*c abc
} 1
test string-6.4 {string match} {
    string match ab**c abc
} 1
test string-6.5 {string match} {
    string match ab* abcdef
} 1
test string-6.6 {string match} {
    string match *c abc
} 1
test string-6.7 {string match} {
    string match *3*6*9 0123456789
} 1
test string-6.8 {string match} {
    string match *3*6*9 01234567890
} 0
test string-6.9 {string match} {
    string match a?c abc
} 1
test string-6.10 {string match} {
    string match a??c abc
} 0
test string-6.11 {string match} {
    string match ?1??4???8? 0123456789
} 1
test string-6.12 {string match} {
    string match {[abc]bc} abc
} 1
test string-6.13 {string match} {
    string match {a[abc]c} abc
} 1
test string-6.14 {string match} {
    string match {a[xyz]c} abc
} 0
test string-6.15 {string match} {
    string match {12[2-7]45} 12345
} 1
test string-6.16 {string match} {
    string match {12[ab2-4cd]45} 12345
} 1
test string-6.17 {string match} {
    string match {12[ab2-4cd]45} 12b45
} 1
test string-6.18 {string match} {
    string match {12[ab2-4cd]45} 12d45
} 1
test string-6.19 {string match} {
    string match {12[ab2-4cd]45} 12145
} 0
test string-6.20 {string match} {
    string match {12[ab2-4cd]45} 12545
} 0
test string-6.21 {string match} {
    string match {a\*b} a*b
} 1
test string-6.22 {string match} {
    string match {a\*b} ab
} 0
test string-6.23 {string match} {
    string match {a\*\?\[\]\\\x} "a*?\[\]\\x"
} 1
test string-6.24 {string match} {
    string match ** ""
} 1
test string-6.25 {string match} {
    string match *. ""
} 0
test string-6.26 {string match} {
    string match "" ""
} 1
test string-6.27 {string match} {
    string match \[a a
} 1
test string-6.28 {string match} {
    list [catch {string match a} msg] $msg
} {1 {wrong # args: should be "string match pattern string"}}
test string-6.29 {string match} {
    list [catch {string match a b c} msg] $msg
} {1 {wrong # args: should be "string match pattern string"}}

test string-7.1 {string range} {
    string range abcdefghijklmnop 2 14
} {cdefghijklmno}
test string-7.2 {string range} {
    string range abcdefghijklmnop 7 1000
} {hijklmnop}
test string-7.3 {string range} {
    string range abcdefghijklmnop 10 e
} {klmnop}
test string-7.4 {string range} {
    string range abcdefghijklmnop 10 9
} {}
test string-7.5 {string range} {
    string range abcdefghijklmnop -3 2
} {abc}
test string-7.6 {string range} {
    string range abcdefghijklmnop -3 -2
} {}
test string-7.7 {string range} {
    string range abcdefghijklmnop 1000 1010
} {}
test string-7.8 {string range} {
    string range abcdefghijklmnop -100 end
} {abcdefghijklmnop}
test string-7.9 {string range} {
    list [catch {string range} msg] $msg
} {1 {wrong # args: should be "string range string first last"}}
test string-7.10 {string range} {
    list [catch {string range a 1} msg] $msg
} {1 {wrong # args: should be "string range string first last"}}
test string-7.11 {string range} {
    list [catch {string range a 1 2 3} msg] $msg
} {1 {wrong # args: should be "string range string first last"}}
test string-7.12 {string range} {
    list [catch {string range abc abc 1} msg] $msg
} {1 {syntax error in expression "abc"}}
test string-7.13 {string range} {
    list [catch {string range abc 1 eof} msg] $msg
} {1 {syntax error in expression "eof"}}
test string-7.14 {string range} {
    string range abcdefghijklmnop end-1 end
} {op}
test string-7.15 {string range} {
    string range abcdefghijklmnop e 1000
} {p}
test string-7.16 {string range} {
    string range abcdefghijklmnop end end-1
} {}

test string-8.1 {string trim} {
    string trim "    XYZ      "
} {XYZ}
test string-8.2 {string trim} {
    string trim "\t\nXYZ\t\n\r\n"
} {XYZ}
test string-8.3 {string trim} {
    string trim "  A XYZ A    "
} {A XYZ A}
test string-8.4 {string trim} {
    string trim "XXYYZZABC XXYYZZ" ZYX
} {ABC }
test string-8.5 {string trim} {
    string trim "    \t\r      "
} {}
test string-8.6 {string trim} {
    string trim {abcdefg} {}
} {abcdefg}
test string-8.7 {string trim} {
    string trim {}
} {}
test string-8.8 {string trim} {
    string trim ABC DEF
} {ABC}
test string-8.9 {string trim} {
    list [catch {string trim} msg] $msg
} {1 {wrong # args: should be "string trim string ?chars?"}}
test string-8.10 {string trim} {
    list [catch {string trim a b c} msg] $msg
} {1 {wrong # args: should be "string trim string ?chars?"}}

test string-9.1 {string trimleft} {
    string trimleft "    XYZ      "
} {XYZ      }
test string-9.2 {string trimleft} {
    list [catch {string trimleft} msg] $msg
} {1 {wrong # args: should be "string trimleft string ?chars?"}}

test string-10.1 {string trimright} {
    string trimright "    XYZ      "
} {    XYZ}
test string-10.2 {string trimright} {
    string trimright "   "
} {}
test string-10.3 {string trimright} {
    string trimright ""
} {}
test string-10.4 {string trimright errors} {
    list [catch {string trimright} msg] $msg
} {1 {wrong # args: should be "string trimright string ?chars?"}}
test string-10.5 {string trimright errors} {
    list [catch {string trimg a} msg] $msg
} {1 {bad option "trimg": must be bytelength, compare, equal, first, icompare, iequal, index, last, length, map, match, range, repeat, replace, tolower, toupper, totitle, trim, trimleft, trimright, wordend, or wordstart}}

test string-11.1 {string tolower} {
    string tolower ABCDeF
} {abcdef}
test string-11.2 {string tolower} {
    string tolower "ABC  XyZ"
} {abc  xyz}
test string-11.3 {string tolower} {
    string tolower {123#$&*()}
} {123#$&*()}
test string-11.4 {string tolower too few args} {
    list [catch {string tolower} msg] $msg
} {1 {wrong # args: should be "string tolower string ?first? ?last?"}}
test string-11.5 {string tolower bad args} {
    list [catch {string tolower a b} msg] $msg
} {1 {syntax error in expression "b"}}
test string-11.6 {string tolower too many args} {
    list [catch {string tolower ABC 1 end oops} msg] $msg
} {1 {wrong # args: should be "string tolower string ?first? ?last?"}}
test string-11.7 {string tolower} {
    string tolower ABC 1
} AbC
test string-11.8 {string tolower} {
    string tolower ABC 1 end
} Abc
test string-11.9 {string tolower} {
    string tolower ABC 0 end-1
} abC
test string-11.10 {string tolower called with badly formed Utf string} {
    string tolower [bytestring "\u00fcBER"]
} [bytestring "\u00fcber"]

test string-12.1 {string totitle} {
    string totitle ABCDeF
} {Abcdef}
test string-12.2 {string totitle} {
    string totitle "aBC d Hij xyZ"
} {Abc d hij xyz}
test string-12.3 {string totitle} {
    string totitle {123#$&*()}
} {123#$&*()}
test string-12.4 {string totitle} {
    list [catch {string totitle} msg] $msg
} {1 {wrong # args: should be "string totitle string ?first? ?last?"}}
test string-12.5 {string totitle} {
    list [catch {string totitle a b} msg] $msg
} {1 {syntax error in expression "b"}}
test string-12.6 {string totitle too many args} {
    list [catch {string totitle ABC 1 end oops} msg] $msg
} {1 {wrong # args: should be "string totitle string ?first? ?last?"}}
test string-12.7 {string totitle} {
    string totitle abC 1
} aBC
test string-12.8 {string totitle} {
    string totitle ABC 1 end
} ABc
test string-12.9 {string totitle} {
    string totitle ABC 0 end-1
} AbC
test string-12.10 {string totitle called with badly formed Utf string} {
    string totitle [bytestring "\u00fcBER"]
} [bytestring "\u00fcber"]

test string-13.1 {string toupper} {
    string toupper abCDEf
} {ABCDEF}
test string-13.2 {string toupper} {
    string toupper "abc xYz"
} {ABC XYZ}
test string-13.3 {string toupper} {
    string toupper {123#$&*()}
} {123#$&*()}
test string-13.4 {string toupper} {
    list [catch {string toupper} msg] $msg
} {1 {wrong # args: should be "string toupper string ?first? ?last?"}}
test string-13.5 {string toupper} {
    list [catch {string toupper a b} msg] $msg
} {1 {syntax error in expression "b"}}
test string-13.6 {string toupper} {
    list [catch {string toupper a 1 end oops} msg] $msg
} {1 {wrong # args: should be "string toupper string ?first? ?last?"}}
test string-13.7 {string toupper} {
    string toupper abc 1
} aBc
test string-13.8 {string toupper} {
    string toupper abc 1 end
} aBC
test string-13.9 {string toupper} {
    string toupper abc 0 end-1
} ABc
test string-13.10 {string toupper called with badly formed Utf string} {
    string toupper [bytestring "\u00fcber"]
} [bytestring "\u00fcBER"]

test string-14.1 {string wordend} {
    list [catch {string wordend a} msg] $msg
} {1 {wrong # args: should be "string wordend string index"}}
test string-14.2 {string wordend} {
    list [catch {string wordend a b c} msg] $msg
} {1 {wrong # args: should be "string wordend string index"}}
test string-14.3 {string wordend} {
    list [catch {string wordend a gorp} msg] $msg
} {1 {syntax error in expression "gorp"}}
test string-14.4 {string wordend} {
    string wordend abc. -1
} 3
test string-14.5 {string wordend} {
    string wordend abc. 100
} 4
test string-14.6 {string wordend} {
    string wordend "word_one two three" 2
} 8
test string-14.7 {string wordend} {
    string wordend "one .&# three" 5
} 6
test string-14.8 {string wordend} {
    string worde "x.y" 0
} 1
test string-14.9 {string wordend} {
    string worde "x.y" end-1
} 2

test string-15.1 {string wordstart} {
    list [catch {string word a} msg] $msg
} {1 {ambiguous option "word": must be bytelength, compare, equal, first, icompare, iequal, index, last, length, map, match, range, repeat, replace, tolower, toupper, totitle, trim, trimleft, trimright, wordend, or wordstart}}
test string-15.2 {string wordstart} {
    list [catch {string wordstart a} msg] $msg
} {1 {wrong # args: should be "string wordstart string index"}}
test string-15.3 {string wordstart} {
    list [catch {string wordstart a b c} msg] $msg
} {1 {wrong # args: should be "string wordstart string index"}}
test string-15.4 {string wordstart} {
    list [catch {string wordstart a gorp} msg] $msg
} {1 {syntax error in expression "gorp"}}
test string-15.5 {string wordstart} {
    string wordstart "one two three_words" 400
} 8
test string-15.6 {string wordstart} {
    string wordstart "one two three_words" 2
} 0
test string-15.7 {string wordstart} {
    string wordstart "one two three_words" -2
} 0
test string-15.8 {string wordstart} {
    string wordstart "one .*&^ three" 6
} 6
test string-15.9 {string wordstart} {
    string wordstart "one two three" 4
} 4
test string-15.10 {string wordstart} {
    string wordstart "one two three" end-5
} 7

test string-16.1 {error conditions} {
    list [catch {string gorp a b} msg] $msg
} {1 {bad option "gorp": must be bytelength, compare, equal, first, icompare, iequal, index, last, length, map, match, range, repeat, replace, tolower, toupper, totitle, trim, trimleft, trimright, wordend, or wordstart}}
test string-16.2 {error conditions} {
    list [catch {string} msg] $msg
} {1 {wrong # args: should be "string option arg ?arg ...?"}}

# only need a few tests on equal, since it uses the same code as
# string compare, but just modifies the return output
test string-17.1 {string equal} {
    string equal abcde abdef
} 0
test string-17.2 {string equal} {
    string eq abcde ABCDE
} 0
test string-17.3 {string equal} {
    string equal abcde abcde
} 1

test string-18.1 {string icompare} {
    string icompare abcde abdef
} -1
test string-18.2 {string icompare} {
    string ic abcde ABCDE
} 0
test string-18.3 {string icompare} {
    string icompare abcde abcde
} 0
test string-18.4 {string icompare too few args} {
    list [catch {string icompare a} msg] $msg
} {1 {wrong # args: should be "string icompare string1 string2 ?length?"}}
test string-18.5 {string icompare bad args} {
    list [catch {string icompare a b c} msg] $msg
} {1 {expected integer but got "c"}}
test string-18.6 {string icompare too many args} {
    list [catch {string icompare a b 1 c} msg] $msg
} {1 {wrong # args: should be "string icompare string1 string2 ?length?"}}
test string-18.7 {string icompare with length} {
    string icompare abcde Abxyz 2
} 0
test string-18.8 {string icompare with special index} {
    list [catch {string icompare Abcde abxyz end-3} msg] $msg
} {1 {expected integer but got "end-3"}}

test string-19.1 {string iequal} {
    string iequal abcde abdef
} 0
test string-19.2 {string iequal} {
    string ieq abcde ABCDE
} 1
test string-19.3 {string iequal} {
    string iequal abcde abcde
} 1

test string-20.1 {string map} {
    list [catch {string map} msg] $msg
} {1 {wrong # args: should be "string map charMap string"}}
test string-20.2 {string map} {
    list [catch {string map {a b} abba oops} msg] $msg
} {1 {wrong # args: should be "string map charMap string"}}
test string-20.3 {string map} {
    string map {a b} abba
} {bbbb}
test string-20.4 {string map} {
    string map {abc 321 ab * a A} aabcabaababcab
} {A321*A*321*}
test string-20.5 {string map} {
    list [catch {string map {a b c} abba} msg] $msg
} {1 {char map list unbalanced}}
test string-20.6 {string map} {
    string map {\x00 NULL blah \x00nix} {qwerty}
} {qwerty}

test string-21.1 {string repeat} {
    list [catch {string repeat} msg] $msg
} {1 {wrong # args: should be "string repeat string count"}}
test string-21.2 {string repeat} {
    list [catch {string repeat abc 10 oops} msg] $msg
} {1 {wrong # args: should be "string repeat string count"}}
test string-21.3 {string repeat} {
    string repeat {} 100
} {}
test string-21.4 {string repeat} {
    string repeat { } 5
} {     }
test string-21.5 {string repeat} {
    string repeat abc 3
} {abcabcabc}
test string-21.6 {string repeat} {
    string repeat abc -1
} {}
test string-21.7 {string repeat} {
    list [catch {string repeat abc end} msg] $msg
} {1 {expected integer but got "end"}}

test string-22.1 {string replace} {
} {}
test string-22.2 {string replace} {
    string replace abcdefghijklmnop 2 14
} {abp}
test string-22.3 {string replace} {
    string replace abcdefghijklmnop 7 1000
} {abcdefg}
test string-22.4 {string replace} {
    string replace abcdefghijklmnop 10 e
} {abcdefghij}
test string-22.5 {string replace} {
    string replace abcdefghijklmnop 10 9
} {abcdefghijklmnop}
test string-22.6 {string replace} {
    string replace abcdefghijklmnop -3 2
} {defghijklmnop}
test string-22.7 {string replace} {
    string replace abcdefghijklmnop -3 -2
} {abcdefghijklmnop}
test string-22.8 {string replace} {
    string replace abcdefghijklmnop 1000 1010
} {abcdefghijklmnop}
test string-22.9 {string replace} {
    string replace abcdefghijklmnop -100 end
} {}
test string-22.10 {string replace} {
    list [catch {string replace} msg] $msg
} {1 {wrong # args: should be "string replace string first last ?string?"}}
test string-22.11 {string replace} {
    list [catch {string replace a 1} msg] $msg
} {1 {wrong # args: should be "string replace string first last ?string?"}}
test string-22.12 {string replace} {
    list [catch {string replace a 1 2 3 4} msg] $msg
} {1 {wrong # args: should be "string replace string first last ?string?"}}
test string-22.13 {string replace} {
    list [catch {string replace abc abc 1} msg] $msg
} {1 {syntax error in expression "abc"}}
test string-22.14 {string replace} {
    list [catch {string replace abc 1 eof} msg] $msg
} {1 {syntax error in expression "eof"}}
test string-22.15 {string replace} {
    string replace abcdefghijklmnop end-10 end-2 NEW
} {abcdeNEWop}
test string-22.16 {string replace} {
    string replace abcdefghijklmnop 0 e foo
} {foo}
test string-22.17 {string replace} {
    string replace abcdefghijklmnop end end-1
} {abcdefghijklmnop}

# cleanup
::tcltest::cleanupTests
return