]> git.proxmox.com Git - rustc.git/blob - src/liballoc/boxed_test.rs
Imported Upstream version 1.6.0+dfsg1
[rustc.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;
19 use std::boxed::Box;
20
21 #[test]
22 fn test_owned_clone() {
23 let a = Box::new(5);
24 let b: Box<i32> = a.clone();
25 assert!(a == b);
26 }
27
28 #[derive(PartialEq, Eq)]
29 struct Test;
30
31 #[test]
32 fn any_move() {
33 let a = Box::new(8) as Box<Any>;
34 let b = Box::new(Test) as Box<Any>;
35
36 match a.downcast::<i32>() {
37 Ok(a) => {
38 assert!(a == Box::new(8));
39 }
40 Err(..) => panic!(),
41 }
42 match b.downcast::<Test>() {
43 Ok(a) => {
44 assert!(a == Box::new(Test));
45 }
46 Err(..) => panic!(),
47 }
48
49 let a = Box::new(8) as Box<Any>;
50 let b = Box::new(Test) as Box<Any>;
51
52 assert!(a.downcast::<Box<Test>>().is_err());
53 assert!(b.downcast::<Box<i32>>().is_err());
54 }
55
56 #[test]
57 fn test_show() {
58 let a = Box::new(8) as Box<Any>;
59 let b = Box::new(Test) as Box<Any>;
60 let a_str = format!("{:?}", a);
61 let b_str = format!("{:?}", b);
62 assert_eq!(a_str, "Any");
63 assert_eq!(b_str, "Any");
64
65 static EIGHT: usize = 8;
66 static TEST: Test = Test;
67 let a = &EIGHT as &Any;
68 let b = &TEST as &Any;
69 let s = format!("{:?}", a);
70 assert_eq!(s, "Any");
71 let s = format!("{:?}", b);
72 assert_eq!(s, "Any");
73 }
74
75 #[test]
76 fn deref() {
77 fn homura<T: Deref<Target = i32>>(_: T) {}
78 homura(Box::new(765));
79 }
80
81 #[test]
82 fn raw_sized() {
83 let x = Box::new(17);
84 let p = Box::into_raw(x);
85 unsafe {
86 assert_eq!(17, *p);
87 *p = 19;
88 let y = Box::from_raw(p);
89 assert_eq!(19, *y);
90 }
91 }
92
93 #[test]
94 fn raw_trait() {
95 trait Foo {
96 fn get(&self) -> u32;
97 fn set(&mut self, value: u32);
98 }
99
100 struct Bar(u32);
101
102 impl Foo for Bar {
103 fn get(&self) -> u32 {
104 self.0
105 }
106
107 fn set(&mut self, value: u32) {
108 self.0 = value;
109 }
110 }
111
112 let x: Box<Foo> = Box::new(Bar(17));
113 let p = Box::into_raw(x);
114 unsafe {
115 assert_eq!(17, (*p).get());
116 (*p).set(19);
117 let y: Box<Foo> = Box::from_raw(p);
118 assert_eq!(19, y.get());
119 }
120 }