* '-' 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;
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;
}
} 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;
} 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;
}
++ptr;
c->precision = -1;
} else
- return -1;
+ c->precision = 0;
c->flags |= f_precision;
}
/* length modifier */
#include <unistd.h>
#include <signal.h>
#include <sys/wait.h>
+#include <stddef.h>
#include "utf8.h"
#include "mem.h"
#include "syscalls.h"
#include "kvp.h"
#include "sink.h"
+#include "printf.h"
static int tests, errors;
static int fail_first;
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);
/* mixer.c */
/* plugin.c */
/* printf.c */
+ test_printf();
/* queue.c */
/* sink.c */
test_sink();