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
|
# tree.test: tests for the tree structure. -*- tcl -*-
#
# 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) 1998-2000 by Ajuba Solutions.
# All rights reserved.
#
# RCS: @(#) $Id: tree.test,v 1.41 2005/09/28 04:51:24 andreas_kupries Exp $
if {[lsearch [namespace children] ::tcltest] == -1} {
package require tcltest
namespace import ::tcltest::*
}
# -------------------------------------------------------------------------
# Ensure we test _this_ local copy and one installed somewhere else.
#
package forget struct::tree
package forget struct::list
package forget tcllibc
catch {namespace delete ::struct::tree}
catch {namespace delete ::struct::list}
catch {rename ::struct::tree {}}
set moddir [file dirname [file dirname [file join [pwd] [info script]]]]
set tcllibc [file join $moddir tcllibc pkgIndex.tcl]
if {[file exists $tcllibc]} {
# Fake load of specific package index, so that the following
# require will not search through the package paths.
set dir [file dirname $tcllibc]
source $tcllibc
unset dir
package require tcllibc
}
if {[catch {source [file join $moddir struct list.tcl]} msg]} {
puts "skipped [file tail [info script]] (list.tcl): $msg"
return
}
if {[catch {source [file join $moddir struct tree.tcl]} msg]} {
puts "skipped [file tail [info script]] (tree.tcl): $msg"
return
}
struct::tree::SwitchTo {}
foreach e [struct::tree::KnownImpl] {
if {[::struct::tree::LoadAccel $e]} {
puts "- struct::tree [package provide struct::tree] (using $e)"
switch -exact -- $e {
critcl {
puts " - tcllibc [package present tcllibc]"
}
tcl {
puts " - struct::list [package present struct::list]"
}
}
}
}
#----------------------------------------------------------------------
# Takes a dictionary, returns a list containing the same dictionary,
# however the keys are sorted alphabetically. This allows for a true
# comparison of dictionary results.
proc dictsort {dict} {
array set a $dict
set out [list]
foreach key [lsort [array names a]] {
lappend out $key $a($key)
}
return $out
}
# Callback for tree walking. Remembers the node
# in a global variable.
proc walker {node} {
lappend ::FOO $node
}
proc pwalker {tree n a} {
lappend ::t $a $n
}
proc pwalkern {tree n a} {
lappend ::t $n
}
proc pwalkercont {tree n a} {
if {[string equal $n "b"]} {lappend ::t . ; return -code continue}
lappend ::t $a $n
}
proc pwalkerbreak {tree n a} {
if {[string equal $n "b"]} {lappend ::t . ; return -code break}
lappend ::t $a $n
}
proc pwalkerret {tree n a} {
if {[string equal $n "b"]} {
lappend ::t .
return -code return good-return
}
lappend ::t $a $n
}
proc pwalkererr {tree n a} {
if {[string equal $n "b"]} {
lappend ::t .
error fubar
}
lappend ::t $a $n
}
proc pwalkerprune {tree n a} {
lappend ::t $a $n
if {$::prune && ($n == 2)} {struct::tree::prune}
}
proc pwalkerpruneb {tree n a} {
lappend ::t $a $n
if {($n == 2)} {struct::tree::prune}
}
# Validate a serialization against the tree it
# was generated from.
proc validate_serial {t serial {rootname {}}} {
if {$rootname == {}} {
set rootname [$t rootname]
}
# List length is multiple of 3
if {[llength $serial] % 3} {
return serial/wrong#elements
}
# Scan through list and built a number helper
# structures (arrays).
array set a {}
array set p {}
array set ch {}
foreach {node parent attr} $serial {
# Node has to exist in tree
if {![$t exists $node]} {
return node/$node/unknown
}
if {![info exists ch($node)]} {set ch($node) {}}
# Parent reference has to be empty or
# integer, == 0 %3, >=0, < length serial
if {$parent != {}} {
if {![string is integer -strict $parent]} {
return node/$node/parent/no-integer/$parent
}
if {$parent % 3} {
return node/$node/parent/not-triple/$parent
}
if {$parent < 0} {
return node/$node/parent/out-of-bounds/$parent
}
if {$parent >= [llength $serial]} {
return node/$node/parent/out-of-bounds/$parent
}
# Resolve parent index into node name, has to match
set parentnode [lindex $serial $parent]
if {![$t exists $parentnode]} {
return node/$node/parent/unknown/$parent/$parentnode
}
if {![string equal [$t parent $node] $parentnode]} {
return node/$node/parent/mismatch/$parent/$parentnode/[$t parent $node]
}
lappend ch($parentnode) $node
} else {
set p($node) {}
}
# Attr list has to be of even length.
if {[llength $attr] % 2} {
return attr/$node/wrong#elements
}
# Attr have to exist and match in all respects
if {![string equal \
[dictsort $attr] \
[dictsort [$t getall $node]]]} {
return attr/$node/mismatch
}
}
# Second pass, check that the children information is encoded
# correctly. Reconstructed data has to match originals.
foreach {node parent attr} $serial {
if {![string equal $ch($node) [$t children $node]]} {
return node/$node/children/mismatch
}
}
# Reverse check
# - List of nodes from the 'rootname' and check
# that it and all its children are present
# in the structure.
set ::FOO {}
mytree walk $rootname n {walker $n}
foreach n $::FOO {
if {![info exists ch($n)]} {
return node/$n/mismatch/reachable/missing
}
}
if {[llength $::FOO] != [llength $serial]/3} {
return structure/mismatch/#nodes/multiples
}
if {[llength $::FOO] != [array size ch]} {
return structure/mismatch/#nodes/multiples/ii
}
return ok
}
############################################################
## Iterate over all loaded implementations, activate
## them in turn, and run the tests for the active
## implementation. As a mini-benchmark we time how
## long each run takes.
puts "\ntree impl. performance"
set usec 0
set tests [file join [file dirname [info script]] tree.testsuite]
catch {memory validate on}
foreach impl [struct::tree::Implementations] {
struct::tree::SwitchTo $impl
# The global variable 'impl' is part of the public
# API the testsuit (in tree.testsuite) can expect
# from the environment.
namespace import -force struct::tree
switch -exact -- $impl {
critcl {
set MY mytree
proc tmWrong {m loarg n {xarg {}}} {
return [tcltest::wrongNumArgs "mytree $m" $loarg $n]
}
proc tmTooMany {m loarg {xarg {}}} {
return [tcltest::tooManyArgs "mytree $m" $loarg]
}
}
tcl {
set MY ::mytree
proc tmWrong {m loarg n {xarg {}}} {
if {$xarg == {}} {set xarg $loarg}
if {$xarg != {}} {set xarg " $xarg"}
incr n
return [tcltest::wrongNumArgs "::struct::tree::_$m" "name$xarg" $n]
}
proc tmTooMany {m loarg {xarg {}}} {
if {$xarg == {}} {set xarg $loarg}
if {$xarg != {}} {set xarg " $xarg"}
return [tcltest::tooManyArgs "::struct::tree::_$m" "name$xarg"]
}
}
}
set usec [time {source $tests} 1]
puts "$impl:\t$usec"
}
catch {memory validate off}
unset usec
unset tests
puts ""
# Reset system to fully inactive state.
struct::tree::SwitchTo {}
############################################################
::tcltest::cleanupTests
|