# crc32.test - Copyright (C) 2002 Pat Thoyts <patthoyts@users.sourceforge.net>
#
# Tests for the crc32 commands
#
# -------------------------------------------------------------------------
# See the file "license.terms" for information on usage and redistribution
# of this file, and for a DISCLAIMER OF ALL WARRANTIES.
# -------------------------------------------------------------------------
# RCS: @(#) $Id: crc32.test 376851 2003-01-07 00:40:03Z jigen $

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

package require crc32

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

test crc32-1.0 {crc32 with no parameters } {
    catch {::crc::crc32} result
    string match "wrong # args: *" $result
} {1}

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

foreach {n msg expected} {
    1    ""
    "0"
    2    "a"
    "3904355907"
    3    "abc"
    "891568578"
    4    "message digest"
    "538287487"
    5    "abcdefghijklmnopqrstuvwxyz"
    "1277644989"
    6    "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
    "532866770"
    7    "12345678901234567890123456789012345678901234567890123456789012345678901234567890"
    "2091469426"
    8    "\uFFFE\u0000\u0001\u0002"
    "2968055525"
} {
    test crc32-2.$n {crc32 and unsigned integer} {
	::crc::crc32 $msg
    } $expected
}

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

foreach {n msg expected} {
    1    ""
    "0x0"
    2    "a"
    "0xE8B7BE43"
    3    "abc"
    "0x352441C2"
    4    "message digest"
    "0x20159D7F"
    5    "abcdefghijklmnopqrstuvwxyz"
    "0x4C2750BD"
    6    "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
    "0x1FC2E6D2"
    7    "12345678901234567890123456789012345678901234567890123456789012345678901234567890"
    "0x7CA94A72"
    8    "\uFFFE\u0000\u0001\u0002"
    "0xB0E8EEE5"
} {
    test crc32-3.$n {crc32 as hexadecimal string} {
	::crc::crc32 -format 0x%X $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 crc32-4.0 {crc32 file option} {
    set r1 [::crc::crc32 -file $crc::testfile]
    set r2 [::crc::crc32 [crc::loaddata $crc::testfile]]
    if {$r1 != $r2} {
        set r "differing results: $r1 != $r2"
    } else {
        set r ok
    }
} {ok}

foreach {n seed msg expected} {
    1    0  ""
    "4294967295"
    2    1  ""
    "4294967294"
    3    0  "Hello, World!"
    "482441901"
    4    1  "Hello, World!"
    "3243746088"
} {
    test crc32-4.$n {crc32 seed option} {
	::crc::crc32 -seed $seed $msg
    } $expected
}


if {![catch {package present Trf 2.0}]} {
    test crc32-5.0 {crc32 check Tcl and Trf version identity} {
        set data [crc::loaddata $crc::testfile]
        set r1 [::crc::Crc32_trf $data]
        set r2 [::crc::Crc32_tcl $data]
        if {int($r1) != int($r2)} {
            set r "differing results: $r1 != $r2"
        } else {
            set r ok
        }
    } {ok}
}

test crc32-6.0 {crc implementation option} {
    proc crc::junk {s seed} {
        return 0
    }

    ::crc::crc32 -impl crc::junk {Hello, World!}
} {0}

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

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

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