Index: ossp-pkg/al/al_test.c RCS File: /v/ossp/cvs/ossp-pkg/al/al_test.c,v rcsdiff -q -kk '-r1.1' '-r1.2' -u '/v/ossp/cvs/ossp-pkg/al/al_test.c,v' 2>/dev/null --- al_test.c 2002/12/16 12:24:52 1.1 +++ al_test.c 2002/12/16 13:33:23 1.2 @@ -13,7 +13,7 @@ ** the above copyright notice and this permission notice appear in all ** copies. ** -** THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED +** THIS SOFTWARE IS PROVIDED ``AS IS"" AND ANY EXPRESSED OR IMPLIED ** WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF ** MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. ** IN NO EVENT SHALL THE AUTHORS AND COPYRIGHT HOLDERS AND THEIR @@ -35,13 +35,8 @@ #include #include -#include -#include -#include -#include -#include -#include -#include +#include +#include #include "ts.h" #include "al.h" @@ -52,54 +47,55 @@ #define LABEL3 ((al_label_t)&label3) #define S(s) s, strlen(s) -#define RCHK(name, rc, rc0) \ + +#define EVAL(name,rc,rc0,block) \ + ts_test_check(TS_CTX, name); \ + block \ if (rc != rc0) \ ts_test_fail(TS_CTX, "%s -> %d[%s] (expected %d[%s])\n", \ - rc, al_error(al), rc0, al_error(rc0)) -#define CCHK(name, al) \ + rc, al_error(rc), rc0, al_error(rc0)) + +#define CHECKLEN(name, al) \ do { \ size_t good, bad; \ - if (chklen(al, &good, &bad)) { \ + if (checklen(al, &good, &bad)) \ ts_test_fail(TS_CTX, "%s -> corrupted length %d (expected %d)\n", \ good, bad); \ - } \ } while (0) -static const char *fill(char *p, int n) -{ - static char buf[4 * 80 + 1]; - int max = sizeof(buf)-5; - int k; - - k=0; - while (n > 0 && k < max) { - if (isprint(*p)) { - buf[k] = *p; - k += 1; - } else { - sprintf(buf+k,"<%2.2x>",*p); - k += 4; - } - ++p; - --n; - } - buf[k] = '\0'; +#define EVAL0(name,block) EVAL(name,rc,AL_OK,block) +#define EVAL1(name,al,block) EVAL(name,rc,AL_OK,block); CHECKLEN(name, al); - return (const char *)buf; -} +#define CHECKLEN1(name, al, n) \ + do { \ + CHECKLEN(name, al); \ + if (al_bytes(al) != (n)) \ + ts_test_fail(TS_CTX, "%s -> invalid length %d (expected %d)\n", \ + al_bytes(al), (n)); \ + } while (0) static int checklen(al_t *al, size_t *goodp, size_t *badp) { + al_rc_t rc; al_tx_t *tx; al_chunk_t *cur; size_t total, total2; total = 0; - al_txalloc(al, &tx); - al_traverse(al, 0, al_bytes(al), AL_FORWARD, NULL, tx); + rc = al_txalloc(al, &tx); + if (rc != AL_OK) + return -1; + + rc = al_traverse(al, 0, al_bytes(al), AL_FORWARD, NULL, tx); + if (rc != AL_OK) { + al_txfree(al, tx); + return -1; + } + while (al_traverse_next(al, tx, &cur) == AL_OK) total += al_chunk_len(cur); + al_traverse_end(al, tx, 1); al_txfree(al, tx); @@ -121,13 +117,17 @@ al_rc_t rc; al_t *al; - rc = al_create(&al); - RCHK('al_create',rc,AL_OK); - rc = al_append_bytes(al, S("Hello world\n")); - RCHK('al_append_bytes',rc,AL_OK); - CCHK('al_append_bytes', al); - rc = al_destroy(&al); - RCHK('al_destroy',rc,AL_OK); + EVAL0("al_create", { + rc = al_create(&al); + }); + + EVAL1("al_append_bytes", al, { + rc = al_append_bytes(al, S("Hello world\n"), NULL); + }); + + EVAL0("al_destroy", { + rc = al_destroy(al); + }); } /* test: splicing */ @@ -137,49 +137,59 @@ al_t *src, *ins, *dst; int i; - rc = al_create(&src); - RCHK('al_create(&src)',rc,AL_OK); - rc = al_create(&ins); - RCHK('al_create(&ins)',rc,AL_OK); - rc = al_create(&dst); - RCHK('al_create(&dst)',rc,AL_OK); + EVAL0("al_create(&src)", { + rc = al_create(&src); + }); + EVAL0("al_create(&ins)", { + rc = al_create(&ins); + }); + EVAL0("al_create(&dst)", { + rc = al_create(&dst); + }); for (i=0; i<500; ++i) { - rc = al_append_bytes(src, S("Huhu world\n"), LABEL); - RCHK('al_append_bytes(src)',rc,AL_OK); + EVAL1("al_append_bytes(&src)", src, { + rc = al_append_bytes(src, S("Huhu world\n"), LABEL); + }); } - rc = al_append_bytes(src, S("Goodbye world\n"), LABEL); - RCHK('al_append_bytes(src)',rc,AL_OK); - CCHK('al_append_bytes(src)',src); - rc = al_append_bytes(src, S("Goodbye world\n"), LABEL2); - RCHK('al_append_bytes(src)',rc,AL_OK); - CCHK('al_append_bytes(src)',src); - rc = al_prepend_bytes(src, S("Hello world\n"), LABEL2); - RCHK('al_prepend_bytes(src)',rc,AL_OK); - CCHK('al_prepend_bytes(src)',src); - rc = al_prepend_bytes(src, S("Hello world\n"), LABEL); - RCHK('al_prepend_bytes(src)',rc,AL_OK); - CCHK('al_prepend_bytes(src)',src); - - rc = al_prepend_bytes(ins, S("KICK "), LABEL3); - RCHK('al_prepend_bytes(ins)',rc,AL_OK); - CCHK('al_prepend_bytes(ins)',ins); - rc = al_append_bytes(ins, S("ME\n"), LABEL3); - RCHK('al_append_bytes(ins)',rc,AL_OK); - CCHK('al_append_bytes(ins)',ins); - - rc = al_splice(src, 4, 80, ins, dst); - RCHK('al_splice',rc,AL_OK); - CCHK('al_splice(src)',src); - CCHK('al_splice(ins)',ins); - CCHK('al_splice(dst)',dst); - - rc = al_destroy(dst); - RCHK('al_destroy(dst)',rc,AL_OK); - rc = al_destroy(ins); - RCHK('al_destroy(ins)',rc,AL_OK); - rc = al_destroy(src); - RCHK('al_destroy(src)',rc,AL_OK); + + EVAL1("al_append_bytes(src)", src, { + rc = al_append_bytes(src, S("Goodbye world\n"), LABEL); + }); + EVAL1("al_append_bytes(src)", src, { + rc = al_append_bytes(src, S("Goodbye world\n"), LABEL2); + }); + EVAL1("al_prepend_byts(src)", src, { + rc = al_prepend_bytes(src, S("Hello world\n"), LABEL2); + }); + EVAL1("al_prepend_bytes(src)", src, { + rc = al_prepend_bytes(src, S("Hello world\n"), LABEL); + + }); + + EVAL1("al_prepend_bytes(ins)", ins, { + rc = al_prepend_bytes(ins, S("KICK "), LABEL3); + }); + EVAL1("al_append_bytes(ins)", ins, { + rc = al_append_bytes(ins, S("ME\n"), LABEL3); + }); + + EVAL0("al_splice", { + rc = al_splice(src, 4, 80, ins, dst); + }); + CHECKLEN1("al_splice(src)",src, 5480); + CHECKLEN1("al_splice(ins)",ins, 0); + CHECKLEN1("al_splice(dst)",dst, 80); + + EVAL0("al_destroy(dst)", { + rc = al_destroy(dst); + }); + EVAL0("al_destroy(ins)", { + rc = al_destroy(ins); + }); + EVAL0("al_destroy(src)", { + rc = al_destroy(src); + }); } /* test: labelling */ @@ -187,25 +197,106 @@ { al_rc_t rc; al_t *al; + al_tx_t *tx; + al_chunk_t *cur; + int i, k; + static struct { size_t len; int ln; } chunks[] = { + { 5, 1 }, + { 2, 2 }, + { 2, 3 }, + { 5, 2 }, + { 14, 1 }, + { 0, 0 } + }; + static al_label_t labels[] = { + NULL, + LABEL, + LABEL2, + LABEL3 + }; + + EVAL0("al_create", { + rc = al_create(&al); + }); + EVAL0("al_append_bytes", { + rc = al_append_bytes(al, S("Hello world. Goodbye world.\n"), LABEL); + }); + + /* + * 28 chars LABEL + */ + + EVAL1("al_setlabel", al, { + rc = al_setlabel(al, 7, 2, NULL, LABEL3); + }); + + /* + * 7 chars LABEL + * 2 chars LABEL3 + * 19 chars LABEL + */ + + EVAL1("al_setlabel", al, { + rc = al_setlabel(al, 5, 9, LABEL, LABEL2); + }); + + /* + * 5 chars LABEL + * 2 chars LABEL2 + * 2 chars LABEL3 + * 5 chars LABEL2 + * 14 chars LABEL + */ + + EVAL0("al_txalloc", { + rc = al_txalloc(al, &tx); + }); + + EVAL0("al_traverse", { + rc = al_traverse(al, 0, al_bytes(al), AL_FORWARD, NULL, tx); + }); + + i = 0; + ts_test_check(TS_CTX, "al_traverse_next"); + while (al_traverse_next(al, tx, &cur) == AL_OK) { + size_t n = al_chunk_len(cur); + al_label_t l = al_chunk_label(cur); + + for (k=3; k>0; --k) + if (labels[k] == l) + break; + + if (chunks[i].len == 0) { + ts_test_fail(TS_CTX, + "al_traverse_next: found chunk %d[L%d] (none expected)\n", + n, k); + continue; + } - rc = al_create(&al); - RCHK('al_create',rc,AL_OK); - rc = al_append_bytes(&al, S("Hello world\n"), LABEL); - RCHK('al_create',rc,AL_OK); - - rc = al_setlabel(al, 7, 2, NULL, LABEL3); - RCHK('al_setlabel',rc,AL_OK); - CCHK('al_setlabel',al); - rc = al_setlabel(al, 5, 9, NULL, LABEL2); - CCHK('al_setlabel',al); + if (n != chunks[i].len || k != chunks[i].ln) { + ts_test_fail(TS_CTX, + "al_traverse_next: found chunk %d[L%d] (expected %d[L%d])\n", + n, k, chunks[i].len, chunks[i].ln); + } + ++i; + } - rc = al_destroy(al); - RCHK('al_destroy(al)',rc,AL_OK); + EVAL0("al_traverse_end", { + rc = al_traverse_end(al, tx, 1); + }); + + EVAL0("al_txfree", { + rc = al_txfree(al, tx); + }); + + EVAL0("al_destroy", { + rc = al_destroy(al); + }); } /* test: attach */ static char *reclaim_ptr = NULL; -static size_t *reclaim_size = 0; +static size_t reclaim_size = 0; static void *reclaim_u = NULL; static int reclaim_count = 0; static void reclaim(char *p, size_t n, void *u) @@ -221,11 +312,11 @@ reclaim_size = n; reclaim_u = u; } -static const char *reclaim_result(char *p, size_t n, void *u) +static const char *reclaim_result(const char *p, size_t n, void *u) { if (reclaim_count == 0) return "reclaim never called"; - if (reclaim_count > 0) + if (reclaim_count > 1) return "repeated call"; if (p != reclaim_ptr || @@ -242,21 +333,23 @@ al_t *al; char baf[] = "Mittendrin\n"; char *context = "CONTEXT"; - char *mess; + const char *mess; - rc = al_create(&al); - RCHK('al_create', rc, AL_OK); - rc = al_append_bytes(al, S("Hello world\n"), NULL); - RCHK('al_append_bytes', rc, AL_OK); - CCHK('al_append_bytes', al); - rc = al_attach_buffer(al, S(baf), NULL, reclaim, context); - RCHK('al_attach_buffer', rc, AL_OK); - CCHK('al_attach_buffer', al); - rc = al_append_bytes(al, S("Goodbye world\n"), NULL); - RCHK('al_append_bytes', rc, AL_OK); - CCHK('al_append_bytes', al); - rc = al_destroy(&al); - RCHK('al_destroy', rc, AL_OK); + EVAL0("al_create", { + rc = al_create(&al); + }); + EVAL1("al_append_bytes", al, { + rc = al_append_bytes(al, S("Hello world\n"), NULL); + }); + EVAL1("al_attach_buffer", al, { + rc = al_attach_buffer(al, S(baf), NULL, reclaim, context); + }); + EVAL1("al_append_bytes", al, { + rc = al_append_bytes(al, S("Goodbye world\n"), NULL); + }); + EVAL0("al_destroy", { + rc = al_destroy(al); + }); mess = reclaim_result(S(baf), context); if (mess != NULL)