1#
2# string.test
3#
4# Tests for the string-related commands.
5#---------------------------------------------------------------------------
6# Copyright 1992-1999 Karl Lehenbauer and Mark Diekhans.
7#
8# Permission to use, copy, modify, and distribute this software and its
9# documentation for any purpose and without fee is hereby granted, provided
10# that the above copyright notice appear in all copies.  Karl Lehenbauer and
11# Mark Diekhans make no representations about the suitability of this
12# software for any purpose.  It is provided "as is" without express or
13# implied warranty.
14#------------------------------------------------------------------------------
15# $Id: string.test,v 1.3 2002/09/26 00:19:02 hobbs Exp $
16#------------------------------------------------------------------------------
17#
18
19if {[cequal [info procs Test] {}]} {
20    source [file join [file dirname [info script]] testlib.tcl]
21}
22
23# Test the 'cindex' command.
24
25Test string-1.1 {cindex tests} {
26    cindex ABCDEFG 1
27} 0 {B}
28
29Test string-1.2 {cindex tests} {
30    cindex ABCDEFG 3+1
31} 0 {E}
32
33Test string-1.3 {cindex tests} {
34    cindex ABCDEFG 3*2
35} 0 {G}
36
37Test string-1.4 {cindex tests} {
38    cindex ABCDEFG 7
39} 0 {}
40
41Test string-1.5 {cindex tests} {
42    cindex ABCDEFG end-2
43} 0 {E}
44
45Test string-1.6 {cindex tests} {
46    cindex ABCDEFG len-3
47} 0 {E}
48
49Test string-1.7 {cindex tests} {
50    cindex ABCDEFG lenx-3
51} 1 "syntax error in expression \"7x-3\"[expr {
52    ($tcl_version>8.3) ? ": extra tokens at end of expression" : ""
53}]"
54
55Test string-1.8 {cindex tests} {
56    cindex ABCDEFG
57} 1 {wrong # args: cindex string indexExpr}
58
59Test string-1.9 {cindex tests} {
60    cindex ABCDEFG 1 10
61} 1 {wrong # args: cindex string indexExpr}
62
63Test string-1.10 {cindex tests} {
64    cindex A\0BCDEFG 2
65} 0 {B}
66
67Test string-1.11 {cindex tests} {
68    cindex A\0BCDEFG 1
69} 0 "\0"
70
71Test string-1.12 {cindex unicode tests} {
72    cindex \u7266abc\u7266x 1
73} 0 "a"
74
75Test string-1.13 {cindex unicode tests} {
76    cindex \u7266abc\u7266x 0
77} 0 "\u7266"
78
79Test string-1.14 {cindex unicode tests} {
80    cindex \u7266abc\u7266x 4
81} 0 "\u7266"
82
83Test string-1.15 {cindex unicode tests} {
84    cindex \u7266abc\u7266x 5
85} 0 "x"
86
87
88# Test the 'clength' command.
89
90Test string-2.1 {clength tests} {
91    clength ABCDEFG
92} 0 7
93
94Test string-2.2 {clength tests} {
95    clength "ABCD XYZ"
96} 0 8
97
98Test string-2.3 {clength tests} {
99    clength
100} 1 {wrong # args: clength string}
101
102Test string-2.4 {clength tests} {
103    clength "AB\0D X\0Z"
104} 0 8
105
106Test string-2.5 {clength unicode tests} {
107    clength \u7266abc\u7266x
108} 0 6
109
110Test string-2.6 {clength unicode tests} {
111    clength abc\u7266x\u7266
112} 0 6
113
114# Test the crange command.
115
116Test string-3.1 {crange tests} {
117    crange ABCDEFG 1 3
118} 0 {BCD}
119
120Test string-3.2 {crange tests} {
121    crange ABCDEFG 2 end
122} 0 {CDEFG}
123
124Test string-3.3 {crange tests} {
125    set foo [replicate ABCD 500]
126    crange $foo 25*4 500-1
127} 0 [replicate ABCD 100]
128
129Test string-3.4 {crange tests} {
130    crange
131} 1 {wrong # args: crange string firstExpr lastExpr}
132
133Test string-3.5 {crange tests} {
134    crange ABCD 4 1
135} 0 {}
136
137Test string-3.6 {crange tests} {
138    crange ABCD end-2 len-1
139} 0 {BCD}
140
141Test string-3.7 {crange tests} {
142    crange ABCD len-3 end-1
143} 0 {BC}
144
145Test string-3.8 {crange tests} {
146    # 8.4+ enhanced the error return from expressions
147    crange ABCD lenx-3 end-1
148}  1 "syntax error in expression \"4x-3\"[expr {
149    ($tcl_version>8.3) ? ": extra tokens at end of expression" : ""
150}]"
151
152Test string-3.9 {crange tests} {
153    set text .tex
154    set l 4
155    crange $text $l+1 end
156} 0 {}
157
158Test string-3.10 {crange tests} {
159    crange AB\0DEFG 1 3
160} 0 "B\0D"
161
162Test string-3.11 {crange tests} {
163    crange ABC\0E\0G 2 end
164} 0 "C\0E\0G"
165
166
167Test string-3.12 {crange unicode tests} {
168    crange \u7266abc\u7266x 2 end
169} 0 "bc\u7266x"
170
171
172# Test the 'replicate' command
173
174Test string-4.1 {replicate tests} {
175    replicate AbCd 4
176} 0 {AbCdAbCdAbCdAbCd}
177
178Test string-4.2 {replicate tests} {
179    replicate X 1000
180} 0 "[replicate X 250][replicate X 250][replicate X 250][replicate X 250]"
181
182Test string-4.3 {replicate tests} {
183    replicate X
184} 1 {wrong # args: replicate string countExpr}
185
186Test string-4.4 {replicate tests} {
187    replicate Ab\0d 4
188} 0 "Ab\0dAb\0dAb\0dAb\0d"
189
190Test string-4.5 {replicate unicode tests} {
191    replicate \u7266abc\u7266x  3
192} 0 "\u7266abc\u7266x\u7266abc\u7266x\u7266abc\u7266x"
193
194# Test the csubstr command.
195
196Test string-5.1 {csubstr tests} {
197    csubstr ABCDEFG 1 2+1
198} 0 {BCD}
199
200Test string-5.2 {csubstr tests} {
201    csubstr ABCDEFG 1+1 end
202} 0 {CDEFG}
203
204Test string-5.3 {csubstr tests} {
205    set foo [replicate ABCD 500]
206    csubstr $foo 25*4 100*4
207} 0 [replicate ABCD 100]
208
209Test string-5.4 {csubstr tests} {
210    csubstr
211} 1 {wrong # args: csubstr string firstExpr lengthExpr}
212
213Test string-5.5 {csubstr tests} {
214    csubstr ABCD 4 1
215} 0 {}
216
217Test string-5.6 {csubstr tests} {
218    csubstr ABCD 1 end-1
219} 0 {BC}
220
221Test string-5.7 {csubstr tests} {
222    csubstr ABCD len-2 end
223} 0 {CD}
224
225Test string-5.8 {csubstr tests} {
226    csubstr ABCD 0 len
227} 0 {ABCD}
228
229Test string-5.9 {csubstr tests} {
230    csubstr AB\0D len-2 end
231} 0 "\0D"
232
233Test string-5.8 {csubstr tests} {
234    csubstr AB\0D 0 len
235} 0 "AB\0D"
236
237Test string-5.9 {csubstr unicode tests} {
238    csubstr \u7266abc\u7266x 0 1
239} 0 \u7266
240
241Test string-5.10 {csubstr unicode tests} {
242    csubstr \u7266abc\u7266x 1 end-1
243} 0 abc\u7266
244
245# Test the translit command.
246
247Test string-6.1 {translit tests} {
248    set str "Captain Midnight Secret Decoder Ring"
249    translit {A-MN-Za-mn-z} {N-ZA-Mn-za-m} $str
250} 0 {Pncgnva Zvqavtug Frperg Qrpbqre Evat}
251
252Test string-6.2 {translit tests} {
253    set str "Captain Midnight Secret Decoder Ring"
254    set str2 [translit {A-MN-Za-mn-z} {N-ZA-Mn-za-m} $str]
255    translit {A-MN-Za-mn-z} {N-ZA-Mn-za-m} $str2
256} 0 {Captain Midnight Secret Decoder Ring}
257
258Test string-6.4 {translit tests} {
259    translit
260} 1 {wrong # args: translit from to string}
261
262# Type conversion was broken.
263Test string-6.5 {translit tests} {
264    catch {unset xxx}
265    set s [list This_is_a_test value]
266    array set xxx [translit _ - $s]
267    array get xxx
268} 0 {This-is-a-test value}
269catch {unset xxx}
270
271# Check for detection of unsupported UniCode
272Test string-6.6 {translit tests} {
273    set str "Captain Midnight Secret Decoder Ring"
274    translit "A-MN-Za-m\u1234-z" "N-ZA-Mn-za-m" $str
275} 1 {Unicode character found in in-range, the translit command does not yet support Unicode}
276
277Test string-6.7 {translit tests} {
278    set str "Captain Midnight Secret Decoder Ring"
279    translit "A-MN-Za-mn-z" "N-ZA-Mn-za-\u5134" $str
280} 1 {Unicode character found in out-range, the translit command does not yet support Unicode}
281
282Test string-6.8 {translit tests} {
283    set str "Captain Midnight Secret \u1543ecoder Ring"
284    translit "A-MN-Za-mn-z" "N-ZA-Mn-za-m" $str
285} 1 {Unicode character found in string to translate, the translit command does not yet support Unicode}
286
287
288# Test the ctoken command
289
290Test string-7.1 {ctoken tests} {
291    ctoken
292} 1 {wrong # args: ctoken strvar separators}
293
294Test string-7.2 {ctoken tests} {
295    ctoken a b c
296} 1 {wrong # args: ctoken strvar separators}
297
298Test string-7.3 {ctoken tests} {
299    set orgstr "  \t  this\tis \n  a   test   "
300    set s1 [ctoken orgstr " \t\n"]
301    set s1v $orgstr
302    set s2 [ctoken orgstr " \t\n"]
303    set s2v $orgstr
304    set s3 [ctoken orgstr " \t\n"]
305    set s3v $orgstr
306    set s4 [ctoken orgstr " \t\n"]
307    set s4v $orgstr
308    set s5 [ctoken orgstr " \t\n"]
309    set s5v $orgstr
310    list $s1 $s1v $s2 $s2v $s3 $s3v $s4 $s4v $s5 $s5v
311} 0 [list "this"  "\tis \n  a   test   " \
312          "is"    " \n  a   test   " \
313          "a"     "   test   " \
314          "test"  "   " \
315          ""      ""]
316Test string-7.2 {ctoken tests} {
317    ctoken "No such variable" " \t"
318} 1 {can't read "No such variable": no such variable}
319
320
321Test string-9.1 {cequal tests} {
322    cequal
323} 1 {wrong # args: cequal string1 string2}
324
325Test string-9.2 {cequal tests} {
326    cequal a b c
327} 1 {wrong # args: cequal string1 string2}
328
329Test string-9.3 {cequal tests} {
330    cequal ab c
331} 0 0
332
333Test string-9.4 {cequal tests} {
334    cequal abcded abcded
335} 0 1
336
337Test string-9.5 {cequal tests} {
338    cequal a\0 a
339} 0 0
340
341Test string-9.6 {cequal tests} {
342    cequal ab\0cd\0ed ab\0cd\0ed
343} 0 1
344
345Test string-9.7 {cequal tests} {
346    cequal file5 file4
347} 0 0
348
349Test string-9.8 {cequal unicode tests} {
350    cequal \u7266abc\u7266x \u7266abc\u7266x
351} 0 1
352
353Test string-9.9 {cequal unicode tests} {
354    cequal \u7266abc\u7267x \u7266abc\u7266x
355} 0 0
356
357# ccollate command
358
359Test string-10.1 {ccollate tests} {
360    ccollate
361} 1 {wrong # args: ccollate ?options? string1 string2}
362
363Test string-10.2 {ccollate tests} {
364    ccollate aaa bbb ccc ddd
365} 1 {wrong # args: ccollate ?options? string1 string2}
366
367Test string-10.3 {ccollate tests} {
368    ccollate -bbb ccc ddd
369} 1 {Invalid option "-bbb", expected "-local"}
370
371Test string-10.4 {ccollate tests} {
372    ccollate nnn ccc ddd
373} 1 {Invalid option "nnn", expected "-local"}
374
375Test string-10.5 {ccollate tests} {
376    ccollate abcdef abcdef
377} 0 0
378
379Test string-10.6 {ccollate tests} {
380    ccollate abcdefg abcdef
381} 0 1
382
383Test string-10.7 {ccollate tests} {
384    ccollate abcde abcdef
385} 0 -1
386
387Test string-10.8 {ccollate tests} {
388    ccollate -local abcdefg abcdef
389} 0 1
390
391Test string-10.9 {ccollate tests} {
392    ccollate -local abcde abcdef
393} 0 -1
394
395Test string-11.1 {cconcat tests} {
396    cconcat
397} 0 {}
398
399Test string-11.2 {cconcat tests} {
400    cconcat Aaa Bbb
401} 0 {AaaBbb}
402
403
404Test string-11.3 {cconcat tests} {
405    cconcat Aaa " " Bbb
406} 0 {Aaa Bbb}
407
408Test string-11.4 {cconcat tests} {
409    cconcat A\0a B\0b
410} 0 "A\0aB\0b"
411
412
413Test string-11.4 {cconcat tests} {
414    cconcat Aaa " " \0 Bbb
415} 0 "Aaa \0Bbb"
416
417
418# cleanup
419::tcltest::cleanupTests
420return
421