提交 647cf58c 编写于 作者: I Ivan Leskin 提交者: Ashwin Agrawal

Unit test for zstd_compression.c

Add a unit test (and its infrastructure) for 'zstd_compress()'.

The test checks whether 'zstd_compress()' returns correct output in case
compression fails (compressed data is larger than uncompressed). To do that,
'ZSTD_compressCCtx()' is mocked to always return 'ZSTD_error_dstSize_tooSmall'.

Also, an 'ifndef' is added around 'PG_MODULE_MAGIC' in zstd_compression.c
上级 f8afec06
......@@ -20,11 +20,13 @@ endif
# Install into cdb_init.d, so that the catalog changes performed by initdb,
# and the compressor is available in all databases.
.PHONY: install-data
install-data:
$(INSTALL_DATA) zstd_compression.sql '$(DESTDIR)$(datadir)/cdb_init.d/zstd_compression.sql'
install: install-data
.PHONY: uninstall-data
uninstall-data:
......@@ -32,3 +34,12 @@ uninstall-data:
uninstall: uninstall-data
.PHONY: unittest-check unittest-clean
unittest-check:
$(MAKE) -C test check
unittest-clean:
$(MAKE) -C test clean
subdir=gpcontrib/zstd/test
top_builddir=../../..
include $(top_builddir)/src/Makefile.global
TARGETS= zstd_compression
include $(top_builddir)/src/backend/mock.mk
/* mock functions for zstd.h */
size_t
ZSTD_compressCCtx(ZSTD_CCtx * ctx, void *dst, size_t dstCapacity, const void *src, size_t srcSize, int compressionLevel)
{
return (size_t) -ZSTD_error_dstSize_tooSmall;
}
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include "cmockery.h"
#define UNIT_TESTING
/* Include unit under test */
#include "../zstd_compression.c"
/* Include mock functions for zstd.h */
#include "mock/zstd_mock.c"
/*
* Test compression failure when compressed data is larger than uncompressed
*/
void
test_uncompressed_sz_equals_compressed_sz(void **state)
{
int32 uncompressed_size = 42;
int32 compressed_size = 0;
DirectFunctionCall6(
zstd_compress,
PointerGetDatum(NULL),
Int32GetDatum(uncompressed_size),
PointerGetDatum(NULL),
Int32GetDatum(uncompressed_size),
PointerGetDatum(&compressed_size),
PointerGetDatum(NULL)
);
assert_int_equal(compressed_size, uncompressed_size);
}
int
main(int argc, char *argv[])
{
cmockery_parse_arguments(argc, argv);
const UnitTest tests[] = {
unit_test(test_uncompressed_sz_equals_compressed_sz),
};
MemoryContextInit();
return run_tests(tests);
}
......@@ -18,6 +18,10 @@
#include <zstd.h>
#include <zstd_errors.h>
#ifndef PG_MODULE_MAGIC
PG_MODULE_MAGIC;
#endif
Datum zstd_constructor(PG_FUNCTION_ARGS);
Datum zstd_destructor(PG_FUNCTION_ARGS);
Datum zstd_compress(PG_FUNCTION_ARGS);
......@@ -30,7 +34,6 @@ PG_FUNCTION_INFO_V1(zstd_compress);
PG_FUNCTION_INFO_V1(zstd_decompress);
PG_FUNCTION_INFO_V1(zstd_validator);
PG_MODULE_MAGIC;
/* Internal state for zstd */
typedef struct zstd_state
......
......@@ -81,7 +81,7 @@ comptype_to_name(char *comptype)
len = strlen(comptype);
dct = asc_tolower(comptype, len);
len = strlen(dct);
memcpy(&(NameStr(compname)), dct, len);
NameStr(compname)[len] = '\0';
pfree(dct);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册