]> git.lizzy.rs Git - rust.git/blob - src/libcoretest/any.rs
Merge pull request #20510 from tshepang/patch-6
[rust.git] / src / libcoretest / any.rs
1 // Copyright 2014 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 use core::any::*;
11 use test::Bencher;
12 use test;
13
14 #[derive(PartialEq, Show)]
15 struct Test;
16
17 static TEST: &'static str = "Test";
18
19 #[test]
20 fn any_referenced() {
21     let (a, b, c) = (&5u as &Any, &TEST as &Any, &Test as &Any);
22
23     assert!(a.is::<uint>());
24     assert!(!b.is::<uint>());
25     assert!(!c.is::<uint>());
26
27     assert!(!a.is::<&'static str>());
28     assert!(b.is::<&'static str>());
29     assert!(!c.is::<&'static str>());
30
31     assert!(!a.is::<Test>());
32     assert!(!b.is::<Test>());
33     assert!(c.is::<Test>());
34 }
35
36 #[test]
37 fn any_owning() {
38     let (a, b, c) = (box 5u as Box<Any>, box TEST as Box<Any>, box Test as Box<Any>);
39
40     assert!(a.is::<uint>());
41     assert!(!b.is::<uint>());
42     assert!(!c.is::<uint>());
43
44     assert!(!a.is::<&'static str>());
45     assert!(b.is::<&'static str>());
46     assert!(!c.is::<&'static str>());
47
48     assert!(!a.is::<Test>());
49     assert!(!b.is::<Test>());
50     assert!(c.is::<Test>());
51 }
52
53 #[test]
54 fn any_downcast_ref() {
55     let a = &5u as &Any;
56
57     match a.downcast_ref::<uint>() {
58         Some(&5) => {}
59         x => panic!("Unexpected value {}", x)
60     }
61
62     match a.downcast_ref::<Test>() {
63         None => {}
64         x => panic!("Unexpected value {}", x)
65     }
66 }
67
68 #[test]
69 fn any_downcast_mut() {
70     let mut a = 5u;
71     let mut b = box 7u;
72
73     let a_r = &mut a as &mut Any;
74     let tmp: &mut uint = &mut *b;
75     let b_r = tmp as &mut Any;
76
77     match a_r.downcast_mut::<uint>() {
78         Some(x) => {
79             assert_eq!(*x, 5u);
80             *x = 612;
81         }
82         x => panic!("Unexpected value {}", x)
83     }
84
85     match b_r.downcast_mut::<uint>() {
86         Some(x) => {
87             assert_eq!(*x, 7u);
88             *x = 413;
89         }
90         x => panic!("Unexpected value {}", x)
91     }
92
93     match a_r.downcast_mut::<Test>() {
94         None => (),
95         x => panic!("Unexpected value {}", x)
96     }
97
98     match b_r.downcast_mut::<Test>() {
99         None => (),
100         x => panic!("Unexpected value {}", x)
101     }
102
103     match a_r.downcast_mut::<uint>() {
104         Some(&612) => {}
105         x => panic!("Unexpected value {}", x)
106     }
107
108     match b_r.downcast_mut::<uint>() {
109         Some(&413) => {}
110         x => panic!("Unexpected value {}", x)
111     }
112 }
113
114 #[test]
115 fn any_fixed_vec() {
116     let test = [0u; 8];
117     let test = &test as &Any;
118     assert!(test.is::<[uint; 8]>());
119     assert!(!test.is::<[uint; 10]>());
120 }
121
122
123 #[bench]
124 fn bench_downcast_ref(b: &mut Bencher) {
125     b.iter(|| {
126         let mut x = 0i;
127         let mut y = &mut x as &mut Any;
128         test::black_box(&mut y);
129         test::black_box(y.downcast_ref::<int>() == Some(&0));
130     });
131 }