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