1 // Copyright 2014-2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
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.
11 // no-pretty-expanded unnecessary unsafe block generated
14 #![allow(unused_must_use)]
15 #![allow(unknown_features)]
16 #![feature(box_syntax)]
18 use std::fmt::{self, Write};
26 impl fmt::LowerHex for A {
27 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
31 impl fmt::UpperHex for B {
32 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
36 impl fmt::Display for C {
37 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
38 f.pad_integral(true, "☃", "123")
41 impl fmt::Binary for D {
42 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
43 try!(f.write_str("aa"));
44 try!(f.write_char('☃'));
50 ($a:expr, $b:expr) => { assert_eq!($a, $b) }
54 // Various edge cases without formats
56 t!(format!("hello"), "hello");
57 t!(format!("hello {{"), "hello {");
59 // default formatters should work
60 t!(format!("{}", 1.0f32), "1");
61 t!(format!("{}", 1.0f64), "1");
62 t!(format!("{}", "a"), "a");
63 t!(format!("{}", "a".to_string()), "a");
64 t!(format!("{}", false), "false");
65 t!(format!("{}", 'a'), "a");
67 // At least exercise all the formats
68 t!(format!("{}", true), "true");
69 t!(format!("{}", '☃'), "☃");
70 t!(format!("{}", 10), "10");
71 t!(format!("{}", 10_usize), "10");
72 t!(format!("{:?}", '☃'), "'\\u{2603}'");
73 t!(format!("{:?}", 10), "10");
74 t!(format!("{:?}", 10_usize), "10");
75 t!(format!("{:?}", "true"), "\"true\"");
76 t!(format!("{:?}", "foo\nbar"), "\"foo\\nbar\"");
77 t!(format!("{:o}", 10_usize), "12");
78 t!(format!("{:x}", 10_usize), "a");
79 t!(format!("{:X}", 10_usize), "A");
80 t!(format!("{}", "foo"), "foo");
81 t!(format!("{}", "foo".to_string()), "foo");
82 if cfg!(target_pointer_width = "32") {
83 t!(format!("{:#p}", 0x1234 as *const isize), "0x00001234");
84 t!(format!("{:#p}", 0x1234 as *mut isize), "0x00001234");
86 t!(format!("{:#p}", 0x1234 as *const isize), "0x0000000000001234");
87 t!(format!("{:#p}", 0x1234 as *mut isize), "0x0000000000001234");
89 t!(format!("{:p}", 0x1234 as *const isize), "0x1234");
90 t!(format!("{:p}", 0x1234 as *mut isize), "0x1234");
91 t!(format!("{:x}", A), "aloha");
92 t!(format!("{:X}", B), "adios");
93 t!(format!("foo {} ☃☃☃☃☃☃", "bar"), "foo bar ☃☃☃☃☃☃");
94 t!(format!("{1} {0}", 0, 1), "1 0");
95 t!(format!("{foo} {bar}", foo=0, bar=1), "0 1");
96 t!(format!("{foo} {1} {bar} {0}", 0, 1, foo=2, bar=3), "2 1 3 0");
97 t!(format!("{} {0}", "a"), "a a");
98 t!(format!("{foo_bar}", foo_bar=1), "1");
99 t!(format!("{}", 5 + 5), "10");
100 t!(format!("{:#4}", C), "☃123");
101 t!(format!("{:b}", D), "aa☃bb");
103 let a: &fmt::Debug = &1;
104 t!(format!("{:?}", a), "1");
107 // Formatting strings and their arguments
108 t!(format!("{}", "a"), "a");
109 t!(format!("{:4}", "a"), "a ");
110 t!(format!("{:4}", "☃"), "☃ ");
111 t!(format!("{:>4}", "a"), " a");
112 t!(format!("{:<4}", "a"), "a ");
113 t!(format!("{:^5}", "a"), " a ");
114 t!(format!("{:^5}", "aa"), " aa ");
115 t!(format!("{:^4}", "a"), " a ");
116 t!(format!("{:^4}", "aa"), " aa ");
117 t!(format!("{:.4}", "a"), "a");
118 t!(format!("{:4.4}", "a"), "a ");
119 t!(format!("{:4.4}", "aaaaaaaaaaaaaaaaaa"), "aaaa");
120 t!(format!("{:<4.4}", "aaaaaaaaaaaaaaaaaa"), "aaaa");
121 t!(format!("{:>4.4}", "aaaaaaaaaaaaaaaaaa"), "aaaa");
122 t!(format!("{:^4.4}", "aaaaaaaaaaaaaaaaaa"), "aaaa");
123 t!(format!("{:>10.4}", "aaaaaaaaaaaaaaaaaa"), "aaaa");
124 t!(format!("{:2.4}", "aaaaa"), "aaaa");
125 t!(format!("{:2.4}", "aaaa"), "aaaa");
126 t!(format!("{:2.4}", "aaa"), "aaa");
127 t!(format!("{:2.4}", "aa"), "aa");
128 t!(format!("{:2.4}", "a"), "a ");
129 t!(format!("{:0>2}", "a"), "0a");
130 t!(format!("{:.*}", 4, "aaaaaaaaaaaaaaaaaa"), "aaaa");
131 t!(format!("{:.1$}", "aaaaaaaaaaaaaaaaaa", 4), "aaaa");
132 t!(format!("{:.a$}", "aaaaaaaaaaaaaaaaaa", a=4), "aaaa");
133 t!(format!("{:1$}", "a", 4), "a ");
134 t!(format!("{1:0$}", 4, "a"), "a ");
135 t!(format!("{:a$}", "a", a=4), "a ");
136 t!(format!("{:-#}", "a"), "a");
137 t!(format!("{:+#}", "a"), "a");
140 t!(format!("{:}", 1.0f32), "1");
141 t!(format!("{:}", 1.0f64), "1");
142 t!(format!("{:.3}", 1.0f64), "1.000");
143 t!(format!("{:10.3}", 1.0f64), " 1.000");
144 t!(format!("{:+10.3}", 1.0f64), " +1.000");
145 t!(format!("{:+10.3}", -1.0f64), " -1.000");
147 t!(format!("{:e}", 1.2345e6f32), "1.2345e6");
148 t!(format!("{:e}", 1.2345e6f64), "1.2345e6");
149 t!(format!("{:E}", 1.2345e6f64), "1.2345E6");
150 t!(format!("{:.3e}", 1.2345e6f64), "1.234e6");
151 t!(format!("{:10.3e}", 1.2345e6f64), " 1.234e6");
152 t!(format!("{:+10.3e}", 1.2345e6f64), " +1.234e6");
153 t!(format!("{:+10.3e}", -1.2345e6f64), " -1.234e6");
156 t!(format!("{}", -0.0), "0");
157 t!(format!("{:?}", -0.0), "-0");
158 t!(format!("{:?}", 0.0), "0");
161 // Test that pointers don't get truncated.
163 let val = usize::MAX;
164 let exp = format!("{:#x}", val);
165 t!(format!("{:p}", val as *const isize), exp);
169 t!(format!("{{"), "{");
170 t!(format!("}}"), "}");
176 // make sure that format! doesn't move out of local variables
177 let a: Box<_> = box 3;
181 // make sure that format! doesn't cause spurious unused-unsafe warnings when
182 // it's inside of an outer unsafe block
184 let a: isize = ::std::mem::transmute(3_usize);
190 // test that trailing commas are acceptable
191 format!("{}", "test",);
192 format!("{foo}", foo="test",);
195 // Basic test to make sure that we can invoke the `write!` macro with an
196 // fmt::Write instance.
199 let mut buf = String::new();
200 write!(&mut buf, "{}", 3);
203 write!(w, "{foo}", foo=4);
204 write!(w, "{}", "hello");
205 writeln!(w, "{}", "line");
206 writeln!(w, "{foo}", foo="bar");
211 t!(buf, "34helloline\nbar\n☃str");
214 // Just make sure that the macros are defined, there's not really a lot that we
215 // can do with them just yet (to test the output)
218 print!("{:?}", vec!(0u8));
220 println!("this is a {}", "test");
221 println!("{foo}", foo="bar");
224 // Just make sure that the macros are defined, there's not really a lot that we
225 // can do with them just yet (to test the output)
226 fn test_format_args() {
228 let mut buf = String::new();
231 write!(w, "{}", format_args!("{}", 1));
232 write!(w, "{}", format_args!("test"));
233 write!(w, "{}", format_args!("{test}", test=3));
238 let s = fmt::format(format_args!("hello {}", "world"));
239 t!(s, "hello world");
240 let s = format!("{}: {}", "args were", format_args!("hello {}", "world"));
241 t!(s, "args were: hello world");
245 // Make sure format!() arguments are always evaluated in a left-to-right
248 static mut FOO: isize = 0;
254 assert_eq!(format!("{} {} {a} {b} {} {c}",
255 foo(), foo(), foo(), a=foo(), b=foo(), c=foo()),
256 "1 2 4 5 3 6".to_string());