Test cases for nmem, iconv
authorAdam Dickmeiss <adam@indexdata.dk>
Wed, 23 Apr 2003 20:34:08 +0000 (20:34 +0000)
committerAdam Dickmeiss <adam@indexdata.dk>
Wed, 23 Apr 2003 20:34:08 +0000 (20:34 +0000)
util/Makefile.am
util/siconvtst.c [deleted file]
util/tsticonv.c [new file with mode: 0644]
util/tstnmem.c [new file with mode: 0644]
util/yaziconv.c [new file with mode: 0644]

index 237aa9c..ce94b0d 100644 (file)
@@ -1,10 +1,10 @@
 ## Copyright (C) 1994-2003, Index Data
 ## All rights reserved.
-## $Id: Makefile.am,v 1.16 2003-02-18 14:28:52 adam Exp $
+## $Id: Makefile.am,v 1.17 2003-04-23 20:34:08 adam Exp $
 
 noinst_LTLIBRARIES = libutil.la
 
-#lib_LTLIBRARIES = libyazthread.la
+TESTS = tsticonv tstnmem
 
 bin_SCRIPTS = yaz-comp
 
@@ -14,11 +14,19 @@ AM_CPPFLAGS=-I$(top_srcdir)/include
 
 noinst_PROGRAMS = marcdump yaziconv
 
+EXTRA_PROGRAMS = tsticonv tstnmem
+
 marcdump_LDADD = libutil.la 
 marcdump_SOURCES = marcdump.c
 
 yaziconv_LDADD = libutil.la 
-yaziconv_SOURCES = siconvtst.c
+yaziconv_SOURCES = yaziconv.c
+
+tsticonv_LDADD = libutil.la
+tsticonv_SOURCES = tsticonv.c
+
+tstnmem_LDADD = libutil.la
+tstnmem_SOURCES = tstnmem.c
 
 marc8.c: charconv.sgm charconv.tcl
        cd $(srcdir); ./charconv.tcl -p marc8 -s 50 charconv.sgm marc8.c
diff --git a/util/siconvtst.c b/util/siconvtst.c
deleted file mode 100644 (file)
index 9f7038e..0000000
+++ /dev/null
@@ -1,181 +0,0 @@
-/*
- * Copyright (c) 1997-2003, Index Data
- * See the file LICENSE for details.
- *
- * $Id: siconvtst.c,v 1.8 2003-01-06 08:20:28 adam Exp $
- */
-
-#if HAVE_CONFIG_H
-#include <config.h>
-#endif
-
-#include <errno.h>
-#include <string.h>
-#include <ctype.h>
-
-#include <yaz/yaz-util.h>
-
-#define CHUNK_IN 64
-#define CHUNK_OUT 64
-
-void convert (FILE *inf, yaz_iconv_t cd, int verbose)
-{
-    char inbuf0[CHUNK_IN], *inbuf = inbuf0;
-    char outbuf0[CHUNK_OUT], *outbuf = outbuf0;
-    size_t inbytesleft = CHUNK_IN;
-    size_t outbytesleft = CHUNK_OUT;
-    int mustread = 1;
-
-    while (1)
-    {
-        size_t r;
-        if (mustread)
-        {
-            r = fread (inbuf, 1, inbytesleft, inf);
-            if (inbytesleft != r)
-            {
-                if (ferror(inf))
-                {
-                    fprintf (stderr, "yaziconv: error reading file\n");
-                    exit (6);
-                }
-                if (r == 0)
-                {
-                    if (outbuf != outbuf0)
-                        fwrite (outbuf0, 1, outbuf - outbuf0, stdout);
-                    break;
-                }
-                inbytesleft = r;
-            }
-        }
-        if (verbose > 1)
-        {
-            fprintf (stderr, "yaz_iconv: inbytesleft=%d outbytesleft=%d\n",
-                     inbytesleft, outbytesleft);
-
-        }
-        r = yaz_iconv (cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
-        if (r == (size_t)(-1))
-        {
-            int e = yaz_iconv_error(cd);
-            if (e == YAZ_ICONV_EILSEQ)
-            {
-                fprintf (stderr, "invalid sequence\n");
-                return ;
-            }
-            else if (e == YAZ_ICONV_EINVAL) /* incomplete input */
-            { 
-                size_t i;
-                for (i = 0; i<inbytesleft; i++)
-                    inbuf0[i] = inbuf[i];
-
-                r = fread(inbuf0 + i, 1, CHUNK_IN - i, inf);
-                if (r != CHUNK_IN - i)
-                {
-                    if (ferror(inf))
-                    {
-                        fprintf (stderr, "yaziconv: error reading file\n");
-                        exit(6);
-                    }
-                }
-                if (r == 0)
-                {
-                    fprintf (stderr, "invalid sequence\n");
-                    return ;
-                }
-                inbytesleft += r;
-                inbuf = inbuf0;
-                mustread = 0;
-            }
-            else if (e == YAZ_ICONV_E2BIG) /* no more output space */
-            {
-                fwrite (outbuf0, 1, outbuf - outbuf0, stdout);
-                outbuf = outbuf0;
-                outbytesleft = CHUNK_OUT;
-                mustread = 0;
-            }
-            else
-            {
-                fprintf (stderr, "yaziconv: unknown error\n");
-                exit (7);
-            }
-        }
-        else
-        {
-            inbuf = inbuf0;
-            inbytesleft = CHUNK_IN;
-
-            fwrite (outbuf0, 1, outbuf - outbuf0, stdout);
-            outbuf = outbuf0;
-            outbytesleft = CHUNK_OUT;
-
-            mustread = 1;
-        }
-    }
-}
-
-int main (int argc, char **argv)
-{
-    int ret;
-    int verbose = 0;
-    char *from = 0;
-    char *to = 0;
-    char *arg;
-    yaz_iconv_t cd;
-    FILE *inf = stdin;
-
-    while ((ret = options ("vf:t:", argv, argc, &arg)) != -2)
-    {
-        switch (ret)
-        {
-        case 0:
-            inf = fopen (arg, "rb");
-            if (!inf)
-            {
-                fprintf (stderr, "yaziconv: cannot open %s", arg);
-                exit (2);
-            }
-            break;
-        case 'f':
-            from = arg;
-            break;
-        case 't':
-            to = arg;
-            break;
-        case 'v':
-            verbose++;
-            break;
-        default:
-            fprintf (stderr, "yaziconv: Usage\n"
-                     "siconv -f encoding -t encoding [-v] [file]\n");
-            exit(1);
-        }
-    }
-    if (!to)
-    {
-        fprintf (stderr, "yaziconv: -t encoding missing\n");
-        exit (3);
-    }
-    if (!from)
-    {
-        fprintf (stderr, "yaziconv: -f encoding missing\n");
-        exit (4);
-    }
-    cd = yaz_iconv_open (to, from);
-    if (!cd)
-    {
-        fprintf (stderr, "yaziconv: unsupported encoding\n");
-        exit (5);
-    }
-    else
-    {
-        if (verbose)
-        {
-            fprintf (stderr, "yaziconv: using %s\n",
-                     yaz_iconv_isbuiltin(cd) ? "YAZ" : "iconv");
-        }
-    }
-    convert (inf, cd, verbose);
-    yaz_iconv_close (cd);
-    return 0;
-}
diff --git a/util/tsticonv.c b/util/tsticonv.c
new file mode 100644 (file)
index 0000000..da88d7b
--- /dev/null
@@ -0,0 +1,94 @@
+/*
+ * Copyright (c) 2002-2003, Index Data
+ * See the file LICENSE for details.
+ *
+ * $Id: tsticonv.c,v 1.1 2003-04-23 20:34:08 adam Exp $
+ */
+
+#if HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <errno.h>
+#include <string.h>
+#include <ctype.h>
+
+#include <yaz/yaz-util.h>
+
+const char *buf[] = {
+       "ax" ,
+       "\330",
+       "eneb\346r",
+               0 };
+
+static dconvert(int mandatory, const char *tmpcode)
+{
+    int i;
+    yaz_iconv_t cd;
+    for (i = 0; buf[i]; i++)
+    {
+        int j;
+        size_t r;
+       char *inbuf = (char*) buf[i];
+       size_t inbytesleft = strlen(inbuf);
+       char outbuf0[24];
+       char outbuf1[10];
+       char *outbuf = outbuf0;
+       size_t outbytesleft = sizeof(outbuf0);
+
+        cd = yaz_iconv_open(tmpcode, "ISO-8859-1");
+       if (!cd)
+        {
+            if (!mandatory)
+                return;
+            printf ("tsticonv 1\n");
+           exit(1);
+        }
+       r = yaz_iconv(cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
+       if (r == (size_t)(-1))
+        {
+            int e = yaz_iconv_error(cd);
+
+            printf ("tsticonv 2 e=%d\n", e);
+           exit(2);
+        }
+       yaz_iconv_close(cd);
+        
+       cd = yaz_iconv_open("ISO-8859-1", tmpcode);
+       if (!cd)
+        {
+            if (!mandatory)
+                return;
+            printf ("tsticonv 3\n");
+           exit(3);
+        }
+       inbuf = outbuf0;
+       inbytesleft = sizeof(outbuf0) - outbytesleft;
+
+       outbuf = outbuf1;
+       outbytesleft = sizeof(outbuf1);
+       r = yaz_iconv(cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
+       if (r == (size_t)(-1)) {
+            int e = yaz_iconv_error(cd);
+
+            printf ("tsticonv 4 e=%d\n", e);
+           exit(4);
+       }
+       if (strlen(buf[i]) == (sizeof(outbuf1) - outbytesleft) &&
+            memcmp(outbuf1, buf[i], strlen(buf[i])))
+        {
+            printf ("tsticonv 5\n");
+            exit(5);
+       }
+       yaz_iconv_close(cd);
+    }
+}
+       
+int main (int argc, char **argv)
+{
+    dconvert(1, "UTF-8");
+    dconvert(1, "ISO-8859-1");
+    dconvert(1, "UCS4");
+    dconvert(0, "CP865");
+    exit (0);
+}
diff --git a/util/tstnmem.c b/util/tstnmem.c
new file mode 100644 (file)
index 0000000..c8b0569
--- /dev/null
@@ -0,0 +1,44 @@
+/*
+ * Copyright (c) 2002-2003, Index Data
+ * See the file LICENSE for details.
+ *
+ * $Id: tstnmem.c,v 1.1 2003-04-23 20:34:08 adam Exp $
+ */
+
+#if HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <errno.h>
+#include <string.h>
+#include <ctype.h>
+
+#include <yaz/nmem.h>
+
+int main (int argc, char **argv)
+{
+    void *cp;
+    NMEM n;
+    int j;
+
+    nmem_init();
+    n = nmem_create();
+    if (!n)
+        exit (1);
+    for (j = 1; j<500; j++)
+    {
+        cp = nmem_malloc(n, j);
+        if (!cp)
+            exit(2);
+    }
+    
+    for (j = 2000; j<20000; j+= 2000)
+    {
+        cp = nmem_malloc(n, j);
+        if (!cp)
+            exit(3);
+    }
+    nmem_destroy(n);
+    nmem_exit();
+    exit(0);
+}
diff --git a/util/yaziconv.c b/util/yaziconv.c
new file mode 100644 (file)
index 0000000..958aa9a
--- /dev/null
@@ -0,0 +1,181 @@
+/*
+ * Copyright (c) 1997-2003, Index Data
+ * See the file LICENSE for details.
+ *
+ * $Id: yaziconv.c,v 1.1 2003-04-23 20:34:08 adam Exp $
+ */
+
+#if HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <errno.h>
+#include <string.h>
+#include <ctype.h>
+
+#include <yaz/yaz-util.h>
+
+#define CHUNK_IN 64
+#define CHUNK_OUT 64
+
+void convert (FILE *inf, yaz_iconv_t cd, int verbose)
+{
+    char inbuf0[CHUNK_IN], *inbuf = inbuf0;
+    char outbuf0[CHUNK_OUT], *outbuf = outbuf0;
+    size_t inbytesleft = CHUNK_IN;
+    size_t outbytesleft = CHUNK_OUT;
+    int mustread = 1;
+
+    while (1)
+    {
+        size_t r;
+        if (mustread)
+        {
+            r = fread (inbuf, 1, inbytesleft, inf);
+            if (inbytesleft != r)
+            {
+                if (ferror(inf))
+                {
+                    fprintf (stderr, "yaziconv: error reading file\n");
+                    exit (6);
+                }
+                if (r == 0)
+                {
+                    if (outbuf != outbuf0)
+                        fwrite (outbuf0, 1, outbuf - outbuf0, stdout);
+                    break;
+                }
+                inbytesleft = r;
+            }
+        }
+        if (verbose > 1)
+        {
+            fprintf (stderr, "yaz_iconv: inbytesleft=%d outbytesleft=%d\n",
+                     inbytesleft, outbytesleft);
+
+        }
+        r = yaz_iconv (cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
+        if (r == (size_t)(-1))
+        {
+            int e = yaz_iconv_error(cd);
+            if (e == YAZ_ICONV_EILSEQ)
+            {
+                fprintf (stderr, "invalid sequence\n");
+                return ;
+            }
+            else if (e == YAZ_ICONV_EINVAL) /* incomplete input */
+            { 
+                size_t i;
+                for (i = 0; i<inbytesleft; i++)
+                    inbuf0[i] = inbuf[i];
+
+                r = fread(inbuf0 + i, 1, CHUNK_IN - i, inf);
+                if (r != CHUNK_IN - i)
+                {
+                    if (ferror(inf))
+                    {
+                        fprintf (stderr, "yaziconv: error reading file\n");
+                        exit(6);
+                    }
+                }
+                if (r == 0)
+                {
+                    fprintf (stderr, "invalid sequence\n");
+                    return ;
+                }
+                inbytesleft += r;
+                inbuf = inbuf0;
+                mustread = 0;
+            }
+            else if (e == YAZ_ICONV_E2BIG) /* no more output space */
+            {
+                fwrite (outbuf0, 1, outbuf - outbuf0, stdout);
+                outbuf = outbuf0;
+                outbytesleft = CHUNK_OUT;
+                mustread = 0;
+            }
+            else
+            {
+                fprintf (stderr, "yaziconv: unknown error\n");
+                exit (7);
+            }
+        }
+        else
+        {
+            inbuf = inbuf0;
+            inbytesleft = CHUNK_IN;
+
+            fwrite (outbuf0, 1, outbuf - outbuf0, stdout);
+            outbuf = outbuf0;
+            outbytesleft = CHUNK_OUT;
+
+            mustread = 1;
+        }
+    }
+}
+
+int main (int argc, char **argv)
+{
+    int ret;
+    int verbose = 0;
+    char *from = 0;
+    char *to = 0;
+    char *arg;
+    yaz_iconv_t cd;
+    FILE *inf = stdin;
+
+    while ((ret = options ("vf:t:", argv, argc, &arg)) != -2)
+    {
+        switch (ret)
+        {
+        case 0:
+            inf = fopen (arg, "rb");
+            if (!inf)
+            {
+                fprintf (stderr, "yaziconv: cannot open %s", arg);
+                exit (2);
+            }
+            break;
+        case 'f':
+            from = arg;
+            break;
+        case 't':
+            to = arg;
+            break;
+        case 'v':
+            verbose++;
+            break;
+        default:
+            fprintf (stderr, "yaziconv: Usage\n"
+                     "siconv -f encoding -t encoding [-v] [file]\n");
+            exit(1);
+        }
+    }
+    if (!to)
+    {
+        fprintf (stderr, "yaziconv: -t encoding missing\n");
+        exit (3);
+    }
+    if (!from)
+    {
+        fprintf (stderr, "yaziconv: -f encoding missing\n");
+        exit (4);
+    }
+    cd = yaz_iconv_open (to, from);
+    if (!cd)
+    {
+        fprintf (stderr, "yaziconv: unsupported encoding\n");
+        exit (5);
+    }
+    else
+    {
+        if (verbose)
+        {
+            fprintf (stderr, "yaziconv: using %s\n",
+                     yaz_iconv_isbuiltin(cd) ? "YAZ" : "iconv");
+        }
+    }
+    convert (inf, cd, verbose);
+    yaz_iconv_close (cd);
+    return 0;
+}