Debian Codegraph @

terps/tads/tads3/test/data/sprintf.t

raw
1 #include <tads.h>
2 #include <bignum.h>
3
4 main(args)
5 {
6 test('Hello!');
7 test('Percent %% test!');
8
9 test('Integers: decimal=%d, unsigned=%u, octal=%o, hex=%x, '
10 + 'HEX=%X, binary=%b',
11 -123, -456, 03344, 0x55aa66bb, 0x88cc99dd, 0xf0f0);
12 test('Unsigned: u=%u, u=%u, hex=%x, hex=%x, octal=%o, octal=%o',
13 -12345, -12345.0, -255, -255.0, -1023, -1023.0);
14 test('Plus signs: %+d, %+d, %+d, %+d', 123, -456, 7890, 0);
15 test('Plus w/space: % d, % d, % d, % d', 123, -456, 7890, 0);
16 test('# format: %#x, %#x, %#X, %#X, %#o, %#o',
17 0, 0x123abc, 0, 0x345def, 0, 07654);
18
19 test('Roman numerals I: %r, %R, %r, %R, %r, %R',
20 0, -5, 100, 111, 3888, 4999);
21 test('Roman numerals II: %r, %R, %r, %R, %r, %R',
22 true, nil, 123.45, 678.90, ' 517!!', 9999);
23
24 test('Integers with field widths: [%8d] [%_ 8d] [%_*8d] [%016d] [%_*8x]',
25 100, 200, -1234, 4567, 0x400);
26 test('Field widths too small: [%4d] [%4d] [%4d] [%4x]',
27 1234, -123, 12345, 0xabcde);
28 test('Left align: [%-5d] [%-5d] [%-5d] [%-5d] [%_*-10d] [%- 5d]',
29 123, -1234, 12345, 123456, 87654, 987);
30
31 test('Integers with precisions: [%+.8d] [%.20d] [%.8x] [%.8o]',
32 123, -1234567890987.0, 0x456abc, 0334455);
33
34 test('Grouping: [%,d] [%,d] [%,d] [%,d] [%,8d] [%,10d] [%,10d]',
35 1, 12, 123, 1234, 1234567, 1234567, -1234567);
36
37 test('Positionals: %[3]d, %[2]d, %d, %d, %[1]d, %d', 1, 2, 3);
38 test('Positionals with widths: [%[2]6d], [%[1]06d]', 100, 200);
39
40 test('Strings as integers: %d, %x, %d', '12345', '65535', '987.654');
41 test('BigNumbers as integers: %d, %d, %d, %d, %d, %020x',
42 0.5, 0.5001, 1.5, 1.75, 9111222333.0, 2.0.raiseToPower(68)-1);
43
44 test('Strings: [%s] [%s] [%s] [%s] [%s]',
45 'This is a test string!', 100, 123.4567, nil);
46 test('Strings with widths: [%8s] [%8s] [%8s]',
47 'Short', 'Too long for the limit', 'Right on');
48 test('Strings w/widths, left-aligned: [%-8s] [%-8s] [%-8s]',
49 'Short', 'Too long for the limit', 'Right on');
50 test('Strings with limits: [%8.8s] [%8.8s] [%8.8s]',
51 'Short', 'Too long for the limit', 'Perfect!');
52
53 test('Chars: [%c] [%c] [%c] [%c]', 65, 'hello', 97.1, nil);
54
55 test('f format: %f, %f, %f, %f',
56 1234, 321.123456789, 3456789, 3.456e-20);
57 test('f with precision: %.10f, %.20f, %.3f',
58 1234.567890987654321, 8899.001122334455667788998877, 789.87654321);
59 test('f with zero precision: %.0f, %.0f, %.f',
60 1234.567890987654321, 8899.001122334455667788998877, 789.87654321);
61 test('f with zero precision and #: %#.0f, %#.0f, %#.f',
62 1234.567890987654321, 8899.001122334455667788998877, 789.87654321);
63 test('e format: %e, %e, %e, %e',
64 123, -999444777, 1234567.890, 1000000.0);
65 test('E format: %E, %E, %E',
66 0.00456, 1.87654321e20, -3.14159265e-4567);
67 test('e with precision: %.10e, %.3e, %.6e',
68 0.012345, 0.7766554433, 777.888999111);
69 test('e with zero precision: %.0e, %.e, %.e',
70 0.012345, 0.7766554433, 777.888999111);
71 test('e with zero precision and #: %#.0e, %#.e, %#.e',
72 0.012345, 0.7766554433, 777.888999111);
73 test('Floats with signs: %+f, %+f, %+f', 1234.5678, -9876.54321, 0);
74 test('Floats with spaces: % f, % f, % f', 1234.5678, -9876.54321, 0);
75 test('Floats with widths: [%10f] [%10f]', 10203040, .24681357);
76 test('g format: %g, %g, %g, %g, %g',
77 100, 123456789, -23456789, .0001234567, .00012345678);
78 test('G format: %G, %G, %G',
79 0.00012345, 0.000012345, 0.0000012345);
80 test('More g: %g, %g, %g, %g',
81 1234, 12345, 123456, 1234567);
82 test('g with precision: %.3g, %.8g, %.5g',
83 12.789, 123.456, 123456.789);
84 test('#g: %#g, %#g, %#g, %#g, %#g, %#g',
85 1234, 12345, 123456, 1234567, 123000, 123e20);
86 }
87
88 test(fmt, [args])
89 {
90 "<<fmt.htmlify()>>\n-&gt; <<
91 sprintf(fmt, args...).findReplace(' ', '\ ').htmlify()>>\b";
92 }