c2bc234aefb407e777452154edae344a9a70bf0e
[idzebra-moved-to-github.git] / test / codec / tstcodec.c
1 /* $Id: tstcodec.c,v 1.5 2005-01-02 20:11:46 adam Exp $
2    Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002,2003,2004
3    Index Data Aps
4
5 This file is part of the Zebra server.
6
7 Zebra is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
11
12 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with Zebra; see the file LICENSE.zebra.  If not, write to the
19 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA.
21 */
22
23 #include "../../index/index.h"
24
25 char *prog = "";
26
27 int tst_encode(int num)
28 {
29     struct it_key key;
30     int i;
31     void *codec_handle =iscz1_start();
32     char *dst_buf = malloc(200 + num * 10);
33     char *dst = dst_buf;
34     if (!dst_buf)
35     {
36         printf ("%s: out of memory (num=%d)\n", prog, num);
37         return 10;
38     }
39
40     for (i = 0; i<num; i++)
41     {
42         const char *src = (const char *) &key;
43
44         key.len = 2;
45         key.mem[0] = i << 8;
46         key.mem[1] = i & 255;
47         iscz1_encode (codec_handle, &dst, &src);
48         if (dst > dst_buf + num*10)
49         {
50             printf ("%s: i=%d size overflow\n", prog, i);
51             return 1;
52         }
53     }
54     iscz1_stop(codec_handle);
55
56     codec_handle =iscz1_start();
57
58     if (1)
59     {
60         const char *src = dst_buf;
61         for (i = 0; i<num; i++)
62         {
63             char *dst = (char *) &key;
64             const char *src0 = src;
65             iscz1_decode(codec_handle, &dst, &src);
66             
67             if (key.len != 2)
68             {
69                 printf ("%s: i=%d key.len=%d expected 2\n", prog,
70                         i, key.len);
71                 while (src0 != src)
72                 {
73                     printf (" %02X (%d decimal)", *src0, *src0);
74                     src0++;
75                 }
76                 printf ("\n");
77                 return 2;
78             }
79             if (key.mem[0] != (i<<8))
80             {
81                 printf ("%s: i=%d mem[0]=" ZINT_FORMAT " expected "
82                         "%d\n", prog, i, key.mem[0], i>>8);
83                 while (src0 != src)
84                 {
85                     printf (" %02X (%d decimal)", *src0, *src0);
86                     src0++;
87                 }
88                 printf ("\n");
89                 return 3;
90             }
91             if (key.mem[1] != (i&255))
92             {
93                 printf ("%s: i=%d mem[0]=" ZINT_FORMAT " expected %d\n",
94                         prog, i, key.mem[1], i&255);
95                 while (src0 != src)
96                 {
97                     printf (" %02X (%d decimal)", *src0, *src0);
98                     src0++;
99                 }
100                 printf ("\n");
101                 return 4;
102             }
103         }
104     }
105
106     iscz1_stop(codec_handle);
107     free(dst_buf);
108     return 0;
109 }
110
111 void tstcodec1()
112 {
113     char buf[100];
114     char *dst = buf;
115     const char *src;
116     struct it_key key1;
117     struct it_key key2;
118     void *codec_handle =iscz1_start();
119
120     memset(&key1, 0, sizeof(key1));
121     memset(&key2, 0, sizeof(key2));
122
123     key1.len = 4;
124     key1.mem[0] = 4*65536+1016;
125     key1.mem[1] = 24339;
126     key1.mem[2] = 125060;
127     key1.mem[3] = 1;
128
129     src = (char*) &key1;
130     dst = buf;
131     iscz1_encode(codec_handle, &dst, &src);
132
133     iscz1_stop(codec_handle);
134
135     codec_handle =iscz1_start();
136
137     dst = (char*) &key2;
138     src = buf;
139     
140     iscz1_decode(codec_handle, &dst, &src);
141
142     iscz1_stop(codec_handle);
143
144     if (memcmp(&key1, &key2, sizeof(key1)))
145     {
146         const char *cp1 = (char*) &key1;
147         const char *cp2 = (char*) &key2;
148         int i;
149         for (i = 0; i<sizeof(key1); i++)
150             printf ("offset=%d char1=%d char2=%d\n", i, cp1[i], cp2[i]);
151     }
152 }
153     
154
155 int main(int argc, char **argv)
156 {
157     int num = 0;
158     prog = *argv;
159     if (argc > 1)
160         num = atoi(argv[1]);
161     if (num < 1 || num > 100000000)
162         num = 10000;
163     tstcodec1();
164     exit(tst_encode(num));
165 }
166