Add test cases to #437

This commit is contained in:
jiangjufa 2019-07-25 11:13:49 +08:00
parent 79baebe50e
commit 8b12fe4d60
1 changed files with 637 additions and 0 deletions

View File

@ -6,6 +6,7 @@
/* @(#) $Id$ */
#include "zlib.h"
#include "deflate.h"
#include <stdio.h>
#ifdef STDC
@ -33,6 +34,8 @@ static z_const char hello[] = "hello, hello!";
static const char dictionary[] = "hello";
static uLong dictId; /* Adler32 value of the dictionary */
Bytef* dictNew ;
uInt* dictLen ;
void test_deflate OF((Byte *compr, uLong comprLen));
void test_inflate OF((Byte *compr, uLong comprLen,
@ -537,6 +540,632 @@ void test_dict_inflate(compr, comprLen, uncompr, uncomprLen)
}
}
/* ===========================================================================
* Test deflateBound() with small buffers
*/
void test_deflate_bound(compr, comprLen)
Byte *compr;
uLong comprLen;
{
z_stream c_stream; /* compression stream */
int err;
uLong len = (uLong)strlen(hello)+1;
uInt *outBuf =NULL;
c_stream.zalloc = zalloc;
c_stream.zfree = zfree;
c_stream.opaque = (voidpf)0;
c_stream.avail_in = len;
c_stream.next_in = (Bytef *)hello;
c_stream.avail_out = 0;
c_stream.next_out = (Bytef *)outBuf;
if ((err = deflateInit(&c_stream, Z_DEFAULT_COMPRESSION)) == Z_OK)
{
//calculate actual output length and update structure
uInt estimateLen = deflateBound(&c_stream, len);
outBuf = malloc(estimateLen);
if (outBuf != NULL)
{
//update zlib configuration
c_stream.avail_out = (uInt)estimateLen;
c_stream.next_out = (Bytef *)outBuf;
// do the compression
err=deflate(&c_stream, Z_FINISH);
if(Z_STREAM_END==err)
{
printf("deflateBound(): OK\n");
}else
{
CHECK_ERR(err, "deflate");
}
}
}
err = deflateEnd(&c_stream);
CHECK_ERR(err, "deflateEnd");
}
/* ===========================================================================
* Test deflateCopy() with small buffers
*/
void test_deflate_copy(compr, comprLen)
Byte *compr;
uLong comprLen;
{
z_stream c_stream,c_stream_copy; /* compression stream */
int err;
uLong len = (uLong)strlen(hello)+1;
c_stream.zalloc = zalloc;
c_stream.zfree = zfree;
c_stream.opaque = (voidpf)0;
err = deflateInit(&c_stream, Z_DEFAULT_COMPRESSION);
CHECK_ERR(err, "deflateInit");
c_stream.next_in = (z_const unsigned char *)hello;
c_stream.next_out = compr;
while (c_stream.total_in != len && c_stream.total_out < comprLen) {
c_stream.avail_in = c_stream.avail_out = 1; /* force small buffers */
err = deflate(&c_stream, Z_NO_FLUSH);
CHECK_ERR(err, "deflate");
}
/* Finish the stream, still forcing small buffers: */
for (;;) {
c_stream.avail_out = 1;
err = deflate(&c_stream, Z_FINISH);
if (err == Z_STREAM_END) break;
CHECK_ERR(err, "deflate");
}
err=deflateCopy(&c_stream_copy,&c_stream);
CHECK_ERR(err, "deflate_copy");
if(c_stream.state->status==c_stream_copy.state->status){
printf("deflate_copy(): OK\n");
}
err = deflateEnd(&c_stream);
CHECK_ERR(err, "deflateEnd");
}
/* ===========================================================================
* Test deflateGetDictionary() with small buffers
*/
void test_deflate_get_dict(compr, comprLen)
Byte *compr;
uLong comprLen;
{
z_stream c_stream; /* compression stream */
int err;
c_stream.zalloc = zalloc;
c_stream.zfree = zfree;
c_stream.opaque = (voidpf)0;
err = deflateInit(&c_stream, Z_BEST_COMPRESSION);
CHECK_ERR(err, "deflateInit");
c_stream.next_out = compr;
c_stream.avail_out = (uInt)comprLen;
c_stream.next_in = (z_const unsigned char *)hello;
c_stream.avail_in = (uInt)strlen(hello)+1;
err = deflate(&c_stream, Z_FINISH);
if (err != Z_STREAM_END) {
fprintf(stderr, "deflate should report Z_STREAM_END\n");
exit(1);
}
dictNew = calloc(256, 1);
dictLen = calloc(4, 1);
err = deflateGetDictionary(&c_stream,
(Bytef*)dictNew, (uInt*)dictLen);
CHECK_ERR(err, "deflateGetDictionary");
if(Z_OK==err)
{
printf("deflateGetDictionary(): %s\n",dictNew);
}
err = deflateEnd(&c_stream);
CHECK_ERR(err, "deflateEnd");
}
/* ===========================================================================
* Test deflatePending() with small buffers
*/
void test_deflate_pending(compr, comprLen)
Byte *compr;
uLong comprLen;
{
z_stream c_stream; /* compression stream */
int err;
int * bits=calloc(256, 1);
unsigned *ped=calloc(256, 1);
uLong len = (uLong)strlen(hello)+1;
c_stream.zalloc = zalloc;
c_stream.zfree = zfree;
c_stream.opaque = (voidpf)0;
err = deflateInit(&c_stream, Z_DEFAULT_COMPRESSION);
CHECK_ERR(err, "deflateInit");
c_stream.next_in = (z_const unsigned char *)hello;
c_stream.next_out = compr;
while (c_stream.total_in != len && c_stream.total_out < comprLen) {
c_stream.avail_in = c_stream.avail_out = 1; /* force small buffers */
err = deflate(&c_stream, Z_NO_FLUSH);
CHECK_ERR(err, "deflate");
}
err=deflatePending(&c_stream,ped,bits);
CHECK_ERR(err, "deflatePending");
if(*bits >=0 && *bits <=7 && *ped>=0)
{
printf("deflatePending(): OK\n");
}else{
printf("deflatePending(): error\n");
}
/* Finish the stream, still forcing small buffers: */
for (;;) {
c_stream.avail_out = 1;
err = deflate(&c_stream, Z_FINISH);
if (err == Z_STREAM_END) break;
CHECK_ERR(err, "deflate");
}
err = deflateEnd(&c_stream);
CHECK_ERR(err, "deflateEnd");
}
/* ===========================================================================
* Test deflatePrime() with small buffers
*/
void test_deflate_prime(compr, comprLen)
Byte *compr;
uLong comprLen;
{
z_stream c_stream; /* compression stream */
int err;
int bits=0;
int values=0;
uLong len = (uLong)strlen(hello)+1;
c_stream.zalloc = zalloc;
c_stream.zfree = zfree;
c_stream.opaque = (voidpf)0;
//windowBits can also be 8..15 for raw deflateThe default value is 15
err = deflateInit2(&c_stream, Z_DEFAULT_COMPRESSION, Z_DEFLATED, -MAX_WBITS, 8, Z_DEFAULT_STRATEGY);
CHECK_ERR(err, "deflateInit2");
err = deflatePrime(&c_stream, bits,values);
CHECK_ERR(err, "deflatePrime");
if(Z_OK==err)
{
printf("deflatePrime(): OK\n");
}
c_stream.next_in = (z_const unsigned char *)hello;
c_stream.next_out = compr;
while (c_stream.total_in != len && c_stream.total_out < comprLen) {
c_stream.avail_in = c_stream.avail_out = 1; /* force small buffers */
err = deflate(&c_stream, Z_NO_FLUSH);
CHECK_ERR(err, "deflate");
}
/* Finish the stream, still forcing small buffers: */
for (;;) {
c_stream.avail_out = 1;
err = deflate(&c_stream, Z_FINISH);
if (err == Z_STREAM_END) break;
CHECK_ERR(err, "deflate");
}
err = deflateEnd(&c_stream);
CHECK_ERR(err, "deflateEnd");
}
/* ===========================================================================
* Test deflateSetHeader() with small buffers
*/
void test_deflate_set_header(compr, comprLen)
Byte *compr;
uLong comprLen;
{
gz_headerp head=calloc(256, 1);
z_stream c_stream; /* compression stream */
int err;
uLong len = (uLong)strlen(hello)+1;
c_stream.zalloc = zalloc;
c_stream.zfree = zfree;
c_stream.opaque = (voidpf)0;
//gzip
err = deflateInit2(&c_stream, Z_DEFAULT_COMPRESSION, Z_DEFLATED, MAX_WBITS + 16, 8, Z_DEFAULT_STRATEGY);
CHECK_ERR(err, "deflateInit2");
head->text=1;
err = deflateSetHeader(&c_stream, head);
CHECK_ERR(err, "deflateSetHeader");
if(Z_OK==err)
{
printf("deflateSetHeader(): OK\n");
}
c_stream.next_in = (z_const unsigned char *)hello;
c_stream.next_out = compr;
while (c_stream.total_in != len && c_stream.total_out < comprLen) {
c_stream.avail_in = c_stream.avail_out = 1; /* force small buffers */
err = deflate(&c_stream, Z_NO_FLUSH);
CHECK_ERR(err, "deflate");
}
/* Finish the stream, still forcing small buffers: */
for (;;) {
c_stream.avail_out = 1;
err = deflate(&c_stream, Z_FINISH);
if (err == Z_STREAM_END) break;
CHECK_ERR(err, "deflate");
}
err = deflateEnd(&c_stream);
CHECK_ERR(err, "deflateEnd");
}
/* ===========================================================================
* Test deflateTune() with small buffers
*/
void test_deflate_tune(compr, comprLen)
Byte *compr;
uLong comprLen;
{
z_stream c_stream; /* compression stream */
int err;
int good_length=3;
int max_lazy=5;
int nice_length=18;
int max_chain=6;
uLong len = (uLong)strlen(hello)+1;
c_stream.zalloc = zalloc;
c_stream.zfree = zfree;
c_stream.opaque = (voidpf)0;
err = deflateInit(&c_stream, Z_BEST_COMPRESSION);
CHECK_ERR(err, "deflateInit");
err = deflateTune(&c_stream,(uInt)good_length,(uInt)max_lazy,nice_length,(uInt)max_chain);
CHECK_ERR(err, "deflateTune");
if(Z_OK==err)
{
printf("deflateTune(): OK\n");
}
c_stream.next_in = (z_const unsigned char *)hello;
c_stream.next_out = compr;
while (c_stream.total_in != len && c_stream.total_out < comprLen) {
c_stream.avail_in = c_stream.avail_out = 1; /* force small buffers */
err = deflate(&c_stream, Z_NO_FLUSH);
CHECK_ERR(err, "deflate");
}
/* Finish the stream, still forcing small buffers: */
for (;;) {
c_stream.avail_out = 1;
err = deflate(&c_stream, Z_FINISH);
if (err == Z_STREAM_END) break;
CHECK_ERR(err, "deflate");
}
err = deflateEnd(&c_stream);
CHECK_ERR(err, "deflateEnd");
}
/* ===========================================================================
* Usage: example [output.gz [input.gz]]
*/
@ -594,6 +1223,14 @@ int main(argc, argv)
test_dict_deflate(compr, comprLen);
test_dict_inflate(compr, comprLen, uncompr, uncomprLen);
test_deflate_bound(compr, comprLen);
test_deflate_copy(compr, comprLen);
test_deflate_get_dict(compr, comprLen);
test_deflate_set_header(compr, comprLen);
test_deflate_tune(compr, comprLen);
test_deflate_pending(compr, comprLen);
test_deflate_prime(compr, comprLen);
free(compr);
free(uncompr);