]> mj.ucw.cz Git - libucw.git/blobdiff - lib/lizard-test.c
Merged obj2buck.h and buck2obj.h to object.h, the number of includes
[libucw.git] / lib / lizard-test.c
index 49983b36948d509a2ee6a439d1643e1769807f1d..ba78e022cadd28ac52d404c31e45a921dab0f302 100644 (file)
@@ -1,21 +1,23 @@
 #include "lib/lib.h"
 #include "lib/conf.h"
 #include "lib/fastbuf.h"
-#include "lib/lizzard.h"
+#include "lib/lizard.h"
 #include <stdio.h>
 #include <stdlib.h>
 #include <fcntl.h>
 #include <unistd.h>
+#include <sys/user.h>
 
-static char *options = CF_SHORT_OPTS "cdt";
+static char *options = CF_SHORT_OPTS "cdtx";
 static char *help = "\
-Usage: lizzard-test <options> input-file [output-file]\n\
+Usage: lizard-test <options> input-file [output-file]\n\
 \n\
 Options:\n"
 CF_USAGE
-"-c\t\tCompress (default)\n\
+"-c\t\tCompress\n\
 -d\t\tDecompress\n\
--t\t\tCompress, decompress, and compare (in memory only)\n\
+-t\t\tCompress, decompress, and compare (in memory only, default)\n\
+-x\t\tLet the test crash by shrinking the output buffer\n\
 ";
 
 static void NONRET
@@ -29,7 +31,8 @@ int
 main(int argc, char **argv)
 {
   int opt;
-  uns action = 'c';
+  uns action = 't';
+  uns crash = 0;
   log_init(argv[0]);
   while ((opt = cf_getopt(argc, argv, options, CF_NO_LONG_OPTS, NULL)) >= 0)
     switch (opt)
@@ -39,6 +42,9 @@ main(int argc, char **argv)
       case 't':
        action = opt;
        break;
+      case 'x':
+       crash++;
+       break;
       default:
        usage();
     }
@@ -47,7 +53,7 @@ main(int argc, char **argv)
     usage();
 
   void *mi, *mo;
-  uns li, lo;
+  int li, lo;
 
   struct stat st;
   stat(argv[optind], &st);
@@ -55,8 +61,8 @@ main(int argc, char **argv)
   struct fastbuf *fi = bopen(argv[optind], O_RDONLY, 1<<16);
   if (action != 'd')
   {
-    lo = li * LIZZARD_MAX_MULTIPLY + LIZZARD_MAX_ADD;
-    li += LIZZARD_NEEDS_CHARS;
+    lo = li * LIZARD_MAX_MULTIPLY + LIZARD_MAX_ADD;
+    li += LIZARD_NEEDS_CHARS;
   }
   else
   {
@@ -73,9 +79,9 @@ main(int argc, char **argv)
     printf("->expected %d ", lo);
   fflush(stdout);
   if (action != 'd')
-    lo = lizzard_compress(mi, li, mo);
+    lo = lizard_compress(mi, li, mo);
   else
-    lo = lizzard_decompress(mi, mo);
+    lo = lizard_decompress(mi, mo);
   printf("-> %d ", lo);
   fflush(stdout);
 
@@ -89,16 +95,20 @@ main(int argc, char **argv)
   }
   else
   {
-    void *mv;
-    uns lv;
-    mv = xmalloc(li);
-    lv = lizzard_decompress(mo, mv);
-    printf("-> %d ", lv);
-    fflush(stdout);
-    if (lv != li || memcmp(mi, mv, lv))
+    int smaller_li;
+    if (li >= (int) PAGE_SIZE)
+      smaller_li = li - PAGE_SIZE;
+    else
+      smaller_li = 0;
+    struct lizard_buffer *buf = lizard_alloc();
+    byte *ptr = lizard_decompress_safe(mo, buf, crash ? smaller_li : li);
+    if (!ptr)
+      printf("err: %m");
+    else if (memcmp(mi, ptr, li))
       printf("WRONG");
     else
       printf("OK");
+    lizard_free(buf);
   }
   printf("\n");
 }