# Commands covered: zlib -*- tcl -*- # # This file contains a collection of tests for one or more of the Tcl # built-in commands. Sourcing this file into Tcl runs the tests and # generates output for errors. No output means no errors were found. # # Copyright (c) 2000 by Scriptics Corporation. # # See the file "license.terms" for information on usage and redistribution # of this file, and for a DISCLAIMER OF ALL WARRANTIES. # # RCS: @(#) $Id: tclsample.test,v 1.1 2002/03/06 05:35:31 hobbs Exp $ if {[lsearch [namespace children] ::tcltest] == -1} { package require tcltest namespace import ::tcltest::* } package require zlib set in [open [file dirname [info script]]/test.txt] set ::zlibtestdata [read $in] close $in test zlib-1.1 {incorrect command usage} { list [catch {zlib} errMsg] $errMsg } {1 {wrong # args: should be "zlib command arg ?...?"}} test zlib-1.2 {adler32 checksum} { zlib adler32 foobar } {145425018} test zlib-1.3 {CRC32 checksum} { zlib crc32 foobar } {-1628037227} test zlib-1.4 {compress / decompress test} { set in "blablablablab" set compressed [zlib compress $in] set out [zlib decompress $compressed] if { ![string equal $in $out] } { return mismatch } return match } {match} test zlib-1.4 {deflate / inflate test} { set in "blablablablab" set compressed [zlib deflate $in] set out [zlib inflate $compressed] if { ![string equal $in $out] } { return mismatch } return match } {match} test zlib-1.5 {gzip / gunzip test} { set in "blablablablab" set compressed [zlib gzip $in] set out [zlib gunzip $compressed] if { ![string equal $in $out] } { return mismatch } return match } {match} test zlib-2.1 {stream gzip test, single add/finalize} { set in "blablablablab" set s [zlib stream gzip] set compressed [$s add -finalize $in] set out [zlib gunzip $compressed] if { ![string equal $in $out] } { return mismatch } $s close return match } {match} test zlib-2.2 {stream compress test, multi add & finalize} { set in "blablablablab" set in2 "next bit to compress" set s [zlib stream gzip] set compressed [$s add $in] append compressed [$s add -finalize $in2] set out [zlib gunzip $compressed] if { ![string equal $in$in2 $out] } { return mismatch } $s close return match } {match} test zlib-2.3 {stream compress test, multi put, get & finalize} { set in "First bit to compress, " set in2 "next bit to compress" set s [zlib stream gzip] $s put $in set compressed [$s get] $s put -finalize $in2 append compressed [$s get] if { $compressed ne [zlib gzip $in$in2] } { puts "mismatch gzip [string length $compressed] [string length [zlib gzip $in$in2]]" } set out [zlib gunzip $compressed] $s close return $out } {First bit to compress, next bit to compress} test zlib-2.4 {stream compress test, multi put, partial gets & finalize} { set compressed "" set in $::zlibtestdata set s [zlib stream gzip] # Create some compressed data set cnt 0 while { $in ne "" && $cnt < 10 } { incr cnt $s put [string range $in 0 149] set in [string range $in 150 end] } $s flush # loop until we have all compressed bytes so far. while { [set tmp [$s get 100]] ne "" } { #puts "Got [string length $tmp]" append compressed $tmp } # And compress the final part while { $in ne "" } { $s put [string range $in 0 149] set in [string range $in 150 end] } # Get it the remainder in one go: append compressed [$s get] # Finalize $s finalize # And get the tail final compressed: append compressed [$s get] #puts "Compressed size [string length $compressed]" $s close return [zlib gunzip $compressed] } $::zlibtestdata test zlib-2.5 {stream decompress test, multi put, get & finalize, eof} { set compressed [zlib compress $::zlibtestdata] set s [zlib stream decompress] while { $compressed ne "" } { $s put [string range $compressed 0 149] set compressed [string range $compressed 150 end] } set decompressed [$s get 100] while { ![$s eof] } { append decompressed [$s get 100] } $s close return $decompressed } $::zlibtestdata test zlib-2.6 {stream decompress adler32 test} { set compressed [zlib compress $::zlibtestdata] set s [zlib stream decompress] $s put $compressed $s get set adler [$s adler32] $s close return "Got adler $adler, expected [zlib adler32 $::zlibtestdata]" } {Got adler 1670468176, expected 1670468176} test zlib-2.5 {stream reset test, decompress w/ multi put, get & finalize, eof} { set s [zlib stream decompress] set compressed [zlib compress "bad$::zlibtestdata"] while { $compressed ne "" } { $s put [string range $compressed 0 149] set compressed [string range $compressed 150 end] } set decompressed [$s get 100] append decompressed [$s get 100] $s reset $s put [zlib compress "$::zlibtestdata"] set decompressed [$s get] $s close return $decompressed } $::zlibtestdata # cleanup ::tcltest::cleanupTests return