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