Bump copyright year
[idzebra-moved-to-github.git] / test / codec / tstcodec.c
1 /* This file is part of the Zebra server.
2    Copyright (C) 2004-2013 Index Data
3
4 Zebra is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
7 version.
8
9 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17
18 */
19
20 #if HAVE_CONFIG_H
21 #include <config.h>
22 #endif
23 #include <stdlib.h>
24 #include "../../index/index.h"
25
26 char *prog = "";
27
28 int tst_encode(int num)
29 {
30     struct it_key key;
31     int i;
32     void *codec_handle =iscz1_start();
33     char *dst_buf = malloc(200 + num * 10);
34     char *dst = dst_buf;
35     if (!dst_buf)
36     {
37         printf ("%s: out of memory (num=%d)\n", prog, num);
38         return 10;
39     }
40
41     for (i = 0; i<num; i++)
42     {
43         const char *src = (const char *) &key;
44
45         key.len = 2;
46         key.mem[0] = i << 8;
47         key.mem[1] = i & 255;
48         iscz1_encode (codec_handle, &dst, &src);
49         if (dst > dst_buf + num*10)
50         {
51             printf ("%s: i=%d size overflow\n", prog, i);
52             return 1;
53         }
54     }
55     iscz1_stop(codec_handle);
56
57     codec_handle =iscz1_start();
58
59     if (1)
60     {
61         const char *src = dst_buf;
62         for (i = 0; i<num; i++)
63         {
64             char *dst = (char *) &key;
65             const char *src0 = src;
66             iscz1_decode(codec_handle, &dst, &src);
67
68             if (key.len != 2)
69             {
70                 printf ("%s: i=%d key.len=%d expected 2\n", prog,
71                         i, key.len);
72                 while (src0 != src)
73                 {
74                     printf (" %02X (%d decimal)", *src0, *src0);
75                     src0++;
76                 }
77                 printf ("\n");
78                 return 2;
79             }
80             if (key.mem[0] != (i<<8))
81             {
82                 printf ("%s: i=%d mem[0]=" ZINT_FORMAT " expected "
83                         "%d\n", prog, i, key.mem[0], i>>8);
84                 while (src0 != src)
85                 {
86                     printf (" %02X (%d decimal)", *src0, *src0);
87                     src0++;
88                 }
89                 printf ("\n");
90                 return 3;
91             }
92             if (key.mem[1] != (i&255))
93             {
94                 printf ("%s: i=%d mem[0]=" ZINT_FORMAT " expected %d\n",
95                         prog, i, key.mem[1], i&255);
96                 while (src0 != src)
97                 {
98                     printf (" %02X (%d decimal)", *src0, *src0);
99                     src0++;
100                 }
101                 printf ("\n");
102                 return 4;
103             }
104         }
105     }
106
107     iscz1_stop(codec_handle);
108     free(dst_buf);
109     return 0;
110 }
111
112 void tstcodec1(void)
113 {
114     char buf[100];
115     char *dst = buf;
116     const char *src;
117     struct it_key key1;
118     struct it_key key2;
119     void *codec_handle =iscz1_start();
120
121     memset(&key1, 0, sizeof(key1));
122     memset(&key2, 0, sizeof(key2));
123
124     key1.len = 4;
125     key1.mem[0] = 4*65536+1016;
126     key1.mem[1] = 24339;
127     key1.mem[2] = 125060;
128     key1.mem[3] = 1;
129
130     src = (char*) &key1;
131     dst = buf;
132     iscz1_encode(codec_handle, &dst, &src);
133
134     iscz1_stop(codec_handle);
135
136     codec_handle =iscz1_start();
137
138     dst = (char*) &key2;
139     src = buf;
140
141     iscz1_decode(codec_handle, &dst, &src);
142
143     iscz1_stop(codec_handle);
144
145     if (memcmp(&key1, &key2, sizeof(key1)))
146     {
147         const char *cp1 = (char*) &key1;
148         const char *cp2 = (char*) &key2;
149         int i;
150         for (i = 0; i<sizeof(key1); i++)
151             printf ("offset=%d char1=%d char2=%d\n", i, cp1[i], cp2[i]);
152     }
153 }
154
155 int tstcodec2(int num)
156 {
157     int errors = 0;
158     int i;
159     int max = 2048;
160     int neg = 0;  /* iscz1_{en,de}code does not handle negative numbers */
161     void *encode_handle =iscz1_start();
162     void *decode_handle =iscz1_start();
163
164     srand(12);
165     for (i = 0; i<num; i++)
166     {
167         struct it_key ar1, ar2;
168         int j;
169         ar1.len = 1;
170
171         for (j = 0; j<ar1.len; j++)
172         {
173             int r = (rand() % max) - neg;
174             ar1.mem[j] = r;
175         }
176         if (1)
177         {
178             char dstbuf[100];
179             const char *src = (const char *) &ar1;
180             char *dst = dstbuf;
181             iscz1_encode(encode_handle, &dst, &src);
182
183             src = dstbuf;
184             dst = (char *) &ar2;
185             iscz1_decode(decode_handle, &dst, &src);
186         }
187
188         if (ar1.len != ar2.len)
189         {
190             printf("tstcodec2: length does not match\n");
191             errors++;
192         }
193         for (j = 0; j<ar1.len; j++)
194         {
195             if (ar1.mem[j] != ar2.mem[j])
196             {
197                 printf("diff:\n");
198                 for (j = 0; j<ar1.len; j++)
199                     printf(" %d " ZINT_FORMAT " " ZINT_FORMAT "\n",
200                            j, ar1.mem[j], ar2.mem[j]);
201                 errors++;
202                 break;
203             }
204         }
205     }
206     iscz1_stop(encode_handle);
207     iscz1_stop(decode_handle);
208     return errors;
209 }
210
211
212 int main(int argc, char **argv)
213 {
214     int num = 0;
215     int ret;
216     prog = *argv;
217     if (argc > 1)
218         num = atoi(argv[1]);
219     if (num < 1 || num > 100000000)
220         num = 10000;
221     tstcodec1();
222     ret = tstcodec2(500);
223     ret = tst_encode(num);
224     exit(ret);
225 }
226
227 /*
228  * Local variables:
229  * c-basic-offset: 4
230  * c-file-style: "Stroustrup"
231  * indent-tabs-mode: nil
232  * End:
233  * vim: shiftwidth=4 tabstop=8 expandtab
234  */
235