]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/ifmt.rs
610cba1eb1fa63389d08456db5165331c7cc84c7
[rust.git] / src / test / run-pass / ifmt.rs
1 // Copyright 2014 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.
4 //
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.
10
11 // xfail-fast: check-fast screws up repr paths
12
13 #[feature(macro_rules)];
14 #[deny(warnings)];
15
16 use std::fmt;
17 use std::io::MemWriter;
18 use std::io;
19 use std::str;
20
21 struct A;
22 struct B;
23
24 impl fmt::Signed for A {
25     fn fmt(_: &A, f: &mut fmt::Formatter) { f.buf.write("aloha".as_bytes()); }
26 }
27 impl fmt::Signed for B {
28     fn fmt(_: &B, f: &mut fmt::Formatter) { f.buf.write("adios".as_bytes()); }
29 }
30
31 macro_rules! t(($a:expr, $b:expr) => { assert_eq!($a, $b.to_owned()) })
32
33 pub fn main() {
34
35     // Make sure there's a poly formatter that takes anything
36     t!(format!("{:?}", 1), "1");
37     t!(format!("{:?}", A), "A");
38     t!(format!("{:?}", ()), "()");
39     t!(format!("{:?}", @(~1, "foo")), "@(~1, \"foo\")");
40
41     // Various edge cases without formats
42     t!(format!(""), "");
43     t!(format!("hello"), "hello");
44     t!(format!("hello \\{"), "hello {");
45
46     // default formatters should work
47     t!(format!("{}", 1i), "1");
48     t!(format!("{}", 1i8), "1");
49     t!(format!("{}", 1i16), "1");
50     t!(format!("{}", 1i32), "1");
51     t!(format!("{}", 1i64), "1");
52     t!(format!("{}", 1u), "1");
53     t!(format!("{}", 1u8), "1");
54     t!(format!("{}", 1u16), "1");
55     t!(format!("{}", 1u32), "1");
56     t!(format!("{}", 1u64), "1");
57     t!(format!("{}", 1.0f32), "1");
58     t!(format!("{}", 1.0f64), "1");
59     t!(format!("{}", "a"), "a");
60     t!(format!("{}", ~"a"), "a");
61     t!(format!("{}", @"a"), "a");
62     t!(format!("{}", false), "false");
63     t!(format!("{}", 'a'), "a");
64
65     // At least exercise all the formats
66     t!(format!("{:b}", true), "true");
67     t!(format!("{:c}", '☃'), "☃");
68     t!(format!("{:d}", 10), "10");
69     t!(format!("{:i}", 10), "10");
70     t!(format!("{:u}", 10u), "10");
71     t!(format!("{:o}", 10u), "12");
72     t!(format!("{:x}", 10u), "a");
73     t!(format!("{:X}", 10u), "A");
74     t!(format!("{:s}", "foo"), "foo");
75     t!(format!("{:s}", ~"foo"), "foo");
76     t!(format!("{:s}", @"foo"), "foo");
77     t!(format!("{:p}", 0x1234 as *int), "0x1234");
78     t!(format!("{:p}", 0x1234 as *mut int), "0x1234");
79     t!(format!("{:d}", A), "aloha");
80     t!(format!("{:d}", B), "adios");
81     t!(format!("foo {:s} ☃☃☃☃☃☃", "bar"), "foo bar ☃☃☃☃☃☃");
82     t!(format!("{1} {0}", 0, 1), "1 0");
83     t!(format!("{foo} {bar}", foo=0, bar=1), "0 1");
84     t!(format!("{foo} {1} {bar} {0}", 0, 1, foo=2, bar=3), "2 1 3 0");
85     t!(format!("{} {0}", "a"), "a a");
86     t!(format!("{foo_bar}", foo_bar=1), "1");
87
88     // Methods should probably work
89     t!(format!("{0, plural, =1{a#} =2{b#} zero{c#} other{d#}}", 0u), "c0");
90     t!(format!("{0, plural, =1{a#} =2{b#} zero{c#} other{d#}}", 1u), "a1");
91     t!(format!("{0, plural, =1{a#} =2{b#} zero{c#} other{d#}}", 2u), "b2");
92     t!(format!("{0, plural, =1{a#} =2{b#} zero{c#} other{d#}}", 3u), "d3");
93     t!(format!("{0, select, a{a#} b{b#} c{c#} other{d#}}", "a"), "aa");
94     t!(format!("{0, select, a{a#} b{b#} c{c#} other{d#}}", "b"), "bb");
95     t!(format!("{0, select, a{a#} b{b#} c{c#} other{d#}}", "c"), "cc");
96     t!(format!("{0, select, a{a#} b{b#} c{c#} other{d#}}", "d"), "dd");
97     t!(format!("{1, select, a{#{0:s}} other{#}}", "b", "a"), "ab");
98     t!(format!("{1, select, a{#{0}} other{#}}", "c", "b"), "b");
99
100     // Formatting strings and their arguments
101     t!(format!("{:s}", "a"), "a");
102     t!(format!("{:4s}", "a"), "a   ");
103     t!(format!("{:>4s}", "a"), "   a");
104     t!(format!("{:<4s}", "a"), "a   ");
105     t!(format!("{:.4s}", "a"), "a");
106     t!(format!("{:4.4s}", "a"), "a   ");
107     t!(format!("{:4.4s}", "aaaaaaaaaaaaaaaaaa"), "aaaa");
108     t!(format!("{:<4.4s}", "aaaaaaaaaaaaaaaaaa"), "aaaa");
109     t!(format!("{:>4.4s}", "aaaaaaaaaaaaaaaaaa"), "aaaa");
110     t!(format!("{:>10.4s}", "aaaaaaaaaaaaaaaaaa"), "aaaa");
111     t!(format!("{:2.4s}", "aaaaa"), "aaaa");
112     t!(format!("{:2.4s}", "aaaa"), "aaaa");
113     t!(format!("{:2.4s}", "aaa"), "aaa");
114     t!(format!("{:2.4s}", "aa"), "aa");
115     t!(format!("{:2.4s}", "a"), "a ");
116     t!(format!("{:0>2s}", "a"), "0a");
117     t!(format!("{:.*s}", 4, "aaaaaaaaaaaaaaaaaa"), "aaaa");
118     t!(format!("{:.1$s}", "aaaaaaaaaaaaaaaaaa", 4), "aaaa");
119     t!(format!("{:.a$s}", "aaaaaaaaaaaaaaaaaa", a=4), "aaaa");
120     t!(format!("{:1$s}", "a", 4), "a   ");
121     t!(format!("{1:0$s}", 4, "a"), "a   ");
122     t!(format!("{:a$s}", "a", a=4), "a   ");
123     t!(format!("{:-#s}", "a"), "a");
124     t!(format!("{:+#s}", "a"), "a");
125
126     // Formatting integers should select the right implementation based off the
127     // type of the argument. Also, hex/octal/binary should be defined for
128     // integers, but they shouldn't emit the negative sign.
129     t!(format!("{:d}", -1i), "-1");
130     t!(format!("{:d}", -1i8), "-1");
131     t!(format!("{:d}", -1i16), "-1");
132     t!(format!("{:d}", -1i32), "-1");
133     t!(format!("{:d}", -1i64), "-1");
134     t!(format!("{:t}", 1i), "1");
135     t!(format!("{:t}", 1i8), "1");
136     t!(format!("{:t}", 1i16), "1");
137     t!(format!("{:t}", 1i32), "1");
138     t!(format!("{:t}", 1i64), "1");
139     t!(format!("{:x}", 1i), "1");
140     t!(format!("{:x}", 1i8), "1");
141     t!(format!("{:x}", 1i16), "1");
142     t!(format!("{:x}", 1i32), "1");
143     t!(format!("{:x}", 1i64), "1");
144     t!(format!("{:X}", 1i), "1");
145     t!(format!("{:X}", 1i8), "1");
146     t!(format!("{:X}", 1i16), "1");
147     t!(format!("{:X}", 1i32), "1");
148     t!(format!("{:X}", 1i64), "1");
149     t!(format!("{:o}", 1i), "1");
150     t!(format!("{:o}", 1i8), "1");
151     t!(format!("{:o}", 1i16), "1");
152     t!(format!("{:o}", 1i32), "1");
153     t!(format!("{:o}", 1i64), "1");
154
155     t!(format!("{:u}", 1u), "1");
156     t!(format!("{:u}", 1u8), "1");
157     t!(format!("{:u}", 1u16), "1");
158     t!(format!("{:u}", 1u32), "1");
159     t!(format!("{:u}", 1u64), "1");
160     t!(format!("{:t}", 1u), "1");
161     t!(format!("{:t}", 1u8), "1");
162     t!(format!("{:t}", 1u16), "1");
163     t!(format!("{:t}", 1u32), "1");
164     t!(format!("{:t}", 1u64), "1");
165     t!(format!("{:x}", 1u), "1");
166     t!(format!("{:x}", 1u8), "1");
167     t!(format!("{:x}", 1u16), "1");
168     t!(format!("{:x}", 1u32), "1");
169     t!(format!("{:x}", 1u64), "1");
170     t!(format!("{:X}", 1u), "1");
171     t!(format!("{:X}", 1u8), "1");
172     t!(format!("{:X}", 1u16), "1");
173     t!(format!("{:X}", 1u32), "1");
174     t!(format!("{:X}", 1u64), "1");
175     t!(format!("{:o}", 1u), "1");
176     t!(format!("{:o}", 1u8), "1");
177     t!(format!("{:o}", 1u16), "1");
178     t!(format!("{:o}", 1u32), "1");
179     t!(format!("{:o}", 1u64), "1");
180
181     // Test the flags for formatting integers
182     t!(format!("{:3d}", 1),  "  1");
183     t!(format!("{:>3d}", 1),  "  1");
184     t!(format!("{:>+3d}", 1), " +1");
185     t!(format!("{:<3d}", 1), "1  ");
186     t!(format!("{:#d}", 1), "1");
187     t!(format!("{:#x}", 10), "0xa");
188     t!(format!("{:#X}", 10), "0xA");
189     t!(format!("{:#5x}", 10), "  0xa");
190     t!(format!("{:#o}", 10), "0o12");
191     t!(format!("{:08x}", 10),  "0000000a");
192     t!(format!("{:8x}", 10),   "       a");
193     t!(format!("{:<8x}", 10),  "a       ");
194     t!(format!("{:>8x}", 10),  "       a");
195     t!(format!("{:#08x}", 10), "0x00000a");
196     t!(format!("{:08d}", -10), "-0000010");
197     t!(format!("{:x}", -1u8), "ff");
198     t!(format!("{:X}", -1u8), "FF");
199     t!(format!("{:t}", -1u8), "11111111");
200     t!(format!("{:o}", -1u8), "377");
201     t!(format!("{:#x}", -1u8), "0xff");
202     t!(format!("{:#X}", -1u8), "0xFF");
203     t!(format!("{:#t}", -1u8), "0b11111111");
204     t!(format!("{:#o}", -1u8), "0o377");
205
206     // Signed combinations
207     t!(format!("{:+5d}", 1),  "   +1");
208     t!(format!("{:+5d}", -1), "   -1");
209     t!(format!("{:05d}", 1),   "00001");
210     t!(format!("{:05d}", -1),  "-0001");
211     t!(format!("{:+05d}", 1),  "+0001");
212     t!(format!("{:+05d}", -1), "-0001");
213
214     // Some float stuff
215     t!(format!("{:f}", 1.0f32), "1");
216     t!(format!("{:f}", 1.0f64), "1");
217     t!(format!("{:.3f}", 1.0f64), "1.000");
218     t!(format!("{:10.3f}", 1.0f64),   "     1.000");
219     t!(format!("{:+10.3f}", 1.0f64),  "    +1.000");
220     t!(format!("{:+10.3f}", -1.0f64), "    -1.000");
221
222     t!(format!("{:e}", 1.2345e6f32), "1.2345e6");
223     t!(format!("{:e}", 1.2345e6f64), "1.2345e6");
224     t!(format!("{:E}", 1.2345e6f64), "1.2345E6");
225     t!(format!("{:.3e}", 1.2345e6f64), "1.234e6");
226     t!(format!("{:10.3e}", 1.2345e6f64),   "   1.234e6");
227     t!(format!("{:+10.3e}", 1.2345e6f64),  "  +1.234e6");
228     t!(format!("{:+10.3e}", -1.2345e6f64), "  -1.234e6");
229
230     // Escaping
231     t!(format!("\\{"), "{");
232     t!(format!("\\}"), "}");
233     t!(format!("\\#"), "#");
234     t!(format!("\\\\"), "\\");
235
236     test_write();
237     test_print();
238
239     // make sure that format! doesn't move out of local variables
240     let a = ~3;
241     format!("{:?}", a);
242     format!("{:?}", a);
243
244     // make sure that format! doesn't cause spurious unused-unsafe warnings when
245     // it's inside of an outer unsafe block
246     unsafe {
247         let a: int = ::std::cast::transmute(3u);
248         format!("{}", a);
249     }
250
251     test_format_args();
252
253     // test that trailing commas are acceptable
254     format!("{}", "test",);
255     format!("{foo}", foo="test",);
256 }
257
258 // Basic test to make sure that we can invoke the `write!` macro with an
259 // io::Writer instance.
260 fn test_write() {
261     let mut buf = MemWriter::new();
262     write!(&mut buf as &mut io::Writer, "{}", 3);
263     {
264         let w = &mut buf as &mut io::Writer;
265         write!(w, "{foo}", foo=4);
266         write!(w, "{:s}", "hello");
267         writeln!(w, "{}", "line");
268         writeln!(w, "{foo}", foo="bar");
269     }
270
271     let s = str::from_utf8_owned(buf.unwrap()).unwrap();
272     t!(s, "34helloline\nbar\n");
273 }
274
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_print() {
278     print!("hi");
279     print!("{:?}", ~[0u8]);
280     println!("hello");
281     println!("this is a {}", "test");
282     println!("{foo}", foo="bar");
283 }
284
285 // Just make sure that the macros are defined, there's not really a lot that we
286 // can do with them just yet (to test the output)
287 fn test_format_args() {
288     let mut buf = MemWriter::new();
289     {
290         let w = &mut buf as &mut io::Writer;
291         format_args!(|args| { fmt::write(w, args) }, "{}", 1);
292         format_args!(|args| { fmt::write(w, args) }, "test");
293         format_args!(|args| { fmt::write(w, args) }, "{test}", test=3);
294     }
295     let s = str::from_utf8_owned(buf.unwrap()).unwrap();
296     t!(s, "1test3");
297
298     let s = format_args!(fmt::format, "hello {}", "world");
299     t!(s, "hello world");
300 }