]> git.proxmox.com Git - rustc.git/blame - src/liballoc/tests.rs
New upstream version 1.41.1+dfsg1
[rustc.git] / src / liballoc / tests.rs
CommitLineData
85aaf69f
SL
1//! Test for `boxed` mod.
2
3use core::any::Any;
85aaf69f 4use core::clone::Clone;
60c5eb7d 5use core::convert::TryInto;
8bb4bdeb
XL
6use core::f64;
7use core::i64;
60c5eb7d
XL
8use core::ops::Deref;
9use core::result::Result::{Err, Ok};
85aaf69f 10
85aaf69f 11use std::boxed::Box;
85aaf69f
SL
12
13#[test]
14fn test_owned_clone() {
15 let a = Box::new(5);
16 let b: Box<i32> = a.clone();
17 assert!(a == b);
18}
19
20#[derive(PartialEq, Eq)]
21struct Test;
22
23#[test]
24fn any_move() {
8faf50e0
XL
25 let a = Box::new(8) as Box<dyn Any>;
26 let b = Box::new(Test) as Box<dyn Any>;
85aaf69f
SL
27
28 match a.downcast::<i32>() {
b039eaaf
SL
29 Ok(a) => {
30 assert!(a == Box::new(8));
31 }
32 Err(..) => panic!(),
85aaf69f
SL
33 }
34 match b.downcast::<Test>() {
b039eaaf
SL
35 Ok(a) => {
36 assert!(a == Box::new(Test));
37 }
38 Err(..) => panic!(),
85aaf69f
SL
39 }
40
8faf50e0
XL
41 let a = Box::new(8) as Box<dyn Any>;
42 let b = Box::new(Test) as Box<dyn Any>;
85aaf69f
SL
43
44 assert!(a.downcast::<Box<Test>>().is_err());
45 assert!(b.downcast::<Box<i32>>().is_err());
46}
47
48#[test]
49fn test_show() {
8faf50e0
XL
50 let a = Box::new(8) as Box<dyn Any>;
51 let b = Box::new(Test) as Box<dyn Any>;
85aaf69f
SL
52 let a_str = format!("{:?}", a);
53 let b_str = format!("{:?}", b);
c34b1796
AL
54 assert_eq!(a_str, "Any");
55 assert_eq!(b_str, "Any");
85aaf69f
SL
56
57 static EIGHT: usize = 8;
58 static TEST: Test = Test;
8faf50e0
XL
59 let a = &EIGHT as &dyn Any;
60 let b = &TEST as &dyn Any;
85aaf69f 61 let s = format!("{:?}", a);
c34b1796 62 assert_eq!(s, "Any");
85aaf69f 63 let s = format!("{:?}", b);
c34b1796 64 assert_eq!(s, "Any");
85aaf69f
SL
65}
66
67#[test]
68fn deref() {
92a42be0 69 fn homura<T: Deref<Target = i32>>(_: T) {}
c34b1796 70 homura(Box::new(765));
85aaf69f
SL
71}
72
73#[test]
74fn raw_sized() {
c1a9b12d
SL
75 let x = Box::new(17);
76 let p = Box::into_raw(x);
85aaf69f 77 unsafe {
85aaf69f
SL
78 assert_eq!(17, *p);
79 *p = 19;
80 let y = Box::from_raw(p);
81 assert_eq!(19, *y);
82 }
83}
84
85#[test]
86fn raw_trait() {
87 trait Foo {
88 fn get(&self) -> u32;
89 fn set(&mut self, value: u32);
90 }
91
92 struct Bar(u32);
93
94 impl Foo for Bar {
95 fn get(&self) -> u32 {
96 self.0
97 }
98
99 fn set(&mut self, value: u32) {
100 self.0 = value;
101 }
102 }
103
8faf50e0 104 let x: Box<dyn Foo> = Box::new(Bar(17));
c1a9b12d 105 let p = Box::into_raw(x);
85aaf69f 106 unsafe {
85aaf69f
SL
107 assert_eq!(17, (*p).get());
108 (*p).set(19);
8faf50e0 109 let y: Box<dyn Foo> = Box::from_raw(p);
85aaf69f
SL
110 assert_eq!(19, y.get());
111 }
112}
8bb4bdeb
XL
113
114#[test]
115fn f64_slice() {
116 let slice: &[f64] = &[-1.0, 0.0, 1.0, f64::INFINITY];
117 let boxed: Box<[f64]> = Box::from(slice);
118 assert_eq!(&*boxed, slice)
119}
120
121#[test]
122fn i64_slice() {
123 let slice: &[i64] = &[i64::MIN, -2, -1, 0, 1, 2, i64::MAX];
124 let boxed: Box<[i64]> = Box::from(slice);
125 assert_eq!(&*boxed, slice)
126}
127
128#[test]
129fn str_slice() {
130 let s = "Hello, world!";
131 let boxed: Box<str> = Box::from(s);
132 assert_eq!(&*boxed, s)
133}
a1dfa0c6
XL
134
135#[test]
136fn boxed_slice_from_iter() {
137 let iter = 0..100;
138 let boxed: Box<[u32]> = iter.collect();
139 assert_eq!(boxed.len(), 100);
140 assert_eq!(boxed[7], 7);
141}
416331ca
XL
142
143#[test]
144fn test_array_from_slice() {
145 let v = vec![1, 2, 3];
146 let r: Box<[u32]> = v.into_boxed_slice();
147
148 let a: Result<Box<[u32; 3]>, _> = r.clone().try_into();
149 assert!(a.is_ok());
150
151 let a: Result<Box<[u32; 2]>, _> = r.clone().try_into();
152 assert!(a.is_err());
153}