diff options
author | Steve Bennett <steveb@workware.net.au> | 2010-01-24 10:58:31 +1000 |
---|---|---|
committer | Steve Bennett <steveb@workware.net.au> | 2010-10-15 11:02:40 +1000 |
commit | d7bcbc515647b1d6897c16e5b3ef314a10ccd02e (patch) | |
tree | a25ae6757a3a9e6309cfba18e07e6494baee8f69 /tests | |
parent | f32ada62c0567ce439868b04f5de0ebe2a504e79 (diff) | |
download | jimtcl-d7bcbc515647b1d6897c16e5b3ef314a10ccd02e.zip jimtcl-d7bcbc515647b1d6897c16e5b3ef314a10ccd02e.tar.gz jimtcl-d7bcbc515647b1d6897c16e5b3ef314a10ccd02e.tar.bz2 |
Implement 'array' in C
Diffstat (limited to 'tests')
-rw-r--r-- | tests/array.test | 68 | ||||
-rw-r--r-- | tests/perf.test | 120 |
2 files changed, 188 insertions, 0 deletions
diff --git a/tests/array.test b/tests/array.test new file mode 100644 index 0000000..6007d2f --- /dev/null +++ b/tests/array.test @@ -0,0 +1,68 @@ +source testing.tcl + +array set a { + 1 one + 2 two + 22 "twenty two" + 3 three +} + +test array-1.1 "array exists - true" { + array exists a +} {1} + +test array-1.2 "array exists - false" { + array exists b +} {0} + +test array-1.3 "array size" { + array size a +} {4} + +test array-1.4 "array size - nonexistant" { + array size b +} {0} + +test array-1.5 "array get" { + set result {} + foreach {name value} [array get a] { + lappend result $name $value + } + lsort $result +} {1 2 22 3 one three {twenty two} two} + +test array-1.6 "array get - pattern" { + set result {} + foreach {name value} [array get a 2*] { + lappend result $name $value + } + lsort $result +} {2 22 {twenty two} two} + +test array-1.7 "array names" { + lsort [array names a] +} {1 2 22 3} + +test array-1.8 "array get - pattern" { + lsort [array names a 2*] +} {2 22} + +#set b $a +array set b [array get a] + +test array-1.9 "array set - replace" { + array set b {22 twenty-two} + set b(22) +} {twenty-two} + +test array-1.10 "array unset - pattern" { + array unset b 2* + array names b +} {1 3} + +test array-1.11 "array unset - all" { + array unset b + list [array size b] [array exists b] +} {0 0} + +testreport diff --git a/tests/perf.test b/tests/perf.test new file mode 100644 index 0000000..f7fab7e --- /dev/null +++ b/tests/perf.test @@ -0,0 +1,120 @@ +set version [info patchlevel] + +proc bench {name cmd} { + if {[catch { + set t [time $cmd] + set ms [expr {[lindex $t 0] / 1000}] + }]} { + set ms ? + } + puts "$::version: $name ${ms}ms" +} + +proc set_dict_sugar {} { + for {set i 0} {$i < 100000} {incr i} { + set a(b) $i + } +} + + +proc read_file {file} { + set f [open $file] + while {[gets $f buf] >= 0} { + } + close $f +} + +proc read_file_split {file} { + set f [open $file] + while {[gets $f buf] >= 0} { + split $buf \t + } + close $f +} + +proc read_file_split_assign_foreach {file} { + set f [open $file] + while {[gets $f buf] >= 0} { + set split [split $buf \t] + foreach {info(chan) info(datetime) info(duration) info(title) subtitle_genre info(desc) info(rating) dummy} [split $buf \t] {break} + #array unset info + } + close $f +} + +proc read_file_split_assign_foreach_dict {file} { + set f [open $file] + while {[gets $f buf] >= 0} { + set split [split $buf \t] + foreach {chan datetime duration title subtitle_genre desc rating dummy} [split $buf \t] {break} + dict set info chan $chan + dict set info duration $duration + dict set info title $title + dict set info subtitle_genre $subtitle_genre + dict set info desc $desc + dict set info rating $rating + #array unset info + } + close $f +} + +proc read_file_split_assign_foreach_dictsugar {file} { + set f [open $file] + while {[gets $f buf] >= 0} { + set split [split $buf \t] + foreach {chan datetime duration title subtitle_genre desc rating dummy} [split $buf \t] {break} + set info(chan) $chan + set info(duration) $duration + set info(title) $title + set info(subtitle_genre) $subtitle_genre + set info(desc) $desc + set info(rating) $rating + #array unset info + } + close $f +} + +proc read_file_split_assign_foreach_simple {file} { + set f [open $file] + while {[gets $f buf] >= 0} { + set split [split $buf \t] + foreach {chan datetime duration title subtitle_genre desc rating dummy} [split $buf \t] {break} + #array unset info + } + close $f +} + +proc read_file_split_assign_lindex {file} { + set f [open $file] + while {[gets $f buf] >= 0} { + set split [split $buf \t] + set info(chan) [lindex $split 0] + set info(datetime) [lindex $split 1] + set info(duration) [lindex $split 2] + set info(title) [lindex $split 3] + set info(subtitle_genre) [lindex $split 4] + set info(desc) [lindex $split 5] + set info(rating) [lindex $split 6] + #array unset info + } + close $f +} + +# Create a really big file +set f [open test.in w] +for {set i 0} {$i < 50000} {incr i} { + puts $f "a\tb\tc\te\tf\tg\th\ti\tj\tk" +} +close $f + +bench "set dictsugar" {set_dict_sugar} +bench "read file #1" {read_file test.in} +bench "read file #2" {read_file test.in} +bench "read file split" {read_file_split test.in} +bench "read file split assign foreach" {read_file_split_assign_foreach test.in} +bench "read file split assign foreach dict" {read_file_split_assign_foreach_dict test.in} +bench "read file split assign foreach dictsugar" {read_file_split_assign_foreach_dictsugar test.in} +bench "read file split assign foreach simple" {read_file_split_assign_foreach_simple test.in} +bench "read file split assign lindex" {read_file_split_assign_lindex test.in} + +file delete test.in |