]> git.lizzy.rs Git - rust.git/blob - src/liballoc/boxed_test.rs
Auto merge of #45064 - alexcrichton:reduce-codegen-units, r=michaelwoerister
[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::{Err, Ok};
16 use core::clone::Clone;
17 use core::f64;
18 use core::i64;
19
20 use std::boxed::Box;
21
22 #[test]
23 fn test_owned_clone() {
24     let a = Box::new(5);
25     let b: Box<i32> = a.clone();
26     assert!(a == b);
27 }
28
29 #[derive(PartialEq, Eq)]
30 struct Test;
31
32 #[test]
33 fn any_move() {
34     let a = Box::new(8) as Box<Any>;
35     let b = Box::new(Test) as Box<Any>;
36
37     match a.downcast::<i32>() {
38         Ok(a) => {
39             assert!(a == Box::new(8));
40         }
41         Err(..) => panic!(),
42     }
43     match b.downcast::<Test>() {
44         Ok(a) => {
45             assert!(a == Box::new(Test));
46         }
47         Err(..) => panic!(),
48     }
49
50     let a = Box::new(8) as Box<Any>;
51     let b = Box::new(Test) as Box<Any>;
52
53     assert!(a.downcast::<Box<Test>>().is_err());
54     assert!(b.downcast::<Box<i32>>().is_err());
55 }
56
57 #[test]
58 fn test_show() {
59     let a = Box::new(8) as Box<Any>;
60     let b = Box::new(Test) as Box<Any>;
61     let a_str = format!("{:?}", a);
62     let b_str = format!("{:?}", b);
63     assert_eq!(a_str, "Any");
64     assert_eq!(b_str, "Any");
65
66     static EIGHT: usize = 8;
67     static TEST: Test = Test;
68     let a = &EIGHT as &Any;
69     let b = &TEST as &Any;
70     let s = format!("{:?}", a);
71     assert_eq!(s, "Any");
72     let s = format!("{:?}", b);
73     assert_eq!(s, "Any");
74 }
75
76 #[test]
77 fn deref() {
78     fn homura<T: Deref<Target = i32>>(_: T) {}
79     homura(Box::new(765));
80 }
81
82 #[test]
83 fn raw_sized() {
84     let x = Box::new(17);
85     let p = Box::into_raw(x);
86     unsafe {
87         assert_eq!(17, *p);
88         *p = 19;
89         let y = Box::from_raw(p);
90         assert_eq!(19, *y);
91     }
92 }
93
94 #[test]
95 fn raw_trait() {
96     trait Foo {
97         fn get(&self) -> u32;
98         fn set(&mut self, value: u32);
99     }
100
101     struct Bar(u32);
102
103     impl Foo for Bar {
104         fn get(&self) -> u32 {
105             self.0
106         }
107
108         fn set(&mut self, value: u32) {
109             self.0 = value;
110         }
111     }
112
113     let x: Box<Foo> = Box::new(Bar(17));
114     let p = Box::into_raw(x);
115     unsafe {
116         assert_eq!(17, (*p).get());
117         (*p).set(19);
118         let y: Box<Foo> = Box::from_raw(p);
119         assert_eq!(19, y.get());
120     }
121 }
122
123 #[test]
124 fn f64_slice() {
125     let slice: &[f64] = &[-1.0, 0.0, 1.0, f64::INFINITY];
126     let boxed: Box<[f64]> = Box::from(slice);
127     assert_eq!(&*boxed, slice)
128 }
129
130 #[test]
131 fn i64_slice() {
132     let slice: &[i64] = &[i64::MIN, -2, -1, 0, 1, 2, i64::MAX];
133     let boxed: Box<[i64]> = Box::from(slice);
134     assert_eq!(&*boxed, slice)
135 }
136
137 #[test]
138 fn str_slice() {
139     let s = "Hello, world!";
140     let boxed: Box<str> = Box::from(s);
141     assert_eq!(&*boxed, s)
142 }