Omit CVS Id. Update copyright year.
[idzebra-moved-to-github.git] / test / codec / tstcodec.c
1 /* This file is part of the Zebra server.
2    Copyright (C) 1995-2008 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 #include <stdlib.h>
21 #include "../../index/index.h"
22
23 char *prog = "";
24
25 int tst_encode(int num)
26 {
27     struct it_key key;
28     int i;
29     void *codec_handle =iscz1_start();
30     char *dst_buf = malloc(200 + num * 10);
31     char *dst = dst_buf;
32     if (!dst_buf)
33     {
34         printf ("%s: out of memory (num=%d)\n", prog, num);
35         return 10;
36     }
37
38     for (i = 0; i<num; i++)
39     {
40         const char *src = (const char *) &key;
41
42         key.len = 2;
43         key.mem[0] = i << 8;
44         key.mem[1] = i & 255;
45         iscz1_encode (codec_handle, &dst, &src);
46         if (dst > dst_buf + num*10)
47         {
48             printf ("%s: i=%d size overflow\n", prog, i);
49             return 1;
50         }
51     }
52     iscz1_stop(codec_handle);
53
54     codec_handle =iscz1_start();
55
56     if (1)
57     {
58         const char *src = dst_buf;
59         for (i = 0; i<num; i++)
60         {
61             char *dst = (char *) &key;
62             const char *src0 = src;
63             iscz1_decode(codec_handle, &dst, &src);
64             
65             if (key.len != 2)
66             {
67                 printf ("%s: i=%d key.len=%d expected 2\n", prog,
68                         i, key.len);
69                 while (src0 != src)
70                 {
71                     printf (" %02X (%d decimal)", *src0, *src0);
72                     src0++;
73                 }
74                 printf ("\n");
75                 return 2;
76             }
77             if (key.mem[0] != (i<<8))
78             {
79                 printf ("%s: i=%d mem[0]=" ZINT_FORMAT " expected "
80                         "%d\n", prog, i, key.mem[0], i>>8);
81                 while (src0 != src)
82                 {
83                     printf (" %02X (%d decimal)", *src0, *src0);
84                     src0++;
85                 }
86                 printf ("\n");
87                 return 3;
88             }
89             if (key.mem[1] != (i&255))
90             {
91                 printf ("%s: i=%d mem[0]=" ZINT_FORMAT " expected %d\n",
92                         prog, i, key.mem[1], i&255);
93                 while (src0 != src)
94                 {
95                     printf (" %02X (%d decimal)", *src0, *src0);
96                     src0++;
97                 }
98                 printf ("\n");
99                 return 4;
100             }
101         }
102     }
103
104     iscz1_stop(codec_handle);
105     free(dst_buf);
106     return 0;
107 }
108
109 void tstcodec1(void)
110 {
111     char buf[100];
112     char *dst = buf;
113     const char *src;
114     struct it_key key1;
115     struct it_key key2;
116     void *codec_handle =iscz1_start();
117
118     memset(&key1, 0, sizeof(key1));
119     memset(&key2, 0, sizeof(key2));
120
121     key1.len = 4;
122     key1.mem[0] = 4*65536+1016;
123     key1.mem[1] = 24339;
124     key1.mem[2] = 125060;
125     key1.mem[3] = 1;
126
127     src = (char*) &key1;
128     dst = buf;
129     iscz1_encode(codec_handle, &dst, &src);
130
131     iscz1_stop(codec_handle);
132
133     codec_handle =iscz1_start();
134
135     dst = (char*) &key2;
136     src = buf;
137     
138     iscz1_decode(codec_handle, &dst, &src);
139
140     iscz1_stop(codec_handle);
141
142     if (memcmp(&key1, &key2, sizeof(key1)))
143     {
144         const char *cp1 = (char*) &key1;
145         const char *cp2 = (char*) &key2;
146         int i;
147         for (i = 0; i<sizeof(key1); i++)
148             printf ("offset=%d char1=%d char2=%d\n", i, cp1[i], cp2[i]);
149     }
150 }
151
152 int tstcodec2(int num)
153 {
154     int errors = 0;
155     int i;
156     int max = 2048;
157     int neg = 0;  /* iscz1_{en,de}code does not handle negative numbers */
158     void *encode_handle =iscz1_start();
159     void *decode_handle =iscz1_start();
160
161     srand(12);
162     for (i = 0; i<num; i++)
163     {
164         struct it_key ar1, ar2;
165         int j;
166         ar1.len = 1;
167
168         for (j = 0; j<ar1.len; j++)
169         {
170             int r = (rand() % max) - neg;
171             ar1.mem[j] = r;
172         }
173         if (1)
174         {
175             char dstbuf[100];
176             const char *src = (const char *) &ar1;
177             char *dst = dstbuf;
178             iscz1_encode(encode_handle, &dst, &src);
179             
180             src = dstbuf;
181             dst = (char *) &ar2;
182             iscz1_decode(decode_handle, &dst, &src);
183         }
184
185         if (ar1.len != ar2.len)
186         {
187             printf("tstcodec2: length does not match\n");
188             errors++;
189         }
190         for (j = 0; j<ar1.len; j++)
191         {
192             if (ar1.mem[j] != ar2.mem[j])
193             {
194                 printf("diff:\n");
195                 for (j = 0; j<ar1.len; j++)
196                     printf(" %d " ZINT_FORMAT " " ZINT_FORMAT "\n",
197                            j, ar1.mem[j], ar2.mem[j]);
198                 errors++;
199                 break;
200             }
201         }
202     }
203     iscz1_stop(encode_handle);
204     iscz1_stop(decode_handle);
205     return errors;
206 }
207
208
209 int main(int argc, char **argv)
210 {
211     int num = 0;
212     int ret;
213     prog = *argv;
214     if (argc > 1)
215         num = atoi(argv[1]);
216     if (num < 1 || num > 100000000)
217         num = 10000;
218     tstcodec1();
219     ret = tstcodec2(500);
220     ret = tst_encode(num);
221     exit(ret);
222 }
223     
224 /*
225  * Local variables:
226  * c-basic-offset: 4
227  * indent-tabs-mode: nil
228  * End:
229  * vim: shiftwidth=4 tabstop=8 expandtab
230  */
231