#include #include #include #include typedef struct Complex { double r, i; } Complex; int Xfmt(fmt·State *io) { Complex c; c = va_arg(io->args, Complex); return fmt·write(io, "(real=%g,imag=%g)", c.r, c.i); } int main(int argc, char *argv[]) { fmt·print("basic tests\n"); fmt·print("\tx: %x\n", 0x87654321); fmt·print("\tu: %u\n", 0x87654321); fmt·print("\td: %d\n", 0x87654321); fmt·print("\ts: %s\n", "hi there"); fmt·print("\tc: %c\n", '!'); fmt·print("\tg: %g %g %g\n", 3.14159, 3.14159e10, 3.14159e-10); fmt·print("\te: %e %e %e\n", 3.14159, 3.14159e10, 3.14159e-10); fmt·print("\tf: %f %f %f\n", 3.14159, 3.14159e10, 3.14159e-10); fmt·print("\tsmiley: %C\n", (rune)0x263a); fmt·print("\t%g %.18g\n", 2e25, 2e25); fmt·print("\t%2.18g\n", 1.0); fmt·print("\t%2.18f\n", 1.0); fmt·print("\t%f\n", 3.1415927/4); fmt·print("\t%d\n", 23); fmt·print("\t%i\n", 23); fmt·print("\t%0.10d\n", 12345); fmt·print("%%4%%d tests\n"); fmt·print("\t%3$d %4$06d %2$d %1$d\n", 444, 333, 111, 222); fmt·print("\t%3$d %4$06d %2$d %1$d\n", 444, 333, 111, 222); fmt·print("\t%3$d %4$*5$06d %2$d %1$d\n", 444, 333, 111, 222, 20); fmt·print("\t%3$hd %4$*5$06d %2$d %1$d\n", 444, 333, (short)111, 222, 20); fmt·print("\t%3$lld %4$*5$06d %2$d %1$d\n", 444, 333, 111LL, 222, 20); /* test %'d formats */ fmt·print("%%'%%d tests\n"); fmt·print("\t%'d %'d %'d\n", 1, 2222, 33333333); fmt·print("\t%'019d\n", 0); fmt·print("\t%08d %08d %08d\n", 1, 2222, 33333333); fmt·print("\t%'08d %'08d %'08d\n", 1, 2222, 33333333); fmt·print("\t%'x %'X %'b\n", 0x11111111, 0xabcd1234, 12345); fmt·print("\t%'lld %'lld %'lld\n", 1LL, 222222222LL, 3333333333333LL); fmt·print("\t%019lld %019lld %019lld\n", 1LL, 222222222LL, 3333333333333LL); fmt·print("\t%'019lld %'019lld %'019lld\n", 1LL, 222222222LL, 3333333333333LL); fmt·print("\t%'020lld %'020lld %'020lld\n", 1LL, 222222222LL, 3333333333333LL); fmt·print("\t%'llx %'llX %'llb\n", 0x111111111111LL, 0xabcd12345678LL, 112342345LL); /* test precision */ fmt·print("precision tests\n"); fmt·print("%020.10d\n", 100); /* test install */ fmt·install('X', Xfmt); Complex c = { 1.5, -2.3 }; fmt·print("x = %X\n", c); return 0; }