From 85231fd8cdf32d861e196d7dfa827b7239157817 Mon Sep 17 00:00:00 2001 From: rsc Date: Sun, 21 May 2006 20:49:16 +0000 Subject: fmt changes from Google --- src/lib9/testfmt.c | 114 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 114 insertions(+) create mode 100644 src/lib9/testfmt.c (limited to 'src/lib9/testfmt.c') diff --git a/src/lib9/testfmt.c b/src/lib9/testfmt.c new file mode 100644 index 00000000..c1a91400 --- /dev/null +++ b/src/lib9/testfmt.c @@ -0,0 +1,114 @@ +#include +#include +#include + +int failed; + +/* Consume argument and ignore it */ +int +Zflag(Fmt* f) +{ + if(va_arg(f->args, int)) + ; + return 1; /* it's a flag */ +} + +void +verify(char *s, char *t) +{ + if(strcmp(s, t) != 0){ + failed = 1; + fprintf(stderr, "error: (%s) != (%s)\n", s, t); + } + free(s); +} + +Rune lightsmiley = 0x263a; +Rune darksmiley = 0x263b; + +/* Test printer that loads unusual decimal point and separator */ +char* +mysmprint(char *fmt, ...) +{ + Fmt f; + + if(fmtstrinit(&f) < 0) + return 0; + va_start(f.args, fmt); + f.decimal = smprint("%C", lightsmiley); + f.thousands = smprint("%C", darksmiley); + f.grouping = "\1\2\3\4"; + if(dofmt(&f, fmt) < 0) + return 0; + va_end(f.args); + return fmtstrflush(&f); +} + + +void +main(int argc, char **argv) +{ + quotefmtinstall(); + fmtinstall('Z', Zflag); + fmtinstall(L'\x263a', Zflag); + + verify(smprint("hello world"), "hello world"); +#ifdef PLAN9PORT + verify(smprint("x: %ux", 0x87654321), "x: 87654321"); +#else + verify(smprint("x: %x", 0x87654321), "x: 87654321"); +#endif + verify(smprint("d: %d", 0x87654321), "d: -2023406815"); + verify(smprint("s: %s", "hi there"), "s: hi there"); + verify(smprint("q: %q", "hi i'm here"), "q: 'hi i''m here'"); + verify(smprint("c: %c", '!'), "c: !"); + verify(smprint("g: %g %g %g", 3.14159, 3.14159e10, 3.14159e-10), "g: 3.14159 3.14159e+10 3.14159e-10"); + verify(smprint("e: %e %e %e", 3.14159, 3.14159e10, 3.14159e-10), "e: 3.141590e+00 3.141590e+10 3.141590e-10"); + verify(smprint("f: %f %f %f", 3.14159, 3.14159e10, 3.14159e-10), "f: 3.141590 31415900000.000000 0.000000"); + verify(smprint("smiley: %C", (Rune)0x263a), "smiley: \xe2\x98\xba"); + verify(smprint("%g %.18g", 2e25, 2e25), "2e+25 2e+25"); + verify(smprint("%2.18g", 1.0), " 1"); + verify(smprint("%f", 3.1415927/4), "0.785398"); + verify(smprint("%d", 23), "23"); + verify(smprint("%i", 23), "23"); + verify(smprint("%Zi", 1234, 23), "23"); + + /* test $ reorderings */ + verify(smprint("%3$d %4$06d %2$d %1$d", 444, 333, 111, 222), "111 000222 333 444"); + verify(smprint("%3$Zd %5$06d %2$d %1$d", 444, 333, 555, 111, 222), "111 000222 333 444"); + verify(smprint("%3$d %4$*5$06d %2$d %1$d", 444, 333, 111, 222, 20), "111 000222 333 444"); + verify(smprint("%3$hd %4$*5$06d %2$d %1$d", 444, 333, (short)111, 222, 20), "111 000222 333 444"); + verify(smprint("%3$\xe2\x98\xba""d %5$06d %2$d %1$d", 444, 333, 555, 111, 222), "111 000222 333 444"); + + /* test %'d formats */ + verify(smprint("%'d %'d %'d", 1, 2222, 33333333), "1 2,222 33,333,333"); + verify(smprint("%'019d", 0), "000,000,000,000,000"); + verify(smprint("%'08d %'08d %'08d", 1, 2222, 33333333), "0,000,001 0,002,222 33,333,333"); +#ifdef PLAN9PORT + verify(smprint("%'ux %'uX %'ub", 0x11111111, 0xabcd1234, 12345), "1111:1111 ABCD:1234 11:0000:0011:1001"); +#else + verify(smprint("%'x %'X %'b", 0x11111111, 0xabcd1234, 12345), "1111:1111 ABCD:1234 11:0000:0011:1001"); +#endif + verify(smprint("%'lld %'lld %'lld", 1LL, 222222222LL, 3333333333333LL), "1 222,222,222 3,333,333,333,333"); + verify(smprint("%'019lld %'019lld %'019lld", 1LL, 222222222LL, 3333333333333LL), "000,000,000,000,001 000,000,222,222,222 003,333,333,333,333"); +#ifdef PLAN9PORT + verify(smprint("%'llux %'lluX %'llub", 0x111111111111LL, 0xabcd12345678LL, 112342345LL), "1111:1111:1111 ABCD:1234:5678 110:1011:0010:0011:0101:0100:1001"); +#else + verify(smprint("%'llx %'llX %'llb", 0x111111111111LL, 0xabcd12345678LL, 112342345LL), "1111:1111:1111 ABCD:1234:5678 110:1011:0010:0011:0101:0100:1001"); +#endif + + /* test %'d with custom (utf-8!) separators */ + /* x and b still use : */ + verify(mysmprint("%'d %'d %'d", 1, 2222, 33333333), "1 2\xe2\x98\xbb""22\xe2\x98\xbb""2 33\xe2\x98\xbb""333\xe2\x98\xbb""33\xe2\x98\xbb""3"); +#ifdef PLAN9PORT + verify(mysmprint("%'ux %'uX %'ub", 0x11111111, 0xabcd1234, 12345), "1111:1111 ABCD:1234 11:0000:0011:1001"); +#else + verify(mysmprint("%'x %'X %'b", 0x11111111, 0xabcd1234, 12345), "1111:1111 ABCD:1234 11:0000:0011:1001"); +#endif + verify(mysmprint("%'lld %'lld %'lld", 1LL, 222222222LL, 3333333333333LL), "1 222\xe2\x98\xbb""222\xe2\x98\xbb""22\xe2\x98\xbb""2 333\xe2\x98\xbb""3333\xe2\x98\xbb""333\xe2\x98\xbb""33\xe2\x98\xbb""3"); + verify(mysmprint("%'llx %'llX %'llb", 0x111111111111LL, 0xabcd12345678LL, 112342345LL), "1111:1111:1111 ABCD:1234:5678 110:1011:0010:0011:0101:0100:1001"); + + if(failed) + sysfatal("tests failed"); + exits(0); +} -- cgit v1.2.3