1 ! Copyright (C) 2008 John Benediktsson
2 ! See http://factorcode.org/license.txt for BSD license
4 USING: calendar kernel formatting tools.test ;
8 [ "%s" printf ] must-infer
9 [ "%s" sprintf ] must-infer
11 [ t ] [ "" "" sprintf = ] unit-test
12 [ t ] [ "asdf" "asdf" sprintf = ] unit-test
13 [ t ] [ "10" 10 "%d" sprintf = ] unit-test
14 [ t ] [ "+10" 10 "%+d" sprintf = ] unit-test
15 [ t ] [ "-10" -10 "%d" sprintf = ] unit-test
16 [ t ] [ " -10" -10 "%5d" sprintf = ] unit-test
17 [ t ] [ "-0010" -10 "%05d" sprintf = ] unit-test
18 [ t ] [ "+0010" 10 "%+05d" sprintf = ] unit-test
19 [ t ] [ "123.456000" 123.456 "%f" sprintf = ] unit-test
20 [ t ] [ "2.44" 2.436 "%.2f" sprintf = ] unit-test
21 [ t ] [ "123.10" 123.1 "%01.2f" sprintf = ] unit-test
22 [ t ] [ "1.2346" 1.23456789 "%.4f" sprintf = ] unit-test
23 [ t ] [ " 1.23" 1.23456789 "%6.2f" sprintf = ] unit-test
24 [ t ] [ "1.234000e+08" 123400000 "%e" sprintf = ] unit-test
25 [ t ] [ "-1.234000e+08" -123400000 "%e" sprintf = ] unit-test
26 [ t ] [ "1.234567e+08" 123456700 "%e" sprintf = ] unit-test
27 [ t ] [ "3.625e+08" 362525200 "%.3e" sprintf = ] unit-test
28 [ t ] [ "2.500000e-03" 0.0025 "%e" sprintf = ] unit-test
29 [ t ] [ "2.500000E-03" 0.0025 "%E" sprintf = ] unit-test
30 [ t ] [ " 1.0E+01" 10 "%10.1E" sprintf = ] unit-test
31 [ t ] [ " -1.0E+01" -10 "%10.1E" sprintf = ] unit-test
32 [ t ] [ " -1.0E+01" -10 "%+10.1E" sprintf = ] unit-test
33 [ t ] [ " +1.0E+01" 10 "%+10.1E" sprintf = ] unit-test
34 [ t ] [ "-001.0E+01" -10 "%+010.1E" sprintf = ] unit-test
35 [ t ] [ "+001.0E+01" 10 "%+010.1E" sprintf = ] unit-test
36 [ t ] [ "ff" HEX: ff "%x" sprintf = ] unit-test
37 [ t ] [ "FF" HEX: ff "%X" sprintf = ] unit-test
38 [ t ] [ "0f" HEX: f "%02x" sprintf = ] unit-test
39 [ t ] [ "0F" HEX: f "%02X" sprintf = ] unit-test
41 2008 9 10 "%04d-%02d-%02d" sprintf = ] unit-test
42 [ t ] [ "Hello, World!"
43 "Hello, World!" "%s" sprintf = ] unit-test
45 "printf test" sprintf = ] unit-test
46 [ t ] [ "char a = 'a'"
47 CHAR: a "char %c = 'a'" sprintf = ] unit-test
48 [ t ] [ "00" HEX: 0 "%02x" sprintf = ] unit-test
49 [ t ] [ "ff" HEX: ff "%02x" sprintf = ] unit-test
50 [ t ] [ "0 message(s)"
51 0 "message" "%d %s(s)" sprintf = ] unit-test
52 [ t ] [ "0 message(s) with %"
53 0 "message" "%d %s(s) with %%" sprintf = ] unit-test
54 [ t ] [ "justif: \"left \""
55 "left" "justif: \"%-10s\"" sprintf = ] unit-test
56 [ t ] [ "justif: \" right\""
57 "right" "justif: \"%10s\"" sprintf = ] unit-test
58 [ t ] [ " 3: 0003 zero padded"
59 3 " 3: %04d zero padded" sprintf = ] unit-test
60 [ t ] [ " 3: 3 left justif"
61 3 " 3: %-4d left justif" sprintf = ] unit-test
62 [ t ] [ " 3: 3 right justif"
63 3 " 3: %4d right justif" sprintf = ] unit-test
64 [ t ] [ " -3: -003 zero padded"
65 -3 " -3: %04d zero padded" sprintf = ] unit-test
66 [ t ] [ " -3: -3 left justif"
67 -3 " -3: %-4d left justif" sprintf = ] unit-test
68 [ t ] [ " -3: -3 right justif"
69 -3 " -3: %4d right justif" sprintf = ] unit-test
70 [ t ] [ "There are 10 monkeys in the kitchen"
71 10 "kitchen" "There are %d monkeys in the %s" sprintf = ] unit-test
72 [ f ] [ "%d" 10 "%d" sprintf = ] unit-test
73 [ t ] [ "[monkey]" "monkey" "[%s]" sprintf = ] unit-test
74 [ t ] [ "[ monkey]" "monkey" "[%10s]" sprintf = ] unit-test
75 [ t ] [ "[monkey ]" "monkey" "[%-10s]" sprintf = ] unit-test
76 [ t ] [ "[0000monkey]" "monkey" "[%010s]" sprintf = ] unit-test
77 [ t ] [ "[####monkey]" "monkey" "[%'#10s]" sprintf = ] unit-test
78 [ t ] [ "[many monke]" "many monkeys" "[%10.10s]" sprintf = ] unit-test
81 [ "%H:%M:%S" strftime ] must-infer
83 : testtime ( -- timestamp )
84 2008 10 9 12 3 15 instant <timestamp> ;
86 [ t ] [ "12:03:15" testtime "%H:%M:%S" strftime = ] unit-test
87 [ t ] [ "12:03:15" testtime "%X" strftime = ] unit-test
88 [ t ] [ "10/09/2008" testtime "%m/%d/%Y" strftime = ] unit-test
89 [ t ] [ "10/09/2008" testtime "%x" strftime = ] unit-test
90 [ t ] [ "10/09/08" testtime "%m/%d/%y" strftime = ] unit-test
91 [ t ] [ "Thu" testtime "%a" strftime = ] unit-test
92 [ t ] [ "Thursday" testtime "%A" strftime = ] unit-test
93 [ t ] [ "Oct" testtime "%b" strftime = ] unit-test
94 [ t ] [ "October" testtime "%B" strftime = ] unit-test
95 [ t ] [ "Thu Oct 09 12:03:15 2008" testtime "%c" strftime = ] unit-test
96 [ t ] [ "PM" testtime "%p" strftime = ] unit-test