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