+#include <yaz/test.h>
+
+static int compare_buffers(char *msg, int no,
+ int expect_len, const char *expect_buf,
+ int got_len, const char *got_buf)
+{
+ if (expect_len == got_len
+ && !memcmp(expect_buf, got_buf, expect_len))
+ return 1;
+
+ if (0) /* use 1 see how the buffers differ (for debug purposes) */
+ {
+ int i;
+ printf("tsticonv test=%s i=%d failed\n", msg, no);
+ printf("off got exp\n");
+ for (i = 0; i<got_len || i<expect_len; i++)
+ {
+ char got_char[10];
+ char expect_char[10];
+
+ if (i < got_len)
+ sprintf(got_char, "%02X", got_buf[i]);
+ else
+ sprintf(got_char, "? ");
+
+ if (i < expect_len)
+ sprintf(expect_char, "%02X", expect_buf[i]);
+ else
+ sprintf(expect_char, "? ");
+
+ printf("%02d %s %s %c\n",
+ i, got_char, expect_char, got_buf[i] == expect_buf[i] ?
+ ' ' : '*');
+
+ }
+ }
+ return 0;
+}
+
+static int tst_convert_l(yaz_iconv_t cd, size_t in_len, const char *in_buf,
+ size_t expect_len, const char *expect_buf)
+{
+ size_t r;
+ char *inbuf= (char*) in_buf;
+ size_t inbytesleft = in_len > 0 ? in_len : strlen(in_buf);
+ char outbuf0[64];
+ char *outbuf = outbuf0;
+
+ while (inbytesleft)
+ {
+ size_t outbytesleft = outbuf0 + sizeof(outbuf0) - outbuf;
+ if (outbytesleft > 12)
+ outbytesleft = 12;
+ r = yaz_iconv(cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
+ if (r == (size_t) (-1))
+ {
+ int e = yaz_iconv_error(cd);
+ if (e != YAZ_ICONV_E2BIG)
+ return 0;
+ }
+ else
+ break;
+ }
+ return compare_buffers("tsticonv 22", 0,
+ expect_len, expect_buf,
+ outbuf - outbuf0, outbuf0);
+}
+
+static int tst_convert(yaz_iconv_t cd, const char *buf, const char *cmpbuf)
+{
+ int ret = 0;
+ WRBUF b = wrbuf_alloc();
+ char outbuf[12];
+ size_t inbytesleft = strlen(buf);
+ const char *inp = buf;
+ int rounds = 0;
+ for (rounds = 0; inbytesleft && rounds < sizeof(outbuf); rounds++)
+ {
+ size_t outbytesleft = sizeof(outbuf);
+ char *outp = outbuf;
+ size_t r = yaz_iconv(cd, (char**) &inp, &inbytesleft,
+ &outp, &outbytesleft);
+ wrbuf_write(b, outbuf, outp - outbuf);
+ if (r == (size_t) (-1))
+ {
+ int e = yaz_iconv_error(cd);
+ if (e != YAZ_ICONV_E2BIG)
+ break;
+ }
+ }
+ if (wrbuf_len(b) == strlen(cmpbuf)
+ && !memcmp(cmpbuf, wrbuf_buf(b), wrbuf_len(b)))
+ ret = 1;
+ else
+ yaz_log(YLOG_LOG, "GOT (%.*s)", wrbuf_len(b), wrbuf_buf(b));
+ wrbuf_free(b, 1);
+ return ret;
+}
+