home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / languages / tcl / tclX6.5c / tests / bsearch.test < prev    next >
Encoding:
Text File  |  1992-12-19  |  2.0 KB  |  69 lines

  1. #
  2. # bsearch.test
  3. #
  4. # Tests for the bsearch command.
  5. #---------------------------------------------------------------------------
  6. # Copyright 1992 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 2.0 1992/10/16 04:49:24 markd Rel $
  16. #------------------------------------------------------------------------------
  17. #
  18.  
  19. if {[info procs test] != "test"} then {source testlib.tcl}
  20.  
  21. # Create a test file
  22.  
  23. catch {unlink {BSEARCH.TMP}}
  24.  
  25. set testFH [open BSEARCH.TMP w]
  26. for {set cnt 0} {$cnt < 100} {incr cnt} {
  27.      puts $testFH [GenRec $cnt]
  28. }
  29. close $testFH
  30.  
  31. # Test bsearch
  32.  
  33. proc BsearchTestCmp {key line} {
  34.     set linekey [lindex $line 2]
  35.     return [string compare $key $linekey]
  36. }
  37.  
  38. set testFH [open BSEARCH.TMP r]
  39. set toggle 0
  40. for {set cnt 0} {$cnt < 100} {incr cnt} {
  41.     set key1 [format "Key:%04d" $cnt]
  42.     set key2 [format "KeyX:%04d" $cnt]
  43.     if {($cnt % 6) == 0} {
  44.         set expect [GenRec $cnt]
  45.         if {$toggle} {
  46.             Test bsearch-1.1 {bsearch tests} {
  47.                 bsearch $testFH $key1
  48.             } 0 $expect
  49.             Test bsearch-1.2 {bsearch tests} {
  50.                 bsearch $testFH $key2 {} BsearchTestCmp
  51.             } 0 $expect
  52.         } else {
  53.             set rec {}
  54.             Test bsearch-1.3 {bsearch tests} {
  55.                  list [bsearch $testFH $key1 rec] $rec
  56.             } 0 [list 1 $expect]
  57.             set rec {}
  58.             Test bsearch-1.4 {bsearch tests} {
  59.                  list [bsearch $testFH $key2 rec BsearchTestCmp] $rec
  60.             } 0 [list 1 $expect]
  61.         }
  62.         set toggle [expr !$toggle]
  63.     }
  64. }
  65. close $testFH
  66.  
  67. catch {unlink {BSEARCH.TMP}}
  68.  
  69.