# sum.test - Copyright (C) 2002 Pat Thoyts <patthoyts@users.sourceforge.net>
#
# Tests for the Tcllib sum command
#
# -------------------------------------------------------------------------
# See the file "license.terms" for information on usage and redistribution
# of this file, and for a DISCLAIMER OF ALL WARRANTIES.
# -------------------------------------------------------------------------
# RCS: @(#) $Id: sum.test 364105 2002-01-23 20:56:30Z jigen $

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

package require sum

# -------------------------------------------------------------------------

test sum-1.0 {sum with no parameters } {
    catch {::crc::sum} result
    set result
} {wrong # args: should be  "sum ?-bsd|-sysv? ?-format string? -file name | data"}

# -------------------------------------------------------------------------

foreach {n msg expected} {
    1    ""
    "0"
    2    "a"
    "97"
    3    "abc"
    "16556"
    4    "cba"
    "49322"
    5    "message digest"
    "26423"
    6    "abcdefghijklmnopqrstuvwxyz"
    "53553"
    7    "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
    "25587"
    8    "12345678901234567890123456789012345678901234567890123456789012345678901234567890"
    "21845"
    9    "\uFFFE\u0000\u0001\u0002"
    "16418"
} {
    test sum-2.$n {sum using BSD algorithm and unsigned integer} {
	::crc::sum -bsd $msg
    } $expected
}

# -------------------------------------------------------------------------
foreach {n msg expected} {
    1    ""
    "0"
    2    "a"
    "97"
    3    "abc"
    "294"
    4    "cba"
    "294"
    5    "message digest"
    "1413"
    6    "abcdefghijklmnopqrstuvwxyz"
    "2847"
    7    "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
    "5387"
    8    "12345678901234567890123456789012345678901234567890123456789012345678901234567890"
    "4200"
    9    "\uFFFE\u0000\u0001\u0002"
    "257"
} {
    test sum-3.$n {sum using SysV algorithm and unsigned integer} {
	::crc::sum -sysv $msg
    } $expected
}

# -------------------------------------------------------------------------

set crc::testfile [info script]

proc crc::loaddata {filename} {
    set f [open $filename r]
    fconfigure $f -translation binary
    set data [read $f]
    close $f
    return $data
}

test sum-4.0 {sum file option (BSD)} {
    set r1 [crc::sum -bsd -file $crc::testfile]
    set r2 [crc::sum -bsd [crc::loaddata $crc::testfile]]
    if {$r1 != $r2} {
        set r "differing results: $r1 != $r2"
    } else {
        set r ok
    }
} {ok}

test sum-4.1 {sum file option (SysV)} {
    set r1 [crc::sum -sysv -file $crc::testfile]
    set r2 [crc::sum -sysv [crc::loaddata $crc::testfile]]
    if {$r1 != $r2} {
        set r "differing results: $r1 != $r2"
    } else {
        set r ok
    }
} {ok}

# -------------------------------------------------------------------------

test sum-5.0 {sum format option (BSD)} {
    crc::sum -bsd -format 0x%X [string repeat x 200]
} {0xF8EE}

test sum-5.1 {sum format option (SysV)} {
    crc::sum -sysv -format 0x%X [string repeat x 200]
} {0x5DC0}

# -------------------------------------------------------------------------

catch {unset crc::testfile}
::tcltest::cleanupTests

# Local Variables:
#  mode: tcl
#  indent-tabs-mode: nil
# End:

