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