]> git.lizzy.rs Git - rust.git/blob - src/libcoretest/mem.rs
Auto merge of #22517 - brson:relnotes, r=Gankro
[rust.git] / src / libcoretest / mem.rs
1 // Copyright 2014-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 use core::mem::*;
11 use test::Bencher;
12
13 #[test]
14 fn size_of_basic() {
15     assert_eq!(size_of::<u8>(), 1);
16     assert_eq!(size_of::<u16>(), 2);
17     assert_eq!(size_of::<u32>(), 4);
18     assert_eq!(size_of::<u64>(), 8);
19 }
20
21 #[test]
22 #[cfg(target_pointer_width = "32")]
23 fn size_of_32() {
24     assert_eq!(size_of::<uint>(), 4);
25     assert_eq!(size_of::<*const uint>(), 4);
26 }
27
28 #[test]
29 #[cfg(target_pointer_width = "64")]
30 fn size_of_64() {
31     assert_eq!(size_of::<uint>(), 8);
32     assert_eq!(size_of::<*const uint>(), 8);
33 }
34
35 #[test]
36 fn size_of_val_basic() {
37     assert_eq!(size_of_val(&1u8), 1);
38     assert_eq!(size_of_val(&1u16), 2);
39     assert_eq!(size_of_val(&1u32), 4);
40     assert_eq!(size_of_val(&1u64), 8);
41 }
42
43 #[test]
44 fn align_of_basic() {
45     assert_eq!(align_of::<u8>(), 1);
46     assert_eq!(align_of::<u16>(), 2);
47     assert_eq!(align_of::<u32>(), 4);
48 }
49
50 #[test]
51 #[cfg(target_pointer_width = "32")]
52 fn align_of_32() {
53     assert_eq!(align_of::<uint>(), 4);
54     assert_eq!(align_of::<*const uint>(), 4);
55 }
56
57 #[test]
58 #[cfg(target_pointer_width = "64")]
59 fn align_of_64() {
60     assert_eq!(align_of::<uint>(), 8);
61     assert_eq!(align_of::<*const uint>(), 8);
62 }
63
64 #[test]
65 fn align_of_val_basic() {
66     assert_eq!(align_of_val(&1u8), 1);
67     assert_eq!(align_of_val(&1u16), 2);
68     assert_eq!(align_of_val(&1u32), 4);
69 }
70
71 #[test]
72 fn test_swap() {
73     let mut x = 31337;
74     let mut y = 42;
75     swap(&mut x, &mut y);
76     assert_eq!(x, 42);
77     assert_eq!(y, 31337);
78 }
79
80 #[test]
81 fn test_replace() {
82     let mut x = Some("test".to_string());
83     let y = replace(&mut x, None);
84     assert!(x.is_none());
85     assert!(y.is_some());
86 }
87
88 #[test]
89 fn test_transmute_copy() {
90     assert_eq!(1, unsafe { transmute_copy(&1) });
91 }
92
93 #[test]
94 fn test_transmute() {
95     trait Foo {}
96     impl Foo for int {}
97
98     let a = box 100 as Box<Foo>;
99     unsafe {
100         let x: ::core::raw::TraitObject = transmute(a);
101         assert!(*(x.data as *const int) == 100);
102         let _x: Box<Foo> = transmute(x);
103     }
104
105     unsafe {
106         assert!(vec![76u8] == transmute::<_, Vec<u8>>("L".to_string()));
107     }
108 }
109
110 // FIXME #13642 (these benchmarks should be in another place)
111 /// Completely miscellaneous language-construct benchmarks.
112 // Static/dynamic method dispatch
113
114 struct Struct {
115     field: int
116 }
117
118 trait Trait {
119     fn method(&self) -> int;
120 }
121
122 impl Trait for Struct {
123     fn method(&self) -> int {
124         self.field
125     }
126 }
127
128 #[bench]
129 fn trait_vtable_method_call(b: &mut Bencher) {
130     let s = Struct { field: 10 };
131     let t = &s as &Trait;
132     b.iter(|| {
133         t.method()
134     });
135 }
136
137 #[bench]
138 fn trait_static_method_call(b: &mut Bencher) {
139     let s = Struct { field: 10 };
140     b.iter(|| {
141         s.method()
142     });
143 }
144
145 // Overhead of various match forms
146
147 #[bench]
148 fn match_option_some(b: &mut Bencher) {
149     let x = Some(10);
150     b.iter(|| {
151         match x {
152             Some(y) => y,
153             None => 11
154         }
155     });
156 }
157
158 #[bench]
159 fn match_vec_pattern(b: &mut Bencher) {
160     let x = [1,2,3,4,5,6];
161     b.iter(|| {
162         match x {
163             [1,2,3,..] => 10,
164             _ => 11,
165         }
166     });
167 }