]> git.lizzy.rs Git - rust.git/blob - src/liballoc/boxed_test.rs
Rollup merge of #31295 - steveklabnik:gh31266, r=alexcrichton
[rust.git] / src / liballoc / boxed_test.rs
1 // Copyright 2012-2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 //! Test for `boxed` mod.
12
13 use core::any::Any;
14 use core::ops::Deref;
15 use core::result::Result::{Ok, Err};
16 use core::clone::Clone;
17
18 use std::boxed::Box;
19
20 #[test]
21 fn test_owned_clone() {
22     let a = Box::new(5);
23     let b: Box<i32> = a.clone();
24     assert!(a == b);
25 }
26
27 #[derive(PartialEq, Eq)]
28 struct Test;
29
30 #[test]
31 fn any_move() {
32     let a = Box::new(8) as Box<Any>;
33     let b = Box::new(Test) as Box<Any>;
34
35     match a.downcast::<i32>() {
36         Ok(a) => {
37             assert!(a == Box::new(8));
38         }
39         Err(..) => panic!(),
40     }
41     match b.downcast::<Test>() {
42         Ok(a) => {
43             assert!(a == Box::new(Test));
44         }
45         Err(..) => panic!(),
46     }
47
48     let a = Box::new(8) as Box<Any>;
49     let b = Box::new(Test) as Box<Any>;
50
51     assert!(a.downcast::<Box<Test>>().is_err());
52     assert!(b.downcast::<Box<i32>>().is_err());
53 }
54
55 #[test]
56 fn test_show() {
57     let a = Box::new(8) as Box<Any>;
58     let b = Box::new(Test) as Box<Any>;
59     let a_str = format!("{:?}", a);
60     let b_str = format!("{:?}", b);
61     assert_eq!(a_str, "Any");
62     assert_eq!(b_str, "Any");
63
64     static EIGHT: usize = 8;
65     static TEST: Test = Test;
66     let a = &EIGHT as &Any;
67     let b = &TEST as &Any;
68     let s = format!("{:?}", a);
69     assert_eq!(s, "Any");
70     let s = format!("{:?}", b);
71     assert_eq!(s, "Any");
72 }
73
74 #[test]
75 fn deref() {
76     fn homura<T: Deref<Target = i32>>(_: T) {}
77     homura(Box::new(765));
78 }
79
80 #[test]
81 fn raw_sized() {
82     let x = Box::new(17);
83     let p = Box::into_raw(x);
84     unsafe {
85         assert_eq!(17, *p);
86         *p = 19;
87         let y = Box::from_raw(p);
88         assert_eq!(19, *y);
89     }
90 }
91
92 #[test]
93 fn raw_trait() {
94     trait Foo {
95         fn get(&self) -> u32;
96         fn set(&mut self, value: u32);
97     }
98
99     struct Bar(u32);
100
101     impl Foo for Bar {
102         fn get(&self) -> u32 {
103             self.0
104         }
105
106         fn set(&mut self, value: u32) {
107             self.0 = value;
108         }
109     }
110
111     let x: Box<Foo> = Box::new(Bar(17));
112     let p = Box::into_raw(x);
113     unsafe {
114         assert_eq!(17, (*p).get());
115         (*p).set(19);
116         let y: Box<Foo> = Box::from_raw(p);
117         assert_eq!(19, y.get());
118     }
119 }