chiark / gitweb /
tests and fixes for printf.c
authorRichard Kettlewell <rjk@greenend.org.uk>
Sat, 1 Dec 2007 16:09:16 +0000 (16:09 +0000)
committerRichard Kettlewell <rjk@greenend.org.uk>
Sat, 1 Dec 2007 16:09:16 +0000 (16:09 +0000)
lib/printf.c
lib/test.c

index d52a1cc7fa595e8d6f860ccaa0ec5f0924534ba1..15478ce0a71c53b4277d3310d6a09b71382c8ac8 100644 (file)
@@ -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 */
index 3df6a9f76a87e6c6cbf9cfae48160db657f11128..e55545aadabc2aa603b043433758e8d67413ccbf 100644 (file)
@@ -33,6 +33,7 @@
 #include <unistd.h>
 #include <signal.h>
 #include <sys/wait.h>
+#include <stddef.h>
 
 #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();