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