2015-01-20 14:57:56 -06:00
|
|
|
//! Test for `boxed` mod.
|
|
|
|
|
|
|
|
use core::any::Any;
|
|
|
|
use core::clone::Clone;
|
2019-11-29 21:30:49 -06:00
|
|
|
use core::convert::TryInto;
|
|
|
|
use core::ops::Deref;
|
|
|
|
use core::result::Result::{Err, Ok};
|
2015-01-20 14:57:56 -06:00
|
|
|
|
|
|
|
use std::boxed::Box;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_owned_clone() {
|
2015-01-25 15:05:03 -06:00
|
|
|
let a = Box::new(5);
|
2015-02-09 01:00:46 -06:00
|
|
|
let b: Box<i32> = a.clone();
|
2015-01-20 14:57:56 -06:00
|
|
|
assert!(a == b);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(PartialEq, Eq)]
|
|
|
|
struct Test;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn any_move() {
|
2018-07-10 15:32:19 -05:00
|
|
|
let a = Box::new(8) as Box<dyn Any>;
|
|
|
|
let b = Box::new(Test) as Box<dyn Any>;
|
2015-01-20 14:57:56 -06:00
|
|
|
|
2015-02-09 01:00:46 -06:00
|
|
|
match a.downcast::<i32>() {
|
2015-09-23 17:00:54 -05:00
|
|
|
Ok(a) => {
|
|
|
|
assert!(a == Box::new(8));
|
|
|
|
}
|
|
|
|
Err(..) => panic!(),
|
2015-01-20 14:57:56 -06:00
|
|
|
}
|
|
|
|
match b.downcast::<Test>() {
|
2015-09-23 17:00:54 -05:00
|
|
|
Ok(a) => {
|
|
|
|
assert!(a == Box::new(Test));
|
|
|
|
}
|
|
|
|
Err(..) => panic!(),
|
2015-01-20 14:57:56 -06:00
|
|
|
}
|
|
|
|
|
2018-07-10 15:32:19 -05:00
|
|
|
let a = Box::new(8) as Box<dyn Any>;
|
|
|
|
let b = Box::new(Test) as Box<dyn Any>;
|
2015-01-20 14:57:56 -06:00
|
|
|
|
|
|
|
assert!(a.downcast::<Box<Test>>().is_err());
|
2015-02-09 01:00:46 -06:00
|
|
|
assert!(b.downcast::<Box<i32>>().is_err());
|
2015-01-20 14:57:56 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_show() {
|
2018-07-10 15:32:19 -05:00
|
|
|
let a = Box::new(8) as Box<dyn Any>;
|
|
|
|
let b = Box::new(Test) as Box<dyn Any>;
|
2022-02-12 13:16:17 -06:00
|
|
|
let a_str = format!("{a:?}");
|
|
|
|
let b_str = format!("{b:?}");
|
2021-04-21 08:45:41 -05:00
|
|
|
assert_eq!(a_str, "Any { .. }");
|
|
|
|
assert_eq!(b_str, "Any { .. }");
|
2015-01-20 14:57:56 -06:00
|
|
|
|
2015-01-25 04:58:43 -06:00
|
|
|
static EIGHT: usize = 8;
|
2015-01-20 14:57:56 -06:00
|
|
|
static TEST: Test = Test;
|
2018-07-10 15:32:19 -05:00
|
|
|
let a = &EIGHT as &dyn Any;
|
|
|
|
let b = &TEST as &dyn Any;
|
2022-02-12 13:16:17 -06:00
|
|
|
let s = format!("{a:?}");
|
2021-04-21 08:45:41 -05:00
|
|
|
assert_eq!(s, "Any { .. }");
|
2022-02-12 13:16:17 -06:00
|
|
|
let s = format!("{b:?}");
|
2021-04-21 08:45:41 -05:00
|
|
|
assert_eq!(s, "Any { .. }");
|
2015-01-20 14:57:56 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn deref() {
|
2015-11-22 20:32:40 -06:00
|
|
|
fn homura<T: Deref<Target = i32>>(_: T) {}
|
2015-03-03 02:42:26 -06:00
|
|
|
homura(Box::new(765));
|
2015-01-20 14:57:56 -06:00
|
|
|
}
|
2015-02-01 11:15:44 -06:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn raw_sized() {
|
2015-06-26 15:29:40 -05:00
|
|
|
let x = Box::new(17);
|
|
|
|
let p = Box::into_raw(x);
|
2015-02-01 11:15:44 -06:00
|
|
|
unsafe {
|
|
|
|
assert_eq!(17, *p);
|
|
|
|
*p = 19;
|
|
|
|
let y = Box::from_raw(p);
|
|
|
|
assert_eq!(19, *y);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn raw_trait() {
|
|
|
|
trait Foo {
|
|
|
|
fn get(&self) -> u32;
|
|
|
|
fn set(&mut self, value: u32);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Bar(u32);
|
|
|
|
|
|
|
|
impl Foo for Bar {
|
|
|
|
fn get(&self) -> u32 {
|
|
|
|
self.0
|
|
|
|
}
|
|
|
|
|
|
|
|
fn set(&mut self, value: u32) {
|
|
|
|
self.0 = value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-10 15:32:19 -05:00
|
|
|
let x: Box<dyn Foo> = Box::new(Bar(17));
|
2015-06-26 15:29:40 -05:00
|
|
|
let p = Box::into_raw(x);
|
2015-02-01 11:15:44 -06:00
|
|
|
unsafe {
|
|
|
|
assert_eq!(17, (*p).get());
|
|
|
|
(*p).set(19);
|
2018-07-10 15:32:19 -05:00
|
|
|
let y: Box<dyn Foo> = Box::from_raw(p);
|
2015-02-01 11:15:44 -06:00
|
|
|
assert_eq!(19, y.get());
|
|
|
|
}
|
|
|
|
}
|
2017-01-31 21:46:16 -06:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn f64_slice() {
|
|
|
|
let slice: &[f64] = &[-1.0, 0.0, 1.0, f64::INFINITY];
|
|
|
|
let boxed: Box<[f64]> = Box::from(slice);
|
|
|
|
assert_eq!(&*boxed, slice)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn i64_slice() {
|
|
|
|
let slice: &[i64] = &[i64::MIN, -2, -1, 0, 1, 2, i64::MAX];
|
|
|
|
let boxed: Box<[i64]> = Box::from(slice);
|
|
|
|
assert_eq!(&*boxed, slice)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn str_slice() {
|
|
|
|
let s = "Hello, world!";
|
|
|
|
let boxed: Box<str> = Box::from(s);
|
|
|
|
assert_eq!(&*boxed, s)
|
|
|
|
}
|
2018-11-10 04:43:39 -06:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn boxed_slice_from_iter() {
|
|
|
|
let iter = 0..100;
|
|
|
|
let boxed: Box<[u32]> = iter.collect();
|
|
|
|
assert_eq!(boxed.len(), 100);
|
|
|
|
assert_eq!(boxed[7], 7);
|
|
|
|
}
|
2019-06-04 07:15:47 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_array_from_slice() {
|
|
|
|
let v = vec![1, 2, 3];
|
|
|
|
let r: Box<[u32]> = v.into_boxed_slice();
|
|
|
|
|
|
|
|
let a: Result<Box<[u32; 3]>, _> = r.clone().try_into();
|
|
|
|
assert!(a.is_ok());
|
|
|
|
|
|
|
|
let a: Result<Box<[u32; 2]>, _> = r.clone().try_into();
|
|
|
|
assert!(a.is_err());
|
|
|
|
}
|