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