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