]> git.lizzy.rs Git - rust.git/blob - library/alloc/tests/fmt.rs
Rollup merge of #103117 - joshtriplett:use-is-terminal, r=eholk
[rust.git] / library / alloc / tests / fmt.rs
1 #![deny(warnings)]
2
3 use std::cell::RefCell;
4 use std::fmt::{self, Write};
5
6 #[test]
7 fn test_format() {
8     let s = fmt::format(format_args!("Hello, {}!", "world"));
9     assert_eq!(s, "Hello, world!");
10 }
11
12 struct A;
13 struct B;
14 struct C;
15 struct D;
16
17 impl fmt::LowerHex for A {
18     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
19         f.write_str("aloha")
20     }
21 }
22 impl fmt::UpperHex for B {
23     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
24         f.write_str("adios")
25     }
26 }
27 impl fmt::Display for C {
28     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
29         f.pad_integral(true, "☃", "123")
30     }
31 }
32 impl fmt::Binary for D {
33     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
34         f.write_str("aa")?;
35         f.write_char('☃')?;
36         f.write_str("bb")
37     }
38 }
39
40 macro_rules! t {
41     ($a:expr, $b:expr) => {
42         assert_eq!($a, $b)
43     };
44 }
45
46 #[test]
47 fn test_format_macro_interface() {
48     // Various edge cases without formats
49     t!(format!(""), "");
50     t!(format!("hello"), "hello");
51     t!(format!("hello {{"), "hello {");
52
53     // default formatters should work
54     t!(format!("{}", 1.0f32), "1");
55     t!(format!("{}", 1.0f64), "1");
56     t!(format!("{}", "a"), "a");
57     t!(format!("{}", "a".to_string()), "a");
58     t!(format!("{}", false), "false");
59     t!(format!("{}", 'a'), "a");
60
61     // At least exercise all the formats
62     t!(format!("{}", true), "true");
63     t!(format!("{}", '☃'), "☃");
64     t!(format!("{}", 10), "10");
65     t!(format!("{}", 10_usize), "10");
66     t!(format!("{:?}", '☃'), "'☃'");
67     t!(format!("{:?}", 10), "10");
68     t!(format!("{:?}", 10_usize), "10");
69     t!(format!("{:?}", "true"), "\"true\"");
70     t!(format!("{:?}", "foo\nbar"), "\"foo\\nbar\"");
71     t!(format!("{:?}", "foo\n\"bar\"\r\n\'baz\'\t\\qux\\"), r#""foo\n\"bar\"\r\n'baz'\t\\qux\\""#);
72     t!(format!("{:?}", "foo\0bar\x01baz\u{7f}q\u{75}x"), r#""foo\0bar\u{1}baz\u{7f}qux""#);
73     t!(format!("{:o}", 10_usize), "12");
74     t!(format!("{:x}", 10_usize), "a");
75     t!(format!("{:X}", 10_usize), "A");
76     t!(format!("{}", "foo"), "foo");
77     t!(format!("{}", "foo".to_string()), "foo");
78     if cfg!(target_pointer_width = "32") {
79         t!(format!("{:#p}", 0x1234 as *const isize), "0x00001234");
80         t!(format!("{:#p}", 0x1234 as *mut isize), "0x00001234");
81     } else {
82         t!(format!("{:#p}", 0x1234 as *const isize), "0x0000000000001234");
83         t!(format!("{:#p}", 0x1234 as *mut isize), "0x0000000000001234");
84     }
85     t!(format!("{:p}", 0x1234 as *const isize), "0x1234");
86     t!(format!("{:p}", 0x1234 as *mut isize), "0x1234");
87     t!(format!("{A:x}"), "aloha");
88     t!(format!("{B:X}"), "adios");
89     t!(format!("foo {} ☃☃☃☃☃☃", "bar"), "foo bar ☃☃☃☃☃☃");
90     t!(format!("{1} {0}", 0, 1), "1 0");
91     t!(format!("{foo} {bar}", foo = 0, bar = 1), "0 1");
92     t!(format!("{foo} {1} {bar} {0}", 0, 1, foo = 2, bar = 3), "2 1 3 0");
93     t!(format!("{} {0}", "a"), "a a");
94     t!(format!("{_foo}", _foo = 6usize), "6");
95     t!(format!("{foo_bar}", foo_bar = 1), "1");
96     t!(format!("{}", 5 + 5), "10");
97     t!(format!("{C:#4}"), "☃123");
98     t!(format!("{D:b}"), "aa☃bb");
99
100     let a: &dyn fmt::Debug = &1;
101     t!(format!("{a:?}"), "1");
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");
119     t!(format!("{:>10.4}", "aaaaaaaaaaaaaaaaaa"), "      aaaa");
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");
129     t!(format!("{:._a$}", "aaaaaaaaaaaaaaaaaa", _a = 4), "aaaa");
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");
135     t!(format!("{:/^10.8}", "1234567890"), "/12345678/");
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
153     // Float edge cases
154     t!(format!("{}", -0.0), "-0");
155     t!(format!("{:?}", 0.0), "0.0");
156
157     // sign aware zero padding
158     t!(format!("{:<3}", 1), "1  ");
159     t!(format!("{:>3}", 1), "  1");
160     t!(format!("{:^3}", 1), " 1 ");
161     t!(format!("{:03}", 1), "001");
162     t!(format!("{:<03}", 1), "001");
163     t!(format!("{:>03}", 1), "001");
164     t!(format!("{:^03}", 1), "001");
165     t!(format!("{:+03}", 1), "+01");
166     t!(format!("{:<+03}", 1), "+01");
167     t!(format!("{:>+03}", 1), "+01");
168     t!(format!("{:^+03}", 1), "+01");
169     t!(format!("{:#05x}", 1), "0x001");
170     t!(format!("{:<#05x}", 1), "0x001");
171     t!(format!("{:>#05x}", 1), "0x001");
172     t!(format!("{:^#05x}", 1), "0x001");
173     t!(format!("{:05}", 1.2), "001.2");
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), "-01.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
186     // Ergonomic format_args!
187     t!(format!("{0:x} {0:X}", 15), "f F");
188     t!(format!("{0:x} {0:X} {}", 15), "f F 15");
189     t!(format!("{:x}{0:X}{a:x}{:X}{1:x}{a:X}", 13, 14, a = 15), "dDfEeF");
190     t!(format!("{a:x} {a:X}", a = 15), "f F");
191
192     // And its edge cases
193     t!(
194         format!(
195             "{a:.0$} {b:.0$} {0:.0$}\n{a:.c$} {b:.c$} {c:.c$}",
196             4,
197             a = "abcdefg",
198             b = "hijklmn",
199             c = 3
200         ),
201         "abcd hijk 4\nabc hij 3"
202     );
203     t!(format!("{a:.*} {0} {:.*}", 4, 3, "efgh", a = "abcdef"), "abcd 4 efg");
204     t!(format!("{:.a$} {a} {a:#x}", "aaaaaa", a = 2), "aa 2 0x2");
205
206     // Test that pointers don't get truncated.
207     {
208         let val = usize::MAX;
209         let exp = format!("{val:#x}");
210         t!(format!("{:p}", std::ptr::invalid::<isize>(val)), exp);
211     }
212
213     // Escaping
214     t!(format!("{{"), "{");
215     t!(format!("}}"), "}");
216
217     // make sure that format! doesn't move out of local variables
218     let a = Box::new(3);
219     format!("{a}");
220     format!("{a}");
221
222     // make sure that format! doesn't cause spurious unused-unsafe warnings when
223     // it's inside of an outer unsafe block
224     unsafe {
225         let a: isize = ::std::mem::transmute(3_usize);
226         format!("{a}");
227     }
228
229     // test that trailing commas are acceptable
230     format!("{}", "test",);
231     format!("{foo}", foo = "test",);
232 }
233
234 // Basic test to make sure that we can invoke the `write!` macro with an
235 // fmt::Write instance.
236 #[test]
237 fn test_write() {
238     let mut buf = String::new();
239     let _ = write!(&mut buf, "{}", 3);
240     {
241         let w = &mut buf;
242         let _ = write!(w, "{foo}", foo = 4);
243         let _ = write!(w, "{}", "hello");
244         let _ = writeln!(w, "{}", "line");
245         let _ = writeln!(w, "{foo}", foo = "bar");
246         let _ = w.write_char('☃');
247         let _ = w.write_str("str");
248     }
249
250     t!(buf, "34helloline\nbar\n☃str");
251 }
252
253 // Just make sure that the macros are defined, there's not really a lot that we
254 // can do with them just yet (to test the output)
255 #[test]
256 fn test_print() {
257     print!("hi");
258     print!("{:?}", vec![0u8]);
259     println!("hello");
260     println!("this is a {}", "test");
261     println!("{foo}", foo = "bar");
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 #[test]
267 fn test_format_args() {
268     let mut buf = String::new();
269     {
270         let w = &mut buf;
271         let _ = write!(w, "{}", format_args!("{}", 1));
272         let _ = write!(w, "{}", format_args!("test"));
273         let _ = write!(w, "{}", format_args!("{test}", test = 3));
274     }
275     let s = buf;
276     t!(s, "1test3");
277
278     let s = fmt::format(format_args!("hello {}", "world"));
279     t!(s, "hello world");
280     let s = format!("{}: {}", "args were", format_args!("hello {}", "world"));
281     t!(s, "args were: hello world");
282 }
283
284 #[test]
285 fn test_order() {
286     // Make sure format!() arguments are always evaluated in a left-to-right
287     // ordering
288     fn foo() -> isize {
289         static mut FOO: isize = 0;
290         unsafe {
291             FOO += 1;
292             FOO
293         }
294     }
295     assert_eq!(
296         format!("{} {} {a} {b} {} {c}", foo(), foo(), foo(), a = foo(), b = foo(), c = foo()),
297         "1 2 4 5 3 6".to_string()
298     );
299 }
300
301 #[test]
302 fn test_once() {
303     // Make sure each argument are evaluated only once even though it may be
304     // formatted multiple times
305     fn foo() -> isize {
306         static mut FOO: isize = 0;
307         unsafe {
308             FOO += 1;
309             FOO
310         }
311     }
312     assert_eq!(format!("{0} {0} {0} {a} {a} {a}", foo(), a = foo()), "1 1 1 2 2 2".to_string());
313 }
314
315 #[test]
316 fn test_refcell() {
317     let refcell = RefCell::new(5);
318     assert_eq!(format!("{refcell:?}"), "RefCell { value: 5 }");
319     let borrow = refcell.borrow_mut();
320     assert_eq!(format!("{refcell:?}"), "RefCell { value: <borrowed> }");
321     drop(borrow);
322     assert_eq!(format!("{refcell:?}"), "RefCell { value: 5 }");
323 }