]> git.lizzy.rs Git - rust.git/blob - library/core/tests/fmt/mod.rs
Rollup merge of #94839 - TaKO8Ki:suggest-using-double-colon-for-struct-field-type...
[rust.git] / library / core / tests / fmt / mod.rs
1 mod builders;
2 mod float;
3 mod num;
4
5 #[test]
6 fn test_format_flags() {
7     // No residual flags left by pointer formatting
8     let p = "".as_ptr();
9     assert_eq!(format!("{:p} {:x}", p, 16), format!("{p:p} 10"));
10
11     assert_eq!(format!("{: >3}", 'a'), "  a");
12 }
13
14 #[test]
15 fn test_pointer_formats_data_pointer() {
16     let b: &[u8] = b"";
17     let s: &str = "";
18     assert_eq!(format!("{s:p}"), format!("{:p}", s.as_ptr()));
19     assert_eq!(format!("{b:p}"), format!("{:p}", b.as_ptr()));
20 }
21
22 #[test]
23 fn test_estimated_capacity() {
24     assert_eq!(format_args!("").estimated_capacity(), 0);
25     assert_eq!(format_args!("{}", "").estimated_capacity(), 0);
26     assert_eq!(format_args!("Hello").estimated_capacity(), 5);
27     assert_eq!(format_args!("Hello, {}!", "").estimated_capacity(), 16);
28     assert_eq!(format_args!("{}, hello!", "World").estimated_capacity(), 0);
29     assert_eq!(format_args!("{}. 16-bytes piece", "World").estimated_capacity(), 32);
30 }
31
32 #[test]
33 fn pad_integral_resets() {
34     struct Bar;
35
36     impl core::fmt::Display for Bar {
37         fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
38             "1".fmt(f)?;
39             f.pad_integral(true, "", "5")?;
40             "1".fmt(f)
41         }
42     }
43
44     assert_eq!(format!("{Bar:<03}"), "1  0051  ");
45 }