Skip to content

Commit 949c966

Browse files
committed
move format! interface tests
1 parent 85b2d9b commit 949c966

File tree

2 files changed

+322
-320
lines changed

2 files changed

+322
-320
lines changed

Diff for: library/alloc/tests/fmt.rs

+322-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,328 @@
1-
use std::fmt;
1+
#![deny(warnings)]
2+
#![allow(unused_must_use)]
3+
4+
use std::cell::RefCell;
5+
use std::fmt::{self, Write};
26

37
#[test]
48
fn test_format() {
59
let s = fmt::format(format_args!("Hello, {}!", "world"));
610
assert_eq!(s, "Hello, world!");
711
}
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!(
73+
format!("{:?}", "foo\n\"bar\"\r\n\'baz\'\t\\qux\\"),
74+
r#""foo\n\"bar\"\r\n\'baz\'\t\\qux\\""#
75+
);
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");
85+
} else {
86+
t!(format!("{:#p}", 0x1234 as *const isize), "0x0000000000001234");
87+
t!(format!("{:#p}", 0x1234 as *mut isize), "0x0000000000001234");
88+
}
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");
103+
104+
let a: &dyn fmt::Debug = &1;
105+
t!(format!("{:?}", a), "1");
106+
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/");
140+
141+
// Some float stuff
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");
148+
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");
156+
157+
// Float edge cases
158+
t!(format!("{}", -0.0), "0");
159+
t!(format!("{:?}", -0.0), "-0.0");
160+
t!(format!("{:?}", 0.0), "0.0");
161+
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");
190+
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");
196+
197+
// And its edge cases
198+
t!(
199+
format!(
200+
"{a:.0$} {b:.0$} {0:.0$}\n{a:.c$} {b:.c$} {c:.c$}",
201+
4,
202+
a = "abcdefg",
203+
b = "hijklmn",
204+
c = 3
205+
),
206+
"abcd hijk 4\nabc hij 3"
207+
);
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");
210+
211+
// Test that pointers don't get truncated.
212+
{
213+
let val = usize::MAX;
214+
let exp = format!("{:#x}", val);
215+
t!(format!("{:p}", val as *const isize), exp);
216+
}
217+
218+
// Escaping
219+
t!(format!("{{"), "{");
220+
t!(format!("}}"), "}");
221+
222+
// make sure that format! doesn't move out of local variables
223+
let a = Box::new(3);
224+
format!("{}", a);
225+
format!("{}", a);
226+
227+
// make sure that format! doesn't cause spurious unused-unsafe warnings when
228+
// it's inside of an outer unsafe block
229+
unsafe {
230+
let a: isize = ::std::mem::transmute(3_usize);
231+
format!("{}", a);
232+
}
233+
234+
// test that trailing commas are acceptable
235+
format!("{}", "test",);
236+
format!("{foo}", foo = "test",);
237+
}
238+
239+
// Basic test to make sure that we can invoke the `write!` macro with an
240+
// fmt::Write instance.
241+
#[test]
242+
fn test_write() {
243+
let mut buf = String::new();
244+
write!(&mut buf, "{}", 3);
245+
{
246+
let w = &mut buf;
247+
write!(w, "{foo}", foo = 4);
248+
write!(w, "{}", "hello");
249+
writeln!(w, "{}", "line");
250+
writeln!(w, "{foo}", foo = "bar");
251+
w.write_char('☃');
252+
w.write_str("str");
253+
}
254+
255+
t!(buf, "34helloline\nbar\n☃str");
256+
}
257+
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)
260+
#[test]
261+
fn test_print() {
262+
print!("hi");
263+
print!("{:?}", vec![0u8]);
264+
println!("hello");
265+
println!("this is a {}", "test");
266+
println!("{foo}", foo = "bar");
267+
}
268+
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)
271+
#[test]
272+
fn test_format_args() {
273+
let mut buf = String::new();
274+
{
275+
let w = &mut buf;
276+
write!(w, "{}", format_args!("{}", 1));
277+
write!(w, "{}", format_args!("test"));
278+
write!(w, "{}", format_args!("{test}", test = 3));
279+
}
280+
let s = buf;
281+
t!(s, "1test3");
282+
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");
287+
}
288+
289+
#[test]
290+
fn test_order() {
291+
// Make sure format!() arguments are always evaluated in a left-to-right
292+
// ordering
293+
fn foo() -> isize {
294+
static mut FOO: isize = 0;
295+
unsafe {
296+
FOO += 1;
297+
FOO
298+
}
299+
}
300+
assert_eq!(
301+
format!("{} {} {a} {b} {} {c}", foo(), foo(), foo(), a = foo(), b = foo(), c = foo()),
302+
"1 2 4 5 3 6".to_string()
303+
);
304+
}
305+
306+
#[test]
307+
fn test_once() {
308+
// Make sure each argument are evaluated only once even though it may be
309+
// formatted multiple times
310+
fn foo() -> isize {
311+
static mut FOO: isize = 0;
312+
unsafe {
313+
FOO += 1;
314+
FOO
315+
}
316+
}
317+
assert_eq!(format!("{0} {0} {0} {a} {a} {a}", foo(), a = foo()), "1 1 1 2 2 2".to_string());
318+
}
319+
320+
#[test]
321+
fn test_refcell() {
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> }");
326+
drop(borrow);
327+
assert_eq!(format!("{:?}", refcell), "RefCell { value: 5 }");
328+
}

0 commit comments

Comments
 (0)