# FileCmd.test --
#
#	Tests the implementation of reading from a file.
#
# Copyright (c) 1999 by Moses DeJong
#
# See the file "license.terms" for information on usage and redistribution
# of this file, and for a DISCLAIMER OF ALL WARRANTIES.
#
# RCS: @(#) $Id: FileCmd.test,v 1.3 2000/12/23 10:27:03 mdejong Exp $

if {[lsearch [namespace children] ::tcltest] == -1} {
    package require tcltest
    namespace import -force ::tcltest::*
}

setupJavaPackage

# We make a zero buffer of length 1000 ahead of time so
# that we do not spend a ton of time writing in a loop.

set zero ""
set buf 0000000000
for {set i 0} {$i < 10} {incr i} {
 append zero $buf
}
# zero buffer is now 100 zeros long!
unset buf

# writeZero : Write out a file of the given number of zeros

proc writeZero { name len } {
    global zero
    file delete -force $name
    set fd [open $name w]
    catch {fconfigure stdout -buffering full -translation binary}

    set written 0
    set zerosize [string length $zero]
    while {$len >= $zerosize} {
        puts -nonewline $fd $zero
        incr len -$zerosize
        incr written $zerosize
    }

    for {set i 0} {$i < $len} {incr i} {
	puts -nonewline $fd 0
	incr written 1
    }
    close $fd

    #check the size of the file
    if {[file size $name] != $written} {
	error "file size is [file size $name], it should have been $written"
    }
}

# These tests will check the implementation of the read command in Jacl and Tcl Blend

test FileCmd-1.1 {read all bytes from a file} {
  set fname zeros
  writeZero $fname 10
  set fd [open $fname r]
  set data [read $fd]
  close $fd
  string length $data
} 10

test FileCmd-1.2 {read bytes from a file} {
  set fname zeros
  writeZero $fname 10
  set fd [open $fname r]
  set data [read $fd 10]
  close $fd
  string length $data
} 10

test FileCmd-1.3 {read all data in one line} {
  set fname zeros
  #buffer size is set in the source code, if it changes these tests will need updating
  writeZero $fname 10
  set fd [open $fname r]
  set numbytes [gets $fd data]
  close $fd
  list \
  [string length $data] \
  $numbytes
} {10 10}

test FileCmd-1.4 {read size bigger that buffer size in source code} {
  set fname zeros
  # buffer size is set in the source code, if it changes these tests will need updating
  writeZero $fname 1025
  set fd [open $fname r]
  set data [read $fd]
  close $fd
  string length $data
} 1025

test FileCmd-1.5 {read size bigger that buffer size in source code} {
  set fname zeros
  # buffer size is set in the source code, if it changes these tests will need updating
  writeZero $fname 1025
  set fd [open $fname r]
  set data [read $fd 1025]
  close $fd
 string length $data
} 1025

test FileCmd-1.6 {read size bigger that buffer size in source code} {
  set fname zeros
  # buffer size is set in the source code, if it changes these tests will need updating
  writeZero $fname 1025
  set fd [open $fname r]
  set numbytes [gets $fd data]
  close $fd
  list \
  [string length $data] \
  $numbytes
} {1025 1025}

test FileCmd-1.7 {read size bigger than that buffer size in source code} {
  set fname zeros
  writeZero $fname 2000
  set fd [open $fname r]
  set data [read $fd]
  close $fd
  string length $data
} 2000

test FileCmd-1.8 {read size bigger than that buffer size in source code} {
  set fname zeros
  writeZero $fname 2000
  set fd [open $fname r]
  set data [read $fd]
  close $fd
  string length $data
} 2000

test FileCmd-1.9 {read size bigger than that buffer size in source code} {
  set fname zeros
  writeZero $fname 2000
  set fd [open $fname r]
  set numbytes [gets $fd data]
  close $fd
  list \
  [string length $data] \
  $numbytes
} {2000 2000}

test FileCmd-1.10 {read more bytes than the file size} {
  set fname zeros
  #buffer size is set in the source code, if it changes these tests will need updating
  writeZero $fname 2000
  set fd [open $fname r]
  set data [read $fd 3000]
  close $fd
  string length $data
} 2000


# cleanup
file delete -force zeros
::tcltest::cleanupTests
return
