aboutsummaryrefslogtreecommitdiff
path: root/src/lib9/testfmt.c
diff options
context:
space:
mode:
authorrsc <devnull@localhost>2006-05-21 20:49:16 +0000
committerrsc <devnull@localhost>2006-05-21 20:49:16 +0000
commit85231fd8cdf32d861e196d7dfa827b7239157817 (patch)
tree467cd12c69cbb79e90e28a9750a4397cc3b80143 /src/lib9/testfmt.c
parent8d7133308db580d2356d5d1dd30f0b9a1f0a7417 (diff)
downloadplan9port-85231fd8cdf32d861e196d7dfa827b7239157817.tar.gz
plan9port-85231fd8cdf32d861e196d7dfa827b7239157817.tar.bz2
plan9port-85231fd8cdf32d861e196d7dfa827b7239157817.zip
fmt changes from Google
Diffstat (limited to 'src/lib9/testfmt.c')
-rw-r--r--src/lib9/testfmt.c114
1 files changed, 114 insertions, 0 deletions
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 <u.h>
+#include <libc.h>
+#include <stdio.h>
+
+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);
+}