#
# fstat.test
#
# Tests for the fstat command.
#---------------------------------------------------------------------------
# Copyright 1992-1999 Karl Lehenbauer and Mark Diekhans.
#
# Permission to use, copy, modify, and distribute this software and its
# documentation for any purpose and without fee is hereby granted, provided
# that the above copyright notice appear in all copies.  Karl Lehenbauer and
# Mark Diekhans make no representations about the suitability of this
# software for any purpose.  It is provided "as is" without express or
# implied warranty.
#------------------------------------------------------------------------------
# $Id: fstat.test,v 1.1.1.1 1999/06/28 20:35:25 damon Exp $
#------------------------------------------------------------------------------
#

if [cequal [info procs Test] {}] {source testlib.tcl}

TestRemove gorp.file
set gorpFH [open gorp.file w]
puts $gorpFH "Test string"
close $gorpFH
if ![cequal $tcl_platform(platform) windows] { ;# WIN32???
    chmod 0765 gorp.file
}

set gorpFH [open gorp.file r+]

Test fstat-1.1 {array return} {
    catch {unset stat}
    fstat $gorpFH stat stat
    lsort [array names stat]
} 0 {atime ctime dev gid ino mode mtime nlink size tty type uid}

if [cequal $tcl_platform(platform) windows] {
    set expect {1 13 0 file 0}
} else {
    set expect {1 12 501 file 0}
}
Test fstat-1.2 {array return} {
    catch {unset stat}
    fstat $gorpFH stat stat
    list $stat(nlink) $stat(size) [expr $stat(mode)&0777] $stat(type) \
         $stat(tty)
} 0 $expect

if [cequal $tcl_platform(platform) windows] {
    set expect {0 0 13}
} else {
    set expect {1 1 12}
}
Test fstat-1.3 {array return} {
    catch {unset stat}
    fstat $gorpFH stat stat
    list [expr {[file mtime gorp.file] == $stat(mtime)}] \
	    [expr {[file atime gorp.file] == $stat(atime)}] $stat(size)
} 0 $expect

Test fstat-2.1 {keyed list returns} {
    catch {unset stat}
    set stat [fstat $gorpFH]
    lsort [keylkeys stat]
} 0 {atime ctime dev gid ino mode mtime nlink size tty type uid}

if [cequal $tcl_platform(platform) windows] {
    set expect {1 13 0 file}
} else {
    set expect {1 12 501 file}
}
Test fstat-2.2 {keyed list returns} {
    set stat [fstat $gorpFH]
    list [keylget stat nlink] [keylget stat size] \
         [expr [keylget stat mode ]&0777] [keylget stat type]
} 0 $expect

if [cequal $tcl_platform(platform) windows] {
    set expect {0 0 13}  ;# Can't get times on open file.
} else {
    set expect {1 1 12}
}
Test fstat-2.3 {keyed list returns} {
    set stat [fstat $gorpFH]
    list [expr {[file mtime gorp.file] == [keylget stat mtime]}] \
	    [expr {[file atime gorp.file] == [keylget stat atime]}] \
            [keylget stat size]
} 0 $expect


Test fstat-3.1 {individual item returns} {
    set old [fstat $gorpFH mtime]
    sleep 2
    puts $gorpFH "More text"
    flush $gorpFH
    set new [fstat $gorpFH mtime]
    if {($new > $old) && ($new <= ($old+5))} {
        concat OK
    } else {
        error "Bad mtimes: old = $old, new = $new"
    }
} 0 {OK} unixOnly

Test fstat-3.2 {individual item returns} {
    set oldsize [fstat $gorpFH size]
    puts $gorpFH "More text"
    flush $gorpFH
    expr {[fstat $gorpFH size] - $oldsize}
} 0 8 unixOnly ;# PC doen't really write disk till end.

if [cequal $tcl_platform(platform) windows] {
    set expect 11
} else {
    set expect 10
}
Test fstat-3.3 {individual item returns} {
    set fh [open fstat.tmp w]
    set oldsize [fstat $fh size]
    puts $fh "More text"
    close $fh
    set fh [open fstat.tmp]
    set diff [expr {[fstat $fh size] - $oldsize}]
    close $fh
    set diff
} 0 $expect

Test fstat-4.1 {type return} {
    set fh [open .]
    set type [fstat $fh type]
    close $fh
    set type
} 0 directory {unixOnly}

Test fstat-4.2 {type return} {
    fstat $gorpFH type
} 0 file

#
# Check to see that the values that are returned are at least numeric where
# expected
#
Test fstat-4.3 {type return} {
    set dataList {}
    foreach type {dev ino mode nlink uid gid size atime mtime ctime tty
                  type} {
        set data [fstat $gorpFH $type]
        if [string match "-*" $data] {
            set data [csubstr $data 1 end]
        }
        lappend dataList [ctype digit $data]
    }
    set dataList
} 0 {1 1 1 1 1 1 1 1 1 1 1 0}


Test fstat-5.1 {error handling} {
    fstat
} 1 {wrong # args: fstat fileId ?item?|?stat arrayVar?}

Test fstat-5.2 {error handling} {
    fstat foo
} 1 {can not find channel named "foo"}

Test fstat-5.3 {error handling} {
    fstat $gorpFH foo
} 1 {Got "foo", expected one of "atime", "ctime", "dev", "gid", "ino", "mode", "mtime", "nlink", "size", "tty", "type", "uid", "remotehost", or "localhost"}

Test fstat-5.4 {error handling} {
    catch {unset foo}
    fstat $gorpFH foo foo
} 1 {expected item name of "stat" when using array name}

Test fstat-5.5 {error handling} {
    catch {unset foo}
    fstat $gorpFH stat foo baz
} 1 {wrong # args: fstat fileId ?item?|?stat arrayVar?}

# FIX: Need localhost/remotehost tests.

catch {close $gorpFH}
TestRemove gorp.file fstat.tmp


