Debian Codegraph @

tests/stringObj.test

raw
1 # Commands covered: none
2 #
3 # This file contains tests for the procedures in tclStringObj.c that implement
4 # the Tcl type manager for the string type.
5 #
6 # Sourcing this file into Tcl runs the tests and generates output for errors.
7 # No output means no errors were found.
8 #
9 # Copyright (c) 1995-1997 Sun Microsystems, Inc.
10 # Copyright (c) 1998-1999 by Scriptics Corporation.
11 #
12 # See the file "license.terms" for information on usage and redistribution of
13 # this file, and for a DISCLAIMER OF ALL WARRANTIES.
14
15 if {"::tcltest" ni [namespace children]} {
16 package require tcltest 2.5
17 namespace import -force ::tcltest::*
18 }
19
20 ::tcltest::loadTestedCommands
21 catch [list package require -exact Tcltest [info patchlevel]]
22
23 testConstraint testobj [llength [info commands testobj]]
24 testConstraint testbytestring [llength [info commands testbytestring]]
25 testConstraint testdstring [llength [info commands testdstring]]
26
27 test stringObj-1.1 {string type registration} testobj {
28 set t [testobj types]
29 set first [string first "string" $t]
30 set result [expr {$first >= 0}]
31 } 1
32
33 test stringObj-2.1 {Tcl_NewStringObj} testobj {
34 set result ""
35 lappend result [testobj freeallvars]
36 lappend result [teststringobj set 1 abcd]
37 lappend result [testobj type 1]
38 lappend result [testobj refcount 1]
39 } {{} abcd string 2}
40
41 test stringObj-3.1 {Tcl_SetStringObj, existing "empty string" object} testobj {
42 set result ""
43 lappend result [testobj freeallvars]
44 lappend result [testobj newobj 1]
45 lappend result [teststringobj set 1 xyz] ;# makes existing obj a string
46 lappend result [testobj type 1]
47 lappend result [testobj refcount 1]
48 } {{} {} xyz string 2}
49 test stringObj-3.2 {Tcl_SetStringObj, existing non-"empty string" object} testobj {
50 set result ""
51 lappend result [testobj freeallvars]
52 lappend result [testintobj set 1 512]
53 lappend result [teststringobj set 1 foo] ;# makes existing obj a string
54 lappend result [testobj type 1]
55 lappend result [testobj refcount 1]
56 } {{} 512 foo string 2}
57
58 test stringObj-4.1 {Tcl_SetObjLength procedure, string gets shorter} testobj {
59 testobj freeallvars
60 teststringobj set 1 test
61 teststringobj setlength 1 3
62 list [teststringobj length 1] [teststringobj length2 1] \
63 [teststringobj get 1]
64 } {3 4 tes}
65 test stringObj-4.2 {Tcl_SetObjLength procedure, string gets longer} testobj {
66 testobj freeallvars
67 teststringobj set 1 abcdef
68 teststringobj setlength 1 10
69 teststringobj length 1
70 } 10
71 test stringObj-4.3 {Tcl_SetObjLength procedure, string gets longer} testobj {
72 testobj freeallvars
73 teststringobj set 1 abcdef
74 teststringobj append 1 xyzq -1
75 list [teststringobj length 1] [teststringobj length2 1] \
76 [teststringobj get 1]
77 } {10 20 abcdefxyzq}
78 test stringObj-4.4 {Tcl_SetObjLength procedure, "empty string", length 0} testobj {
79 testobj freeallvars
80 testobj newobj 1
81 teststringobj setlength 1 0
82 list [teststringobj length2 1] [teststringobj get 1]
83 } {0 {}}
84
85 test stringObj-5.1 {Tcl_AppendToObj procedure, type conversion} testobj {
86 testobj freeallvars
87 testintobj set2 1 43
88 teststringobj append 1 xyz -1
89 teststringobj get 1
90 } {43xyz}
91 test stringObj-5.2 {Tcl_AppendToObj procedure, length calculation} testobj {
92 testobj freeallvars
93 teststringobj set 1 {x y }
94 teststringobj append 1 bbCCddEE 4
95 teststringobj append 1 123 -1
96 teststringobj get 1
97 } {x y bbCC123}
98 test stringObj-5.3 {Tcl_AppendToObj procedure, reallocating space} testobj {
99 testobj freeallvars
100 teststringobj set 1 xyz
101 teststringobj setlength 1 15
102 teststringobj setlength 1 2
103 set result {}
104 teststringobj append 1 1234567890123 -1
105 lappend result [teststringobj length 1] [teststringobj length2 1]
106 teststringobj setlength 1 10
107 teststringobj append 1 abcdef -1
108 lappend result [teststringobj length 1] [teststringobj length2 1] \
109 [teststringobj get 1]
110 } {15 15 16 32 xy12345678abcdef}
111
112 test stringObj-6.1 {Tcl_AppendStringsToObj procedure, type conversion} testobj {
113 testobj freeallvars
114 teststringobj set2 1 [list a b]
115 teststringobj appendstrings 1 xyz { 1234 } foo
116 teststringobj get 1
117 } {a bxyz 1234 foo}
118 test stringObj-6.2 {Tcl_AppendStringsToObj procedure, counting space} testobj {
119 testobj freeallvars
120 teststringobj set 1 abc
121 teststringobj appendstrings 1
122 list [teststringobj length 1] [teststringobj get 1]
123 } {3 abc}
124 test stringObj-6.3 {Tcl_AppendStringsToObj procedure, counting space} testobj {
125 testobj freeallvars
126 teststringobj set 1 abc
127 teststringobj appendstrings 1 {} {} {} {}
128 list [teststringobj length 1] [teststringobj get 1]
129 } {3 abc}
130 test stringObj-6.4 {Tcl_AppendStringsToObj procedure, counting space} testobj {
131 testobj freeallvars
132 teststringobj set 1 abc
133 teststringobj appendstrings 1 { 123 } abcdefg
134 list [teststringobj length 1] [teststringobj get 1]
135 } {15 {abc 123 abcdefg}}
136 test stringObj-6.5 {Tcl_AppendStringsToObj procedure, don't double space if initial string empty} testobj {
137 testobj freeallvars
138 testobj newobj 1
139 teststringobj appendstrings 1 123 abcdefg
140 list [teststringobj length 1] [teststringobj length2 1] [teststringobj get 1]
141 } {10 20 123abcdefg}
142 test stringObj-6.6 {Tcl_AppendStringsToObj procedure, space reallocation} testobj {
143 testobj freeallvars
144 teststringobj set 1 abc
145 teststringobj setlength 1 10
146 teststringobj setlength 1 2
147 teststringobj appendstrings 1 34567890
148 list [teststringobj length 1] [teststringobj length2 1] \
149 [teststringobj get 1]
150 } {10 10 ab34567890}
151 test stringObj-6.7 {Tcl_AppendStringsToObj procedure, space reallocation} testobj {
152 testobj freeallvars
153 teststringobj set 1 abc
154 teststringobj setlength 1 10
155 teststringobj setlength 1 2
156 teststringobj appendstrings 1 34567890x
157 list [teststringobj length 1] [teststringobj length2 1] \
158 [teststringobj get 1]
159 } {11 22 ab34567890x}
160 test stringObj-6.8 {Tcl_AppendStringsToObj procedure, object totally empty} testobj {
161 testobj freeallvars
162 testobj newobj 1
163 teststringobj appendstrings 1 {}
164 list [teststringobj length2 1] [teststringobj get 1]
165 } {0 {}}
166 test stringObj-6.9 {Tcl_AppendStringsToObj, pure unicode} testobj {
167 testobj freeallvars
168 teststringobj set2 1 [string replace abc 1 1 d]
169 teststringobj appendstrings 1 foo bar soom
170 teststringobj get 1
171 } adcfoobarsoom
172
173 test stringObj-7.1 {SetStringFromAny procedure} testobj {
174 testobj freeallvars
175 teststringobj set2 1 [list a b]
176 teststringobj append 1 x -1
177 list [teststringobj length 1] [teststringobj length2 1] \
178 [teststringobj get 1]
179 } {4 8 {a bx}}
180 test stringObj-7.2 {SetStringFromAny procedure, null object} testobj {
181 testobj freeallvars
182 testobj newobj 1
183 teststringobj appendstrings 1 {}
184 list [teststringobj length 1] [teststringobj length2 1] \
185 [teststringobj get 1]
186 } {0 0 {}}
187 test stringObj-7.3 {SetStringFromAny called with non-string obj} testobj {
188 set x 2345
189 list [incr x] [testobj objtype $x] [string index $x end] \
190 [testobj objtype $x]
191 } {2346 int 6 string}
192 test stringObj-7.4 {SetStringFromAny called with string obj} testobj {
193 set x "abcdef"
194 list [string length $x] [testobj objtype $x] \
195 [string length $x] [testobj objtype $x]
196 } {6 string 6 string}
197
198 test stringObj-8.1 {DupStringInternalRep procedure} testobj {
199 testobj freeallvars
200 teststringobj set 1 {}
201 teststringobj append 1 abcde -1
202 testobj duplicate 1 2
203 list [teststringobj length 1] [teststringobj length2 1] \
204 [teststringobj maxchars 1] [teststringobj get 1] \
205 [teststringobj length 2] [teststringobj length2 2] \
206 [teststringobj maxchars 2] [teststringobj get 2]
207 } {5 10 0 abcde 5 5 0 abcde}
208 test stringObj-8.2 {DupUnicodeInternalRep, mixed width chars} testobj {
209 set x abc\u00ef\u00bf\u00aeghi
210 string length $x
211 set y $x
212 list [testobj objtype $x] [testobj objtype $y] [append x "\u00ae\u00bf\u00ef"] \
213 [set y] [testobj objtype $x] [testobj objtype $y]
214 } "string string abc\u00ef\u00bf\u00aeghi\u00ae\u00bf\u00ef abc\u00ef\u00bf\u00aeghi string string"
215 test stringObj-8.3 {DupUnicodeInternalRep, mixed width chars} testobj {
216 set x abc\u00ef\u00bf\u00aeghi
217 set y $x
218 string length $x
219 list [testobj objtype $x] [testobj objtype $y] [append x "\u00ae\u00bf\u00ef"] \
220 [set y] [testobj objtype $x] [testobj objtype $y]
221 } "string string abc\u00ef\u00bf\u00aeghi\u00ae\u00bf\u00ef abc\u00ef\u00bf\u00aeghi string string"
222 test stringObj-8.4 {DupUnicodeInternalRep, all byte-size chars} testobj {
223 set x abcdefghi
224 string length $x
225 set y $x
226 list [testobj objtype $x] [testobj objtype $y] [append x jkl] \
227 [set y] [testobj objtype $x] [testobj objtype $y]
228 } {string string abcdefghijkl abcdefghi string string}
229 test stringObj-8.5 {DupUnicodeInternalRep, all byte-size chars} testobj {
230 set x abcdefghi
231 set y $x
232 string length $x
233 list [testobj objtype $x] [testobj objtype $y] [append x jkl] \
234 [set y] [testobj objtype $x] [testobj objtype $y]
235 } {string string abcdefghijkl abcdefghi string string}
236
237 test stringObj-9.1 {TclAppendObjToObj, mixed src & dest} {testobj testdstring} {
238 set x abc\u00ef\u00bf\u00aeghi
239 testdstring free
240 testdstring append \u00ae\u00bf\u00ef -1
241 set y [testdstring get]
242 string length $x
243 list [testobj objtype $x] [testobj objtype $y] [append x $y] \
244 [set y] [testobj objtype $x] [testobj objtype $y]
245 } "string none abc\u00ef\u00bf\u00aeghi\u00ae\u00bf\u00ef \u00ae\u00bf\u00ef string none"
246 test stringObj-9.2 {TclAppendObjToObj, mixed src & dest} testobj {
247 set x abc\u00ef\u00bf\u00aeghi
248 string length $x
249 list [testobj objtype $x] [append x $x] [testobj objtype $x] \
250 [append x $x] [testobj objtype $x]
251 } "string abc\u00ef\u00bf\u00aeghiabc\u00ef\u00bf\u00aeghi string\
252 abc\u00ef\u00bf\u00aeghiabc\u00ef\u00bf\u00aeghiabc\u00ef\u00bf\u00aeghiabc\u00ef\u00bf\u00aeghi\
253 string"
254 test stringObj-9.3 {TclAppendObjToObj, mixed src & 1-byte dest} {testobj testdstring} {
255 set x abcdefghi
256 testdstring free
257 testdstring append \u00ae\u00bf\u00ef -1
258 set y [testdstring get]
259 string length $x
260 list [testobj objtype $x] [testobj objtype $y] [append x $y] \
261 [set y] [testobj objtype $x] [testobj objtype $y]
262 } "string none abcdefghi\u00ae\u00bf\u00ef \u00ae\u00bf\u00ef string none"
263 test stringObj-9.4 {TclAppendObjToObj, 1-byte src & dest} {testobj testdstring} {
264 set x abcdefghi
265 testdstring free
266 testdstring append jkl -1
267 set y [testdstring get]
268 string length $x
269 list [testobj objtype $x] [testobj objtype $y] [append x $y] \
270 [set y] [testobj objtype $x] [testobj objtype $y]
271 } {string none abcdefghijkl jkl string none}
272 test stringObj-9.5 {TclAppendObjToObj, 1-byte src & dest} testobj {
273 set x abcdefghi
274 string length $x
275 list [testobj objtype $x] [append x $x] [testobj objtype $x] \
276 [append x $x] [testobj objtype $x]
277 } {string abcdefghiabcdefghi string abcdefghiabcdefghiabcdefghiabcdefghi\
278 string}
279 test stringObj-9.6 {TclAppendObjToObj, 1-byte src & mixed dest} {testobj testdstring} {
280 set x abc\u00ef\u00bf\u00aeghi
281 testdstring free
282 testdstring append jkl -1
283 set y [testdstring get]
284 string length $x
285 list [testobj objtype $x] [testobj objtype $y] [append x $y] \
286 [set y] [testobj objtype $x] [testobj objtype $y]
287 } "string none abc\u00ef\u00bf\u00aeghijkl jkl string none"
288 test stringObj-9.7 {TclAppendObjToObj, integer src & dest} testobj {
289 set x [expr {4 * 5}]
290 set y [expr {4 + 5}]
291 list [testobj objtype $x] [testobj objtype $y] [append x $y] \
292 [testobj objtype $x] [append x $y] [testobj objtype $x] \
293 [testobj objtype $y]
294 } {int int 209 string 2099 string int}
295 test stringObj-9.8 {TclAppendObjToObj, integer src & dest} testobj {
296 set x [expr {4 * 5}]
297 list [testobj objtype $x] [append x $x] [testobj objtype $x] \
298 [append x $x] [testobj objtype $x]
299 } {int 2020 string 20202020 string}
300 test stringObj-9.9 {TclAppendObjToObj, integer src & 1-byte dest} testobj {
301 set x abcdefghi
302 set y [expr {4 + 5}]
303 string length $x
304 list [testobj objtype $x] [testobj objtype $y] [append x $y] \
305 [set y] [testobj objtype $x] [testobj objtype $y]
306 } {string int abcdefghi9 9 string int}
307 test stringObj-9.10 {TclAppendObjToObj, integer src & mixed dest} testobj {
308 set x abc\u00ef\u00bf\u00aeghi
309 set y [expr {4 + 5}]
310 string length $x
311 list [testobj objtype $x] [testobj objtype $y] [append x $y] \
312 [set y] [testobj objtype $x] [testobj objtype $y]
313 } "string int abc\u00ef\u00bf\u00aeghi9 9 string int"
314 test stringObj-9.11 {TclAppendObjToObj, mixed src & 1-byte dest index check} testobj {
315 # bug 2678, in <=8.2.0, the second obj (the one to append) in
316 # Tcl_AppendObjToObj was not correctly checked to see if it was all one
317 # byte chars, so a Unicode string would be added as one byte chars.
318 set x abcdef
319 set len [string length $x]
320 set y a\u00fcb\u00e5c\u00ef
321 set len [string length $y]
322 append x $y
323 string length $x
324 set q {}
325 for {set i 0} {$i < 12} {incr i} {
326 lappend q [string index $x $i]
327 }
328 set q
329 } "a b c d e f a \u00fc b \u00e5 c \u00ef"
330
331 test stringObj-10.1 {Tcl_GetRange with all byte-size chars} {testobj testdstring} {
332 testdstring free
333 testdstring append abcdef -1
334 set x [testdstring get]
335 list [testobj objtype $x] [set y [string range $x 1 end-1]] \
336 [testobj objtype $x] [testobj objtype $y]
337 } [list none bcde string string]
338 test stringObj-10.2 {Tcl_GetRange with some mixed width chars} {testobj testdstring} {
339 # Because this test does not use \uXXXX notation below instead of
340 # hard-coding the values, it may fail in multibyte locales. However, we
341 # need to test that the parser produces untyped objects even when there
342 # are high-ASCII characters in the input (like "ï"). I don't know what
343 # else to do but inline those characters here.
344 testdstring free
345 testdstring append "abc\u00ef\u00efdef" -1
346 set x [testdstring get]
347 list [testobj objtype $x] [set y [string range $x 1 end-1]] \
348 [testobj objtype $x] [testobj objtype $y]
349 } [list none "bc\u00EF\u00EFde" string string]
350 test stringObj-10.3 {Tcl_GetRange with some mixed width chars} testobj {
351 # set x "abcïïdef"
352 # Use \uXXXX notation below instead of hard-coding the values, otherwise
353 # the test will fail in multibyte locales.
354 set x "abc\u00EF\u00EFdef"
355 string length $x
356 list [testobj objtype $x] [set y [string range $x 1 end-1]] \
357 [testobj objtype $x] [testobj objtype $y]
358 } [list string "bc\u00EF\u00EFde" string string]
359 test stringObj-10.4 {Tcl_GetRange with some mixed width chars} testobj {
360 # set a "ïa¿b®cï¿d®"
361 # Use \uXXXX notation below instead of hard-coding the values, otherwise
362 # the test will fail in multibyte locales.
363 set a "\u00EFa\u00BFb\u00AEc\u00EF\u00BFd\u00AE"
364 set result [list]
365 while {[string length $a] > 0} {
366 set a [string range $a 1 end-1]
367 lappend result $a
368 }
369 set result
370 } [list a\u00BFb\u00AEc\u00EF\u00BFd \
371 \u00BFb\u00AEc\u00EF\u00BF \
372 b\u00AEc\u00EF \
373 \u00AEc \
374 {}]
375
376 test stringObj-11.1 {UpdateStringOfString} testobj {
377 set x 2345
378 list [string index $x end] [testobj objtype $x] [incr x] \
379 [testobj objtype $x]
380 } {5 string 2346 int}
381
382 test stringObj-12.1 {Tcl_GetUniChar with byte-size chars} testobj {
383 set x "abcdefghi"
384 list [string index $x 0] [string index $x 1]
385 } {a b}
386 test stringObj-12.2 {Tcl_GetUniChar with byte-size chars} testobj {
387 set x "abcdefghi"
388 list [string index $x 3] [string index $x end]
389 } {d i}
390 test stringObj-12.3 {Tcl_GetUniChar with byte-size chars} testobj {
391 set x "abcdefghi"
392 list [string index $x end] [string index $x end-1]
393 } {i h}
394 test stringObj-12.4 {Tcl_GetUniChar with mixed width chars} testobj {
395 string index "\u00efa\u00bfb\u00aec\u00ae\u00bfd\u00ef" 0
396 } "\u00ef"
397 test stringObj-12.5 {Tcl_GetUniChar} testobj {
398 set x "\u00efa\u00bfb\u00aec\u00ae\u00bfd\u00ef"
399 list [string index $x 4] [string index $x 0]
400 } "\u00ae \u00ef"
401 test stringObj-12.6 {Tcl_GetUniChar} testobj {
402 string index "\u00efa\u00bfb\u00aec\u00ef\u00bfd\u00ae" end
403 } "\u00ae"
404
405 test stringObj-13.1 {Tcl_GetCharLength with byte-size chars} testobj {
406 set a ""
407 list [string length $a] [string length $a]
408 } {0 0}
409 test stringObj-13.2 {Tcl_GetCharLength with byte-size chars} testobj {
410 string length "a"
411 } 1
412 test stringObj-13.3 {Tcl_GetCharLength with byte-size chars} testobj {
413 set a "abcdef"
414 list [string length $a] [string length $a]
415 } {6 6}
416 test stringObj-13.4 {Tcl_GetCharLength with mixed width chars} testobj {
417 string length "\u00ae"
418 } 1
419 test stringObj-13.5 {Tcl_GetCharLength with mixed width chars} testobj {
420 # string length "○○"
421 # Use \uXXXX notation below instead of hard-coding the values, otherwise
422 # the test will fail in multibyte locales.
423 string length "\u00EF\u00BF\u00AE\u00EF\u00BF\u00AE"
424 } 6
425 test stringObj-13.6 {Tcl_GetCharLength with mixed width chars} testobj {
426 # set a "ïa¿b®cï¿d®"
427 # Use \uXXXX notation below instead of hard-coding the values, otherwise
428 # the test will fail in multibyte locales.
429 set a "\u00EFa\u00BFb\u00AEc\u00EF\u00BFd\u00AE"
430 list [string length $a] [string length $a]
431 } {10 10}
432 test stringObj-13.7 {Tcl_GetCharLength with identity nulls} {testobj testbytestring} {
433 # SF bug #684699
434 string length [testbytestring \x00]
435 } 1
436 test stringObj-13.8 {Tcl_GetCharLength with identity nulls} {testobj testbytestring} {
437 string length [testbytestring \x01\x00\x02]
438 } 3
439
440 test stringObj-14.1 {Tcl_SetObjLength on pure unicode object} testobj {
441 teststringobj set 1 foo
442 teststringobj getunicode 1
443 teststringobj append 1 bar -1
444 teststringobj getunicode 1
445 teststringobj append 1 bar -1
446 teststringobj setlength 1 0
447 teststringobj append 1 bar -1
448 teststringobj get 1
449 } {bar}
450
451 test stringObj-15.1 {Tcl_Append*ToObj: self appends} testobj {
452 teststringobj set 1 foo
453 teststringobj appendself 1 0
454 } foofoo
455 test stringObj-15.2 {Tcl_Append*ToObj: self appends} testobj {
456 teststringobj set 1 foo
457 teststringobj appendself 1 1
458 } foooo
459 test stringObj-15.3 {Tcl_Append*ToObj: self appends} testobj {
460 teststringobj set 1 foo
461 teststringobj appendself 1 2
462 } fooo
463 test stringObj-15.4 {Tcl_Append*ToObj: self appends} testobj {
464 teststringobj set 1 foo
465 teststringobj appendself 1 3
466 } foo
467 test stringObj-15.5 {Tcl_Append*ToObj: self appends} testobj {
468 teststringobj set 1 foo
469 teststringobj appendself2 1 0
470 } foofoo
471 test stringObj-15.6 {Tcl_Append*ToObj: self appends} testobj {
472 teststringobj set 1 foo
473 teststringobj appendself2 1 1
474 } foooo
475 test stringObj-15.7 {Tcl_Append*ToObj: self appends} testobj {
476 teststringobj set 1 foo
477 teststringobj appendself2 1 2
478 } fooo
479 test stringObj-15.8 {Tcl_Append*ToObj: self appends} testobj {
480 teststringobj set 1 foo
481 teststringobj appendself2 1 3
482 } foo
483
484 test stringObj-16.0 {Tcl_GetRange: normal case} testobj {
485 teststringobj set 1 abcde
486 teststringobj range 1 1 3
487 } bcd
488 test stringObj-16.1 {Tcl_GetRange: first > end} testobj {
489 teststringobj set 1 abcde
490 teststringobj range 1 10 5
491 } {}
492 test stringObj-16.2 {Tcl_GetRange: last > end} testobj {
493 teststringobj set 1 abcde
494 teststringobj range 1 3 13
495 } de
496 test stringObj-16.3 {Tcl_GetRange: first = -1} testobj {
497 teststringobj set 1 abcde
498 teststringobj range 1 -1 3
499 } abcd
500 test stringObj-16.4 {Tcl_GetRange: last = -1} testobj {
501 teststringobj set 1 abcde
502 teststringobj range 1 1 -1
503 } bcde
504 test stringObj-16.5 {Tcl_GetRange: fist = last = -1} testobj {
505 teststringobj set 1 abcde
506 teststringobj range 1 -1 -1
507 } abcde
508 test stringObj-16.6 {Tcl_GetRange: old anomaly} testobj {
509 # Older implementations could return "cde"
510 teststringobj set 1 abcde
511 teststringobj range 1 2 0
512 } {}
513
514
515 if {[testConstraint testobj]} {
516 testobj freeallvars
517 }
518
519 # cleanup
520 ::tcltest::cleanupTests
521 return