1 // Copyright 2013 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 // xfail-fast: check-fast screws up repr paths
16 use std::rt::io::Decorator;
17 use std::rt::io::mem::MemWriter;
19 use std::rt::io::Writer;
26 impl fmt::Signed for A {
27 fn fmt(_: &A, f: &mut fmt::Formatter) { f.buf.write("aloha".as_bytes()); }
29 impl fmt::Signed for B {
30 fn fmt(_: &B, f: &mut fmt::Formatter) { f.buf.write("adios".as_bytes()); }
33 macro_rules! t(($a:expr, $b:expr) => { assert_eq!($a, $b.to_owned()) })
37 // Make sure there's a poly formatter that takes anything
38 t!(format!("{:?}", 1), "1");
39 t!(format!("{:?}", A), "A");
40 t!(format!("{:?}", ()), "()");
41 t!(format!("{:?}", @(~1, "foo")), "@(~1, \"foo\")");
43 // Various edge cases without formats
45 t!(format!("hello"), "hello");
46 t!(format!("hello \\{"), "hello {");
48 // default formatters should work
49 t!(format!("{}", 1i), "1");
50 t!(format!("{}", 1i8), "1");
51 t!(format!("{}", 1i16), "1");
52 t!(format!("{}", 1i32), "1");
53 t!(format!("{}", 1i64), "1");
54 t!(format!("{}", 1u), "1");
55 t!(format!("{}", 1u8), "1");
56 t!(format!("{}", 1u16), "1");
57 t!(format!("{}", 1u32), "1");
58 t!(format!("{}", 1u64), "1");
59 t!(format!("{}", 1.0f), "1");
60 t!(format!("{}", 1.0f32), "1");
61 t!(format!("{}", 1.0f64), "1");
62 t!(format!("{}", "a"), "a");
63 t!(format!("{}", ~"a"), "a");
64 t!(format!("{}", @"a"), "a");
65 t!(format!("{}", false), "false");
66 t!(format!("{}", 'a'), "a");
68 // At least exercise all the formats
69 t!(format!("{:b}", true), "true");
70 t!(format!("{:c}", '☃'), "☃");
71 t!(format!("{:d}", 10), "10");
72 t!(format!("{:i}", 10), "10");
73 t!(format!("{:u}", 10u), "10");
74 t!(format!("{:o}", 10u), "12");
75 t!(format!("{:x}", 10u), "a");
76 t!(format!("{:X}", 10u), "A");
77 t!(format!("{:s}", "foo"), "foo");
78 t!(format!("{:s}", ~"foo"), "foo");
79 t!(format!("{:s}", @"foo"), "foo");
80 t!(format!("{:p}", 0x1234 as *int), "0x1234");
81 t!(format!("{:p}", 0x1234 as *mut int), "0x1234");
82 t!(format!("{:d}", A), "aloha");
83 t!(format!("{:d}", B), "adios");
84 t!(format!("foo {:s} ☃☃☃☃☃☃", "bar"), "foo bar ☃☃☃☃☃☃");
85 t!(format!("{1} {0}", 0, 1), "1 0");
86 t!(format!("{foo} {bar}", foo=0, bar=1), "0 1");
87 t!(format!("{foo} {1} {bar} {0}", 0, 1, foo=2, bar=3), "2 1 3 0");
88 t!(format!("{} {0:s}", "a"), "a a");
89 t!(format!("{} {0}", "a"), "a a");
90 t!(format!("{foo_bar}", foo_bar=1), "1");
92 // Methods should probably work
93 t!(format!("{0, plural, =1{a#} =2{b#} zero{c#} other{d#}}", 0u), "c0");
94 t!(format!("{0, plural, =1{a#} =2{b#} zero{c#} other{d#}}", 1u), "a1");
95 t!(format!("{0, plural, =1{a#} =2{b#} zero{c#} other{d#}}", 2u), "b2");
96 t!(format!("{0, plural, =1{a#} =2{b#} zero{c#} other{d#}}", 3u), "d3");
97 t!(format!("{0, select, a{a#} b{b#} c{c#} other{d#}}", "a"), "aa");
98 t!(format!("{0, select, a{a#} b{b#} c{c#} other{d#}}", "b"), "bb");
99 t!(format!("{0, select, a{a#} b{b#} c{c#} other{d#}}", "c"), "cc");
100 t!(format!("{0, select, a{a#} b{b#} c{c#} other{d#}}", "d"), "dd");
101 t!(format!("{1, select, a{#{0:s}} other{#{1}}}", "b", "a"), "ab");
102 t!(format!("{1, select, a{#{0}} other{#{1}}}", "c", "b"), "bb");
104 // Formatting strings and their arguments
105 t!(format!("{:s}", "a"), "a");
106 t!(format!("{:4s}", "a"), "a ");
107 t!(format!("{:>4s}", "a"), " a");
108 t!(format!("{:<4s}", "a"), "a ");
109 t!(format!("{:.4s}", "a"), "a");
110 t!(format!("{:4.4s}", "a"), "a ");
111 t!(format!("{:4.4s}", "aaaaaaaaaaaaaaaaaa"), "aaaa");
112 t!(format!("{:<4.4s}", "aaaaaaaaaaaaaaaaaa"), "aaaa");
113 t!(format!("{:>4.4s}", "aaaaaaaaaaaaaaaaaa"), "aaaa");
114 t!(format!("{:>10.4s}", "aaaaaaaaaaaaaaaaaa"), "aaaa");
115 t!(format!("{:2.4s}", "aaaaa"), "aaaa");
116 t!(format!("{:2.4s}", "aaaa"), "aaaa");
117 t!(format!("{:2.4s}", "aaa"), "aaa");
118 t!(format!("{:2.4s}", "aa"), "aa");
119 t!(format!("{:2.4s}", "a"), "a ");
120 t!(format!("{:0>2s}", "a"), "0a");
121 t!(format!("{:.*s}", 4, "aaaaaaaaaaaaaaaaaa"), "aaaa");
122 t!(format!("{:.1$s}", "aaaaaaaaaaaaaaaaaa", 4), "aaaa");
123 t!(format!("{:1$s}", "a", 4), "a ");
124 t!(format!("{:-#s}", "a"), "a");
125 t!(format!("{:+#s}", "a"), "a");
127 // Formatting integers should select the right implementation based off the
128 // type of the argument. Also, hex/octal/binary should be defined for
129 // integers, but they shouldn't emit the negative sign.
130 t!(format!("{:d}", -1i), "-1");
131 t!(format!("{:d}", -1i8), "-1");
132 t!(format!("{:d}", -1i16), "-1");
133 t!(format!("{:d}", -1i32), "-1");
134 t!(format!("{:d}", -1i64), "-1");
135 t!(format!("{:t}", 1i), "1");
136 t!(format!("{:t}", 1i8), "1");
137 t!(format!("{:t}", 1i16), "1");
138 t!(format!("{:t}", 1i32), "1");
139 t!(format!("{:t}", 1i64), "1");
140 t!(format!("{:x}", 1i), "1");
141 t!(format!("{:x}", 1i8), "1");
142 t!(format!("{:x}", 1i16), "1");
143 t!(format!("{:x}", 1i32), "1");
144 t!(format!("{:x}", 1i64), "1");
145 t!(format!("{:X}", 1i), "1");
146 t!(format!("{:X}", 1i8), "1");
147 t!(format!("{:X}", 1i16), "1");
148 t!(format!("{:X}", 1i32), "1");
149 t!(format!("{:X}", 1i64), "1");
150 t!(format!("{:o}", 1i), "1");
151 t!(format!("{:o}", 1i8), "1");
152 t!(format!("{:o}", 1i16), "1");
153 t!(format!("{:o}", 1i32), "1");
154 t!(format!("{:o}", 1i64), "1");
156 t!(format!("{:u}", 1u), "1");
157 t!(format!("{:u}", 1u8), "1");
158 t!(format!("{:u}", 1u16), "1");
159 t!(format!("{:u}", 1u32), "1");
160 t!(format!("{:u}", 1u64), "1");
161 t!(format!("{:t}", 1u), "1");
162 t!(format!("{:t}", 1u8), "1");
163 t!(format!("{:t}", 1u16), "1");
164 t!(format!("{:t}", 1u32), "1");
165 t!(format!("{:t}", 1u64), "1");
166 t!(format!("{:x}", 1u), "1");
167 t!(format!("{:x}", 1u8), "1");
168 t!(format!("{:x}", 1u16), "1");
169 t!(format!("{:x}", 1u32), "1");
170 t!(format!("{:x}", 1u64), "1");
171 t!(format!("{:X}", 1u), "1");
172 t!(format!("{:X}", 1u8), "1");
173 t!(format!("{:X}", 1u16), "1");
174 t!(format!("{:X}", 1u32), "1");
175 t!(format!("{:X}", 1u64), "1");
176 t!(format!("{:o}", 1u), "1");
177 t!(format!("{:o}", 1u8), "1");
178 t!(format!("{:o}", 1u16), "1");
179 t!(format!("{:o}", 1u32), "1");
180 t!(format!("{:o}", 1u64), "1");
182 // Test the flags for formatting integers
183 t!(format!("{:3d}", 1), " 1");
184 t!(format!("{:>3d}", 1), " 1");
185 t!(format!("{:>+3d}", 1), " +1");
186 t!(format!("{:<3d}", 1), "1 ");
187 t!(format!("{:#d}", 1), "1");
188 t!(format!("{:#x}", 10), "0xa");
189 t!(format!("{:#X}", 10), "0xA");
190 t!(format!("{:#5x}", 10), " 0xa");
191 t!(format!("{:#o}", 10), "0o12");
192 t!(format!("{:08x}", 10), "0000000a");
193 t!(format!("{:8x}", 10), " a");
194 t!(format!("{:<8x}", 10), "a ");
195 t!(format!("{:>8x}", 10), " a");
196 t!(format!("{:#08x}", 10), "0x00000a");
197 t!(format!("{:08d}", -10), "-0000010");
198 t!(format!("{:x}", -1u8), "ff");
199 t!(format!("{:X}", -1u8), "FF");
200 t!(format!("{:t}", -1u8), "11111111");
201 t!(format!("{:o}", -1u8), "377");
202 t!(format!("{:#x}", -1u8), "0xff");
203 t!(format!("{:#X}", -1u8), "0xFF");
204 t!(format!("{:#t}", -1u8), "0b11111111");
205 t!(format!("{:#o}", -1u8), "0o377");
207 // Signed combinations
208 t!(format!("{:+5d}", 1), " +1");
209 t!(format!("{:+5d}", -1), " -1");
210 t!(format!("{:05d}", 1), "00001");
211 t!(format!("{:05d}", -1), "-0001");
212 t!(format!("{:+05d}", 1), "+0001");
213 t!(format!("{:+05d}", -1), "-0001");
216 t!(format!("{:f}", 1.0f), "1");
217 t!(format!("{:f}", 1.0f32), "1");
218 t!(format!("{:f}", 1.0f64), "1");
219 t!(format!("{:.3f}", 1.0f), "1.000");
220 t!(format!("{:10.3f}", 1.0f), " 1.000");
221 t!(format!("{:+10.3f}", 1.0f), " +1.000");
222 t!(format!("{:+10.3f}", -1.0f), " -1.000");
225 t!(format!("\\{"), "{");
226 t!(format!("\\}"), "}");
227 t!(format!("\\#"), "#");
228 t!(format!("\\\\"), "\\");
233 // make sure that format! doesn't move out of local variables
238 // make sure that format! doesn't cause spurious unused-unsafe warnings when
239 // it's inside of an outer unsafe block
241 let a: int = ::std::cast::transmute(3u);
248 // Basic test to make sure that we can invoke the `write!` macro with an
249 // io::Writer instance.
251 let mut buf = MemWriter::new();
252 write!(&mut buf as &mut io::Writer, "{}", 3);
254 let w = &mut buf as &mut io::Writer;
255 write!(w, "{foo}", foo=4);
256 write!(w, "{:s}", "hello");
257 writeln!(w, "{}", "line");
258 writeln!(w, "{foo}", foo="bar");
261 let s = str::from_utf8_owned(buf.inner());
262 t!(s, "34helloline\nbar\n");
265 // Just make sure that the macros are defined, there's not really a lot that we
266 // can do with them just yet (to test the output)
269 print!("{:?}", ~[0u8]);
271 println!("this is a {}", "test");
272 println!("{foo}", foo="bar");
275 // Just make sure that the macros are defined, there's not really a lot that we
276 // can do with them just yet (to test the output)
277 fn test_format_args() {
278 let mut buf = MemWriter::new();
280 let w = &mut buf as &mut io::Writer;
281 format_args!(|args| { fmt::write(w, args) }, "{}", 1);
282 format_args!(|args| { fmt::write(w, args) }, "test");
283 format_args!(|args| { fmt::write(w, args) }, "{test}", test=3);
285 let s = str::from_utf8_owned(buf.inner());
288 let s = format_args!(fmt::format, "hello {}", "world");
289 t!(s, "hello world");