| 1 |
# Commands covered: list |
| 2 |
# |
| 3 |
# This file contains a collection of tests for one or more of the Tcl |
| 4 |
# built-in commands. Sourcing this file into Tcl runs the tests and |
| 5 |
# generates output for errors. No output means no errors were found. |
| 6 |
# |
| 7 |
# Copyright (c) 1991-1993 The Regents of the University of California. |
| 8 |
# Copyright (c) 1994 Sun Microsystems, Inc. |
| 9 |
# Copyright (c) 1998-1999 Scriptics Corporation. |
| 10 |
# |
| 11 |
# See the file "license.terms" for information on usage and redistribution |
| 12 |
# of 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 |
# First, a bunch of individual tests |
| 20 |
|
| 21 |
test list-1.1 {basic tests} {list a b c} {a b c} |
| 22 |
test list-1.2 {basic tests} {list {a b} c} {{a b} c} |
| 23 |
test list-1.3 {basic tests} {list \{a b c} {\{a b c} |
| 24 |
test list-1.4 {basic tests} "list a{}} b{} c}" "a\\{\\}\\} b{} c\\}" |
| 25 |
test list-1.5 {basic tests} {list a\[ b\] } "{a\[} b\\]" |
| 26 |
test list-1.6 {basic tests} {list c\ d\t } "{c } {d\t}" |
| 27 |
test list-1.7 {basic tests} {list e\n f\$ } "{e\n} {f\$}" |
| 28 |
test list-1.8 {basic tests} {list g\; h\\} {{g;} h\\} |
| 29 |
test list-1.9 {basic tests} "list a\\\[} b\\\]} " "a\\\[\\\} b\\\]\\\}" |
| 30 |
test list-1.10 {basic tests} "list c\\\} d\\t} " "c\\} d\\t\\}" |
| 31 |
test list-1.11 {basic tests} "list e\\n} f\\$} " "e\\n\\} f\\$\\}" |
| 32 |
test list-1.12 {basic tests} "list g\\;} h\\\\} " "g\\;\\} {h\\}}" |
| 33 |
test list-1.13 {basic tests} {list a {{}} b} {a {{}} b} |
| 34 |
test list-1.14 {basic tests} {list a b xy\\} "a b xy\\\\" |
| 35 |
test list-1.15 {basic tests} "list a b\} e\\" "a b\\} e\\\\" |
| 36 |
test list-1.16 {basic tests} "list a b\}\\\$ e\\\$\\" "a b\\}\\\$ e\\\$\\\\" |
| 37 |
test list-1.17 {basic tests} {list a\f \{\f} "{a\f} \\\{\\f" |
| 38 |
test list-1.18 {basic tests} {list a\r \{\r} "{a\r} \\\{\\r" |
| 39 |
test list-1.19 {basic tests} {list a\v \{\v} "{a\v} \\\{\\v" |
| 40 |
test list-1.20 {basic tests} {list \"\}\{} "\\\"\\}\\{" |
| 41 |
test list-1.21 {basic tests} {list a b c\\\nd} "a b c\\\\\\nd" |
| 42 |
test list-1.22 {basic tests} {list "{ab}\\"} \\{ab\\}\\\\ |
| 43 |
test list-1.23 {basic tests} {list \{} "\\{" |
| 44 |
test list-1.24 {basic tests} {list} {} |
| 45 |
test list-1.25 {basic tests} {list # #} {{#} #} |
| 46 |
test list-1.26 {basic tests} {list #\{ #\{} {\#\{ #\{} |
| 47 |
test list-1.27 {basic null treatment} { |
| 48 |
set l [list "" "\x00" "\x00\x00"] |
| 49 |
set e "{} \x00 \x00\x00" |
| 50 |
string equal $l $e |
| 51 |
} 1 |
| 52 |
test list-1.28 {basic null treatment} { |
| 53 |
set result "\x00a\x00b" |
| 54 |
list $result [string length $result] |
| 55 |
} "\x00a\x00b 4" |
| 56 |
test list-1.29 {basic null treatment} { |
| 57 |
set result "\x00a\x00b" |
| 58 |
set srep "$result 4" |
| 59 |
set lrep [list $result [string length $result]] |
| 60 |
string equal $srep $lrep |
| 61 |
} 1 |
| 62 |
test list-1.30 {basic null treatment} { |
| 63 |
set l [list "\x00abc" "xyz"] |
| 64 |
set e "\x00abc xyz" |
| 65 |
string equal $l $e |
| 66 |
} 1 |
| 67 |
test list-1.31 {bug [e38dce74e2]} { |
| 68 |
set l #foo |
| 69 |
set e {} |
| 70 |
list {*}$l {*}$e |
| 71 |
} {{#foo}} |
| 72 |
test list-1.32 {bug [e38dce74e2]} { |
| 73 |
set l " #foo" |
| 74 |
set e {} |
| 75 |
list {*}$l {*}$e |
| 76 |
} {{#foo}} |
| 77 |
|
| 78 |
# For the next round of tests create a list and then pick it apart |
| 79 |
# with "index" to make sure that we get back exactly what went in. |
| 80 |
|
| 81 |
set num 0 |
| 82 |
proc lcheck {testid a b c} { |
| 83 |
global num d |
| 84 |
set d [list $a $b $c] |
| 85 |
test ${testid}-0 {what goes in must come out} {lindex $d 0} $a |
| 86 |
test ${testid}-1 {what goes in must come out} {lindex $d 1} $b |
| 87 |
test ${testid}-2 {what goes in must come out} {lindex $d 2} $c |
| 88 |
} |
| 89 |
lcheck list-2.1 a b c |
| 90 |
lcheck list-2.2 "a b" c\td e\nf |
| 91 |
lcheck list-2.3 {{a b}} {} { } |
| 92 |
lcheck list-2.4 \$ \$ab ab\$ |
| 93 |
lcheck list-2.5 \; \;ab ab\; |
| 94 |
lcheck list-2.6 \[ \[ab ab\[ |
| 95 |
lcheck list-2.7 \\ \\ab ab\\ |
| 96 |
lcheck list-2.8 {"} {"ab} {ab"} ;#" Stupid emacs highlighting! |
| 97 |
lcheck list-2.9 {a b} { ab} {ab } |
| 98 |
lcheck list-2.10 a{ a{b \{ab |
| 99 |
lcheck list-2.11 a} a}b }ab |
| 100 |
lcheck list-2.12 a\\} {a \}b} {a \{c} |
| 101 |
lcheck list-2.13 xyz \\ 1\\\n2 |
| 102 |
lcheck list-2.14 "{ab}\\" "{ab}xy" abc |
| 103 |
|
| 104 |
concat {} |
| 105 |
|
| 106 |
# Check that tclListObj.c's SetListFromAny handles possible overlarge |
| 107 |
# string rep lengths in the source object. |
| 108 |
|
| 109 |
proc slowsort list { |
| 110 |
set result {} |
| 111 |
set last [expr {[llength $list] - 1}] |
| 112 |
while {$last > 0} { |
| 113 |
set minIndex [expr {[llength $list] - 1}] |
| 114 |
set min [lindex $list $last] |
| 115 |
set i [expr {$minIndex - 1}] |
| 116 |
while {$i >= 0} { |
| 117 |
if {[string compare [lindex $list $i] $min] < 0} { |
| 118 |
set minIndex $i |
| 119 |
set min [lindex $list $i] |
| 120 |
} |
| 121 |
incr i -1 |
| 122 |
} |
| 123 |
set result [concat $result [list $min]] |
| 124 |
if {$minIndex == 0} { |
| 125 |
set list [lrange $list 1 end] |
| 126 |
} else { |
| 127 |
set list [concat [lrange $list 0 [expr {$minIndex - 1}]] \ |
| 128 |
[lrange $list [expr {$minIndex + 1}] end]] |
| 129 |
} |
| 130 |
set last [expr {$last - 1}] |
| 131 |
} |
| 132 |
return [concat $result $list] |
| 133 |
} |
| 134 |
test list-3.1 {SetListFromAny and lrange/concat results} { |
| 135 |
slowsort {fred julie alex carol bill annie} |
| 136 |
} {alex annie bill carol fred julie} |
| 137 |
|
| 138 |
test list-4.1 {Bug 3173086} { |
| 139 |
string is list "{[list \\\\\}]}" |
| 140 |
} 1 |
| 141 |
test list-4.2 {Bug 35a8f1c04a, check correct str-rep} { |
| 142 |
set result {} |
| 143 |
foreach i { |
| 144 |
{#"} {#"""} {#"""""""""""""""} |
| 145 |
"#\"{" "#\"\"\"{" "#\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\{" |
| 146 |
"#\"}" "#\"\"\"}" "#\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\}" |
| 147 |
} { |
| 148 |
set list [list $i] |
| 149 |
set list [string trim " $list "] |
| 150 |
if {[llength $list] > 1 || $i ne [lindex $list 0]} { |
| 151 |
lappend result "wrong string-representation of list by '$i', length: [llength $list], list: '$list'" |
| 152 |
} |
| 153 |
} |
| 154 |
set result [join $result \n] |
| 155 |
} {} |
| 156 |
test list-4.3 {Bug 35a8f1c04a, check correct string length} { |
| 157 |
string length [list #""] |
| 158 |
} 5 |
| 159 |
|
| 160 |
# cleanup |
| 161 |
::tcltest::cleanupTests |
| 162 |
return |