From 0c740e5956fc1f9d7b714d4a621819f1c96c7254 Mon Sep 17 00:00:00 2001 Message-Id: <0c740e5956fc1f9d7b714d4a621819f1c96c7254.1714628457.git.mdw@distorted.org.uk> From: Mark Wooding Date: Sat, 1 Dec 2007 16:09:16 +0000 Subject: [PATCH] tests and fixes for printf.c Organization: Straylight/Edgeware From: Richard Kettlewell --- lib/printf.c | 14 +++---- lib/test.c | 104 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 111 insertions(+), 7 deletions(-) diff --git a/lib/printf.c b/lib/printf.c index d52a1cc..15478ce 100644 --- a/lib/printf.c +++ b/lib/printf.c @@ -284,11 +284,11 @@ static int output_integer(struct state *s, struct conversion *c) { * '-' beats '0'. */ if(c->flags & f_left) { - if(pad && do_pad(s, ' ', pad) < 0) return -1; if(sign && do_write(s, &sign, 1)) return -1; if(xform && do_write(s, c->specifier->xform, xform)) return -1; if(prec && do_pad(s, '0', prec) < 0) return -1; if(ndigits && do_write(s, digits + dp, ndigits)) return -1; + if(pad && do_pad(s, ' ', pad) < 0) return -1; } else if(c->flags & f_zero) { if(sign && do_write(s, &sign, 1)) return -1; if(xform && do_write(s, c->specifier->xform, xform)) return -1; @@ -296,11 +296,11 @@ static int output_integer(struct state *s, struct conversion *c) { if(prec && do_pad(s, '0', prec) < 0) return -1; if(ndigits && do_write(s, digits + dp, ndigits)) return -1; } else { + if(pad && do_pad(s, ' ', pad) < 0) return -1; if(sign && do_write(s, &sign, 1)) return -1; if(xform && do_write(s, c->specifier->xform, xform)) return -1; if(prec && do_pad(s, '0', prec) < 0) return -1; if(ndigits && do_write(s, digits + dp, ndigits)) return -1; - if(pad && do_pad(s, ' ', pad) < 0) return -1; } return 0; } @@ -323,11 +323,11 @@ static int output_string(struct state *s, struct conversion *c) { } else pad = 0; if(c->flags & f_left) { - if(pad && do_pad(s, ' ', pad) < 0) return -1; if(do_write(s, str, len) < 0) return -1; + if(pad && do_pad(s, ' ', pad) < 0) return -1; } else { - if(do_write(s, str, len) < 0) return -1; if(pad && do_pad(s, ' ', pad) < 0) return -1; + if(do_write(s, str, len) < 0) return -1; } return 0; @@ -344,11 +344,11 @@ static int output_char(struct state *s, struct conversion *c) { } else pad = 0; if(c->flags & f_left) { - if(pad && do_pad(s, ' ', pad) < 0) return -1; if(do_write(s, &ch, 1) < 0) return -1; + if(pad && do_pad(s, ' ', pad) < 0) return -1; } else { - if(do_write(s, &ch, 1) < 0) return -1; if(pad && do_pad(s, ' ', pad) < 0) return -1; + if(do_write(s, &ch, 1) < 0) return -1; } return 0; } @@ -415,7 +415,7 @@ static int parse_conversion(struct conversion *c, const char *ptr) { ++ptr; c->precision = -1; } else - return -1; + c->precision = 0; c->flags |= f_precision; } /* length modifier */ diff --git a/lib/test.c b/lib/test.c index 3df6a9f..e55545a 100644 --- a/lib/test.c +++ b/lib/test.c @@ -33,6 +33,7 @@ #include #include #include +#include #include "utf8.h" #include "mem.h" @@ -53,6 +54,7 @@ #include "syscalls.h" #include "kvp.h" #include "sink.h" +#include "printf.h" static int tests, errors; static int fail_first; @@ -1019,6 +1021,107 @@ static void test_sink(void) { check_string(d->vec, "test: 999\nwibble: foobar\n"); } +static const char *do_printf(const char *fmt, ...) { + va_list ap; + char *s; + int rc; + + va_start(ap, fmt); + rc = byte_vasprintf(&s, fmt, ap); + va_end(ap); + if(rc < 0) + return 0; + return s; +} + +static void test_printf(void) { + char c; + short s; + int i; + long l; + long long ll; + intmax_t m; + ssize_t ssz; + ptrdiff_t p; + + fprintf(stderr, "test_printf\n"); + check_string(do_printf("%d", 999), "999"); + check_string(do_printf("%d", -999), "-999"); + check_string(do_printf("%i", 999), "999"); + check_string(do_printf("%i", -999), "-999"); + check_string(do_printf("%u", 999), "999"); + check_string(do_printf("%2u", 999), "999"); + check_string(do_printf("%10u", 999), " 999"); + check_string(do_printf("%-10u", 999), "999 "); + check_string(do_printf("%010u", 999), "0000000999"); + check_string(do_printf("%-10d", -999), "-999 "); + check_string(do_printf("%-010d", -999), "-999 "); /* "-" beats "0" */ + check_string(do_printf("%66u", 999), " 999"); + check_string(do_printf("%o", 999), "1747"); + check_string(do_printf("%#o", 999), "01747"); + check_string(do_printf("%#o", 0), "0"); + check_string(do_printf("%x", 999), "3e7"); + check_string(do_printf("%#x", 999), "0x3e7"); + check_string(do_printf("%#X", 999), "0X3E7"); + check_string(do_printf("%#x", 0), "0"); + check_string(do_printf("%hd", (short)999), "999"); + check_string(do_printf("%hhd", (short)99), "99"); + check_string(do_printf("%ld", 100000L), "100000"); + check_string(do_printf("%lld", 10000000000LL), "10000000000"); + check_string(do_printf("%qd", 10000000000LL), "10000000000"); + check_string(do_printf("%jd", (intmax_t)10000000000LL), "10000000000"); + check_string(do_printf("%zd", (ssize_t)2000000000), "2000000000"); + check_string(do_printf("%td", (ptrdiff_t)2000000000), "2000000000"); + check_string(do_printf("%hu", (short)999), "999"); + check_string(do_printf("%hhu", (short)99), "99"); + check_string(do_printf("%lu", 100000L), "100000"); + check_string(do_printf("%llu", 10000000000LL), "10000000000"); + check_string(do_printf("%ju", (uintmax_t)10000000000LL), "10000000000"); + check_string(do_printf("%zu", (size_t)2000000000), "2000000000"); + check_string(do_printf("%tu", (ptrdiff_t)2000000000), "2000000000"); + check_string(do_printf("%p", (void *)0x100), "0x100"); + check_string(do_printf("%s", "wibble"), "wibble"); + check_string(do_printf("%s-%s", "wibble", "wobble"), "wibble-wobble"); + check_string(do_printf("%10s", "wibble"), " wibble"); + check_string(do_printf("%010s", "wibble"), " wibble"); /* 0 ignored for %s */ + check_string(do_printf("%-10s", "wibble"), "wibble "); + check_string(do_printf("%2s", "wibble"), "wibble"); + check_string(do_printf("%.2s", "wibble"), "wi"); + check_string(do_printf("%.2s", "w"), "w"); + check_string(do_printf("%4.2s", "wibble"), " wi"); + check_string(do_printf("%c", 'a'), "a"); + check_string(do_printf("%4c", 'a'), " a"); + check_string(do_printf("%-4c", 'a'), "a "); + check_string(do_printf("%*c", 0, 'a'), "a"); + check_string(do_printf("x%hhny", &c), "xy"); + insist(c == 1); + check_string(do_printf("xx%hnyy", &s), "xxyy"); + insist(s == 2); + check_string(do_printf("xxx%nyyy", &i), "xxxyyy"); + insist(i == 3); + check_string(do_printf("xxxx%lnyyyy", &l), "xxxxyyyy"); + insist(l == 4); + check_string(do_printf("xxxxx%llnyyyyy", &ll), "xxxxxyyyyy"); + insist(ll == 5); + check_string(do_printf("xxxxxx%jnyyyyyy", &m), "xxxxxxyyyyyy"); + insist(m == 6); + check_string(do_printf("xxxxxxx%znyyyyyyy", &ssz), "xxxxxxxyyyyyyy"); + insist(ssz == 7); + check_string(do_printf("xxxxxxxx%tnyyyyyyyy", &p), "xxxxxxxxyyyyyyyy"); + insist(p == 8); + check_string(do_printf("%*d", 5, 99), " 99"); + check_string(do_printf("%*d", -5, 99), "99 "); + check_string(do_printf("%.*d", 5, 99), "00099"); + check_string(do_printf("%.*d", -5, 99), "99"); + check_string(do_printf("%.0d", 0), ""); + check_string(do_printf("%.d", 0), ""); + check_string(do_printf("%.d", 0), ""); + check_string(do_printf("%%"), "%"); + check_string(do_printf("wibble"), "wibble"); + insist(do_printf("%") == 0); + insist(do_printf("%=") == 0); +} + int main(void) { fail_first = !!getenv("FAIL_FIRST"); insist('\n' == 0x0A); @@ -1055,6 +1158,7 @@ int main(void) { /* mixer.c */ /* plugin.c */ /* printf.c */ + test_printf(); /* queue.c */ /* sink.c */ test_sink(); -- [mdw]