X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~mdw/git/mLib/blobdiff_plain/3618811496a6d131fd4bffa19e262c521d39e819..c81c35dfd10050ffef85d57dc2ad73f52f38a3f2:/struct/t/dstr-putf-test.c diff --git a/struct/t/dstr-putf-test.c b/struct/t/dstr-putf-test.c index 462371d..eb67e60 100644 --- a/struct/t/dstr-putf-test.c +++ b/struct/t/dstr-putf-test.c @@ -1,3 +1,32 @@ +/* -*-c-*- + * + * Test driver for universal hashing + * + * (c) 2009 Straylight/Edgeware + */ + +/*----- Licensing notice --------------------------------------------------* + * + * This file is part of the mLib utilities library. + * + * mLib is free software; you can redistribute it and/or modify + * it under the terms of the GNU Library General Public License as + * published by the Free Software Foundation; either version 2 of the + * License, or (at your option) any later version. + * + * mLib is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with mLib; if not, write to the Free + * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, + * MA 02111-1307, USA. + */ + +/*----- Header files ------------------------------------------------------*/ + #include "config.h" #include @@ -9,86 +38,117 @@ #include "dstr.h" #include "macros.h" +#include "report.h" +#include "tvec.h" + +/*----- Static variables --------------------------------------------------*/ -static int win = 0, lose = 0; +static struct tvec_state tvstate; static dstr d = DSTR_INIT; -static char buf[1024]; +static char strbuf[1024]; -static void check(const char *what, const char *want) -{ - if (STRCMP(want, ==, d.buf)) - win++; - else { - lose++; - fprintf(stderr, "test failed: %s\n expected: %s\n found: %s\n", - what, want, d.buf); - } -} +/*----- Utilities ---------------------------------------------------------*/ -static void PRINTF_LIKE(1, 2) format(const char *fmt, ...) +#define TESTGROUP(name) TVEC_TESTGROUP_TAG(grp, &tvstate, name) + +static PRINTF_LIKE(1, 2) int format(const char *fmt, ...) { va_list ap; + int n; + va_start(ap, fmt); dstr_reset(&d); - dstr_vputf(&d, fmt, &ap); + n = dstr_vputf(&d, fmt, &ap); va_end(ap); + return (n); } -static void PRINTF_LIKE(1, 2) prepare(const char *fmt, ...) +static PRINTF_LIKE(1, 2) void prepare(const char *fmt, ...) { va_list ap; int n; va_start(ap, fmt); #ifdef HAVE_SNPRINTF - n = vsnprintf(buf, sizeof(buf), fmt, ap); + n = vsnprintf(strbuf, sizeof(strbuf), fmt, ap); #else - n = vsprintf(buf, fmt, ap); + n = vsprintf(strbuf, fmt, ap); #endif - assert(0 <= n && n < sizeof(buf)); + assert(0 <= n && n < sizeof(strbuf)); } -#define TEST1(fmtargs) do { \ - format fmtargs; \ - prepare fmtargs; \ - check(#fmtargs, buf); \ +#define TEST_KNOWN(fmtargs, want) do { \ + int n; n = format fmtargs; \ + tvec_claimeq_int(&tvstate, n, strlen(want), \ + __FILE__, __LINE__, "format " #fmtargs); \ + tvec_claimeq_text(&tvstate, d.buf, d.len, want, strlen(want), \ + __FILE__, __LINE__, "format " #fmtargs); \ } while (0) -#define TEST2(fmtargs, want) do { \ - format fmtargs; \ - check(#fmtargs, want); \ +#define TEST_REF(fmtargs) do { \ + int n = format fmtargs; \ + prepare fmtargs; \ + tvec_claimeq_int(&tvstate, n, strlen(strbuf), \ + __FILE__, __LINE__, "format " #fmtargs); \ + tvec_claimeq_text(&tvstate, d.buf, d.len, strbuf, strlen(strbuf), \ + __FILE__, __LINE__, "format " #fmtargs); \ } while (0) -#define LENGTHY \ +#define LENGTHY \ "This is a rather longer string than the code is expecting: will it fit?" -int main(void) +/*----- Main program ------------------------------------------------------*/ + +int main(int argc, char *argv[]) { - TEST2(("Hello, world!"), "Hello, world!"); - TEST2(("just a ->%%<- sign"), "just a ->%<- sign"); - TEST2(("Testing, testing, %d, %d, %d.", 1, 2, 3), - "Testing, testing, 1, 2, 3."); - TEST2(("->%5d<-", 138), "-> 138<-"); - TEST2(("->%*d<-", 5, 138), "-> 138<-"); - TEST2(("->%-*d<-", 5, 138), "->138 <-"); - TEST2(("->%*d<-", -5, 138), "->138 <-"); - TEST2(("->%-*d<-", -5, 138), "->138 <-"); - TEST2(("->%.*s<-", 5, "truncate me"), "->trunc<-"); - TEST2(("->%.*s<-", -5, "don't truncate me"), "->don't truncate me<-"); - TEST2(("Truncation indirect: ->%.*s<-", 10, "a long string to be chopped"), - "Truncation indirect: ->a long str<-"); - TEST2(("%08lx:%s", 0x65604204ul, "tripe-ec"), "65604204:tripe-ec"); - TEST2(("%s", LENGTHY), LENGTHY); - - TEST1(("big float: ->%f<- and integer %d\n", DBL_MAX, 42)); - - TEST2(("Testing, testing, %3$d, %2$d, %1$d.", 3, 2, 1), - "Testing, testing, 1, 2, 3."); - TEST2(("Truncation indirect: ->%1$.*2$s<-", - "a long string to be chopped", 10), - "Truncation indirect: ->a long str<-"); - - if (!lose) printf("All tests successful.\n"); - else printf("FAILED %d of %d tests.\n", lose, win + lose); - return (!!lose); + struct tvec_test test; + int argpos; + + tvec_parseargs(argc, argv, &tvstate, &argpos, &tvec_adhocconfig); + if (argpos < argc) die(2, "no input files expected"); + tvec_adhoc(&tvstate, &test); + + TESTGROUP("basics") { + TEST_KNOWN(("Hello, world!"), "Hello, world!"); + TEST_KNOWN(("just a ->%%<- sign"), "just a ->%<- sign"); + } + + TESTGROUP("integers") { + TEST_KNOWN(("Testing, testing, %d, %d, %d.", 1, 2, 3), + "Testing, testing, 1, 2, 3."); + TEST_KNOWN(("->%5d<-", 138), "-> 138<-"); + TEST_KNOWN(("->%*d<-", 5, 138), "-> 138<-"); + TEST_KNOWN(("->%-*d<-", 5, 138), "->138 <-"); + TEST_KNOWN(("->%*d<-", -5, 138), "->138 <-"); + TEST_KNOWN(("->%-*d<-", -5, 138), "->138 <-"); + } + + TESTGROUP("strings") { + TEST_KNOWN(("->%.*s<-", 5, "truncate me"), "->trunc<-"); + TEST_KNOWN(("->%.*s<-", -5, "don't truncate me"), + "->don't truncate me<-"); + TEST_KNOWN(("Truncation indirect: ->%.*s<-", 10, + "a long string to be chopped"), + "Truncation indirect: ->a long str<-"); + } + + TESTGROUP("combinations") { + TEST_KNOWN(("%08lx:%s", 0x65604204ul, "tripe-ec"), "65604204:tripe-ec"); + TEST_REF(("big float: ->%f<- and integer %d\n", DBL_MAX, 42)); + } + + TESTGROUP("argument-order") { + TEST_KNOWN(("Testing, testing, %3$d, %2$d, %1$d.", 3, 2, 1), + "Testing, testing, 1, 2, 3."); + TEST_KNOWN(("Truncation indirect: ->%1$.*2$s<-", + "a long string to be chopped", 10), + "Truncation indirect: ->a long str<-"); + } + + TESTGROUP("misc") + TEST_KNOWN(("%s", LENGTHY), LENGTHY); + + return (tvec_end(&tvstate)); } + +/*----- That's all, folks -------------------------------------------------*/