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