chiark / gitweb /
math/mptext.c: Reformat and refactor output functions.
[catacomb] / math / mptext.c
1 /* -*-c-*-
2  *
3  * Textual representation of multiprecision numbers
4  *
5  * (c) 1999 Straylight/Edgeware
6  */
7
8 /*----- Licensing notice --------------------------------------------------*
9  *
10  * This file is part of Catacomb.
11  *
12  * Catacomb is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU Library General Public License as
14  * published by the Free Software Foundation; either version 2 of the
15  * License, or (at your option) any later version.
16  *
17  * Catacomb is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU Library General Public License for more details.
21  *
22  * You should have received a copy of the GNU Library General Public
23  * License along with Catacomb; if not, write to the Free
24  * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
25  * MA 02111-1307, USA.
26  */
27
28 /*----- Header files ------------------------------------------------------*/
29
30 #include <ctype.h>
31 #include <limits.h>
32 #include <stdio.h>
33
34 #include "mp.h"
35 #include "mptext.h"
36 #include "paranoia.h"
37
38 /*----- Magical numbers ---------------------------------------------------*/
39
40 /* --- Maximum recursion depth --- *
41  *
42  * This is the number of bits in a @size_t@ object.  Why?
43  *
44  * To see this, let %$b = \textit{MPW\_MAX} + 1$% and let %$Z$% be the
45  * largest @size_t@ value.  Then the largest possible @mp@ is %$M - 1$% where
46  * %$M = b^Z$%.  Let %$r$% be a radix to read or write.  Since the recursion
47  * squares the radix at each step, the highest number reached by the
48  * recursion is %$d$%, where:
49  *
50  *   %$r^{2^d} = b^Z$%.
51  *
52  * Solving gives that %$d = \lg \log_r b^Z$%.  If %$r = 2$%, this is maximum,
53  * so choosing %$d = \lg \lg b^Z = \lg (Z \lg b) = \lg Z + \lg \lg b$%.
54  *
55  * Expressing %$\lg Z$% as @CHAR_BIT * sizeof(size_t)@ yields an
56  * overestimate, since a @size_t@ representation may contain `holes'.
57  * Choosing to represent %$\lg \lg b$% by 10 is almost certainly sufficient
58  * for `some time to come'.
59  */
60
61 #define DEPTH (CHAR_BIT * sizeof(size_t) + 10)
62
63 /*----- Input -------------------------------------------------------------*/
64
65 /* --- @mp_read@ --- *
66  *
67  * Arguments:   @mp *m@ = destination multiprecision number
68  *              @int radix@ = base to assume for data (or zero to guess)
69  *              @const mptext_ops *ops@ = pointer to operations block
70  *              @void *p@ = data for the operations block
71  *
72  * Returns:     The integer read, or zero if it didn't work.
73  *
74  * Use:         Reads an integer from some source.  If the @radix@ is
75  *              specified, the number is assumed to be given in that radix,
76  *              with the letters `a' (either upper- or lower-case) upwards
77  *              standing for digits greater than 9.  Otherwise, base 10 is
78  *              assumed unless the number starts with `0' (octal), `0x' (hex)
79  *              or `nnn_' (base `nnn').  An arbitrary amount of whitespace
80  *              before the number is ignored.
81  */
82
83 /* --- About the algorithm --- *
84  *
85  * The algorithm here is rather aggressive.  I maintain an array of
86  * successive squarings of the radix, and a stack of partial results, each
87  * with a counter attached indicating which radix square to multiply by.
88  * Once the item at the top of the stack reaches the same counter level as
89  * the next item down, they are combined together and the result is given a
90  * counter level one higher than either of the results.
91  *
92  * Gluing the results together at the end is slightly tricky.  Pay attention
93  * to the code.
94  *
95  * This is more complicated because of the need to handle the slightly
96  * bizarre syntax.
97  */
98
99 mp *mp_read(mp *m, int radix, const mptext_ops *ops, void *p)
100 {
101   int ch;                               /* Current char being considered */
102   unsigned f = 0;                       /* Flags about the current number */
103   int r;                                /* Radix to switch over to */
104   mpw rd;                               /* Radix as an @mp@ digit */
105   mp rr;                                /* The @mp@ for the radix */
106   unsigned nf = m ? m->f & MP_BURN : 0; /* New @mp@ flags */
107
108   /* --- Stacks --- */
109
110   mp *pow[DEPTH];                       /* List of powers */
111   unsigned pows;                        /* Next index to fill */
112   struct { unsigned i; mp *m; } s[DEPTH]; /* Main stack */
113   unsigned sp;                          /* Current stack pointer */
114
115   /* --- Flags --- */
116
117 #define f_neg 1u
118 #define f_ok 2u
119 #define f_start 4u
120
121   /* --- Initialize the stacks --- */
122
123   mp_build(&rr, &rd, &rd + 1);
124   pow[0] = &rr;
125   pows = 1;
126
127   sp = 0;
128
129   /* --- Initialize the destination number --- */
130
131   if (m)
132     MP_DROP(m);
133
134   /* --- Read an initial character --- */
135
136   ch = ops->get(p);
137   if (radix >= 0) {
138     while (isspace(ch))
139       ch = ops->get(p);
140   }
141
142   /* --- Handle an initial sign --- */
143
144   if (radix >= 0 && (ch == '-' || ch == '+')) {
145     if (ch == '-')
146       f |= f_neg;
147     do ch = ops->get(p); while isspace(ch);
148   }
149
150   /* --- If the radix is zero, look for leading zeros --- */
151
152   if (radix > 0) {
153     assert(((void)"ascii radix must be <= 62", radix <= 62));
154     rd = radix;
155     r = -1;
156   } else if (radix < 0) {
157     rd = -radix;
158     assert(((void)"binary radix must fit in a byte", rd <= UCHAR_MAX));
159     r = -1;
160   } else if (ch != '0') {
161     rd = 10;
162     r = 0;
163   } else {
164     ch = ops->get(p);
165     switch (ch) {
166       case 'x':
167         rd = 16;
168         goto prefix;
169       case 'o':
170         rd = 8;
171         goto prefix;
172       case 'b':
173         rd = 2;
174         goto prefix;
175       prefix:
176         ch = ops->get(p);
177         break;
178       default:
179         rd = 8;
180         f |= f_ok;
181     }
182     r = -1;
183   }
184
185   /* --- Use fast algorithm for binary radix --- *
186    *
187    * This is the restart point after having parsed a radix number from the
188    * input.  We check whether the radix is binary, and if so use a fast
189    * algorithm which just stacks the bits up in the right order.
190    */
191
192 restart:
193   switch (rd) {
194     unsigned bit;
195
196     case   2: bit = 1; goto bin;
197     case   4: bit = 2; goto bin;
198     case   8: bit = 3; goto bin;
199     case  16: bit = 4; goto bin;
200     case  32: bit = 5; goto bin;
201     case  64: bit = 6; goto bin;
202     case 128: bit = 7; goto bin;
203     default:
204       break;
205
206   /* --- The fast binary algorithm --- *
207    *
208    * We stack bits up starting at the top end of a word.  When one word is
209    * full, we write it to the integer, and start another with the left-over
210    * bits.  When the array in the integer is full, we resize using low-level
211    * calls and copy the current data to the top end.  Finally, we do a single
212    * bit-shift when we know where the end of the number is.
213    */
214
215   bin: {
216     mpw a = 0;
217     unsigned b = MPW_BITS;
218     size_t len, n;
219     mpw *v;
220
221     m = mp_dest(MP_NEW, 1, nf);
222     len = n = m->sz;
223     n = len;
224     v = m->v + n;
225     for (;; ch = ops->get(p)) {
226       unsigned x;
227
228       if (ch < 0)
229         break;
230
231       /* --- Check that the character is a digit and in range --- */
232
233       if (radix < 0)
234         x = ch % rd;
235       else {
236         if (!isalnum(ch))
237           break;
238         if (ch >= '0' && ch <= '9')
239           x = ch - '0';
240         else {
241           if (rd <= 36)
242             ch = tolower(ch);
243           if (ch >= 'a' && ch <= 'z')   /* ASCII dependent! */
244             x = ch - 'a' + 10;
245           else if (ch >= 'A' && ch <= 'Z')
246             x = ch - 'A' + 36;
247           else
248             break;
249         }
250       }
251       if (x >= rd)
252         break;
253
254       /* --- Feed the digit into the accumulator --- */
255
256       f |= f_ok;
257       if (!x && !(f & f_start))
258         continue;
259       f |= f_start;
260       if (b > bit) {
261         b -= bit;
262         a |= MPW(x) << b;
263       } else {
264         a |= MPW(x) >> (bit - b);
265         b += MPW_BITS - bit;
266         *--v = MPW(a);
267         n--;
268         if (!n) {
269           n = len;
270           len <<= 1;
271           v = mpalloc(m->a, len);
272           memcpy(v + n, m->v, MPWS(n));
273           mpfree(m->a, m->v);
274           m->v = v;
275           v = m->v + n;
276         }
277         a = (b < MPW_BITS) ? MPW(x) << b : 0;
278       }
279     }
280
281     /* --- Finish up --- */
282
283     if (!(f & f_ok)) {
284       mp_drop(m);
285       m = 0;
286     } else {
287       *--v = MPW(a);
288       n--;
289       m->sz = len;
290       m->vl = m->v + len;
291       m->f &= ~MP_UNDEF;
292       m = mp_lsr(m, m, (unsigned long)n * MPW_BITS + b);
293     }
294     ops->unget(ch, p);
295     goto done;
296   }}
297
298   /* --- Time to start --- */
299
300   for (;; ch = ops->get(p)) {
301     unsigned x;
302
303     if (ch < 0)
304       break;
305
306     /* --- An underscore indicates a numbered base --- */
307
308     if (ch == '_' && r > 0 && r <= 62) {
309       unsigned i;
310
311       /* --- Clear out the stacks --- */
312
313       for (i = 1; i < pows; i++)
314         MP_DROP(pow[i]);
315       pows = 1;
316       for (i = 0; i < sp; i++)
317         MP_DROP(s[i].m);
318       sp = 0;
319
320       /* --- Restart the search --- */
321
322       rd = r;
323       r = -1;
324       f &= ~f_ok;
325       ch = ops->get(p);
326       goto restart;
327     }
328
329     /* --- Check that the character is a digit and in range --- */
330
331     if (radix < 0)
332       x = ch % rd;
333     else {
334       if (!isalnum(ch))
335         break;
336       if (ch >= '0' && ch <= '9')
337         x = ch - '0';
338       else {
339         if (rd <= 36)
340           ch = tolower(ch);
341         if (ch >= 'a' && ch <= 'z')     /* ASCII dependent! */
342           x = ch - 'a' + 10;
343         else if (ch >= 'A' && ch <= 'Z')
344           x = ch - 'A' + 36;
345         else
346           break;
347       }
348     }
349
350     /* --- Sort out what to do with the character --- */
351
352     if (x >= 10 && r >= 0)
353       r = -1;
354     if (x >= rd)
355       break;
356
357     if (r >= 0)
358       r = r * 10 + x;
359
360     /* --- Stick the character on the end of my integer --- */
361
362     assert(((void)"Number is too unimaginably huge", sp < DEPTH));
363     s[sp].m = m = mp_new(1, nf);
364     m->v[0] = x;
365     s[sp].i = 0;
366
367     /* --- Now grind through the stack --- */
368
369     while (sp > 0 && s[sp - 1].i == s[sp].i) {
370
371       /* --- Combine the top two items --- */
372
373       sp--;
374       m = s[sp].m;
375       m = mp_mul(m, m, pow[s[sp].i]);
376       m = mp_add(m, m, s[sp + 1].m);
377       s[sp].m = m;
378       MP_DROP(s[sp + 1].m);
379       s[sp].i++;
380
381       /* --- Make a new radix power if necessary --- */
382
383       if (s[sp].i >= pows) {
384         assert(((void)"Number is too unimaginably huge", pows < DEPTH));
385         pow[pows] = mp_sqr(MP_NEW, pow[pows - 1]);
386         pows++;
387       }
388     }
389     f |= f_ok;
390     sp++;
391   }
392
393   ops->unget(ch, p);
394
395   /* --- If we're done, compute the rest of the number --- */
396
397   if (f & f_ok) {
398     if (!sp)
399       return (MP_ZERO);
400     else {
401       mp *z = MP_ONE;
402       sp--;
403
404       while (sp > 0) {
405
406         /* --- Combine the top two items --- */
407
408         sp--;
409         m = s[sp].m;
410         z = mp_mul(z, z, pow[s[sp + 1].i]);
411         m = mp_mul(m, m, z);
412         m = mp_add(m, m, s[sp + 1].m);
413         s[sp].m = m;
414         MP_DROP(s[sp + 1].m);
415
416         /* --- Make a new radix power if necessary --- */
417
418         if (s[sp].i >= pows) {
419           assert(((void)"Number is too unimaginably huge", pows < DEPTH));
420           pow[pows] = mp_sqr(MP_NEW, pow[pows - 1]);
421           pows++;
422         }
423       }
424       MP_DROP(z);
425       m = s[0].m;
426     }
427   } else {
428     unsigned i;
429     for (i = 0; i < sp; i++)
430       MP_DROP(s[i].m);
431   }
432
433   /* --- Clear the radix power list --- */
434
435   {
436     unsigned i;
437     for (i = 1; i < pows; i++)
438       MP_DROP(pow[i]);
439   }
440
441   /* --- Bail out if the number was bad --- */
442
443 done:
444   if (!(f & f_ok))
445     return (0);
446
447   /* --- Set the sign and return --- */
448
449   if (f & f_neg)
450     m->f |= MP_NEG;
451   MP_SHRINK(m);
452   return (m);
453
454 #undef f_start
455 #undef f_neg
456 #undef f_ok
457 }
458
459 /*----- Output ------------------------------------------------------------*/
460
461 /* --- @mp_write@ --- *
462  *
463  * Arguments:   @mp *m@ = pointer to a multi-precision integer
464  *              @int radix@ = radix to use when writing the number out
465  *              @const mptext_ops *ops@ = pointer to an operations block
466  *              @void *p@ = data for the operations block
467  *
468  * Returns:     Zero if it worked, nonzero otherwise.
469  *
470  * Use:         Writes a large integer in textual form.
471  */
472
473 static int digit_char(int d, int radix)
474 {
475   if (radix < 0) return (d);
476   else if (d < 10) return (d + '0');
477   else if (d < 26) return (d - 10 + 'a');
478   else return (d - 36 + 'A');
479 }
480
481 /* --- Simple case --- *
482  *
483  * Use a fixed-sized buffer and single-precision arithmetic to pick off
484  * low-order digits.  Put each digit in a buffer, working backwards from the
485  * end.  If the buffer becomes full, recurse to get another one.  Ensure that
486  * there are at least @z@ digits by writing leading zeroes if there aren't
487  * enough real digits.
488  */
489
490 static int write_simple(mpw n, int radix, unsigned z,
491                         const mptext_ops *ops, void *p)
492 {
493   int rc = 0;
494   char buf[64];
495   unsigned i = sizeof(buf);
496   int rd = radix > 0 ? radix : -radix;
497   mpw x;
498
499   do {
500     x = n % rd; n /= rd;
501     buf[--i] = digit_char(x, radix);
502     if (z) z--;
503   } while (i && n);
504
505   if (n)
506     rc = write_simple(n, radix, z, ops, p);
507   else {
508     char zbuf[32];
509     memset(zbuf, (radix < 0) ? 0 : '0', sizeof(zbuf));
510     while (!rc && z >= sizeof(zbuf)) {
511       rc = ops->put(zbuf, sizeof(zbuf), p);
512       z -= sizeof(zbuf);
513     }
514     if (!rc && z) rc = ops->put(zbuf, z, p);
515   }
516   if (!rc) rc = ops->put(buf + i, sizeof(buf) - i, p);
517   BURN(buf);
518   return (rc);
519 }
520
521 /* --- Complicated case --- *
522  *
523  * If the number is small, fall back to the simple case above.  Otherwise
524  * divide and take remainder by current large power of the radix, and emit
525  * each separately.  Don't emit a zero quotient.  Be very careful about
526  * leading zeroes on the remainder part, because they're deeply significant.
527  */
528
529 static int write_complicated(mp *m, int radix, mp **pr,
530                              unsigned i, unsigned z,
531                              const mptext_ops *ops, void *p)
532 {
533   int rc = 0;
534   mp *q = MP_NEW;
535   unsigned d = 1 << i;
536
537   if (MP_LEN(m) < 2)
538     return (write_simple(MP_LEN(m) ? m->v[0] : 0, radix, z, ops, p));
539
540   assert(i);
541   mp_div(&q, &m, m, pr[i]);
542   if (MP_ZEROP(q)) d = z;
543   else {
544     if (z > d) z -= d;
545     else z = 0;
546     rc = write_complicated(q, radix, pr, i - 1, z, ops, p);
547   }
548   if (!rc) rc = write_complicated(m, radix, pr, i - 1, d, ops, p);
549   mp_drop(q);
550   return (rc);
551 }
552
553 /* --- Binary case --- *
554  *
555  * Special case for binary output.  Goes much faster.
556  */
557
558 static int write_binary(mp *m, int bit, int radix,
559                         const mptext_ops *ops, void *p)
560 {
561   mpw *v;
562   mpw a;
563   int rc = 0;
564   unsigned b;
565   unsigned mask;
566   unsigned long n;
567   unsigned f = 0;
568   char buf[8], *q;
569   unsigned x;
570
571 #define f_out 1u
572
573   /* --- Work out where to start --- */
574
575   n = mp_bits(m);
576   if (n % bit) n += bit - (n % bit);
577   b = n % MPW_BITS;
578   n /= MPW_BITS;
579
580   if (n >= MP_LEN(m)) {
581     n--;
582     b += MPW_BITS;
583   }
584
585   v = m->v + n;
586   a = *v;
587   mask = (1 << bit) - 1;
588   q = buf;
589
590   /* --- Main code --- */
591
592   for (;;) {
593     if (b > bit) {
594       b -= bit;
595       x = a >> b;
596     } else {
597       x = a << (bit - b);
598       b += MPW_BITS - bit;
599       if (v == m->v) break;
600       a = *--v;
601       if (b < MPW_BITS) x |= a >> b;
602     }
603     x &= mask;
604     if (!x && !(f & f_out)) continue;
605
606     *q++ = digit_char(x, radix);
607     if (q >= buf + sizeof(buf)) {
608       if ((rc = ops->put(buf, sizeof(buf), p)) != 0) goto done;
609       q = buf;
610     }
611     f |= f_out;
612   }
613
614   x &= mask;
615   *q++ = digit_char(x, radix);
616   rc = ops->put(buf, q - buf, p);
617
618 done:
619   mp_drop(m);
620   return (rc);
621
622 #undef f_out
623 }
624
625 /* --- Main driver code --- */
626
627 int mp_write(mp *m, int radix, const mptext_ops *ops, void *p)
628 {
629   int rc;
630   mp *pr[DEPTH];
631   size_t target;
632   unsigned i = 0;
633   mp *z;
634
635   if (MP_EQ(m, MP_ZERO))
636     return (ops->put(radix > 0 ? "0" : "\0", 1, p));
637
638   /* --- Set various things up --- */
639
640   m = MP_COPY(m);
641   MP_SPLIT(m);
642
643   /* --- Check the radix for sensibleness --- */
644
645   if (radix > 0)
646     assert(((void)"ascii radix must be <= 62", radix <= 62));
647   else if (radix < 0)
648     assert(((void)"binary radix must fit in a byte", -radix <= UCHAR_MAX));
649   else
650     assert(((void)"radix can't be zero in mp_write", 0));
651
652   /* --- If the number is negative, sort that out --- */
653
654   if (MP_NEGP(m)) {
655     assert(radix > 0);
656     if (ops->put("-", 1, p)) return (EOF);
657     m->f &= ~MP_NEG;
658   }
659
660   /* --- Handle binary radix --- */
661
662   switch (radix) {
663     case   2: case   -2: return (write_binary(m, 1, radix, ops, p));
664     case   4: case   -4: return (write_binary(m, 2, radix, ops, p));
665     case   8: case   -8: return (write_binary(m, 3, radix, ops, p));
666     case  16: case  -16: return (write_binary(m, 4, radix, ops, p));
667     case  32: case  -32: return (write_binary(m, 5, radix, ops, p));
668               case  -64: return (write_binary(m, 6, radix, ops, p));
669               case -128: return (write_binary(m, 7, radix, ops, p));
670   }
671
672   /* --- If the number is small, do it the easy way --- */
673
674   if (MP_LEN(m) < 2)
675     rc = write_simple(MP_LEN(m) ? m->v[0] : 0, radix, 0, ops, p);
676
677   /* --- Use a clever algorithm --- *
678    *
679    * Square the radix repeatedly, remembering old results, until I get
680    * something more than half the size of the number @m@.  Use this to divide
681    * the number: the quotient and remainder will be approximately the same
682    * size, and I'll have split them on a digit boundary, so I can just emit
683    * the quotient and remainder recursively, in order.
684    */
685
686   else {
687     target = (MP_LEN(m) + 1) / 2;
688     z = mp_new(1, 0);
689
690     /* --- Set up the exponent table --- */
691
692     z->v[0] = (radix > 0 ? radix : -radix);
693     z->f = 0;
694     for (;;) {
695       assert(((void)"Number is too unimaginably huge", i < DEPTH));
696       pr[i++] = z;
697       if (MP_LEN(z) > target) break;
698       z = mp_sqr(MP_NEW, z);
699     }
700
701     /* --- Write out the answer --- */
702
703     rc = write_complicated(m, radix, pr, i - 1, 0, ops, p);
704
705     /* --- Tidy away the array --- */
706
707     while (i > 0) mp_drop(pr[--i]);
708   }
709
710   /* --- Tidying up code --- */
711
712   MP_DROP(m);
713   return (rc);
714 }
715
716 /*----- Test rig ----------------------------------------------------------*/
717
718 #ifdef TEST_RIG
719
720 #include <mLib/testrig.h>
721
722 static int verify(dstr *v)
723 {
724   int ok = 1;
725   int ib = *(int *)v[0].buf, ob = *(int *)v[2].buf;
726   dstr d = DSTR_INIT;
727   size_t off = 0;
728   mp *m = mp_readdstr(MP_NEW, &v[1], &off, ib);
729   if (m) {
730     if (!ob) {
731       fprintf(stderr, "*** unexpected successful parse\n"
732                       "*** input [%2i] =     ", ib);
733       if (ib < 0)
734         type_hex.dump(&v[1], stderr);
735       else
736         fputs(v[1].buf, stderr);
737       mp_writedstr(m, &d, 10);
738       fprintf(stderr, "\n*** (value = %s)\n", d.buf);
739       ok = 0;
740     } else {
741       mp_writedstr(m, &d, ob);
742       if (d.len != v[3].len || memcmp(d.buf, v[3].buf, d.len) != 0) {
743         fprintf(stderr, "*** failed read or write\n"
744                         "*** input [%2i]      = ", ib);
745         if (ib < 0)
746           type_hex.dump(&v[1], stderr);
747         else
748           fputs(v[1].buf, stderr);
749         fprintf(stderr, "\n*** output [%2i]     = ", ob);
750         if (ob < 0)
751           type_hex.dump(&d, stderr);
752         else
753           fputs(d.buf, stderr);
754         fprintf(stderr, "\n*** expected [%2i]   = ", ob);
755         if (ob < 0)
756           type_hex.dump(&v[3], stderr);
757         else
758           fputs(v[3].buf, stderr);
759         fputc('\n', stderr);
760         ok = 0;
761       }
762     }
763     mp_drop(m);
764   } else {
765     if (ob) {
766       fprintf(stderr, "*** unexpected parse failure\n"
767                       "*** input [%2i]    = ", ib);
768       if (ib < 0)
769         type_hex.dump(&v[1], stderr);
770       else
771         fputs(v[1].buf, stderr);
772       fprintf(stderr, "\n*** expected [%2i]   = ", ob);
773       if (ob < 0)
774         type_hex.dump(&v[3], stderr);
775       else
776         fputs(v[3].buf, stderr);
777       fputc('\n', stderr);
778       ok = 0;
779     }
780   }
781
782   if (v[1].len - off != v[4].len ||
783       memcmp(v[1].buf + off, v[4].buf, v[4].len) != 0) {
784     fprintf(stderr, "*** leftovers incorrect\n"
785                     "*** input [%2i]    = ", ib);
786     if (ib < 0)
787       type_hex.dump(&v[1], stderr);
788     else
789       fputs(v[1].buf, stderr);
790     fprintf(stderr, "\n*** expected `%s'\n"
791                     "*** found `%s'\n",
792             v[4].buf, v[1].buf + off);
793     ok = 0;
794   }
795
796   dstr_destroy(&d);
797   assert(mparena_count(MPARENA_GLOBAL) == 0);
798   return (ok);
799 }
800
801 static test_chunk tests[] = {
802   { "mptext-ascii", verify,
803     { &type_int, &type_string, &type_int, &type_string, &type_string, 0 } },
804   { "mptext-bin-in", verify,
805     { &type_int, &type_hex, &type_int, &type_string, &type_string, 0 } },
806   { "mptext-bin-out", verify,
807     { &type_int, &type_string, &type_int, &type_hex, &type_string, 0 } },
808   { 0, 0, { 0 } }
809 };
810
811 int main(int argc, char *argv[])
812 {
813   sub_init();
814   test_run(argc, argv, tests, SRCDIR "/t/mptext");
815   return (0);
816 }
817
818 #endif
819
820 /*----- That's all, folks -------------------------------------------------*/