Omit CVS Id. Update copyright year.
[idzebra-moved-to-github.git] / test / codec / tstcodec.c
index cf19056..37562c6 100644 (file)
@@ -1,8 +1,5 @@
-/* $Id: tstcodec.c,v 1.2 2004-08-04 08:35:26 adam Exp $
-   Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002,2003,2004
-   Index Data Aps
-
-This file is part of the Zebra server.
+/* This file is part of the Zebra server.
+   Copyright (C) 1995-2008 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
@@ -15,11 +12,12 @@ 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 Zebra; see the file LICENSE.zebra.  If not, write to the
-Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
-02111-1307, USA.
+along with this program; if not, write to the Free Software
+Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+
 */
 
+#include <stdlib.h>
 #include "../../index/index.h"
 
 char *prog = "";
@@ -41,14 +39,9 @@ int tst_encode(int num)
     {
        const char *src = (const char *) &key;
 
-#if IT_KEY_NEW
        key.len = 2;
-       key.mem[0] = i >> 8;
+       key.mem[0] = i << 8;
        key.mem[1] = i & 255;
-#else
-       key.sysno = num;
-       key.seqno = 1;  
-#endif
        iscz1_encode (codec_handle, &dst, &src);
        if (dst > dst_buf + num*10)
        {
@@ -69,7 +62,6 @@ int tst_encode(int num)
            const char *src0 = src;
            iscz1_decode(codec_handle, &dst, &src);
            
-#if IT_KEY_NEW
            if (key.len != 2)
            {
                printf ("%s: i=%d key.len=%d expected 2\n", prog,
@@ -82,7 +74,7 @@ int tst_encode(int num)
                printf ("\n");
                return 2;
            }
-           if (key.mem[0] != (i>>8))
+           if (key.mem[0] != (i<<8))
            {
                printf ("%s: i=%d mem[0]=" ZINT_FORMAT " expected "
                        "%d\n", prog, i, key.mem[0], i>>8);
@@ -106,9 +98,6 @@ int tst_encode(int num)
                printf ("\n");
                return 4;
            }
-#else
-
-#endif
        }
     }
 
@@ -117,14 +106,126 @@ int tst_encode(int num)
     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;
-    exit(tst_encode(num));
+    tstcodec1();
+    ret = tstcodec2(500);
+    ret = tst_encode(num);
+    exit(ret);
 }
     
+/*
+ * Local variables:
+ * c-basic-offset: 4
+ * indent-tabs-mode: nil
+ * End:
+ * vim: shiftwidth=4 tabstop=8 expandtab
+ */
+