File: tstcodec.c

package info (click to toggle)
idzebra 2.2.8-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 10,572 kB
  • sloc: ansic: 54,389; xml: 27,058; sh: 5,892; makefile: 1,102; perl: 210; tcl: 64
file content (235 lines) | stat: -rw-r--r-- 5,777 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/* This file is part of the Zebra server.
   Copyright (C) Index Data

Zebra is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free
Software Foundation; either version 2, or (at your option) any later
version.

Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

*/

#if HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdlib.h>
#include "../../index/index.h"

char *prog = "";

int tst_encode(int num)
{
    struct it_key key;
    int i;
    void *codec_handle =iscz1_start();
    char *dst_buf = malloc(200 + num * 10);
    char *dst = dst_buf;
    if (!dst_buf)
    {
        printf ("%s: out of memory (num=%d)\n", prog, num);
        return 10;
    }

    for (i = 0; i<num; i++)
    {
        const char *src = (const char *) &key;

        key.len = 2;
        key.mem[0] = i << 8;
        key.mem[1] = i & 255;
        iscz1_encode (codec_handle, &dst, &src);
        if (dst > dst_buf + num*10)
        {
            printf ("%s: i=%d size overflow\n", prog, i);
            return 1;
        }
    }
    iscz1_stop(codec_handle);

    codec_handle =iscz1_start();

    if (1)
    {
        const char *src = dst_buf;
        for (i = 0; i<num; i++)
        {
            char *dst = (char *) &key;
            const char *src0 = src;
            iscz1_decode(codec_handle, &dst, &src);

            if (key.len != 2)
            {
                printf ("%s: i=%d key.len=%d expected 2\n", prog,
                        i, key.len);
                while (src0 != src)
                {
                    printf (" %02X (%d decimal)", *src0, *src0);
                    src0++;
                }
                printf ("\n");
                return 2;
            }
            if (key.mem[0] != (i<<8))
            {
                printf ("%s: i=%d mem[0]=" ZINT_FORMAT " expected "
                        "%d\n", prog, i, key.mem[0], i>>8);
                while (src0 != src)
                {
                    printf (" %02X (%d decimal)", *src0, *src0);
                    src0++;
                }
                printf ("\n");
                return 3;
            }
            if (key.mem[1] != (i&255))
            {
                printf ("%s: i=%d mem[0]=" ZINT_FORMAT " expected %d\n",
                        prog, i, key.mem[1], i&255);
                while (src0 != src)
                {
                    printf (" %02X (%d decimal)", *src0, *src0);
                    src0++;
                }
                printf ("\n");
                return 4;
            }
        }
    }

    iscz1_stop(codec_handle);
    free(dst_buf);
    return 0;
}

void tstcodec1(void)
{
    char buf[100];
    char *dst = buf;
    const char *src;
    struct it_key key1;
    struct it_key key2;
    void *codec_handle =iscz1_start();

    memset(&key1, 0, sizeof(key1));
    memset(&key2, 0, sizeof(key2));

    key1.len = 4;
    key1.mem[0] = 4*65536+1016;
    key1.mem[1] = 24339;
    key1.mem[2] = 125060;
    key1.mem[3] = 1;

    src = (char*) &key1;
    dst = buf;
    iscz1_encode(codec_handle, &dst, &src);

    iscz1_stop(codec_handle);

    codec_handle =iscz1_start();

    dst = (char*) &key2;
    src = buf;

    iscz1_decode(codec_handle, &dst, &src);

    iscz1_stop(codec_handle);

    if (memcmp(&key1, &key2, sizeof(key1)))
    {
        const char *cp1 = (char*) &key1;
        const char *cp2 = (char*) &key2;
        int i;
        for (i = 0; i<sizeof(key1); i++)
            printf ("offset=%d char1=%d char2=%d\n", i, cp1[i], cp2[i]);
    }
}

int tstcodec2(int num)
{
    int errors = 0;
    int i;
    int max = 2048;
    int neg = 0;  /* iscz1_{en,de}code does not handle negative numbers */
    void *encode_handle =iscz1_start();
    void *decode_handle =iscz1_start();

    srand(12);
    for (i = 0; i<num; i++)
    {
        struct it_key ar1, ar2;
        int j;
        ar1.len = 1;

        for (j = 0; j<ar1.len; j++)
        {
            int r = (rand() % max) - neg;
            ar1.mem[j] = r;
        }
        if (1)
        {
            char dstbuf[100];
            const char *src = (const char *) &ar1;
            char *dst = dstbuf;
            iscz1_encode(encode_handle, &dst, &src);

            src = dstbuf;
            dst = (char *) &ar2;
            iscz1_decode(decode_handle, &dst, &src);
        }

        if (ar1.len != ar2.len)
        {
            printf("tstcodec2: length does not match\n");
            errors++;
        }
        for (j = 0; j<ar1.len; j++)
        {
            if (ar1.mem[j] != ar2.mem[j])
            {
                printf("diff:\n");
                for (j = 0; j<ar1.len; j++)
                    printf(" %d " ZINT_FORMAT " " ZINT_FORMAT "\n",
                           j, ar1.mem[j], ar2.mem[j]);
                errors++;
                break;
            }
        }
    }
    iscz1_stop(encode_handle);
    iscz1_stop(decode_handle);
    return errors;
}


int main(int argc, char **argv)
{
    int num = 0;
    int ret;
    prog = *argv;
    if (argc > 1)
        num = atoi(argv[1]);
    if (num < 1 || num > 100000000)
        num = 10000;
    tstcodec1();
    ret = tstcodec2(500);
    ret = tst_encode(num);
    exit(ret);
}

/*
 * Local variables:
 * c-basic-offset: 4
 * c-file-style: "Stroustrup"
 * indent-tabs-mode: nil
 * End:
 * vim: shiftwidth=4 tabstop=8 expandtab
 */