Skip to content

Commit 734c57d

Browse files
authored
Rollup merge of #76454 - poliorcetics:ui-to-unit-test-1, r=matklad
UI to unit test for those using Cell/RefCell/UnsafeCell Helps with #76268. I'm working on all files using `Cell` and moving them to unit tests when possible. r? @matklad
2 parents 535d27a + a61b963 commit 734c57d

22 files changed

+667
-687
lines changed

library/alloc/tests/boxed.rs

+8
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use std::cell::Cell;
12
use std::mem::MaybeUninit;
23
use std::ptr::NonNull;
34

@@ -49,3 +50,10 @@ fn box_clone_from_ptr_stability() {
4950
assert_eq!(copy.as_ptr() as usize, copy_raw);
5051
}
5152
}
53+
54+
#[test]
55+
fn box_deref_lval() {
56+
let x = Box::new(Cell::new(5));
57+
x.set(1000);
58+
assert_eq!(x.get(), 1000);
59+
}

library/alloc/tests/fmt.rs

+321-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,327 @@
1-
use std::fmt;
1+
#![deny(warnings)]
2+
3+
use std::cell::RefCell;
4+
use std::fmt::{self, Write};
25

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

0 commit comments

Comments
 (0)