Debian Codegraph @

tests/dstring.test

raw
1 # Commands covered: none
2 #
3 # This file contains a collection of tests for Tcl's dynamic string library
4 # procedures. Sourcing this file into Tcl runs the tests and generates output
5 # for errors. No output means no errors were found.
6 #
7 # Copyright (c) 1993 The Regents of the University of California.
8 # Copyright (c) 1994 Sun Microsystems, Inc.
9 # Copyright (c) 1998-1999 by Scriptics Corporation.
10 #
11 # See the file "license.terms" for information on usage and redistribution of
12 # this file, and for a DISCLAIMER OF ALL WARRANTIES.
13
14 if {"::tcltest" ni [namespace children]} {
15 package require tcltest 2.5
16 namespace import -force ::tcltest::*
17 }
18
19 ::tcltest::loadTestedCommands
20 catch [list package require -exact Tcltest [info patchlevel]]
21
22 testConstraint testdstring [llength [info commands testdstring]]
23 if {[testConstraint testdstring]} {
24 testdstring free
25 }
26
27 test dstring-1.1 {appending and retrieving} -constraints testdstring -setup {
28 testdstring free
29 } -body {
30 testdstring append "abc" -1
31 list [testdstring get] [testdstring length]
32 } -cleanup {
33 testdstring free
34 } -result {abc 3}
35 test dstring-1.2 {appending and retrieving} -constraints testdstring -setup {
36 testdstring free
37 } -body {
38 testdstring append "abc" -1
39 testdstring append " xyzzy" 3
40 testdstring append " 12345" -1
41 list [testdstring get] [testdstring length]
42 } -cleanup {
43 testdstring free
44 } -result {{abc xy 12345} 12}
45 test dstring-1.3 {appending and retrieving} -constraints testdstring -setup {
46 testdstring free
47 } -body {
48 foreach l {a b c d e f g h i j k l m n o p} {
49 testdstring append $l$l$l$l$l$l$l$l$l$l$l$l$l$l$l$l$l$l$l$l$l\n -1
50 }
51 list [testdstring get] [testdstring length]
52 } -cleanup {
53 testdstring free
54 } -result {{aaaaaaaaaaaaaaaaaaaaa
55 bbbbbbbbbbbbbbbbbbbbb
56 ccccccccccccccccccccc
57 ddddddddddddddddddddd
58 eeeeeeeeeeeeeeeeeeeee
59 fffffffffffffffffffff
60 ggggggggggggggggggggg
61 hhhhhhhhhhhhhhhhhhhhh
62 iiiiiiiiiiiiiiiiiiiii
63 jjjjjjjjjjjjjjjjjjjjj
64 kkkkkkkkkkkkkkkkkkkkk
65 lllllllllllllllllllll
66 mmmmmmmmmmmmmmmmmmmmm
67 nnnnnnnnnnnnnnnnnnnnn
68 ooooooooooooooooooooo
69 ppppppppppppppppppppp
70 } 352}
71
72 test dstring-2.1 {appending list elements} -constraints testdstring -setup {
73 testdstring free
74 } -body {
75 testdstring element "abc"
76 testdstring element "d e f"
77 list [testdstring get] [testdstring length]
78 } -cleanup {
79 testdstring free
80 } -result {{abc {d e f}} 11}
81 test dstring-2.2 {appending list elements} -constraints testdstring -setup {
82 testdstring free
83 } -body {
84 testdstring element "x"
85 testdstring element "\{"
86 testdstring element "ab\}"
87 testdstring get
88 } -cleanup {
89 testdstring free
90 } -result {x \{ ab\}}
91 test dstring-2.3 {appending list elements} -constraints testdstring -setup {
92 testdstring free
93 } -body {
94 foreach l {a b c d e f g h i j k l m n o p} {
95 testdstring element $l$l$l$l$l$l$l$l$l$l$l$l$l$l$l$l$l$l$l$l$l
96 }
97 testdstring get
98 } -cleanup {
99 testdstring free
100 } -result {aaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbbbbb ccccccccccccccccccccc ddddddddddddddddddddd eeeeeeeeeeeeeeeeeeeee fffffffffffffffffffff ggggggggggggggggggggg hhhhhhhhhhhhhhhhhhhhh iiiiiiiiiiiiiiiiiiiii jjjjjjjjjjjjjjjjjjjjj kkkkkkkkkkkkkkkkkkkkk lllllllllllllllllllll mmmmmmmmmmmmmmmmmmmmm nnnnnnnnnnnnnnnnnnnnn ooooooooooooooooooooo ppppppppppppppppppppp}
101 test dstring-2.4 {appending list elements} -constraints testdstring -setup {
102 testdstring free
103 } -body {
104 testdstring append "a\{" -1
105 testdstring element abc
106 testdstring append " \{" -1
107 testdstring element xyzzy
108 testdstring get
109 } -cleanup {
110 testdstring free
111 } -result "a{ abc {xyzzy"
112 test dstring-2.5 {appending list elements} -constraints testdstring -setup {
113 testdstring free
114 } -body {
115 testdstring append " \{" -1
116 testdstring element abc
117 testdstring get
118 } -cleanup {
119 testdstring free
120 } -result " {abc"
121 test dstring-2.6 {appending list elements} -constraints testdstring -setup {
122 testdstring free
123 } -body {
124 testdstring append " " -1
125 testdstring element abc
126 testdstring get
127 } -cleanup {
128 testdstring free
129 } -result { abc}
130 test dstring-2.7 {appending list elements} -constraints testdstring -setup {
131 testdstring free
132 } -body {
133 testdstring append "\\ " -1
134 testdstring element abc
135 testdstring get
136 } -cleanup {
137 testdstring free
138 } -result "\\ abc"
139 test dstring-2.8 {appending list elements} -constraints testdstring -setup {
140 testdstring free
141 } -body {
142 testdstring append "x " -1
143 testdstring element abc
144 testdstring get
145 } -cleanup {
146 testdstring free
147 } -result {x abc}
148 test dstring-2.9 {appending list elements} -constraints testdstring -setup {
149 testdstring free
150 } -body {
151 testdstring element #
152 testdstring get
153 } -cleanup {
154 testdstring free
155 } -result {{#}}
156 test dstring-2.10 {appending list elements} -constraints testdstring -setup {
157 testdstring free
158 } -body {
159 testdstring append " " -1
160 testdstring element #
161 testdstring get
162 } -cleanup {
163 testdstring free
164 } -result { {#}}
165 test dstring-2.11 {appending list elements} -constraints testdstring -setup {
166 testdstring free
167 } -body {
168 testdstring append \t -1
169 testdstring element #
170 testdstring get
171 } -cleanup {
172 testdstring free
173 } -result \t{#}
174 test dstring-2.12 {appending list elements} -constraints testdstring -setup {
175 testdstring free
176 } -body {
177 testdstring append x -1
178 testdstring element #
179 testdstring get
180 } -cleanup {
181 testdstring free
182 } -result {x #}
183 test dstring-2.13 {appending list elements} -constraints testdstring -setup {
184 testdstring free
185 } -body {
186 # This test checks the sophistication in Tcl_DStringAppendElement's
187 # decision about whether #-quoting can be disabled.
188 testdstring append "x " -1
189 testdstring element #
190 testdstring get
191 } -cleanup {
192 testdstring free
193 } -result {x #}
194 test dstring-2.14 {appending list elements} -constraints testdstring -setup {
195 testdstring free
196 } -body {
197 testdstring append " " -1
198 testdstring element #
199 testdstring get
200 } -cleanup {
201 testdstring free
202 } -result { {#}}
203 test dstring-2.15 {appending list elements} -constraints testdstring -setup {
204 testdstring free
205 } -body {
206 # This test checks the sophistication in Tcl_DStringAppendElement's
207 # decision about whether #-quoting can be disabled.
208 testdstring append "x " -1
209 testdstring element #
210 testdstring get
211 } -cleanup {
212 testdstring free
213 } -result {x #}
214 test dstring-2.16 {appending list elements - bug [46dda6fc29] segfault} -constraints testdstring -setup {
215 testdstring free
216 } -body {
217 testdstring element "\\\n"; # Will setfault
218 testdstring get
219 } -cleanup {
220 testdstring free
221 } -result \\\\\\n
222 test dstring-2.17 {appending list elements - bug [46dda6fc29] segfault} -constraints testdstring -setup {
223 testdstring free
224 } -body {
225 testdstring element "\\\{"; # Will setfault
226 testdstring get
227 } -cleanup {
228 testdstring free
229 } -result [list [list \{]]
230 test dstring-2.18 {appending list elements - bug [46dda6fc29] segfault} -constraints testdstring -setup {
231 testdstring free
232 } -body {
233 testdstring element "\\\}"; # Will setfault
234 testdstring get
235 } -cleanup {
236 testdstring free
237 } -result [list [list \}]]
238 test dstring-2.19 {appending list elements - bug [46dda6fc29] segfault} -constraints testdstring -setup {
239 testdstring free
240 } -body {
241 testdstring element "\\\\"; # Will setfault
242 testdstring get
243 } -cleanup {
244 testdstring free
245 } -result [list [list \\]]
246
247 test dstring-3.1 {nested sublists} -constraints testdstring -setup {
248 testdstring free
249 } -body {
250 testdstring start
251 testdstring element foo
252 testdstring element bar
253 testdstring end
254 testdstring element another
255 testdstring get
256 } -cleanup {
257 testdstring free
258 } -result {{foo bar} another}
259 test dstring-3.2 {nested sublists} -constraints testdstring -setup {
260 testdstring free
261 } -body {
262 testdstring start
263 testdstring start
264 testdstring element abc
265 testdstring element def
266 testdstring end
267 testdstring end
268 testdstring element ghi
269 testdstring get
270 } -cleanup {
271 testdstring free
272 } -result {{{abc def}} ghi}
273 test dstring-3.3 {nested sublists} -constraints testdstring -setup {
274 testdstring free
275 } -body {
276 testdstring start
277 testdstring start
278 testdstring start
279 testdstring element foo
280 testdstring element foo2
281 testdstring end
282 testdstring end
283 testdstring element foo3
284 testdstring end
285 testdstring element foo4
286 testdstring get
287 } -cleanup {
288 testdstring free
289 } -result {{{{foo foo2}} foo3} foo4}
290 test dstring-3.4 {nested sublists} -constraints testdstring -setup {
291 testdstring free
292 } -body {
293 testdstring element before
294 testdstring start
295 testdstring element during
296 testdstring element more
297 testdstring end
298 testdstring element last
299 testdstring get
300 } -cleanup {
301 testdstring free
302 } -result {before {during more} last}
303 test dstring-3.5 {nested sublists} -constraints testdstring -setup {
304 testdstring free
305 } -body {
306 testdstring element "\{"
307 testdstring start
308 testdstring element first
309 testdstring element second
310 testdstring end
311 testdstring get
312 } -cleanup {
313 testdstring free
314 } -result {\{ {first second}}
315 test dstring-3.6 {appending list elements} -constraints testdstring -setup {
316 testdstring free
317 } -body {
318 testdstring append x -1
319 testdstring start
320 testdstring element #
321 testdstring end
322 testdstring get
323 } -cleanup {
324 testdstring free
325 } -result {x {{#}}}
326 test dstring-3.7 {appending list elements} -constraints testdstring -setup {
327 testdstring free
328 } -body {
329 testdstring append x -1
330 testdstring start
331 testdstring append " " -1
332 testdstring element #
333 testdstring end
334 testdstring get
335 } -cleanup {
336 testdstring free
337 } -result {x { {#}}}
338 test dstring-3.8 {appending list elements} -constraints testdstring -setup {
339 testdstring free
340 } -body {
341 testdstring append x -1
342 testdstring start
343 testdstring append \t -1
344 testdstring element #
345 testdstring end
346 testdstring get
347 } -cleanup {
348 testdstring free
349 } -result "x {\t{#}}"
350 test dstring-3.9 {appending list elements} -constraints testdstring -setup {
351 testdstring free
352 } -body {
353 testdstring append x -1
354 testdstring start
355 testdstring append x -1
356 testdstring element #
357 testdstring end
358 testdstring get
359 } -cleanup {
360 testdstring free
361 } -result {x {x #}}
362 test dstring-3.10 {appending list elements} -constraints testdstring -setup {
363 testdstring free
364 } -body {
365 # This test checks the sophistication in Tcl_DStringAppendElement's
366 # decision about whether #-quoting can be disabled.
367 testdstring append x -1
368 testdstring start
369 testdstring append "x " -1
370 testdstring element #
371 testdstring end
372 testdstring get
373 } -cleanup {
374 testdstring free
375 } -result {x {x #}}
376 test dstring-3.11 {appending list elements} -constraints testdstring -setup {
377 testdstring free
378 } -body {
379 testdstring append x -1
380 testdstring start
381 testdstring append " " -1
382 testdstring element #
383 testdstring end
384 testdstring get
385 } -cleanup {
386 testdstring free
387 } -result {x { {#}}}
388 test dstring-3.12 {appending list elements} -constraints testdstring -setup {
389 testdstring free
390 } -body {
391 # This test checks the sophistication in Tcl_DStringAppendElement's
392 # decision about whether #-quoting can be disabled.
393 testdstring append x -1
394 testdstring start
395 testdstring append "x " -1
396 testdstring element #
397 testdstring end
398 testdstring get
399 } -cleanup {
400 testdstring free
401 } -result {x {x #}}
402
403 test dstring-4.1 {truncation} -constraints testdstring -setup {
404 testdstring free
405 } -body {
406 testdstring append "abcdefg" -1
407 testdstring trunc 3
408 list [testdstring get] [testdstring length]
409 } -cleanup {
410 testdstring free
411 } -result {abc 3}
412 test dstring-4.2 {truncation} -constraints testdstring -setup {
413 testdstring free
414 } -body {
415 testdstring append "xyzzy" -1
416 testdstring trunc 0
417 list [testdstring get] [testdstring length]
418 } -cleanup {
419 testdstring free
420 } -result {{} 0}
421 test dstring-4.3 {truncation} -constraints testdstring -setup {
422 testdstring free
423 } -body {
424 testdstring append "xwvut" -1
425 # Pass a negative length to Tcl_DStringSetLength();
426 # if not caught, causing '\0' to be written out-of-bounds,
427 # try corrupting dsPtr->length which begins
428 # 2*sizeof(int) bytes before dsPtr->staticSpace[],
429 # so that the result is -256 (on little endian systems)
430 # rather than e.g. -8.
431 testdstring trunc -8
432 list [testdstring get] [testdstring length]
433 } -cleanup {
434 testdstring free
435 } -result {{} 0}
436
437 test dstring-5.1 {copying to result} -constraints testdstring -setup {
438 testdstring free
439 } -body {
440 testdstring append xyz -1
441 testdstring result
442 } -cleanup {
443 testdstring free
444 } -result xyz
445 test dstring-5.2 {copying to result} -constraints testdstring -setup {
446 testdstring free
447 unset -nocomplain a
448 } -body {
449 foreach l {a b c d e f g h i j k l m n o p} {
450 testdstring append $l$l$l$l$l$l$l$l$l$l$l$l$l$l$l$l$l$l$l$l$l\n -1
451 }
452 set a [testdstring result]
453 testdstring append abc -1
454 list $a [testdstring get]
455 } -cleanup {
456 testdstring free
457 } -result {{aaaaaaaaaaaaaaaaaaaaa
458 bbbbbbbbbbbbbbbbbbbbb
459 ccccccccccccccccccccc
460 ddddddddddddddddddddd
461 eeeeeeeeeeeeeeeeeeeee
462 fffffffffffffffffffff
463 ggggggggggggggggggggg
464 hhhhhhhhhhhhhhhhhhhhh
465 iiiiiiiiiiiiiiiiiiiii
466 jjjjjjjjjjjjjjjjjjjjj
467 kkkkkkkkkkkkkkkkkkkkk
468 lllllllllllllllllllll
469 mmmmmmmmmmmmmmmmmmmmm
470 nnnnnnnnnnnnnnnnnnnnn
471 ooooooooooooooooooooo
472 ppppppppppppppppppppp
473 } abc}
474
475 test dstring-6.1 {Tcl_DStringGetResult} -constraints testdstring -setup {
476 testdstring free
477 } -body {
478 list [testdstring gresult staticsmall] [testdstring get]
479 } -cleanup {
480 testdstring free
481 } -result {{} short}
482 test dstring-6.2 {Tcl_DStringGetResult} -constraints testdstring -setup {
483 testdstring free
484 } -body {
485 foreach l {a b c d e f g h i j k l m n o p} {
486 testdstring append $l$l$l$l$l$l$l$l$l$l$l$l$l$l$l$l$l$l$l$l$l\n -1
487 }
488 list [testdstring gresult staticsmall] [testdstring get]
489 } -cleanup {
490 testdstring free
491 } -result {{} short}
492 test dstring-6.3 {Tcl_DStringGetResult} -constraints testdstring -body {
493 set result {}
494 lappend result [testdstring gresult staticlarge]
495 testdstring append x 1
496 lappend result [testdstring get]
497 } -cleanup {
498 testdstring free
499 } -result {{} {first0 first1 first2 first3 first4 first5 first6 first7 first8 first9
500 second0 second1 second2 second3 second4 second5 second6 second7 second8 second9
501 third0 third1 third2 third3 third4 third5 third6 third7 third8 third9
502 fourth0 fourth1 fourth2 fourth3 fourth4 fourth5 fourth6 fourth7 fourth8 fourth9
503 fifth0 fifth1 fifth2 fifth3 fifth4 fifth5 fifth6 fifth7 fifth8 fifth9
504 sixth0 sixth1 sixth2 sixth3 sixth4 sixth5 sixth6 sixth7 sixth8 sixth9
505 seventh0 seventh1 seventh2 seventh3 seventh4 seventh5 seventh6 seventh7 seventh8 seventh9
506 x}}
507 test dstring-6.4 {Tcl_DStringGetResult} -constraints testdstring -body {
508 set result {}
509 lappend result [testdstring gresult free]
510 testdstring append y 1
511 lappend result [testdstring get]
512 } -cleanup {
513 testdstring free
514 } -result {{} {This is a malloc-ed stringy}}
515 test dstring-6.5 {Tcl_DStringGetResult} -constraints testdstring -body {
516 set result {}
517 lappend result [testdstring gresult special]
518 testdstring append z 1
519 lappend result [testdstring get]
520 } -cleanup {
521 testdstring free
522 } -result {{} {This is a specially-allocated stringz}}
523
524 # cleanup
525 if {[testConstraint testdstring]} {
526 testdstring free
527 }
528 ::tcltest::cleanupTests
529 return
530
531 # Local Variables:
532 # mode: tcl
533 # fill-column: 78
534 # End: