0ad/source/lib/file/archive/disabled_tests/test_codec_zlib.h
janwas 317f98a6c0 WIP of new file code
archive: pretty much done except for trace and archive_builder
IO: io_manager still needs to be fixed/revised (especially WRT buffer
and alignment management)
posix: done
vfs: need to complete vfs_mount and tie it in to vfs.cpp
dir_util: needs further revisions (VFS interface changed since then)

This was SVN commit r5475.
2007-11-20 18:51:07 +00:00

59 lines
1.9 KiB
C++

#include "lib/self_test.h"
#include "lib/self_test.h"
#include "lib/res/file/archive/codec_zlib.h"
class TestCodecZLib : public CxxTest::TestSuite
{
public:
void test_compress_decompress_compare()
{
size_t inConsumed, outProduced;
u32 checksum;
// generate random input udata
// (limit values to 0..7 so that the udata will actually be compressible)
const size_t usize = 10000;
u8 udata[usize];
for(size_t i = 0; i < usize; i++)
udata[i] = rand() & 0x07;
// compress
u8* cdata; size_t csize;
{
boost::shared_ptr<ICodec> compressor_zlib = CreateCompressor_ZLib();
ICodec* c = compressor_zlib.get();
const size_t csizeMax = c->MaxOutputSize(usize);
cdata = new u8[csizeMax];
TS_ASSERT_OK(c->Process(udata, usize, cdata, csizeMax, inConsumed, outProduced));
TS_ASSERT_EQUALS(inConsumed, usize);
TS_ASSERT_LESS_THAN_EQUALS(outProduced, csizeMax);
u8* cdata2;
TS_ASSERT_OK(c->Finish(cdata2, csize, checksum));
TS_ASSERT_EQUALS(cdata, cdata2);
TS_ASSERT_EQUALS(csize, outProduced);
}
// make sure the data changed during compression
TS_ASSERT(csize != usize || memcmp(udata, cdata, std::min(usize, csize)) != 0);
// decompress
u8 ddata[usize];
{
boost::shared_ptr<ICodec> decompressor_zlib = CreateDecompressor_ZLib();
ICodec* d = decompressor_zlib.get();
TS_ASSERT_OK(decompressor_zlib->Process(cdata, csize, ddata, usize, inConsumed, outProduced));
TS_ASSERT_EQUALS(inConsumed, csize); // ZLib always outputs as much data as possible
TS_ASSERT_EQUALS(outProduced, usize); // .. so these figures are correct before Finish()
u8* ddata2; size_t dsize;
TS_ASSERT_OK(d->Finish(&ddata2, &dsize, &checksum));
TS_ASSERT_EQUALS(ddata, ddata2);
TS_ASSERT_EQUALS(dsize, outProduced);
}
// verify udata survived intact
TS_ASSERT_SAME_DATA(udata, ddata, usize);
delete[] cdata;
}
};