2 #![allow(unused_must_use)]
4 use std::cell::RefCell;
5 use std::fmt::{self, Write};
9 let s = fmt::format(format_args!("Hello, {}!", "world"));
10 assert_eq!(s, "Hello, world!");
18 impl fmt::LowerHex for A {
19 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
23 impl fmt::UpperHex for B {
24 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
28 impl fmt::Display for C {
29 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
30 f.pad_integral(true, "☃", "123")
33 impl fmt::Binary for D {
34 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
42 ($a:expr, $b:expr) => {
48 fn test_format_macro_interface() {
49 // Various edge cases without formats
51 t!(format!("hello"), "hello");
52 t!(format!("hello {{"), "hello {");
54 // default formatters should work
55 t!(format!("{}", 1.0f32), "1");
56 t!(format!("{}", 1.0f64), "1");
57 t!(format!("{}", "a"), "a");
58 t!(format!("{}", "a".to_string()), "a");
59 t!(format!("{}", false), "false");
60 t!(format!("{}", 'a'), "a");
62 // At least exercise all the formats
63 t!(format!("{}", true), "true");
64 t!(format!("{}", '☃'), "☃");
65 t!(format!("{}", 10), "10");
66 t!(format!("{}", 10_usize), "10");
67 t!(format!("{:?}", '☃'), "'☃'");
68 t!(format!("{:?}", 10), "10");
69 t!(format!("{:?}", 10_usize), "10");
70 t!(format!("{:?}", "true"), "\"true\"");
71 t!(format!("{:?}", "foo\nbar"), "\"foo\\nbar\"");
73 format!("{:?}", "foo\n\"bar\"\r\n\'baz\'\t\\qux\\"),
74 r#""foo\n\"bar\"\r\n\'baz\'\t\\qux\\""#
76 t!(format!("{:?}", "foo\0bar\x01baz\u{7f}q\u{75}x"), r#""foo\u{0}bar\u{1}baz\u{7f}qux""#);
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}", _foo = 6usize), "6");
99 t!(format!("{foo_bar}", foo_bar = 1), "1");
100 t!(format!("{}", 5 + 5), "10");
101 t!(format!("{:#4}", C), "☃123");
102 t!(format!("{:b}", D), "aa☃bb");
104 let a: &dyn fmt::Debug = &1;
105 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!("{:._a$}", "aaaaaaaaaaaaaaaaaa", _a = 4), "aaaa");
134 t!(format!("{:1$}", "a", 4), "a ");
135 t!(format!("{1:0$}", 4, "a"), "a ");
136 t!(format!("{:a$}", "a", a = 4), "a ");
137 t!(format!("{:-#}", "a"), "a");
138 t!(format!("{:+#}", "a"), "a");
139 t!(format!("{:/^10.8}", "1234567890"), "/12345678/");
142 t!(format!("{:}", 1.0f32), "1");
143 t!(format!("{:}", 1.0f64), "1");
144 t!(format!("{:.3}", 1.0f64), "1.000");
145 t!(format!("{:10.3}", 1.0f64), " 1.000");
146 t!(format!("{:+10.3}", 1.0f64), " +1.000");
147 t!(format!("{:+10.3}", -1.0f64), " -1.000");
149 t!(format!("{:e}", 1.2345e6f32), "1.2345e6");
150 t!(format!("{:e}", 1.2345e6f64), "1.2345e6");
151 t!(format!("{:E}", 1.2345e6f64), "1.2345E6");
152 t!(format!("{:.3e}", 1.2345e6f64), "1.234e6");
153 t!(format!("{:10.3e}", 1.2345e6f64), " 1.234e6");
154 t!(format!("{:+10.3e}", 1.2345e6f64), " +1.234e6");
155 t!(format!("{:+10.3e}", -1.2345e6f64), " -1.234e6");
158 t!(format!("{}", -0.0), "0");
159 t!(format!("{:?}", -0.0), "-0.0");
160 t!(format!("{:?}", 0.0), "0.0");
162 // sign aware zero padding
163 t!(format!("{:<3}", 1), "1 ");
164 t!(format!("{:>3}", 1), " 1");
165 t!(format!("{:^3}", 1), " 1 ");
166 t!(format!("{:03}", 1), "001");
167 t!(format!("{:<03}", 1), "001");
168 t!(format!("{:>03}", 1), "001");
169 t!(format!("{:^03}", 1), "001");
170 t!(format!("{:+03}", 1), "+01");
171 t!(format!("{:<+03}", 1), "+01");
172 t!(format!("{:>+03}", 1), "+01");
173 t!(format!("{:^+03}", 1), "+01");
174 t!(format!("{:#05x}", 1), "0x001");
175 t!(format!("{:<#05x}", 1), "0x001");
176 t!(format!("{:>#05x}", 1), "0x001");
177 t!(format!("{:^#05x}", 1), "0x001");
178 t!(format!("{:05}", 1.2), "001.2");
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), "-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");
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");
191 // Ergonomic format_args!
192 t!(format!("{0:x} {0:X}", 15), "f F");
193 t!(format!("{0:x} {0:X} {}", 15), "f F 15");
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");
197 // And its edge cases
200 "{a:.0$} {b:.0$} {0:.0$}\n{a:.c$} {b:.c$} {c:.c$}",
206 "abcd hijk 4\nabc hij 3"
208 t!(format!("{a:.*} {0} {:.*}", 4, 3, "efgh", a = "abcdef"), "abcd 4 efg");
209 t!(format!("{:.a$} {a} {a:#x}", "aaaaaa", a = 2), "aa 2 0x2");
211 // Test that pointers don't get truncated.
213 let val = usize::MAX;
214 let exp = format!("{:#x}", val);
215 t!(format!("{:p}", val as *const isize), exp);
219 t!(format!("{{"), "{");
220 t!(format!("}}"), "}");
222 // make sure that format! doesn't move out of local variables
227 // make sure that format! doesn't cause spurious unused-unsafe warnings when
228 // it's inside of an outer unsafe block
230 let a: isize = ::std::mem::transmute(3_usize);
234 // test that trailing commas are acceptable
235 format!("{}", "test",);
236 format!("{foo}", foo = "test",);
239 // Basic test to make sure that we can invoke the `write!` macro with an
240 // fmt::Write instance.
243 let mut buf = String::new();
244 write!(&mut buf, "{}", 3);
247 write!(w, "{foo}", foo = 4);
248 write!(w, "{}", "hello");
249 writeln!(w, "{}", "line");
250 writeln!(w, "{foo}", foo = "bar");
255 t!(buf, "34helloline\nbar\n☃str");
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)
263 print!("{:?}", vec![0u8]);
265 println!("this is a {}", "test");
266 println!("{foo}", foo = "bar");
269 // Just make sure that the macros are defined, there's not really a lot that we
270 // can do with them just yet (to test the output)
272 fn test_format_args() {
273 let mut buf = String::new();
276 write!(w, "{}", format_args!("{}", 1));
277 write!(w, "{}", format_args!("test"));
278 write!(w, "{}", format_args!("{test}", test = 3));
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");
291 // Make sure format!() arguments are always evaluated in a left-to-right
294 static mut FOO: isize = 0;
301 format!("{} {} {a} {b} {} {c}", foo(), foo(), foo(), a = foo(), b = foo(), c = foo()),
302 "1 2 4 5 3 6".to_string()
308 // Make sure each argument are evaluated only once even though it may be
309 // formatted multiple times
311 static mut FOO: isize = 0;
317 assert_eq!(format!("{0} {0} {0} {a} {a} {a}", foo(), a = foo()), "1 1 1 2 2 2".to_string());
322 let refcell = RefCell::new(5);
323 assert_eq!(format!("{:?}", refcell), "RefCell { value: 5 }");
324 let borrow = refcell.borrow_mut();
325 assert_eq!(format!("{:?}", refcell), "RefCell { value: <borrowed> }");
327 assert_eq!(format!("{:?}", refcell), "RefCell { value: 5 }");