Skip to content

Commit

Permalink
Unit test for external sequence producer + static CCtx + streaming (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
embg authored Jun 3, 2024
1 parent f70fb7c commit be6a182
Showing 1 changed file with 61 additions and 2 deletions.
63 changes: 61 additions & 2 deletions tests/zstreamtest.c
Original file line number Diff line number Diff line change
Expand Up @@ -2356,7 +2356,7 @@ static int basicUnitTests(U32 seed, double compressibility, int bigTests)
}
DISPLAYLEVEL(3, "OK \n");

DISPLAYLEVEL(3, "test%3i : Testing external sequence producer with static CCtx: ", testNb++);
DISPLAYLEVEL(3, "test%3i : Testing external sequence producer with static CCtx (one-shot): ", testNb++);
{
size_t const dstBufSize = ZSTD_compressBound(CNBufferSize);
BYTE* const dstBuf = (BYTE*)malloc(dstBufSize);
Expand All @@ -2375,7 +2375,7 @@ static int basicUnitTests(U32 seed, double compressibility, int bigTests)
size_t const cctxSize = ZSTD_estimateCCtxSize_usingCCtxParams(params);
cctxBuf = malloc(cctxSize);
staticCCtx = ZSTD_initStaticCCtx(cctxBuf, cctxSize);
ZSTD_CCtx_setParametersUsingCCtxParams(staticCCtx, params);
CHECK_Z(ZSTD_CCtx_setParametersUsingCCtxParams(staticCCtx, params));
}

// Check that compression with external sequence producer succeeds when expected
Expand Down Expand Up @@ -2408,6 +2408,65 @@ static int basicUnitTests(U32 seed, double compressibility, int bigTests)
}
DISPLAYLEVEL(3, "OK \n");

DISPLAYLEVEL(3, "test%3i : Testing external sequence producer with static CCtx (streaming): ", testNb++);
{
size_t const dstBufSize = ZSTD_compressBound(CNBufferSize);
BYTE* const dstBuf = (BYTE*)malloc(dstBufSize);
size_t const checkBufSize = CNBufferSize;
BYTE* const checkBuf = (BYTE*)malloc(checkBufSize);
ZSTD_CCtx_params* params = ZSTD_createCCtxParams();
ZSTD_CCtx* staticCCtx;
void* cctxBuf;
EMF_testCase seqProdState;

CHECK_Z(ZSTD_CCtxParams_setParameter(params, ZSTD_c_validateSequences, 1));
CHECK_Z(ZSTD_CCtxParams_setParameter(params, ZSTD_c_enableSeqProducerFallback, 0));
ZSTD_CCtxParams_registerSequenceProducer(params, &seqProdState, zstreamSequenceProducer);

{
size_t const cctxSize = ZSTD_estimateCStreamSize_usingCCtxParams(params);
cctxBuf = malloc(cctxSize);
staticCCtx = ZSTD_initStaticCCtx(cctxBuf, cctxSize);
CHECK_Z(ZSTD_CCtx_setParametersUsingCCtxParams(staticCCtx, params));
}

// Check that compression with external sequence producer succeeds when expected
seqProdState = EMF_LOTS_OF_SEQS;
{
ZSTD_inBuffer inBuf = { CNBuffer, CNBufferSize, 0 };
ZSTD_outBuffer outBuf = { dstBuf, dstBufSize, 0 };
size_t dResult;
CHECK_Z(ZSTD_compressStream(staticCCtx, &outBuf, &inBuf));
CHECK_Z(ZSTD_endStream(staticCCtx, &outBuf));
CHECK(inBuf.pos != inBuf.size, "EMF: inBuf.pos != inBuf.size");
dResult = ZSTD_decompress(checkBuf, checkBufSize, outBuf.dst, outBuf.pos);
CHECK(ZSTD_isError(dResult), "EMF: Decompression error: %s", ZSTD_getErrorName(dResult));
CHECK(dResult != CNBufferSize, "EMF: Corruption!");
CHECK(memcmp(CNBuffer, checkBuf, CNBufferSize) != 0, "EMF: Corruption!");
}

CHECK_Z(ZSTD_CCtx_reset(staticCCtx, ZSTD_reset_session_only));

// Check that compression with external sequence producer fails when expected
seqProdState = EMF_BIG_ERROR;
{
ZSTD_inBuffer inBuf = { CNBuffer, CNBufferSize, 0 };
ZSTD_outBuffer outBuf = { dstBuf, dstBufSize, 0 };
size_t const cResult = ZSTD_compressStream(staticCCtx, &outBuf, &inBuf);
CHECK(!ZSTD_isError(cResult), "EMF: Should have raised an error!");
CHECK(
ZSTD_getErrorCode(cResult) != ZSTD_error_sequenceProducer_failed,
"EMF: Wrong error code: %s", ZSTD_getErrorName(cResult)
);
}

free(dstBuf);
free(checkBuf);
free(cctxBuf);
ZSTD_freeCCtxParams(params);
}
DISPLAYLEVEL(3, "OK \n");

DISPLAYLEVEL(3, "test%3i : Decoder should reject invalid frame header on legacy frames: ", testNb++);
{
const unsigned char compressed[] = { 0x26,0xb5,0x2f,0xfd,0x50,0x91,0xfd,0xd8,0xb5 };
Expand Down

0 comments on commit be6a182

Please sign in to comment.