]> mj.ucw.cz Git - libucw.git/blob - lib/lizard-test.c
A new test.
[libucw.git] / lib / lizard-test.c
1 #include "lib/lib.h"
2 #include "lib/getopt.h"
3 #include "lib/fastbuf.h"
4 #include "lib/lizard.h"
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <fcntl.h>
8 #include <unistd.h>
9
10 static char *options = CF_SHORT_OPTS "cdtx";
11 static char *help = "\
12 Usage: lizard-test <options> input-file [output-file]\n\
13 \n\
14 Options:\n"
15 CF_USAGE
16 "-c\t\tCompress\n\
17 -d\t\tDecompress\n\
18 -t\t\tCompress, decompress, and compare (in memory only, default)\n\
19 -x\t\tLet the test crash by shrinking the output buffer\n\
20 ";
21
22 static void NONRET
23 usage(void)
24 {
25   fputs(help, stderr);
26   exit(1);
27 }
28
29 int
30 main(int argc, char **argv)
31 {
32   int opt;
33   uns action = 't';
34   uns crash = 0;
35   log_init(argv[0]);
36   while ((opt = cf_getopt(argc, argv, options, CF_NO_LONG_OPTS, NULL)) >= 0)
37     switch (opt)
38     {
39       case 'c':
40       case 'd':
41       case 't':
42         action = opt;
43         break;
44       case 'x':
45         crash++;
46         break;
47       default:
48         usage();
49     }
50   if (action == 't' && argc != optind+1
51   || action != 't' && argc != optind+2)
52     usage();
53
54   void *mi, *mo;
55   int li, lo;
56   uns adler = 0;
57
58   struct stat st;
59   stat(argv[optind], &st);
60   li = st.st_size;
61   struct fastbuf *fi = bopen(argv[optind], O_RDONLY, 1<<16);
62   if (action != 'd')
63   {
64     lo = li * LIZARD_MAX_MULTIPLY + LIZARD_MAX_ADD;
65     li += LIZARD_NEEDS_CHARS;
66   }
67   else
68   {
69     lo = bgetl(fi);
70     adler = bgetl(fi);
71     li -= 8;
72   }
73   mi = xmalloc(li);
74   mo = xmalloc(lo);
75   li = bread(fi, mi, li);
76   bclose(fi);
77
78   printf("%d ", li);
79   if (action == 'd')
80     printf("->expected %d (%08x) ", lo, adler);
81   fflush(stdout);
82   if (action != 'd')
83     lo = lizard_compress(mi, li, mo);
84   else
85   {
86     lo = lizard_decompress(mi, mo);
87     if (adler32(mo, lo) != adler)
88       printf("wrong Adler32 ");
89   }
90   printf("-> %d ", lo);
91   fflush(stdout);
92
93   if (action != 't')
94   {
95     struct fastbuf *fo = bopen(argv[optind+1], O_CREAT | O_TRUNC | O_WRONLY, 1<<16);
96     if (action == 'c')
97     {
98       bputl(fo, li);
99       bputl(fo, adler32(mi, li));
100     }
101     bwrite(fo, mo, lo);
102     bclose(fo);
103   }
104   else
105   {
106     int smaller_li;
107     if (li >= (int) CPU_PAGE_SIZE)
108       smaller_li = li - CPU_PAGE_SIZE;
109     else
110       smaller_li = 0;
111     struct lizard_buffer *buf = lizard_alloc();
112     byte *ptr = lizard_decompress_safe(mo, buf, crash ? smaller_li : li);
113     if (!ptr)
114       printf("err: %m");
115     else if (memcmp(mi, ptr, li))
116       printf("WRONG");
117     else
118       printf("OK");
119     lizard_free(buf);
120   }
121   printf("\n");
122 }