#
# nice.test
#
# Tests for the nice command.
#---------------------------------------------------------------------------
# Copyright 1993-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: 
#------------------------------------------------------------------------------
#

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

if [cequal $tcl_platform(platform) windows] {
    echo "    * The nice command has not been ported to Win32" 
    return
}

#
# Fork without exec will not work under Tk, skip this test
#
if ![lempty [info commands button]] {
    puts "*************************************************************"
    puts "Nice tests are constructed in a way that does not work"
    puts "under Tk.  Test skipped."
    puts "*************************************************************"
    puts ""
    return
}

#
# Right now, we depend on the base priority being zero.
#
if {[nice] != 0} {
    puts "*************************************************************"
    puts "Nice priority is [nice], not zero.  May cause some failures."
    puts "(but everything is probably ok)"
    puts "*************************************************************"
    puts ""
}

set niceBase [nice]

Test nice-1.1 {nice tests} {
    nice 0 1
} 1 {wrong # args: nice ?priorityincr?}

Test nice-1.2 {nice tests} {
    nice 0
} 0 $niceBase

#
# Since you can't nice back up unless you're root, we spawn a child process 
# to run the nice tests.  
#

flush stdout
flush stderr
set pid [fork]

#
# Parent waits for child to complete.
#
if {$pid > 0} {
    wait $pid
    return
}

Test nice-1.4 {nice tests} {
    list [nice 3] [nice]
} 0 [list [expr $niceBase+3] [expr $niceBase+3]]

Test nice-1.5 {nice tests} {
    list [nice 4] [nice]
} 0 [list [expr $niceBase+3+4] [expr $niceBase+3+4]]

if {[id user] != "root"} {

    Test nice-1.7 {nice tests} {
        set errList {{not owner} {permission denied} {not privileged}
                     {no permission match} {invalid argument}}
        set stat [catch {nice -1} msg]
        set msg [string tolower $msg]
        if {![regexp {^failed to increment priority: (.*)$} $msg {} \
                      errmsg] ||
            ([lsearch $errList $errmsg] < 0)} {
            set stat [list $stat $msg]
        }
        set stat
    } 0 1

    puts "*************************************************************"
    puts "Notice: You are not running as `root', certain nice tests"
    puts "will be skipped"
    puts "*************************************************************"
    exit
}

Test nice-1.8 {nice tests} {
    list [nice -1] [nice]
} 0 [list [expr $niceBase+3+4-1] [expr $niceBase+3+4-1]]

Test nice-1.10 {nice tests} {
    list [nice -9] [nice]
} 0 [list [expr $niceBase+3+4-1-9] [expr $niceBase+3+4-1-9]]

Test nice-1.11 {nice tests} {
    list [nice -10] [nice]
} 0 [list [expr $niceBase+3+4-1-9-10] [expr $niceBase+3+4-1-9-10]]

Test nice-1.12 {nice tests} {
    list [nice -1] [nice]
} 0 [list [expr $niceBase+3+4-1-9-10-1] [expr $niceBase+3+4-1-9-10-1]]

exit


