]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/ifmt.rs
Rollup merge of #60685 - dtolnay:spdx, r=nikomatsakis
[rust.git] / src / test / run-pass / ifmt.rs
1 #![deny(warnings)]
2 #![allow(unused_must_use)]
3 #![allow(unused_features)]
4 #![feature(box_syntax)]
5
6 use std::cell::RefCell;
7 use std::fmt::{self, Write};
8 use std::usize;
9
10 struct A;
11 struct B;
12 struct C;
13 struct D;
14
15 impl fmt::LowerHex for A {
16     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
17         f.write_str("aloha")
18     }
19 }
20 impl fmt::UpperHex for B {
21     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
22         f.write_str("adios")
23     }
24 }
25 impl fmt::Display for C {
26     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
27         f.pad_integral(true, "☃", "123")
28     }
29 }
30 impl fmt::Binary for D {
31     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
32         f.write_str("aa")?;
33         f.write_char('☃')?;
34         f.write_str("bb")
35     }
36 }
37
38 macro_rules! t {
39     ($a:expr, $b:expr) => { assert_eq!($a, $b) }
40 }
41
42 pub fn main() {
43     // Various edge cases without formats
44     t!(format!(""), "");
45     t!(format!("hello"), "hello");
46     t!(format!("hello {{"), "hello {");
47
48     // default formatters should work
49     t!(format!("{}", 1.0f32), "1");
50     t!(format!("{}", 1.0f64), "1");
51     t!(format!("{}", "a"), "a");
52     t!(format!("{}", "a".to_string()), "a");
53     t!(format!("{}", false), "false");
54     t!(format!("{}", 'a'), "a");
55
56     // At least exercise all the formats
57     t!(format!("{}", true), "true");
58     t!(format!("{}", '☃'), "☃");
59     t!(format!("{}", 10), "10");
60     t!(format!("{}", 10_usize), "10");
61     t!(format!("{:?}", '☃'), "'☃'");
62     t!(format!("{:?}", 10), "10");
63     t!(format!("{:?}", 10_usize), "10");
64     t!(format!("{:?}", "true"), "\"true\"");
65     t!(format!("{:?}", "foo\nbar"), "\"foo\\nbar\"");
66     t!(format!("{:?}", "foo\n\"bar\"\r\n\'baz\'\t\\qux\\"),
67        r#""foo\n\"bar\"\r\n\'baz\'\t\\qux\\""#);
68     t!(format!("{:?}", "foo\0bar\x01baz\u{7f}q\u{75}x"),
69        r#""foo\u{0}bar\u{1}baz\u{7f}qux""#);
70     t!(format!("{:o}", 10_usize), "12");
71     t!(format!("{:x}", 10_usize), "a");
72     t!(format!("{:X}", 10_usize), "A");
73     t!(format!("{}", "foo"), "foo");
74     t!(format!("{}", "foo".to_string()), "foo");
75     if cfg!(target_pointer_width = "32") {
76         t!(format!("{:#p}", 0x1234 as *const isize), "0x00001234");
77         t!(format!("{:#p}", 0x1234 as *mut isize), "0x00001234");
78     } else {
79         t!(format!("{:#p}", 0x1234 as *const isize), "0x0000000000001234");
80         t!(format!("{:#p}", 0x1234 as *mut isize), "0x0000000000001234");
81     }
82     t!(format!("{:p}", 0x1234 as *const isize), "0x1234");
83     t!(format!("{:p}", 0x1234 as *mut isize), "0x1234");
84     t!(format!("{:x}", A), "aloha");
85     t!(format!("{:X}", B), "adios");
86     t!(format!("foo {} ☃☃☃☃☃☃", "bar"), "foo bar ☃☃☃☃☃☃");
87     t!(format!("{1} {0}", 0, 1), "1 0");
88     t!(format!("{foo} {bar}", foo=0, bar=1), "0 1");
89     t!(format!("{foo} {1} {bar} {0}", 0, 1, foo=2, bar=3), "2 1 3 0");
90     t!(format!("{} {0}", "a"), "a a");
91     t!(format!("{foo_bar}", foo_bar=1), "1");
92     t!(format!("{}", 5 + 5), "10");
93     t!(format!("{:#4}", C), "☃123");
94     t!(format!("{:b}", D), "aa☃bb");
95
96     let a: &fmt::Debug = &1;
97     t!(format!("{:?}", a), "1");
98
99
100     // Formatting strings and their arguments
101     t!(format!("{}", "a"), "a");
102     t!(format!("{:4}", "a"), "a   ");
103     t!(format!("{:4}", "☃"), "☃   ");
104     t!(format!("{:>4}", "a"), "   a");
105     t!(format!("{:<4}", "a"), "a   ");
106     t!(format!("{:^5}", "a"),  "  a  ");
107     t!(format!("{:^5}", "aa"), " aa  ");
108     t!(format!("{:^4}", "a"),  " a  ");
109     t!(format!("{:^4}", "aa"), " aa ");
110     t!(format!("{:.4}", "a"), "a");
111     t!(format!("{:4.4}", "a"), "a   ");
112     t!(format!("{:4.4}", "aaaaaaaaaaaaaaaaaa"), "aaaa");
113     t!(format!("{:<4.4}", "aaaaaaaaaaaaaaaaaa"), "aaaa");
114     t!(format!("{:>4.4}", "aaaaaaaaaaaaaaaaaa"), "aaaa");
115     t!(format!("{:^4.4}", "aaaaaaaaaaaaaaaaaa"), "aaaa");
116     t!(format!("{:>10.4}", "aaaaaaaaaaaaaaaaaa"), "      aaaa");
117     t!(format!("{:2.4}", "aaaaa"), "aaaa");
118     t!(format!("{:2.4}", "aaaa"), "aaaa");
119     t!(format!("{:2.4}", "aaa"), "aaa");
120     t!(format!("{:2.4}", "aa"), "aa");
121     t!(format!("{:2.4}", "a"), "a ");
122     t!(format!("{:0>2}", "a"), "0a");
123     t!(format!("{:.*}", 4, "aaaaaaaaaaaaaaaaaa"), "aaaa");
124     t!(format!("{:.1$}", "aaaaaaaaaaaaaaaaaa", 4), "aaaa");
125     t!(format!("{:.a$}", "aaaaaaaaaaaaaaaaaa", a=4), "aaaa");
126     t!(format!("{:1$}", "a", 4), "a   ");
127     t!(format!("{1:0$}", 4, "a"), "a   ");
128     t!(format!("{:a$}", "a", a=4), "a   ");
129     t!(format!("{:-#}", "a"), "a");
130     t!(format!("{:+#}", "a"), "a");
131     t!(format!("{:/^10.8}", "1234567890"), "/12345678/");
132
133     // Some float stuff
134     t!(format!("{:}", 1.0f32), "1");
135     t!(format!("{:}", 1.0f64), "1");
136     t!(format!("{:.3}", 1.0f64), "1.000");
137     t!(format!("{:10.3}", 1.0f64),   "     1.000");
138     t!(format!("{:+10.3}", 1.0f64),  "    +1.000");
139     t!(format!("{:+10.3}", -1.0f64), "    -1.000");
140
141     t!(format!("{:e}", 1.2345e6f32), "1.2345e6");
142     t!(format!("{:e}", 1.2345e6f64), "1.2345e6");
143     t!(format!("{:E}", 1.2345e6f64), "1.2345E6");
144     t!(format!("{:.3e}", 1.2345e6f64), "1.234e6");
145     t!(format!("{:10.3e}", 1.2345e6f64),   "   1.234e6");
146     t!(format!("{:+10.3e}", 1.2345e6f64),  "  +1.234e6");
147     t!(format!("{:+10.3e}", -1.2345e6f64), "  -1.234e6");
148
149     // Float edge cases
150     t!(format!("{}", -0.0), "0");
151     t!(format!("{:?}", -0.0), "-0.0");
152     t!(format!("{:?}", 0.0), "0.0");
153
154     // sign aware zero padding
155     t!(format!("{:<3}", 1), "1  ");
156     t!(format!("{:>3}", 1), "  1");
157     t!(format!("{:^3}", 1), " 1 ");
158     t!(format!("{:03}", 1), "001");
159     t!(format!("{:<03}", 1), "001");
160     t!(format!("{:>03}", 1), "001");
161     t!(format!("{:^03}", 1), "001");
162     t!(format!("{:+03}", 1), "+01");
163     t!(format!("{:<+03}", 1), "+01");
164     t!(format!("{:>+03}", 1), "+01");
165     t!(format!("{:^+03}", 1), "+01");
166     t!(format!("{:#05x}", 1), "0x001");
167     t!(format!("{:<#05x}", 1), "0x001");
168     t!(format!("{:>#05x}", 1), "0x001");
169     t!(format!("{:^#05x}", 1), "0x001");
170     t!(format!("{:05}", 1.2), "001.2");
171     t!(format!("{:<05}", 1.2), "001.2");
172     t!(format!("{:>05}", 1.2), "001.2");
173     t!(format!("{:^05}", 1.2), "001.2");
174     t!(format!("{:05}", -1.2), "-01.2");
175     t!(format!("{:<05}", -1.2), "-01.2");
176     t!(format!("{:>05}", -1.2), "-01.2");
177     t!(format!("{:^05}", -1.2), "-01.2");
178     t!(format!("{:+05}", 1.2), "+01.2");
179     t!(format!("{:<+05}", 1.2), "+01.2");
180     t!(format!("{:>+05}", 1.2), "+01.2");
181     t!(format!("{:^+05}", 1.2), "+01.2");
182
183     // Ergonomic format_args!
184     t!(format!("{0:x} {0:X}", 15), "f F");
185     t!(format!("{0:x} {0:X} {}", 15), "f F 15");
186     // NOTE: For now the longer test cases must not be followed immediately by
187     // >1 empty lines, or the pretty printer will break. Since no one wants to
188     // touch the current pretty printer (#751), we have no choice but to work
189     // around it. Some of the following test cases are also affected.
190     t!(format!("{:x}{0:X}{a:x}{:X}{1:x}{a:X}", 13, 14, a=15), "dDfEeF");
191     t!(format!("{a:x} {a:X}", a=15), "f F");
192
193     // And its edge cases
194     t!(format!("{a:.0$} {b:.0$} {0:.0$}\n{a:.c$} {b:.c$} {c:.c$}",
195                4, a="abcdefg", b="hijklmn", c=3),
196                "abcd hijk 4\nabc hij 3");
197     t!(format!("{a:.*} {0} {:.*}", 4, 3, "efgh", a="abcdef"), "abcd 4 efg");
198     t!(format!("{:.a$} {a} {a:#x}", "aaaaaa", a=2), "aa 2 0x2");
199
200
201     // Test that pointers don't get truncated.
202     {
203         let val = usize::MAX;
204         let exp = format!("{:#x}", val);
205         t!(format!("{:p}", val as *const isize), exp);
206     }
207
208     // Escaping
209     t!(format!("{{"), "{");
210     t!(format!("}}"), "}");
211
212     test_write();
213     test_print();
214     test_order();
215     test_once();
216
217     // make sure that format! doesn't move out of local variables
218     let a: Box<_> = box 3;
219     format!("{}", a);
220     format!("{}", a);
221
222     // make sure that format! doesn't cause spurious unused-unsafe warnings when
223     // it's inside of an outer unsafe block
224     unsafe {
225         let a: isize = ::std::mem::transmute(3_usize);
226         format!("{}", a);
227     }
228
229     test_format_args();
230
231     // test that trailing commas are acceptable
232     format!("{}", "test",);
233     format!("{foo}", foo="test",);
234
235     test_refcell();
236 }
237
238 // Basic test to make sure that we can invoke the `write!` macro with an
239 // fmt::Write instance.
240 fn test_write() {
241     let mut buf = String::new();
242     write!(&mut buf, "{}", 3);
243     {
244         let w = &mut buf;
245         write!(w, "{foo}", foo=4);
246         write!(w, "{}", "hello");
247         writeln!(w, "{}", "line");
248         writeln!(w, "{foo}", foo="bar");
249         w.write_char('☃');
250         w.write_str("str");
251     }
252
253     t!(buf, "34helloline\nbar\n☃str");
254 }
255
256 // Just make sure that the macros are defined, there's not really a lot that we
257 // can do with them just yet (to test the output)
258 fn test_print() {
259     print!("hi");
260     print!("{:?}", vec![0u8]);
261     println!("hello");
262     println!("this is a {}", "test");
263     println!("{foo}", foo="bar");
264 }
265
266 // Just make sure that the macros are defined, there's not really a lot that we
267 // can do with them just yet (to test the output)
268 fn test_format_args() {
269     let mut buf = String::new();
270     {
271         let w = &mut buf;
272         write!(w, "{}", format_args!("{}", 1));
273         write!(w, "{}", format_args!("test"));
274         write!(w, "{}", format_args!("{test}", test=3));
275     }
276     let s = buf;
277     t!(s, "1test3");
278
279     let s = fmt::format(format_args!("hello {}", "world"));
280     t!(s, "hello world");
281     let s = format!("{}: {}", "args were", format_args!("hello {}", "world"));
282     t!(s, "args were: hello world");
283 }
284
285 fn test_order() {
286     // Make sure format!() arguments are always evaluated in a left-to-right
287     // ordering
288     fn foo() -> isize {
289         static mut FOO: isize = 0;
290         unsafe {
291             FOO += 1;
292             FOO
293         }
294     }
295     assert_eq!(format!("{} {} {a} {b} {} {c}",
296                        foo(), foo(), foo(), a=foo(), b=foo(), c=foo()),
297                "1 2 4 5 3 6".to_string());
298 }
299
300 fn test_once() {
301     // Make sure each argument are evaluated only once even though it may be
302     // formatted multiple times
303     fn foo() -> isize {
304         static mut FOO: isize = 0;
305         unsafe {
306             FOO += 1;
307             FOO
308         }
309     }
310     assert_eq!(format!("{0} {0} {0} {a} {a} {a}", foo(), a=foo()),
311                "1 1 1 2 2 2".to_string());
312 }
313
314 fn test_refcell() {
315     let refcell = RefCell::new(5);
316     assert_eq!(format!("{:?}", refcell), "RefCell { value: 5 }");
317     let borrow = refcell.borrow_mut();
318     assert_eq!(format!("{:?}", refcell), "RefCell { value: <borrowed> }");
319     drop(borrow);
320     assert_eq!(format!("{:?}", refcell), "RefCell { value: 5 }");
321 }