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
This commit is contained in:
commit
734c57d45c
22 changed files with 667 additions and 687 deletions
|
@ -1,3 +1,4 @@
|
|||
use std::cell::Cell;
|
||||
use std::mem::MaybeUninit;
|
||||
use std::ptr::NonNull;
|
||||
|
||||
|
@ -49,3 +50,10 @@ fn box_clone_from_ptr_stability() {
|
|||
assert_eq!(copy.as_ptr() as usize, copy_raw);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn box_deref_lval() {
|
||||
let x = Box::new(Cell::new(5));
|
||||
x.set(1000);
|
||||
assert_eq!(x.get(), 1000);
|
||||
}
|
||||
|
|
|
@ -1,7 +1,327 @@
|
|||
use std::fmt;
|
||||
#![deny(warnings)]
|
||||
|
||||
use std::cell::RefCell;
|
||||
use std::fmt::{self, Write};
|
||||
|
||||
#[test]
|
||||
fn test_format() {
|
||||
let s = fmt::format(format_args!("Hello, {}!", "world"));
|
||||
assert_eq!(s, "Hello, world!");
|
||||
}
|
||||
|
||||
struct A;
|
||||
struct B;
|
||||
struct C;
|
||||
struct D;
|
||||
|
||||
impl fmt::LowerHex for A {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
f.write_str("aloha")
|
||||
}
|
||||
}
|
||||
impl fmt::UpperHex for B {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
f.write_str("adios")
|
||||
}
|
||||
}
|
||||
impl fmt::Display for C {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
f.pad_integral(true, "☃", "123")
|
||||
}
|
||||
}
|
||||
impl fmt::Binary for D {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
f.write_str("aa")?;
|
||||
f.write_char('☃')?;
|
||||
f.write_str("bb")
|
||||
}
|
||||
}
|
||||
|
||||
macro_rules! t {
|
||||
($a:expr, $b:expr) => {
|
||||
assert_eq!($a, $b)
|
||||
};
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_format_macro_interface() {
|
||||
// Various edge cases without formats
|
||||
t!(format!(""), "");
|
||||
t!(format!("hello"), "hello");
|
||||
t!(format!("hello {{"), "hello {");
|
||||
|
||||
// default formatters should work
|
||||
t!(format!("{}", 1.0f32), "1");
|
||||
t!(format!("{}", 1.0f64), "1");
|
||||
t!(format!("{}", "a"), "a");
|
||||
t!(format!("{}", "a".to_string()), "a");
|
||||
t!(format!("{}", false), "false");
|
||||
t!(format!("{}", 'a'), "a");
|
||||
|
||||
// At least exercise all the formats
|
||||
t!(format!("{}", true), "true");
|
||||
t!(format!("{}", '☃'), "☃");
|
||||
t!(format!("{}", 10), "10");
|
||||
t!(format!("{}", 10_usize), "10");
|
||||
t!(format!("{:?}", '☃'), "'☃'");
|
||||
t!(format!("{:?}", 10), "10");
|
||||
t!(format!("{:?}", 10_usize), "10");
|
||||
t!(format!("{:?}", "true"), "\"true\"");
|
||||
t!(format!("{:?}", "foo\nbar"), "\"foo\\nbar\"");
|
||||
t!(
|
||||
format!("{:?}", "foo\n\"bar\"\r\n\'baz\'\t\\qux\\"),
|
||||
r#""foo\n\"bar\"\r\n\'baz\'\t\\qux\\""#
|
||||
);
|
||||
t!(format!("{:?}", "foo\0bar\x01baz\u{7f}q\u{75}x"), r#""foo\u{0}bar\u{1}baz\u{7f}qux""#);
|
||||
t!(format!("{:o}", 10_usize), "12");
|
||||
t!(format!("{:x}", 10_usize), "a");
|
||||
t!(format!("{:X}", 10_usize), "A");
|
||||
t!(format!("{}", "foo"), "foo");
|
||||
t!(format!("{}", "foo".to_string()), "foo");
|
||||
if cfg!(target_pointer_width = "32") {
|
||||
t!(format!("{:#p}", 0x1234 as *const isize), "0x00001234");
|
||||
t!(format!("{:#p}", 0x1234 as *mut isize), "0x00001234");
|
||||
} else {
|
||||
t!(format!("{:#p}", 0x1234 as *const isize), "0x0000000000001234");
|
||||
t!(format!("{:#p}", 0x1234 as *mut isize), "0x0000000000001234");
|
||||
}
|
||||
t!(format!("{:p}", 0x1234 as *const isize), "0x1234");
|
||||
t!(format!("{:p}", 0x1234 as *mut isize), "0x1234");
|
||||
t!(format!("{:x}", A), "aloha");
|
||||
t!(format!("{:X}", B), "adios");
|
||||
t!(format!("foo {} ☃☃☃☃☃☃", "bar"), "foo bar ☃☃☃☃☃☃");
|
||||
t!(format!("{1} {0}", 0, 1), "1 0");
|
||||
t!(format!("{foo} {bar}", foo = 0, bar = 1), "0 1");
|
||||
t!(format!("{foo} {1} {bar} {0}", 0, 1, foo = 2, bar = 3), "2 1 3 0");
|
||||
t!(format!("{} {0}", "a"), "a a");
|
||||
t!(format!("{_foo}", _foo = 6usize), "6");
|
||||
t!(format!("{foo_bar}", foo_bar = 1), "1");
|
||||
t!(format!("{}", 5 + 5), "10");
|
||||
t!(format!("{:#4}", C), "☃123");
|
||||
t!(format!("{:b}", D), "aa☃bb");
|
||||
|
||||
let a: &dyn fmt::Debug = &1;
|
||||
t!(format!("{:?}", a), "1");
|
||||
|
||||
// Formatting strings and their arguments
|
||||
t!(format!("{}", "a"), "a");
|
||||
t!(format!("{:4}", "a"), "a ");
|
||||
t!(format!("{:4}", "☃"), "☃ ");
|
||||
t!(format!("{:>4}", "a"), " a");
|
||||
t!(format!("{:<4}", "a"), "a ");
|
||||
t!(format!("{:^5}", "a"), " a ");
|
||||
t!(format!("{:^5}", "aa"), " aa ");
|
||||
t!(format!("{:^4}", "a"), " a ");
|
||||
t!(format!("{:^4}", "aa"), " aa ");
|
||||
t!(format!("{:.4}", "a"), "a");
|
||||
t!(format!("{:4.4}", "a"), "a ");
|
||||
t!(format!("{:4.4}", "aaaaaaaaaaaaaaaaaa"), "aaaa");
|
||||
t!(format!("{:<4.4}", "aaaaaaaaaaaaaaaaaa"), "aaaa");
|
||||
t!(format!("{:>4.4}", "aaaaaaaaaaaaaaaaaa"), "aaaa");
|
||||
t!(format!("{:^4.4}", "aaaaaaaaaaaaaaaaaa"), "aaaa");
|
||||
t!(format!("{:>10.4}", "aaaaaaaaaaaaaaaaaa"), " aaaa");
|
||||
t!(format!("{:2.4}", "aaaaa"), "aaaa");
|
||||
t!(format!("{:2.4}", "aaaa"), "aaaa");
|
||||
t!(format!("{:2.4}", "aaa"), "aaa");
|
||||
t!(format!("{:2.4}", "aa"), "aa");
|
||||
t!(format!("{:2.4}", "a"), "a ");
|
||||
t!(format!("{:0>2}", "a"), "0a");
|
||||
t!(format!("{:.*}", 4, "aaaaaaaaaaaaaaaaaa"), "aaaa");
|
||||
t!(format!("{:.1$}", "aaaaaaaaaaaaaaaaaa", 4), "aaaa");
|
||||
t!(format!("{:.a$}", "aaaaaaaaaaaaaaaaaa", a = 4), "aaaa");
|
||||
t!(format!("{:._a$}", "aaaaaaaaaaaaaaaaaa", _a = 4), "aaaa");
|
||||
t!(format!("{:1$}", "a", 4), "a ");
|
||||
t!(format!("{1:0$}", 4, "a"), "a ");
|
||||
t!(format!("{:a$}", "a", a = 4), "a ");
|
||||
t!(format!("{:-#}", "a"), "a");
|
||||
t!(format!("{:+#}", "a"), "a");
|
||||
t!(format!("{:/^10.8}", "1234567890"), "/12345678/");
|
||||
|
||||
// Some float stuff
|
||||
t!(format!("{:}", 1.0f32), "1");
|
||||
t!(format!("{:}", 1.0f64), "1");
|
||||
t!(format!("{:.3}", 1.0f64), "1.000");
|
||||
t!(format!("{:10.3}", 1.0f64), " 1.000");
|
||||
t!(format!("{:+10.3}", 1.0f64), " +1.000");
|
||||
t!(format!("{:+10.3}", -1.0f64), " -1.000");
|
||||
|
||||
t!(format!("{:e}", 1.2345e6f32), "1.2345e6");
|
||||
t!(format!("{:e}", 1.2345e6f64), "1.2345e6");
|
||||
t!(format!("{:E}", 1.2345e6f64), "1.2345E6");
|
||||
t!(format!("{:.3e}", 1.2345e6f64), "1.234e6");
|
||||
t!(format!("{:10.3e}", 1.2345e6f64), " 1.234e6");
|
||||
t!(format!("{:+10.3e}", 1.2345e6f64), " +1.234e6");
|
||||
t!(format!("{:+10.3e}", -1.2345e6f64), " -1.234e6");
|
||||
|
||||
// Float edge cases
|
||||
t!(format!("{}", -0.0), "0");
|
||||
t!(format!("{:?}", -0.0), "-0.0");
|
||||
t!(format!("{:?}", 0.0), "0.0");
|
||||
|
||||
// sign aware zero padding
|
||||
t!(format!("{:<3}", 1), "1 ");
|
||||
t!(format!("{:>3}", 1), " 1");
|
||||
t!(format!("{:^3}", 1), " 1 ");
|
||||
t!(format!("{:03}", 1), "001");
|
||||
t!(format!("{:<03}", 1), "001");
|
||||
t!(format!("{:>03}", 1), "001");
|
||||
t!(format!("{:^03}", 1), "001");
|
||||
t!(format!("{:+03}", 1), "+01");
|
||||
t!(format!("{:<+03}", 1), "+01");
|
||||
t!(format!("{:>+03}", 1), "+01");
|
||||
t!(format!("{:^+03}", 1), "+01");
|
||||
t!(format!("{:#05x}", 1), "0x001");
|
||||
t!(format!("{:<#05x}", 1), "0x001");
|
||||
t!(format!("{:>#05x}", 1), "0x001");
|
||||
t!(format!("{:^#05x}", 1), "0x001");
|
||||
t!(format!("{:05}", 1.2), "001.2");
|
||||
t!(format!("{:<05}", 1.2), "001.2");
|
||||
t!(format!("{:>05}", 1.2), "001.2");
|
||||
t!(format!("{:^05}", 1.2), "001.2");
|
||||
t!(format!("{:05}", -1.2), "-01.2");
|
||||
t!(format!("{:<05}", -1.2), "-01.2");
|
||||
t!(format!("{:>05}", -1.2), "-01.2");
|
||||
t!(format!("{:^05}", -1.2), "-01.2");
|
||||
t!(format!("{:+05}", 1.2), "+01.2");
|
||||
t!(format!("{:<+05}", 1.2), "+01.2");
|
||||
t!(format!("{:>+05}", 1.2), "+01.2");
|
||||
t!(format!("{:^+05}", 1.2), "+01.2");
|
||||
|
||||
// Ergonomic format_args!
|
||||
t!(format!("{0:x} {0:X}", 15), "f F");
|
||||
t!(format!("{0:x} {0:X} {}", 15), "f F 15");
|
||||
t!(format!("{:x}{0:X}{a:x}{:X}{1:x}{a:X}", 13, 14, a = 15), "dDfEeF");
|
||||
t!(format!("{a:x} {a:X}", a = 15), "f F");
|
||||
|
||||
// And its edge cases
|
||||
t!(
|
||||
format!(
|
||||
"{a:.0$} {b:.0$} {0:.0$}\n{a:.c$} {b:.c$} {c:.c$}",
|
||||
4,
|
||||
a = "abcdefg",
|
||||
b = "hijklmn",
|
||||
c = 3
|
||||
),
|
||||
"abcd hijk 4\nabc hij 3"
|
||||
);
|
||||
t!(format!("{a:.*} {0} {:.*}", 4, 3, "efgh", a = "abcdef"), "abcd 4 efg");
|
||||
t!(format!("{:.a$} {a} {a:#x}", "aaaaaa", a = 2), "aa 2 0x2");
|
||||
|
||||
// Test that pointers don't get truncated.
|
||||
{
|
||||
let val = usize::MAX;
|
||||
let exp = format!("{:#x}", val);
|
||||
t!(format!("{:p}", val as *const isize), exp);
|
||||
}
|
||||
|
||||
// Escaping
|
||||
t!(format!("{{"), "{");
|
||||
t!(format!("}}"), "}");
|
||||
|
||||
// make sure that format! doesn't move out of local variables
|
||||
let a = Box::new(3);
|
||||
format!("{}", a);
|
||||
format!("{}", a);
|
||||
|
||||
// make sure that format! doesn't cause spurious unused-unsafe warnings when
|
||||
// it's inside of an outer unsafe block
|
||||
unsafe {
|
||||
let a: isize = ::std::mem::transmute(3_usize);
|
||||
format!("{}", a);
|
||||
}
|
||||
|
||||
// test that trailing commas are acceptable
|
||||
format!("{}", "test",);
|
||||
format!("{foo}", foo = "test",);
|
||||
}
|
||||
|
||||
// Basic test to make sure that we can invoke the `write!` macro with an
|
||||
// fmt::Write instance.
|
||||
#[test]
|
||||
fn test_write() {
|
||||
let mut buf = String::new();
|
||||
let _ = write!(&mut buf, "{}", 3);
|
||||
{
|
||||
let w = &mut buf;
|
||||
let _ = write!(w, "{foo}", foo = 4);
|
||||
let _ = write!(w, "{}", "hello");
|
||||
let _ = writeln!(w, "{}", "line");
|
||||
let _ = writeln!(w, "{foo}", foo = "bar");
|
||||
let _ = w.write_char('☃');
|
||||
let _ = w.write_str("str");
|
||||
}
|
||||
|
||||
t!(buf, "34helloline\nbar\n☃str");
|
||||
}
|
||||
|
||||
// Just make sure that the macros are defined, there's not really a lot that we
|
||||
// can do with them just yet (to test the output)
|
||||
#[test]
|
||||
fn test_print() {
|
||||
print!("hi");
|
||||
print!("{:?}", vec![0u8]);
|
||||
println!("hello");
|
||||
println!("this is a {}", "test");
|
||||
println!("{foo}", foo = "bar");
|
||||
}
|
||||
|
||||
// Just make sure that the macros are defined, there's not really a lot that we
|
||||
// can do with them just yet (to test the output)
|
||||
#[test]
|
||||
fn test_format_args() {
|
||||
let mut buf = String::new();
|
||||
{
|
||||
let w = &mut buf;
|
||||
let _ = write!(w, "{}", format_args!("{}", 1));
|
||||
let _ = write!(w, "{}", format_args!("test"));
|
||||
let _ = write!(w, "{}", format_args!("{test}", test = 3));
|
||||
}
|
||||
let s = buf;
|
||||
t!(s, "1test3");
|
||||
|
||||
let s = fmt::format(format_args!("hello {}", "world"));
|
||||
t!(s, "hello world");
|
||||
let s = format!("{}: {}", "args were", format_args!("hello {}", "world"));
|
||||
t!(s, "args were: hello world");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_order() {
|
||||
// Make sure format!() arguments are always evaluated in a left-to-right
|
||||
// ordering
|
||||
fn foo() -> isize {
|
||||
static mut FOO: isize = 0;
|
||||
unsafe {
|
||||
FOO += 1;
|
||||
FOO
|
||||
}
|
||||
}
|
||||
assert_eq!(
|
||||
format!("{} {} {a} {b} {} {c}", foo(), foo(), foo(), a = foo(), b = foo(), c = foo()),
|
||||
"1 2 4 5 3 6".to_string()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_once() {
|
||||
// Make sure each argument are evaluated only once even though it may be
|
||||
// formatted multiple times
|
||||
fn foo() -> isize {
|
||||
static mut FOO: isize = 0;
|
||||
unsafe {
|
||||
FOO += 1;
|
||||
FOO
|
||||
}
|
||||
}
|
||||
assert_eq!(format!("{0} {0} {0} {a} {a} {a}", foo(), a = foo()), "1 1 1 2 2 2".to_string());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_refcell() {
|
||||
let refcell = RefCell::new(5);
|
||||
assert_eq!(format!("{:?}", refcell), "RefCell { value: 5 }");
|
||||
let borrow = refcell.borrow_mut();
|
||||
assert_eq!(format!("{:?}", refcell), "RefCell { value: <borrowed> }");
|
||||
drop(borrow);
|
||||
assert_eq!(format!("{:?}", refcell), "RefCell { value: 5 }");
|
||||
}
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
use std::borrow::Cow;
|
||||
use std::cell::Cell;
|
||||
use std::collections::TryReserveError::*;
|
||||
use std::fmt::Debug;
|
||||
use std::iter::InPlaceIterable;
|
||||
|
@ -1831,3 +1832,82 @@ fn partialeq_vec_full() {
|
|||
assert_partial_eq_valid!(vec2,vec3; array2,array3);
|
||||
assert_partial_eq_valid!(vec2,vec3; arrayref2,arrayref3);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_vec_cycle() {
|
||||
#[derive(Debug)]
|
||||
struct C<'a> {
|
||||
v: Vec<Cell<Option<&'a C<'a>>>>,
|
||||
}
|
||||
|
||||
impl<'a> C<'a> {
|
||||
fn new() -> C<'a> {
|
||||
C { v: Vec::new() }
|
||||
}
|
||||
}
|
||||
|
||||
let mut c1 = C::new();
|
||||
let mut c2 = C::new();
|
||||
let mut c3 = C::new();
|
||||
|
||||
// Push
|
||||
c1.v.push(Cell::new(None));
|
||||
c1.v.push(Cell::new(None));
|
||||
|
||||
c2.v.push(Cell::new(None));
|
||||
c2.v.push(Cell::new(None));
|
||||
|
||||
c3.v.push(Cell::new(None));
|
||||
c3.v.push(Cell::new(None));
|
||||
|
||||
// Set
|
||||
c1.v[0].set(Some(&c2));
|
||||
c1.v[1].set(Some(&c3));
|
||||
|
||||
c2.v[0].set(Some(&c2));
|
||||
c2.v[1].set(Some(&c3));
|
||||
|
||||
c3.v[0].set(Some(&c1));
|
||||
c3.v[1].set(Some(&c2));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_vec_cycle_wrapped() {
|
||||
struct Refs<'a> {
|
||||
v: Vec<Cell<Option<&'a C<'a>>>>,
|
||||
}
|
||||
|
||||
struct C<'a> {
|
||||
refs: Refs<'a>,
|
||||
}
|
||||
|
||||
impl<'a> Refs<'a> {
|
||||
fn new() -> Refs<'a> {
|
||||
Refs { v: Vec::new() }
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> C<'a> {
|
||||
fn new() -> C<'a> {
|
||||
C { refs: Refs::new() }
|
||||
}
|
||||
}
|
||||
|
||||
let mut c1 = C::new();
|
||||
let mut c2 = C::new();
|
||||
let mut c3 = C::new();
|
||||
|
||||
c1.refs.v.push(Cell::new(None));
|
||||
c1.refs.v.push(Cell::new(None));
|
||||
c2.refs.v.push(Cell::new(None));
|
||||
c2.refs.v.push(Cell::new(None));
|
||||
c3.refs.v.push(Cell::new(None));
|
||||
c3.refs.v.push(Cell::new(None));
|
||||
|
||||
c1.refs.v[0].set(Some(&c2));
|
||||
c1.refs.v[1].set(Some(&c3));
|
||||
c2.refs.v[0].set(Some(&c2));
|
||||
c2.refs.v[1].set(Some(&c3));
|
||||
c3.refs.v[0].set(Some(&c1));
|
||||
c3.refs.v[1].set(Some(&c2));
|
||||
}
|
||||
|
|
|
@ -2238,5 +2238,6 @@ impl<T: ?Sized + Debug> Debug for UnsafeCell<T> {
|
|||
}
|
||||
}
|
||||
|
||||
// If you expected tests to be here, look instead at the ui/ifmt.rs test,
|
||||
// If you expected tests to be here, look instead at the core/tests/fmt.rs file,
|
||||
// it's a lot easier than creating all of the rt::Piece structures here.
|
||||
// There are also tests in the alloc crate, for those that need allocations.
|
||||
|
|
|
@ -345,3 +345,32 @@ fn array_map_drop_safety() {
|
|||
assert_eq!(DROPPED.load(Ordering::SeqCst), num_to_create);
|
||||
panic!("test succeeded")
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cell_allows_array_cycle() {
|
||||
use core::cell::Cell;
|
||||
|
||||
#[derive(Debug)]
|
||||
struct B<'a> {
|
||||
a: [Cell<Option<&'a B<'a>>>; 2],
|
||||
}
|
||||
|
||||
impl<'a> B<'a> {
|
||||
fn new() -> B<'a> {
|
||||
B { a: [Cell::new(None), Cell::new(None)] }
|
||||
}
|
||||
}
|
||||
|
||||
let b1 = B::new();
|
||||
let b2 = B::new();
|
||||
let b3 = B::new();
|
||||
|
||||
b1.a[0].set(Some(&b2));
|
||||
b1.a[1].set(Some(&b3));
|
||||
|
||||
b2.a[0].set(Some(&b2));
|
||||
b2.a[1].set(Some(&b3));
|
||||
|
||||
b3.a[0].set(Some(&b1));
|
||||
b3.a[1].set(Some(&b2));
|
||||
}
|
||||
|
|
|
@ -303,6 +303,53 @@ fn cell_into_inner() {
|
|||
assert_eq!("Hello world".to_owned(), cell.into_inner());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cell_exterior() {
|
||||
#[derive(Copy, Clone)]
|
||||
#[allow(dead_code)]
|
||||
struct Point {
|
||||
x: isize,
|
||||
y: isize,
|
||||
z: isize,
|
||||
}
|
||||
|
||||
fn f(p: &Cell<Point>) {
|
||||
assert_eq!(p.get().z, 12);
|
||||
p.set(Point { x: 10, y: 11, z: 13 });
|
||||
assert_eq!(p.get().z, 13);
|
||||
}
|
||||
|
||||
let a = Point { x: 10, y: 11, z: 12 };
|
||||
let b = &Cell::new(a);
|
||||
assert_eq!(b.get().z, 12);
|
||||
f(b);
|
||||
assert_eq!(a.z, 12);
|
||||
assert_eq!(b.get().z, 13);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cell_does_not_clone() {
|
||||
#[derive(Copy)]
|
||||
#[allow(dead_code)]
|
||||
struct Foo {
|
||||
x: isize,
|
||||
}
|
||||
|
||||
impl Clone for Foo {
|
||||
fn clone(&self) -> Foo {
|
||||
// Using Cell in any way should never cause clone() to be
|
||||
// invoked -- after all, that would permit evil user code to
|
||||
// abuse `Cell` and trigger crashes.
|
||||
|
||||
panic!();
|
||||
}
|
||||
}
|
||||
|
||||
let x = Cell::new(Foo { x: 22 });
|
||||
let _y = x.get();
|
||||
let _z = x.clone();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn refcell_default() {
|
||||
let cell: RefCell<u64> = Default::default();
|
||||
|
@ -367,3 +414,11 @@ fn refcell_replace_borrows() {
|
|||
let _b = x.borrow();
|
||||
x.replace(1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn refcell_format() {
|
||||
let name = RefCell::new("rust");
|
||||
let what = RefCell::new("rocks");
|
||||
let msg = format!("{name} {}", &*what.borrow(), name = &*name.borrow());
|
||||
assert_eq!(msg, "rust rocks".to_string());
|
||||
}
|
||||
|
|
|
@ -376,6 +376,103 @@ fn test_zip_next_back_side_effects_exhausted() {
|
|||
assert_eq!(b, vec![200, 300, 400]);
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
struct CountClone(Cell<i32>);
|
||||
|
||||
fn count_clone() -> CountClone {
|
||||
CountClone(Cell::new(0))
|
||||
}
|
||||
|
||||
impl PartialEq<i32> for CountClone {
|
||||
fn eq(&self, rhs: &i32) -> bool {
|
||||
self.0.get() == *rhs
|
||||
}
|
||||
}
|
||||
|
||||
impl Clone for CountClone {
|
||||
fn clone(&self) -> Self {
|
||||
let ret = CountClone(self.0.clone());
|
||||
let n = self.0.get();
|
||||
self.0.set(n + 1);
|
||||
ret
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_zip_cloned_sideffectful() {
|
||||
let xs = [count_clone(), count_clone(), count_clone(), count_clone()];
|
||||
let ys = [count_clone(), count_clone()];
|
||||
|
||||
for _ in xs.iter().cloned().zip(ys.iter().cloned()) {}
|
||||
|
||||
assert_eq!(&xs, &[1, 1, 1, 0][..]);
|
||||
assert_eq!(&ys, &[1, 1][..]);
|
||||
|
||||
let xs = [count_clone(), count_clone()];
|
||||
let ys = [count_clone(), count_clone(), count_clone(), count_clone()];
|
||||
|
||||
for _ in xs.iter().cloned().zip(ys.iter().cloned()) {}
|
||||
|
||||
assert_eq!(&xs, &[1, 1][..]);
|
||||
assert_eq!(&ys, &[1, 1, 0, 0][..]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_zip_map_sideffectful() {
|
||||
let mut xs = [0; 6];
|
||||
let mut ys = [0; 4];
|
||||
|
||||
for _ in xs.iter_mut().map(|x| *x += 1).zip(ys.iter_mut().map(|y| *y += 1)) {}
|
||||
|
||||
assert_eq!(&xs, &[1, 1, 1, 1, 1, 0]);
|
||||
assert_eq!(&ys, &[1, 1, 1, 1]);
|
||||
|
||||
let mut xs = [0; 4];
|
||||
let mut ys = [0; 6];
|
||||
|
||||
for _ in xs.iter_mut().map(|x| *x += 1).zip(ys.iter_mut().map(|y| *y += 1)) {}
|
||||
|
||||
assert_eq!(&xs, &[1, 1, 1, 1]);
|
||||
assert_eq!(&ys, &[1, 1, 1, 1, 0, 0]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_zip_map_rev_sideffectful() {
|
||||
let mut xs = [0; 6];
|
||||
let mut ys = [0; 4];
|
||||
|
||||
{
|
||||
let mut it = xs.iter_mut().map(|x| *x += 1).zip(ys.iter_mut().map(|y| *y += 1));
|
||||
it.next_back();
|
||||
}
|
||||
assert_eq!(&xs, &[0, 0, 0, 1, 1, 1]);
|
||||
assert_eq!(&ys, &[0, 0, 0, 1]);
|
||||
|
||||
let mut xs = [0; 6];
|
||||
let mut ys = [0; 4];
|
||||
|
||||
{
|
||||
let mut it = xs.iter_mut().map(|x| *x += 1).zip(ys.iter_mut().map(|y| *y += 1));
|
||||
(&mut it).take(5).count();
|
||||
it.next_back();
|
||||
}
|
||||
assert_eq!(&xs, &[1, 1, 1, 1, 1, 1]);
|
||||
assert_eq!(&ys, &[1, 1, 1, 1]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_zip_nested_sideffectful() {
|
||||
let mut xs = [0; 6];
|
||||
let ys = [0; 4];
|
||||
|
||||
{
|
||||
// test that it has the side effect nested inside enumerate
|
||||
let it = xs.iter_mut().map(|x| *x = 1).enumerate().zip(&ys);
|
||||
it.count();
|
||||
}
|
||||
assert_eq!(&xs, &[1, 1, 1, 1, 1, 0]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_zip_nth_back_side_effects_exhausted() {
|
||||
let mut a = Vec::new();
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
use core::cell::Cell;
|
||||
use core::clone::Clone;
|
||||
use core::mem;
|
||||
use core::ops::DerefMut;
|
||||
|
@ -372,3 +373,32 @@ fn option_const() {
|
|||
const IS_NONE: bool = OPTION.is_none();
|
||||
assert!(!IS_NONE);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_unwrap_drop() {
|
||||
struct Dtor<'a> {
|
||||
x: &'a Cell<isize>,
|
||||
}
|
||||
|
||||
impl<'a> std::ops::Drop for Dtor<'a> {
|
||||
fn drop(&mut self) {
|
||||
self.x.set(self.x.get() - 1);
|
||||
}
|
||||
}
|
||||
|
||||
fn unwrap<T>(o: Option<T>) -> T {
|
||||
match o {
|
||||
Some(v) => v,
|
||||
None => panic!(),
|
||||
}
|
||||
}
|
||||
|
||||
let x = &Cell::new(1);
|
||||
|
||||
{
|
||||
let b = Some(Dtor { x });
|
||||
let _c = unwrap(b);
|
||||
}
|
||||
|
||||
assert_eq!(x.get(), 0);
|
||||
}
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
use core::cell::Cell;
|
||||
use core::result::Result::{Err, Ok};
|
||||
|
||||
#[test]
|
||||
|
@ -1980,3 +1981,30 @@ fn test_is_sorted() {
|
|||
assert!(!["c", "bb", "aaa"].is_sorted());
|
||||
assert!(["c", "bb", "aaa"].is_sorted_by_key(|s| s.len()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_slice_run_destructors() {
|
||||
// Make sure that destructors get run on slice literals
|
||||
struct Foo<'a> {
|
||||
x: &'a Cell<isize>,
|
||||
}
|
||||
|
||||
impl<'a> Drop for Foo<'a> {
|
||||
fn drop(&mut self) {
|
||||
self.x.set(self.x.get() + 1);
|
||||
}
|
||||
}
|
||||
|
||||
fn foo(x: &Cell<isize>) -> Foo<'_> {
|
||||
Foo { x }
|
||||
}
|
||||
|
||||
let x = &Cell::new(0);
|
||||
|
||||
{
|
||||
let l = &[foo(x)];
|
||||
assert_eq!(l[0].x.get(), 0);
|
||||
}
|
||||
|
||||
assert_eq!(x.get(), 1);
|
||||
}
|
||||
|
|
|
@ -411,3 +411,6 @@ pub fn catch_unwind<F: FnOnce() -> R + UnwindSafe, R>(f: F) -> Result<R> {
|
|||
pub fn resume_unwind(payload: Box<dyn Any + Send>) -> ! {
|
||||
panicking::rust_panic_without_hook(payload)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
|
|
|
@ -1,16 +1,18 @@
|
|||
// run-pass
|
||||
#![allow(dead_code)]
|
||||
|
||||
use std::panic::{UnwindSafe, AssertUnwindSafe};
|
||||
use std::cell::RefCell;
|
||||
use std::sync::{Mutex, RwLock, Arc};
|
||||
use std::rc::Rc;
|
||||
use crate::cell::RefCell;
|
||||
use crate::panic::{AssertUnwindSafe, UnwindSafe};
|
||||
use crate::rc::Rc;
|
||||
use crate::sync::{Arc, Mutex, RwLock};
|
||||
|
||||
struct Foo { a: i32 }
|
||||
struct Foo {
|
||||
a: i32,
|
||||
}
|
||||
|
||||
fn assert<T: UnwindSafe + ?Sized>() {}
|
||||
|
||||
fn main() {
|
||||
#[test]
|
||||
fn panic_safety_traits() {
|
||||
assert::<i32>();
|
||||
assert::<&i32>();
|
||||
assert::<*mut i32>();
|
||||
|
@ -32,13 +34,16 @@ fn main() {
|
|||
assert::<Arc<i32>>();
|
||||
assert::<Box<[u8]>>();
|
||||
|
||||
trait Trait: UnwindSafe {}
|
||||
assert::<Box<dyn Trait>>();
|
||||
{
|
||||
trait Trait: UnwindSafe {}
|
||||
assert::<Box<dyn Trait>>();
|
||||
}
|
||||
|
||||
fn bar<T>() {
|
||||
assert::<Mutex<T>>();
|
||||
assert::<RwLock<T>>();
|
||||
}
|
||||
|
||||
fn baz<T: UnwindSafe>() {
|
||||
assert::<Box<T>>();
|
||||
assert::<Vec<T>>();
|
|
@ -1,31 +0,0 @@
|
|||
// run-pass
|
||||
|
||||
use std::cell::Cell;
|
||||
|
||||
#[derive(Debug)]
|
||||
struct B<'a> {
|
||||
a: [Cell<Option<&'a B<'a>>>; 2]
|
||||
}
|
||||
|
||||
impl<'a> B<'a> {
|
||||
fn new() -> B<'a> {
|
||||
B { a: [Cell::new(None), Cell::new(None)] }
|
||||
}
|
||||
}
|
||||
|
||||
fn f() {
|
||||
let (b1, b2, b3);
|
||||
b1 = B::new();
|
||||
b2 = B::new();
|
||||
b3 = B::new();
|
||||
b1.a[0].set(Some(&b2));
|
||||
b1.a[1].set(Some(&b3));
|
||||
b2.a[0].set(Some(&b2));
|
||||
b2.a[1].set(Some(&b3));
|
||||
b3.a[0].set(Some(&b1));
|
||||
b3.a[1].set(Some(&b2));
|
||||
}
|
||||
|
||||
fn main() {
|
||||
f();
|
||||
}
|
|
@ -1,31 +0,0 @@
|
|||
// run-pass
|
||||
|
||||
#![allow(non_camel_case_types)]
|
||||
|
||||
use std::cell::Cell;
|
||||
|
||||
// Make sure that destructors get run on slice literals
|
||||
struct foo<'a> {
|
||||
x: &'a Cell<isize>,
|
||||
}
|
||||
|
||||
impl<'a> Drop for foo<'a> {
|
||||
fn drop(&mut self) {
|
||||
self.x.set(self.x.get() + 1);
|
||||
}
|
||||
}
|
||||
|
||||
fn foo(x: &Cell<isize>) -> foo {
|
||||
foo {
|
||||
x: x
|
||||
}
|
||||
}
|
||||
|
||||
pub fn main() {
|
||||
let x = &Cell::new(0);
|
||||
{
|
||||
let l = &[foo(x)];
|
||||
assert_eq!(l[0].x.get(), 0);
|
||||
}
|
||||
assert_eq!(x.get(), 1);
|
||||
}
|
|
@ -1,39 +0,0 @@
|
|||
// run-pass
|
||||
|
||||
use std::cell::Cell;
|
||||
|
||||
#[derive(Debug)]
|
||||
struct C<'a> {
|
||||
v: Vec<Cell<Option<&'a C<'a>>>>,
|
||||
}
|
||||
|
||||
impl<'a> C<'a> {
|
||||
fn new() -> C<'a> {
|
||||
C { v: Vec::new() }
|
||||
}
|
||||
}
|
||||
|
||||
fn f() {
|
||||
let (mut c1, mut c2, mut c3);
|
||||
c1 = C::new();
|
||||
c2 = C::new();
|
||||
c3 = C::new();
|
||||
|
||||
c1.v.push(Cell::new(None));
|
||||
c1.v.push(Cell::new(None));
|
||||
c2.v.push(Cell::new(None));
|
||||
c2.v.push(Cell::new(None));
|
||||
c3.v.push(Cell::new(None));
|
||||
c3.v.push(Cell::new(None));
|
||||
|
||||
c1.v[0].set(Some(&c2));
|
||||
c1.v[1].set(Some(&c3));
|
||||
c2.v[0].set(Some(&c2));
|
||||
c2.v[1].set(Some(&c3));
|
||||
c3.v[0].set(Some(&c1));
|
||||
c3.v[1].set(Some(&c2));
|
||||
}
|
||||
|
||||
fn main() {
|
||||
f();
|
||||
}
|
|
@ -1,50 +0,0 @@
|
|||
// run-pass
|
||||
|
||||
use std::cell::Cell;
|
||||
|
||||
#[derive(Debug)]
|
||||
struct Refs<'a> {
|
||||
v: Vec<Cell<Option<&'a C<'a>>>>,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
struct C<'a> {
|
||||
refs: Refs<'a>,
|
||||
}
|
||||
|
||||
impl<'a> Refs<'a> {
|
||||
fn new() -> Refs<'a> {
|
||||
Refs { v: Vec::new() }
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> C<'a> {
|
||||
fn new() -> C<'a> {
|
||||
C { refs: Refs::new() }
|
||||
}
|
||||
}
|
||||
|
||||
fn f() {
|
||||
let (mut c1, mut c2, mut c3);
|
||||
c1 = C::new();
|
||||
c2 = C::new();
|
||||
c3 = C::new();
|
||||
|
||||
c1.refs.v.push(Cell::new(None));
|
||||
c1.refs.v.push(Cell::new(None));
|
||||
c2.refs.v.push(Cell::new(None));
|
||||
c2.refs.v.push(Cell::new(None));
|
||||
c3.refs.v.push(Cell::new(None));
|
||||
c3.refs.v.push(Cell::new(None));
|
||||
|
||||
c1.refs.v[0].set(Some(&c2));
|
||||
c1.refs.v[1].set(Some(&c3));
|
||||
c2.refs.v[0].set(Some(&c2));
|
||||
c2.refs.v[1].set(Some(&c3));
|
||||
c3.refs.v[0].set(Some(&c1));
|
||||
c3.refs.v[1].set(Some(&c2));
|
||||
}
|
||||
|
||||
fn main() {
|
||||
f();
|
||||
}
|
|
@ -1,26 +0,0 @@
|
|||
// run-pass
|
||||
|
||||
#![allow(dead_code)]
|
||||
|
||||
use std::cell::Cell;
|
||||
|
||||
#[derive(Copy)]
|
||||
struct Foo {
|
||||
x: isize
|
||||
}
|
||||
|
||||
impl Clone for Foo {
|
||||
fn clone(&self) -> Foo {
|
||||
// Using Cell in any way should never cause clone() to be
|
||||
// invoked -- after all, that would permit evil user code to
|
||||
// abuse `Cell` and trigger crashes.
|
||||
|
||||
panic!();
|
||||
}
|
||||
}
|
||||
|
||||
pub fn main() {
|
||||
let x = Cell::new(Foo { x: 22 });
|
||||
let _y = x.get();
|
||||
let _z = x.clone();
|
||||
}
|
|
@ -1,11 +0,0 @@
|
|||
// run-pass
|
||||
|
||||
#![feature(box_syntax)]
|
||||
|
||||
use std::cell::Cell;
|
||||
|
||||
pub fn main() {
|
||||
let x: Box<_> = box Cell::new(5);
|
||||
x.set(1000);
|
||||
println!("{}", x.get());
|
||||
}
|
|
@ -1,24 +0,0 @@
|
|||
// run-pass
|
||||
|
||||
#![allow(dead_code)]
|
||||
|
||||
|
||||
use std::cell::Cell;
|
||||
|
||||
#[derive(Copy, Clone)]
|
||||
struct Point {x: isize, y: isize, z: isize}
|
||||
|
||||
fn f(p: &Cell<Point>) {
|
||||
assert_eq!(p.get().z, 12);
|
||||
p.set(Point {x: 10, y: 11, z: 13});
|
||||
assert_eq!(p.get().z, 13);
|
||||
}
|
||||
|
||||
pub fn main() {
|
||||
let a: Point = Point {x: 10, y: 11, z: 12};
|
||||
let b: &Cell<Point> = &Cell::new(a);
|
||||
assert_eq!(b.get().z, 12);
|
||||
f(b);
|
||||
assert_eq!(a.z, 12);
|
||||
assert_eq!(b.get().z, 13);
|
||||
}
|
|
@ -1,10 +0,0 @@
|
|||
// run-pass
|
||||
|
||||
use std::cell::RefCell;
|
||||
|
||||
pub fn main() {
|
||||
let name = RefCell::new("rust");
|
||||
let what = RefCell::new("rocks");
|
||||
let msg = format!("{name} {}", &*what.borrow(), name=&*name.borrow());
|
||||
assert_eq!(msg, "rust rocks".to_string());
|
||||
}
|
|
@ -1,319 +0,0 @@
|
|||
// run-pass
|
||||
|
||||
#![deny(warnings)]
|
||||
#![allow(unused_must_use)]
|
||||
#![allow(unused_features)]
|
||||
#![feature(box_syntax)]
|
||||
|
||||
use std::cell::RefCell;
|
||||
use std::fmt::{self, Write};
|
||||
use std::usize;
|
||||
|
||||
struct A;
|
||||
struct B;
|
||||
struct C;
|
||||
struct D;
|
||||
|
||||
impl fmt::LowerHex for A {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
f.write_str("aloha")
|
||||
}
|
||||
}
|
||||
impl fmt::UpperHex for B {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
f.write_str("adios")
|
||||
}
|
||||
}
|
||||
impl fmt::Display for C {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
f.pad_integral(true, "☃", "123")
|
||||
}
|
||||
}
|
||||
impl fmt::Binary for D {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
f.write_str("aa")?;
|
||||
f.write_char('☃')?;
|
||||
f.write_str("bb")
|
||||
}
|
||||
}
|
||||
|
||||
macro_rules! t {
|
||||
($a:expr, $b:expr) => { assert_eq!($a, $b) }
|
||||
}
|
||||
|
||||
pub fn main() {
|
||||
// Various edge cases without formats
|
||||
t!(format!(""), "");
|
||||
t!(format!("hello"), "hello");
|
||||
t!(format!("hello {{"), "hello {");
|
||||
|
||||
// default formatters should work
|
||||
t!(format!("{}", 1.0f32), "1");
|
||||
t!(format!("{}", 1.0f64), "1");
|
||||
t!(format!("{}", "a"), "a");
|
||||
t!(format!("{}", "a".to_string()), "a");
|
||||
t!(format!("{}", false), "false");
|
||||
t!(format!("{}", 'a'), "a");
|
||||
|
||||
// At least exercise all the formats
|
||||
t!(format!("{}", true), "true");
|
||||
t!(format!("{}", '☃'), "☃");
|
||||
t!(format!("{}", 10), "10");
|
||||
t!(format!("{}", 10_usize), "10");
|
||||
t!(format!("{:?}", '☃'), "'☃'");
|
||||
t!(format!("{:?}", 10), "10");
|
||||
t!(format!("{:?}", 10_usize), "10");
|
||||
t!(format!("{:?}", "true"), "\"true\"");
|
||||
t!(format!("{:?}", "foo\nbar"), "\"foo\\nbar\"");
|
||||
t!(format!("{:?}", "foo\n\"bar\"\r\n\'baz\'\t\\qux\\"),
|
||||
r#""foo\n\"bar\"\r\n\'baz\'\t\\qux\\""#);
|
||||
t!(format!("{:?}", "foo\0bar\x01baz\u{7f}q\u{75}x"),
|
||||
r#""foo\u{0}bar\u{1}baz\u{7f}qux""#);
|
||||
t!(format!("{:o}", 10_usize), "12");
|
||||
t!(format!("{:x}", 10_usize), "a");
|
||||
t!(format!("{:X}", 10_usize), "A");
|
||||
t!(format!("{}", "foo"), "foo");
|
||||
t!(format!("{}", "foo".to_string()), "foo");
|
||||
if cfg!(target_pointer_width = "32") {
|
||||
t!(format!("{:#p}", 0x1234 as *const isize), "0x00001234");
|
||||
t!(format!("{:#p}", 0x1234 as *mut isize), "0x00001234");
|
||||
} else {
|
||||
t!(format!("{:#p}", 0x1234 as *const isize), "0x0000000000001234");
|
||||
t!(format!("{:#p}", 0x1234 as *mut isize), "0x0000000000001234");
|
||||
}
|
||||
t!(format!("{:p}", 0x1234 as *const isize), "0x1234");
|
||||
t!(format!("{:p}", 0x1234 as *mut isize), "0x1234");
|
||||
t!(format!("{:x}", A), "aloha");
|
||||
t!(format!("{:X}", B), "adios");
|
||||
t!(format!("foo {} ☃☃☃☃☃☃", "bar"), "foo bar ☃☃☃☃☃☃");
|
||||
t!(format!("{1} {0}", 0, 1), "1 0");
|
||||
t!(format!("{foo} {bar}", foo=0, bar=1), "0 1");
|
||||
t!(format!("{foo} {1} {bar} {0}", 0, 1, foo=2, bar=3), "2 1 3 0");
|
||||
t!(format!("{} {0}", "a"), "a a");
|
||||
t!(format!("{_foo}", _foo = 6usize), "6");
|
||||
t!(format!("{foo_bar}", foo_bar=1), "1");
|
||||
t!(format!("{}", 5 + 5), "10");
|
||||
t!(format!("{:#4}", C), "☃123");
|
||||
t!(format!("{:b}", D), "aa☃bb");
|
||||
|
||||
let a: &dyn fmt::Debug = &1;
|
||||
t!(format!("{:?}", a), "1");
|
||||
|
||||
// Formatting strings and their arguments
|
||||
t!(format!("{}", "a"), "a");
|
||||
t!(format!("{:4}", "a"), "a ");
|
||||
t!(format!("{:4}", "☃"), "☃ ");
|
||||
t!(format!("{:>4}", "a"), " a");
|
||||
t!(format!("{:<4}", "a"), "a ");
|
||||
t!(format!("{:^5}", "a"), " a ");
|
||||
t!(format!("{:^5}", "aa"), " aa ");
|
||||
t!(format!("{:^4}", "a"), " a ");
|
||||
t!(format!("{:^4}", "aa"), " aa ");
|
||||
t!(format!("{:.4}", "a"), "a");
|
||||
t!(format!("{:4.4}", "a"), "a ");
|
||||
t!(format!("{:4.4}", "aaaaaaaaaaaaaaaaaa"), "aaaa");
|
||||
t!(format!("{:<4.4}", "aaaaaaaaaaaaaaaaaa"), "aaaa");
|
||||
t!(format!("{:>4.4}", "aaaaaaaaaaaaaaaaaa"), "aaaa");
|
||||
t!(format!("{:^4.4}", "aaaaaaaaaaaaaaaaaa"), "aaaa");
|
||||
t!(format!("{:>10.4}", "aaaaaaaaaaaaaaaaaa"), " aaaa");
|
||||
t!(format!("{:2.4}", "aaaaa"), "aaaa");
|
||||
t!(format!("{:2.4}", "aaaa"), "aaaa");
|
||||
t!(format!("{:2.4}", "aaa"), "aaa");
|
||||
t!(format!("{:2.4}", "aa"), "aa");
|
||||
t!(format!("{:2.4}", "a"), "a ");
|
||||
t!(format!("{:0>2}", "a"), "0a");
|
||||
t!(format!("{:.*}", 4, "aaaaaaaaaaaaaaaaaa"), "aaaa");
|
||||
t!(format!("{:.1$}", "aaaaaaaaaaaaaaaaaa", 4), "aaaa");
|
||||
t!(format!("{:.a$}", "aaaaaaaaaaaaaaaaaa", a=4), "aaaa");
|
||||
t!(format!("{:._a$}", "aaaaaaaaaaaaaaaaaa", _a=4), "aaaa");
|
||||
t!(format!("{:1$}", "a", 4), "a ");
|
||||
t!(format!("{1:0$}", 4, "a"), "a ");
|
||||
t!(format!("{:a$}", "a", a=4), "a ");
|
||||
t!(format!("{:-#}", "a"), "a");
|
||||
t!(format!("{:+#}", "a"), "a");
|
||||
t!(format!("{:/^10.8}", "1234567890"), "/12345678/");
|
||||
|
||||
// Some float stuff
|
||||
t!(format!("{:}", 1.0f32), "1");
|
||||
t!(format!("{:}", 1.0f64), "1");
|
||||
t!(format!("{:.3}", 1.0f64), "1.000");
|
||||
t!(format!("{:10.3}", 1.0f64), " 1.000");
|
||||
t!(format!("{:+10.3}", 1.0f64), " +1.000");
|
||||
t!(format!("{:+10.3}", -1.0f64), " -1.000");
|
||||
|
||||
t!(format!("{:e}", 1.2345e6f32), "1.2345e6");
|
||||
t!(format!("{:e}", 1.2345e6f64), "1.2345e6");
|
||||
t!(format!("{:E}", 1.2345e6f64), "1.2345E6");
|
||||
t!(format!("{:.3e}", 1.2345e6f64), "1.234e6");
|
||||
t!(format!("{:10.3e}", 1.2345e6f64), " 1.234e6");
|
||||
t!(format!("{:+10.3e}", 1.2345e6f64), " +1.234e6");
|
||||
t!(format!("{:+10.3e}", -1.2345e6f64), " -1.234e6");
|
||||
|
||||
// Float edge cases
|
||||
t!(format!("{}", -0.0), "0");
|
||||
t!(format!("{:?}", -0.0), "-0.0");
|
||||
t!(format!("{:?}", 0.0), "0.0");
|
||||
|
||||
// sign aware zero padding
|
||||
t!(format!("{:<3}", 1), "1 ");
|
||||
t!(format!("{:>3}", 1), " 1");
|
||||
t!(format!("{:^3}", 1), " 1 ");
|
||||
t!(format!("{:03}", 1), "001");
|
||||
t!(format!("{:<03}", 1), "001");
|
||||
t!(format!("{:>03}", 1), "001");
|
||||
t!(format!("{:^03}", 1), "001");
|
||||
t!(format!("{:+03}", 1), "+01");
|
||||
t!(format!("{:<+03}", 1), "+01");
|
||||
t!(format!("{:>+03}", 1), "+01");
|
||||
t!(format!("{:^+03}", 1), "+01");
|
||||
t!(format!("{:#05x}", 1), "0x001");
|
||||
t!(format!("{:<#05x}", 1), "0x001");
|
||||
t!(format!("{:>#05x}", 1), "0x001");
|
||||
t!(format!("{:^#05x}", 1), "0x001");
|
||||
t!(format!("{:05}", 1.2), "001.2");
|
||||
t!(format!("{:<05}", 1.2), "001.2");
|
||||
t!(format!("{:>05}", 1.2), "001.2");
|
||||
t!(format!("{:^05}", 1.2), "001.2");
|
||||
t!(format!("{:05}", -1.2), "-01.2");
|
||||
t!(format!("{:<05}", -1.2), "-01.2");
|
||||
t!(format!("{:>05}", -1.2), "-01.2");
|
||||
t!(format!("{:^05}", -1.2), "-01.2");
|
||||
t!(format!("{:+05}", 1.2), "+01.2");
|
||||
t!(format!("{:<+05}", 1.2), "+01.2");
|
||||
t!(format!("{:>+05}", 1.2), "+01.2");
|
||||
t!(format!("{:^+05}", 1.2), "+01.2");
|
||||
|
||||
// Ergonomic format_args!
|
||||
t!(format!("{0:x} {0:X}", 15), "f F");
|
||||
t!(format!("{0:x} {0:X} {}", 15), "f F 15");
|
||||
t!(format!("{:x}{0:X}{a:x}{:X}{1:x}{a:X}", 13, 14, a=15), "dDfEeF");
|
||||
t!(format!("{a:x} {a:X}", a=15), "f F");
|
||||
|
||||
// And its edge cases
|
||||
t!(format!("{a:.0$} {b:.0$} {0:.0$}\n{a:.c$} {b:.c$} {c:.c$}",
|
||||
4, a="abcdefg", b="hijklmn", c=3),
|
||||
"abcd hijk 4\nabc hij 3");
|
||||
t!(format!("{a:.*} {0} {:.*}", 4, 3, "efgh", a="abcdef"), "abcd 4 efg");
|
||||
t!(format!("{:.a$} {a} {a:#x}", "aaaaaa", a=2), "aa 2 0x2");
|
||||
|
||||
// Test that pointers don't get truncated.
|
||||
{
|
||||
let val = usize::MAX;
|
||||
let exp = format!("{:#x}", val);
|
||||
t!(format!("{:p}", val as *const isize), exp);
|
||||
}
|
||||
|
||||
// Escaping
|
||||
t!(format!("{{"), "{");
|
||||
t!(format!("}}"), "}");
|
||||
|
||||
test_write();
|
||||
test_print();
|
||||
test_order();
|
||||
test_once();
|
||||
|
||||
// make sure that format! doesn't move out of local variables
|
||||
let a: Box<_> = box 3;
|
||||
format!("{}", a);
|
||||
format!("{}", a);
|
||||
|
||||
// make sure that format! doesn't cause spurious unused-unsafe warnings when
|
||||
// it's inside of an outer unsafe block
|
||||
unsafe {
|
||||
let a: isize = ::std::mem::transmute(3_usize);
|
||||
format!("{}", a);
|
||||
}
|
||||
|
||||
test_format_args();
|
||||
|
||||
// test that trailing commas are acceptable
|
||||
format!("{}", "test",);
|
||||
format!("{foo}", foo="test",);
|
||||
|
||||
test_refcell();
|
||||
}
|
||||
|
||||
// Basic test to make sure that we can invoke the `write!` macro with an
|
||||
// fmt::Write instance.
|
||||
fn test_write() {
|
||||
let mut buf = String::new();
|
||||
write!(&mut buf, "{}", 3);
|
||||
{
|
||||
let w = &mut buf;
|
||||
write!(w, "{foo}", foo=4);
|
||||
write!(w, "{}", "hello");
|
||||
writeln!(w, "{}", "line");
|
||||
writeln!(w, "{foo}", foo="bar");
|
||||
w.write_char('☃');
|
||||
w.write_str("str");
|
||||
}
|
||||
|
||||
t!(buf, "34helloline\nbar\n☃str");
|
||||
}
|
||||
|
||||
// Just make sure that the macros are defined, there's not really a lot that we
|
||||
// can do with them just yet (to test the output)
|
||||
fn test_print() {
|
||||
print!("hi");
|
||||
print!("{:?}", vec![0u8]);
|
||||
println!("hello");
|
||||
println!("this is a {}", "test");
|
||||
println!("{foo}", foo="bar");
|
||||
}
|
||||
|
||||
// Just make sure that the macros are defined, there's not really a lot that we
|
||||
// can do with them just yet (to test the output)
|
||||
fn test_format_args() {
|
||||
let mut buf = String::new();
|
||||
{
|
||||
let w = &mut buf;
|
||||
write!(w, "{}", format_args!("{}", 1));
|
||||
write!(w, "{}", format_args!("test"));
|
||||
write!(w, "{}", format_args!("{test}", test=3));
|
||||
}
|
||||
let s = buf;
|
||||
t!(s, "1test3");
|
||||
|
||||
let s = fmt::format(format_args!("hello {}", "world"));
|
||||
t!(s, "hello world");
|
||||
let s = format!("{}: {}", "args were", format_args!("hello {}", "world"));
|
||||
t!(s, "args were: hello world");
|
||||
}
|
||||
|
||||
fn test_order() {
|
||||
// Make sure format!() arguments are always evaluated in a left-to-right
|
||||
// ordering
|
||||
fn foo() -> isize {
|
||||
static mut FOO: isize = 0;
|
||||
unsafe {
|
||||
FOO += 1;
|
||||
FOO
|
||||
}
|
||||
}
|
||||
assert_eq!(format!("{} {} {a} {b} {} {c}",
|
||||
foo(), foo(), foo(), a=foo(), b=foo(), c=foo()),
|
||||
"1 2 4 5 3 6".to_string());
|
||||
}
|
||||
|
||||
fn test_once() {
|
||||
// Make sure each argument are evaluated only once even though it may be
|
||||
// formatted multiple times
|
||||
fn foo() -> isize {
|
||||
static mut FOO: isize = 0;
|
||||
unsafe {
|
||||
FOO += 1;
|
||||
FOO
|
||||
}
|
||||
}
|
||||
assert_eq!(format!("{0} {0} {0} {a} {a} {a}", foo(), a=foo()),
|
||||
"1 1 1 2 2 2".to_string());
|
||||
}
|
||||
|
||||
fn test_refcell() {
|
||||
let refcell = RefCell::new(5);
|
||||
assert_eq!(format!("{:?}", refcell), "RefCell { value: 5 }");
|
||||
let borrow = refcell.borrow_mut();
|
||||
assert_eq!(format!("{:?}", refcell), "RefCell { value: <borrowed> }");
|
||||
drop(borrow);
|
||||
assert_eq!(format!("{:?}", refcell), "RefCell { value: 5 }");
|
||||
}
|
|
@ -1,103 +0,0 @@
|
|||
// run-pass
|
||||
// Test that .zip() specialization preserves side effects
|
||||
// in sideeffectful iterator adaptors.
|
||||
|
||||
use std::cell::Cell;
|
||||
|
||||
#[derive(Debug)]
|
||||
struct CountClone(Cell<i32>);
|
||||
|
||||
fn count_clone() -> CountClone { CountClone(Cell::new(0)) }
|
||||
|
||||
impl PartialEq<i32> for CountClone {
|
||||
fn eq(&self, rhs: &i32) -> bool {
|
||||
self.0.get() == *rhs
|
||||
}
|
||||
}
|
||||
|
||||
impl Clone for CountClone {
|
||||
fn clone(&self) -> Self {
|
||||
let ret = CountClone(self.0.clone());
|
||||
let n = self.0.get();
|
||||
self.0.set(n + 1);
|
||||
ret
|
||||
}
|
||||
}
|
||||
|
||||
fn test_zip_cloned_sideffectful() {
|
||||
let xs = [count_clone(), count_clone(), count_clone(), count_clone()];
|
||||
let ys = [count_clone(), count_clone()];
|
||||
|
||||
for _ in xs.iter().cloned().zip(ys.iter().cloned()) { }
|
||||
|
||||
assert_eq!(&xs, &[1, 1, 1, 0][..]);
|
||||
assert_eq!(&ys, &[1, 1][..]);
|
||||
|
||||
let xs = [count_clone(), count_clone()];
|
||||
let ys = [count_clone(), count_clone(), count_clone(), count_clone()];
|
||||
|
||||
for _ in xs.iter().cloned().zip(ys.iter().cloned()) { }
|
||||
|
||||
assert_eq!(&xs, &[1, 1][..]);
|
||||
assert_eq!(&ys, &[1, 1, 0, 0][..]);
|
||||
}
|
||||
|
||||
fn test_zip_map_sideffectful() {
|
||||
let mut xs = [0; 6];
|
||||
let mut ys = [0; 4];
|
||||
|
||||
for _ in xs.iter_mut().map(|x| *x += 1).zip(ys.iter_mut().map(|y| *y += 1)) { }
|
||||
|
||||
assert_eq!(&xs, &[1, 1, 1, 1, 1, 0]);
|
||||
assert_eq!(&ys, &[1, 1, 1, 1]);
|
||||
|
||||
let mut xs = [0; 4];
|
||||
let mut ys = [0; 6];
|
||||
|
||||
for _ in xs.iter_mut().map(|x| *x += 1).zip(ys.iter_mut().map(|y| *y += 1)) { }
|
||||
|
||||
assert_eq!(&xs, &[1, 1, 1, 1]);
|
||||
assert_eq!(&ys, &[1, 1, 1, 1, 0, 0]);
|
||||
}
|
||||
|
||||
fn test_zip_map_rev_sideffectful() {
|
||||
let mut xs = [0; 6];
|
||||
let mut ys = [0; 4];
|
||||
|
||||
{
|
||||
let mut it = xs.iter_mut().map(|x| *x += 1).zip(ys.iter_mut().map(|y| *y += 1));
|
||||
it.next_back();
|
||||
}
|
||||
assert_eq!(&xs, &[0, 0, 0, 1, 1, 1]);
|
||||
assert_eq!(&ys, &[0, 0, 0, 1]);
|
||||
|
||||
let mut xs = [0; 6];
|
||||
let mut ys = [0; 4];
|
||||
|
||||
{
|
||||
let mut it = xs.iter_mut().map(|x| *x += 1).zip(ys.iter_mut().map(|y| *y += 1));
|
||||
(&mut it).take(5).count();
|
||||
it.next_back();
|
||||
}
|
||||
assert_eq!(&xs, &[1, 1, 1, 1, 1, 1]);
|
||||
assert_eq!(&ys, &[1, 1, 1, 1]);
|
||||
}
|
||||
|
||||
fn test_zip_nested_sideffectful() {
|
||||
let mut xs = [0; 6];
|
||||
let ys = [0; 4];
|
||||
|
||||
{
|
||||
// test that it has the side effect nested inside enumerate
|
||||
let it = xs.iter_mut().map(|x| *x = 1).enumerate().zip(&ys);
|
||||
it.count();
|
||||
}
|
||||
assert_eq!(&xs, &[1, 1, 1, 1, 1, 0]);
|
||||
}
|
||||
|
||||
fn main() {
|
||||
test_zip_cloned_sideffectful();
|
||||
test_zip_map_sideffectful();
|
||||
test_zip_map_rev_sideffectful();
|
||||
test_zip_nested_sideffectful();
|
||||
}
|
|
@ -1,32 +0,0 @@
|
|||
// run-pass
|
||||
|
||||
#![allow(non_camel_case_types)]
|
||||
use std::cell::Cell;
|
||||
|
||||
struct dtor<'a> {
|
||||
x: &'a Cell<isize>,
|
||||
}
|
||||
|
||||
impl<'a> Drop for dtor<'a> {
|
||||
fn drop(&mut self) {
|
||||
self.x.set(self.x.get() - 1);
|
||||
}
|
||||
}
|
||||
|
||||
fn unwrap<T>(o: Option<T>) -> T {
|
||||
match o {
|
||||
Some(v) => v,
|
||||
None => panic!()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn main() {
|
||||
let x = &Cell::new(1);
|
||||
|
||||
{
|
||||
let b = Some(dtor { x:x });
|
||||
let _c = unwrap(b);
|
||||
}
|
||||
|
||||
assert_eq!(x.get(), 0);
|
||||
}
|
Loading…
Add table
Reference in a new issue