]> git.ipfire.org Git - thirdparty/zstd.git/commitdiff
Add exposing test case
authorNick Terrell <terrelln@fb.com>
Sun, 11 Dec 2016 03:12:13 +0000 (19:12 -0800)
committerNick Terrell <terrelln@fb.com>
Sun, 11 Dec 2016 03:12:13 +0000 (19:12 -0800)
tests/.gitignore
tests/Makefile
tests/longmatch.c [new file with mode: 0644]

index c8f9ae79e7fd101f1cdec6158104fb0ec9b7bd31..b558ac2580ae6020b51294001ab1631e5186fbf9 100644 (file)
@@ -11,6 +11,7 @@ datagen
 paramgrill
 paramgrill32
 roundTripCrash
+longmatch
 
 # Tmp test directory
 zstdtest
index fbee21448daff946e3ef089b7979365dcf0c279d..6110465f6b13aa42220207086d76e17cd685c32f 100644 (file)
@@ -124,6 +124,9 @@ datagen : $(PRGDIR)/datagen.c datagencli.c
 roundTripCrash : $(ZSTD_FILES) roundTripCrash.c
        $(CC)      $(FLAGS) $^ -o $@$(EXT)
 
+longmatch  : $(ZSTD_FILES) longmatch.c
+       $(CC)      $(FLAGS) $^ -o $@$(EXT)
+
 namespaceTest:
        if $(CC) namespaceTest.c ../lib/common/xxhash.c -o $@ ; then echo compilation should fail; exit 1 ; fi
        $(RM) $@
@@ -140,7 +143,7 @@ clean:
         fullbench-lib$(EXT) fullbench-dll$(EXT) \
         fuzzer$(EXT) fuzzer32$(EXT) zbufftest$(EXT) zbufftest32$(EXT) \
                zstreamtest$(EXT) zstreamtest32$(EXT) \
-        datagen$(EXT) paramgrill$(EXT) roundTripCrash$(EXT)
+        datagen$(EXT) paramgrill$(EXT) roundTripCrash$(EXT) longmatch$(EXT)
        @echo Cleaning completed
 
 
@@ -180,7 +183,7 @@ zstd-playTests: datagen
        file $(ZSTD)
        ZSTD="$(QEMU_SYS) $(ZSTD)" ./playTests.sh $(ZSTDRTTEST)
 
-test: test-zstd test-fullbench test-fuzzer test-zstream
+test: test-zstd test-fullbench test-fuzzer test-zstream test-longmatch
 
 test32: test-zstd32 test-fullbench32 test-fuzzer32 test-zstream32
 
@@ -237,4 +240,7 @@ test-zstream: zstreamtest
 test-zstream32: zstreamtest32
        $(QEMU_SYS) ./zstreamtest32 $(ZSTREAM_TESTTIME)
 
+test-longmatch: longmatch
+       $(QEMU_SYS) ./longmatch
+
 endif
diff --git a/tests/longmatch.c b/tests/longmatch.c
new file mode 100644 (file)
index 0000000..b99dccc
--- /dev/null
@@ -0,0 +1,72 @@
+#define ZSTD_STATIC_LINKING_ONLY
+#include <zstd.h>
+#include <stdio.h>
+#include <stddef.h>
+#include <stdlib.h>
+
+void compress(ZSTD_CStream *ctx, ZSTD_outBuffer out, const void *data, size_t size) {
+  ZSTD_inBuffer in = { data, size, 0 };
+  while (in.pos < in.size) {
+    ZSTD_outBuffer tmp = out;
+    const size_t rc = ZSTD_compressStream(ctx, &tmp, &in);
+    if (ZSTD_isError(rc)) {
+      exit(5);
+    }
+  }
+  ZSTD_outBuffer tmp = out;
+  const size_t rc = ZSTD_flushStream(ctx, &tmp);
+  if (rc != 0) { exit(6); }
+}
+
+int main() {
+  ZSTD_CStream *ctx;
+  ZSTD_parameters params = {};
+  size_t rc;
+  unsigned windowLog;
+  /* Create stream */
+  ctx = ZSTD_createCStream();
+  if (!ctx) { return 1; }
+  /* Set parameters */
+  params.cParams.windowLog = 18;
+  params.cParams.chainLog = 13;
+  params.cParams.hashLog = 14;
+  params.cParams.searchLog = 1;
+  params.cParams.searchLength = 7;
+  params.cParams.targetLength = 16;
+  params.cParams.strategy = ZSTD_fast;
+  windowLog = params.cParams.windowLog;
+  /* Initialize stream */
+  rc = ZSTD_initCStream_advanced(ctx, NULL, 0, params, 0);
+  if (ZSTD_isError(rc)) { return 2; }
+  {
+    uint64_t compressed = 0;
+    const uint64_t toCompress = ((uint64_t)1) << 33;
+    const size_t size = 1 << windowLog;
+    size_t pos = 0;
+    char *srcBuffer = (char*) malloc(1 << windowLog);
+    char *dstBuffer = (char*) malloc(ZSTD_compressBound(1 << windowLog));
+    ZSTD_outBuffer out = { dstBuffer, ZSTD_compressBound(1 << windowLog), 0 };
+    const char match[] = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
+    const size_t randomData = (1 << windowLog) - 2*sizeof(match);
+    for (size_t i = 0; i < sizeof(match); ++i) {
+      srcBuffer[i] = match[i];
+    }
+    for (size_t i = 0; i < randomData; ++i) {
+      srcBuffer[sizeof(match) + i] = (char)(rand() & 0xFF);
+    }
+    for (size_t i = 0; i < sizeof(match); ++i) {
+      srcBuffer[sizeof(match) + randomData + i] = match[i];
+    }
+    compress(ctx, out, srcBuffer, size);
+    compressed += size;
+    while (compressed < toCompress) {
+      const size_t block = rand() % (size - pos + 1);
+      if (pos == size) { pos = 0; }
+      compress(ctx, out, srcBuffer + pos, block);
+      pos += block;
+      compressed += block;
+    }
+    free(srcBuffer);
+    free(dstBuffer);
+  }
+}