1# 2# bsearch.test 3# 4# Tests for the bsearch command. 5#--------------------------------------------------------------------------- 6# Copyright 1992-1999 Karl Lehenbauer and Mark Diekhans. 7# 8# Permission to use, copy, modify, and distribute this software and its 9# documentation for any purpose and without fee is hereby granted, provided 10# that the above copyright notice appear in all copies. Karl Lehenbauer and 11# Mark Diekhans make no representations about the suitability of this 12# software for any purpose. It is provided "as is" without express or 13# implied warranty. 14#------------------------------------------------------------------------------ 15# $Id: bsearch.test,v 1.2 2002/04/02 02:29:43 hobbs Exp $ 16#------------------------------------------------------------------------------ 17# 18 19if {[lsearch [namespace children] ::tcltest] == -1} { 20 package require tcltest 21 namespace import ::tcltest::* 22} 23 24if {[cequal [info procs Test] {}]} { 25 source [file join [file dirname [info script]] testlib.tcl] 26} 27 28# Create a test file 29 30TestRemove BSEARCH.TMP 31 32set testFH [open BSEARCH.TMP w] 33for {set cnt 0} {$cnt < 100} {incr cnt} { 34 puts $testFH [GenRec $cnt] 35} 36close $testFH 37 38# Test bsearch 39 40proc BsearchTestCmp {key line} { 41 set linekey [lindex $line 2] 42 return [string compare $key $linekey] 43} 44 45set testFH [open BSEARCH.TMP r] 46set toggle 0 47for {set cnt 0} {$cnt < 100} {incr cnt} { 48 set key1 [format "Key:%04d" $cnt] 49 set key2 [format "KeyX:%04d" $cnt] 50 if {($cnt % 6) == 0} { 51 set expect [GenRec $cnt] 52 if {$toggle} { 53 test bsearch-1.1.$cnt {bsearch tests} { 54 bsearch $testFH $key1 55 } $expect 56 test bsearch-1.2.$cnt {bsearch tests} { 57 bsearch $testFH $key2 {} BsearchTestCmp 58 } $expect 59 } else { 60 set rec {} 61 test bsearch-1.3.$cnt {bsearch tests} { 62 list [bsearch $testFH $key1 rec] $rec 63 } [list 1 $expect] 64 set rec {} 65 test bsearch-1.4.$cnt {bsearch tests} { 66 list [bsearch $testFH $key2 rec BsearchTestCmp] $rec 67 } [list 1 $expect] 68 } 69 set toggle [expr !$toggle] 70 } 71} 72close $testFH 73 74TestRemove BSEARCH.TMP 75 76# cleanup 77::tcltest::cleanupTests 78return 79