--- al_test.c 2002/10/14 08:04:46 1.1
+++ al_test.c 2002/10/14 12:32:16 1.2
@@ -1,6 +1,7 @@
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
+#include <ctype.h>
#include "al.h"
@@ -8,10 +9,28 @@
al_rc_t printchunk(al_chunk_t *alc, void *u)
{
- printf("C: %8p = %8p + %d\n",
- alc,
- al_chunk_ptr(alc, 0),
- al_chunk_len(alc));
+ char buf[40];
+ char *p = al_chunk_ptr(alc,0);
+ size_t n = al_chunk_len(alc);
+ int k;
+
+ k=0;
+ while (n > 0 && k < sizeof(buf)-5) {
+ if (isprint(*p)) {
+ buf[k++] = *p;
+ } else {
+ sprintf(buf+k,"<%2.2x>",*p);
+ k += 4;
+ }
+ ++p;
+ --n;
+ }
+ buf[k] = '\0';
+
+ printf("C: %08lx + %-6d = %s\n",
+ (long)al_chunk_ptr(alc, 0),
+ al_chunk_len(alc),
+ buf);
return AL_OK;
}
@@ -22,16 +41,50 @@
printf("-DUMP(%s)\n\n",tag);\
} while (0)
-int main()
+void print(const char *tag, al_t *al)
{
- al_t *al, *al2;
char buf[10000];
- size_t n;
+ size_t n, len;
+
+ printf("%s\n",tag);
+ n = al_bytes(al);
+ if (n > sizeof(buf)) n = sizeof(buf);
+ al_flatten(al, 0, n, buf, &len);
+ fwrite(">>", 2, 1, stdout);
+ fwrite(buf, len, 1, stdout);
+ fwrite("<<", 2, 1, stdout);
+ printf("\n\n");
+}
+
+void checklen(al_t *al)
+{
+ al_tx_t *tx;
+ al_chunk_t *cur;
+ size_t total, total2;
+
+ total = 0;
+
+ al_txalloc(al, &tx);
+ al_traverse(al, 0, -1, AL_FORWARD, tx);
+ while (al_traverse_next(al, tx, &cur) == AL_OK)
+ total += al_chunk_len(cur);
+ al_txfree(al, tx);
+
+ total2 = al_bytes(al);
+
+ if (total != total2)
+ printf("ERROR: al_bytes(%p): %d != %d\n",al,total,total2);
+}
+
+int main()
+{
+ al_t *al, *al2, *al3;
char baf[] = "Mittendrin\n";
int i;
al_create(&al);
al_create(&al2);
+ al_create(&al3);
al_append_bytes(al, S("Hello world\n"));
al_attach_buffer(al, S(baf));
@@ -41,33 +94,33 @@
al_append_bytes(al, S("Hello world\n"));
+ al_append_bytes(al3, S("HUHU WORLD\n"));
+
+
+ DUMP("DATA",al);
+ DUMP("BUFFER",al2);
+ DUMP("REPLACEMENT", al3);
- DUMP("ALL",al);
#if 1
- al_splice(al, 102, 200, al2);
+ al_splice(al, 102, 200, al3, al2);
#else
- al_splice(al, 102, 200, NULL);
+ al_splice(al, 102, 200, NULL, NULL);
#endif
- DUMP("SPLICED",al);
- DUMP("BUFFER",al2);
- printf("RESULT\n");
- n = al_bytes(al);
- if (n > 10000) n = 10000;
- al_flatten(al, 0, n, buf);
- fwrite(">>", 2, 1, stdout);
- fwrite(buf, n, 1, stdout);
- fwrite("<<", 2, 1, stdout);
+ checklen(al);
+ checklen(al2);
+ checklen(al3);
- printf("BUFFER\n");
- n = al_bytes(al2);
- if (n > 10000) n = 10000;
- al_flatten(al2, 0, n, buf);
- fwrite(">>", 2, 1, stdout);
- fwrite(buf, n, 1, stdout);
- fwrite("<<", 2, 1, stdout);
+ DUMP("SPLICED",al);
+ print("RESULT", al);
+
+ DUMP("BUFFER",al2);
+ print("BUFFER", al2);
+ DUMP("REPLACEMENT", al3);
+ print("REPLACEMENT", al3);
+ al_destroy(al3);
al_destroy(al2);
al_destroy(al);
|