.autotest

git-svn-id: svn://ultimatepp.org/upp/trunk@10003 f0d560ea-af0d-0410-9eb7-867de7ffcac7
This commit is contained in:
cxl 2016-07-03 12:58:01 +00:00
parent 374e2d7c95
commit 856509b724

View file

@ -4,111 +4,11 @@ using namespace Upp;
#define LLOG(x)
// LZ4 streaming API example : double buffer
// Copyright : Takayuki Matsuoka
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
enum {
// BLOCK_BYTES = 1024 * 8,
BLOCK_BYTES = 1024 * 64,
};
size_t write_int(FILE* fp, int i) {
return fwrite(&i, sizeof(i), 1, fp);
}
size_t write_bin(FILE* fp, const void* array, size_t arrayBytes) {
return fwrite(array, 1, arrayBytes, fp);
}
size_t read_int(FILE* fp, int* i) {
return fread(i, sizeof(*i), 1, fp);
}
size_t read_bin(FILE* fp, void* array, size_t arrayBytes) {
return fread(array, 1, arrayBytes, fp);
}
void test_compress(FILE* outFp, FILE* inpFp)
{
LZ4_stream_t lz4Stream_body = { 0 };
LZ4_stream_t* lz4Stream = &lz4Stream_body;
char inpBuf[2][BLOCK_BYTES];
int inpBufIndex = 0;
Zero(inpBuf);
for(;;) {
char* const inpPtr = inpBuf[inpBufIndex];
const int inpBytes = (int) read_bin(inpFp, inpPtr, BLOCK_BYTES);
if(0 == inpBytes) {
break;
}
{
char cmpBuf[LZ4_COMPRESSBOUND(BLOCK_BYTES)];
LLOG("To compress " << inpBytes << ' ' << MD5String(inpPtr, inpBytes) << " " << inpBufIndex);
const int cmpBytes = LZ4_compress_continue(lz4Stream, inpPtr, cmpBuf, inpBytes);
if(cmpBytes <= 0) {
break;
}
LLOG("Out " << cmpBytes + 4);
write_int(outFp, cmpBytes);
write_bin(outFp, cmpBuf, (size_t) cmpBytes);
}
inpBufIndex = (inpBufIndex + 1) % 2;
}
// write_int(outFp, 0);
}
void test_decompress(FILE* outFp, FILE* inpFp)
{
LZ4_streamDecode_t lz4StreamDecode_body = { 0 };
LZ4_streamDecode_t* lz4StreamDecode = &lz4StreamDecode_body;
char decBuf[2][BLOCK_BYTES];
int decBufIndex = 0;
for(;;) {
char cmpBuf[LZ4_COMPRESSBOUND(BLOCK_BYTES)];
int cmpBytes = 0;
{
const size_t readCount0 = read_int(inpFp, &cmpBytes);
if(readCount0 != 1 || cmpBytes <= 0) {
break;
}
const size_t readCount1 = read_bin(inpFp, cmpBuf, (size_t) cmpBytes);
if(readCount1 != (size_t) cmpBytes) {
break;
}
}
{
char* const decPtr = decBuf[decBufIndex];
const int decBytes = LZ4_decompress_safe_continue(
lz4StreamDecode, cmpBuf, decPtr, cmpBytes, BLOCK_BYTES);
if(decBytes <= 0) {
break;
}
write_bin(outFp, decPtr, (size_t) decBytes);
}
decBufIndex = (decBufIndex + 1) % 2;
}
}
bool FilesEqual(const char *p1, const char *p2)
{
FileIn in1(p1);
@ -131,12 +31,6 @@ void Out(const void *data, int count, Stream *out)
out->Put(data, count);
}
extern "C" {
extern int matchOffsetStat[256];
extern int matchLengthStat[256];
extern int matchLengthBig;
};
CONSOLE_APP_MAIN
{
StdLogSetup(LOG_FILE|LOG_COUT);
@ -154,46 +48,14 @@ CONSOLE_APP_MAIN
total_sz += ff.GetLength();
String lz4 = AppendFileName(outdir, ff.GetName() + ".lz4");
String dec = AppendFileName(outdir, ff.GetName());
String lz4upp = AppendFileName(outdir, ff.GetName() + ".lz4upp");
String decupp = AppendFileName(outdir, ff.GetName());
RLOG("***************** " << file);
// compress
{
RTIMESTOP("Compress");
FILE* inpFp = fopen(file, "rb");
FILE* outFp = fopen(lz4, "wb");
test_compress(outFp, inpFp);
printf("compress : done\n");
fclose(outFp);
fclose(inpFp);
RDUMP(GetFileLength(file));
RDUMP(GetFileLength(lz4));
}
// decompress
{
{ RTIMESTOP("Decompress");
FILE* inpFp = fopen(lz4, "rb");
FILE* outFp = fopen(dec, "wb");
test_decompress(outFp, inpFp);
printf("decompress : done\n");
fclose(outFp);
fclose(inpFp);
}
ASSERT(FilesEqual(file, dec));
}
{
RLOG("=== U++ compress small blocks ===========================================");
{ RTIMESTOP("U++ compress small blocks");
FileOut out(lz4upp);
FileIn in(file);
FileOut out(lz4);
Lz4 lz4;
lz4.Compress();
lz4.WhenOut = callback1(Out, &out);
@ -207,35 +69,13 @@ CONSOLE_APP_MAIN
lz4.End();
out.Close();
}
ASSERT(FilesEqual(lz4upp, lz4));
}
{
RLOG("=== U++ compress big blocks ======");
{ RTIMESTOP("U++");
FileOut out(lz4upp);
FileIn in(file);
Lz4 lz4;
lz4.Compress();
lz4.WhenOut = callback1(Out, &out);
int ch = 0;
for(;;) {
String data = in.Get(150000);
if(IsNull(data))
break;
lz4.Put(~data, data.GetCount());
}
lz4.End();
out.Close();
}
ASSERT(FilesEqual(lz4upp, lz4));
}
{
RLOG("=== U++ decompress small blocks =====");
{ RTIMESTOP("U++ decompress small blocks");
FileIn in(lz4upp);
FileOut out(decupp);
FileIn in(lz4);
FileOut out(dec);
Lz4 lz4;
lz4.Decompress();
lz4.WhenOut = callback1(Out, &out);
@ -250,14 +90,36 @@ CONSOLE_APP_MAIN
ASSERT(!lz4.IsError());
out.Close();
}
ASSERT(FilesEqual(file, decupp));
}
ASSERT(FilesEqual(file, dec));
{
RLOG("=== U++ compress big blocks ======");
{ RTIMESTOP("U++");
FileIn in(file);
FileOut out(lz4);
Lz4 lz4;
lz4.Compress();
lz4.WhenOut = callback1(Out, &out);
int ch = 0;
for(;;) {
String data = in.Get(150000);
if(IsNull(data))
break;
lz4.Put(~data, data.GetCount());
}
lz4.End();
out.Close();
}
}
{
RLOG("=== U++ decompress big blocks =====");
{ RTIMESTOP("U++ decompress big blocks");
FileIn in(lz4upp);
FileOut out(decupp);
FileIn in(lz4);
FileOut out(dec);
Lz4 lz4;
lz4.Decompress();
lz4.WhenOut = callback1(Out, &out);
@ -272,13 +134,12 @@ CONSOLE_APP_MAIN
ASSERT(!lz4.IsError());
out.Close();
}
ASSERT(FilesEqual(file, decupp));
}
ASSERT(FilesEqual(file, dec));
DeleteFile(lz4);
DeleteFile(dec);
DeleteFile(lz4upp);
DeleteFile(decupp);
}
ff.Next();