# crc16.test - Copyright (C) 2002 Pat Thoyts <patthoyts@users.sourceforge.net>
#
# Tests for the crc16 commands
#
# -------------------------------------------------------------------------
# See the file "license.terms" for information on usage and redistribution
# of this file, and for a DISCLAIMER OF ALL WARRANTIES.
# -------------------------------------------------------------------------
# RCS: @(#) $Id: crc16.test 373478 2002-09-25 23:43:58Z jigen $

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

package require crc16

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

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

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

foreach {n msg expected} {
    1    ""
    "0"
    2    "123456789"
    "47933"
    3    "abc"
    "38712"
    4    "ABC"
    "17697"
    5    "This is a string"
    "19524"
    8    "\uFFFE\u0000\u0001\u0002"
    "47537"
} {
    test crc16-2.$n {crc16 and unsigned integer} {
	::crc::crc16 $msg
    } $expected
}

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

foreach {n msg expected} {
    1    ""
    "0x0"
    2    "123456789"
    "0xBB3D"
    3    "abc"
    "0x9738"
    4    "ABC"
    "0x4521"
    5    "This is a string"
    "0x4C44"
    6    "\uFFFE\u0000\u0001\u0002"
    "0xB9B1"
} {
    test crc16-3.$n {crc16 as hexadecimal string} {
	::crc::crc16 -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 crc16-4.0 {crc16 file option} {
    set r1 [::crc::crc16 -file $crc::testfile]
    set r2 [::crc::crc16 [crc::loaddata $crc::testfile]]
    if {$r1 != $r2} {
        set r "differing results: $r1 != $r2"
    } else {
        set r ok
    }
} {ok}

test crc16-5.0 {crc implementation option} {
    proc crc::junk {s seed} {
        return 0
    }

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

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

foreach {n msg expected} {
    1    ""
    "0xFFFF"
    2    "123456789"
    "0x29B1"
    3    "abc"
    "0x514A"
    4    "ABC"
    "0xF508"
    5    "This is a string"
    "0x4BE9"
    8    "\uFFFE\u0000\u0001\u0002"
    "0xAAA4"
} {
    test crc16-6.$n {crc-ccitt and unsigned integer} {
	::crc::crc-ccitt -format 0x%X $msg
    } $expected
}

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

foreach {n msg expected} {
    1    ""
    "0x0"
    2    "123456789"
    "0xCBF43926"
    3    "abc"
    "0x352441C2"
    4    "ABC"
    "0xA3830348"
    5    "This is a string"
    "0x876633F"
    8    "\uFFFE\u0000\u0001\u0002"
    "0xB0E8EEE5"
} {
    test crc16-7.$n {crc-32 from the crc16 algorithms} {
	::crc::crc-32 -format 0x%X $msg
    } $expected
}
# -------------------------------------------------------------------------

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

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