aboutsummaryrefslogtreecommitdiff
path: root/sys/libfmt/test.c
blob: d81a62ef1ea2a4c8c0ed5b2cc077e8c62cc0d13c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <u.h>
#include <base.h>
#include <libutf.h>
#include <libfmt.h>

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;

}