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