]>
git.proxmox.com Git - libgit2.git/blob - tests/core/zstream.c
1 #include "clar_libgit2.h"
5 static const char * data
= "This is a test test test of This is a test" ;
7 #define INFLATE_EXTRA 2
9 static void assert_zlib_equal_ (
10 const void * expected
, size_t e_len
,
11 const void * compressed
, size_t c_len
,
12 const char * msg
, const char * file
, int line
)
15 char * expanded
= git__calloc ( 1 , e_len
+ INFLATE_EXTRA
);
18 memset (& stream
, 0 , sizeof ( stream
));
19 stream
. next_out
= ( Bytef
*) expanded
;
20 stream
. avail_out
= ( uInt
)( e_len
+ INFLATE_EXTRA
);
21 stream
. next_in
= ( Bytef
*) compressed
;
22 stream
. avail_in
= ( uInt
) c_len
;
24 cl_assert ( inflateInit (& stream
) == Z_OK
);
25 cl_assert ( inflate (& stream
, Z_FINISH
));
30 "%d" , ( int ) stream
. total_out
, ( int ) e_len
);
32 file
, line
, "Buffer len was not exact match" , 1 ,
33 "%d" , ( int ) stream
. avail_out
, ( int ) INFLATE_EXTRA
);
36 memcmp ( expanded
, expected
, e_len
) == 0 ,
37 file
, line
, "uncompressed data did not match" , NULL
, 1 );
42 #define assert_zlib_equal(E,EL,C,CL) \
43 assert_zlib_equal_(E, EL, C, CL, #EL " != " #CL, __FILE__, (int)__LINE__)
45 void test_core_zstream__basic ( void )
47 git_zstream z
= GIT_ZSTREAM_INIT
;
49 size_t outlen
= sizeof ( out
);
51 cl_git_pass ( git_zstream_init (& z
));
52 cl_git_pass ( git_zstream_set_input (& z
, data
, strlen ( data
) + 1 ));
53 cl_git_pass ( git_zstream_get_output ( out
, & outlen
, & z
));
54 cl_assert ( git_zstream_done (& z
));
55 cl_assert ( outlen
> 0 );
58 assert_zlib_equal ( data
, strlen ( data
) + 1 , out
, outlen
);
61 void test_core_zstream__buffer ( void )
63 git_buf out
= GIT_BUF_INIT
;
64 cl_git_pass ( git_zstream_deflatebuf (& out
, data
, strlen ( data
) + 1 ));
65 assert_zlib_equal ( data
, strlen ( data
) + 1 , out
. ptr
, out
. size
);
69 #define BIG_STRING_PART "Big Data IS Big - Long Data IS Long - We need a buffer larger than 1024 x 1024 to make sure we trigger chunked compression - Big Big Data IS Bigger than Big - Long Long Data IS Longer than Long"
71 void test_core_zstream__big_data ( void )
73 git_buf in
= GIT_BUF_INIT
;
74 git_buf out
= GIT_BUF_INIT
;
77 /* make a big string that's easy to compress */
78 while ( in
. size
< 1024 * 1024 )
79 cl_git_pass ( git_buf_put (& in
, BIG_STRING_PART
, strlen ( BIG_STRING_PART
)));
81 cl_git_pass ( git_zstream_deflatebuf (& out
, in
. ptr
, in
. size
));
82 assert_zlib_equal ( in
. ptr
, in
. size
, out
. ptr
, out
. size
);
86 /* make a big string that's hard to compress */
89 for ( scan
= 0 ; scan
< in
. size
; ++ scan
)
90 in
. ptr
[ scan
] = ( char ) rand ();
92 cl_git_pass ( git_zstream_deflatebuf (& out
, in
. ptr
, in
. size
));
93 assert_zlib_equal ( in
. ptr
, in
. size
, out
. ptr
, out
. size
);