rust/src/libcoretest/mem.rs

174 lines
3.6 KiB
Rust
Raw Normal View History

// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
use core::mem::*;
use test::Bencher;
#[test]
fn size_of_basic() {
assert_eq!(size_of::<u8>(), 1u);
assert_eq!(size_of::<u16>(), 2u);
assert_eq!(size_of::<u32>(), 4u);
assert_eq!(size_of::<u64>(), 8u);
}
#[test]
2014-10-11 11:32:40 -05:00
#[cfg(any(target_arch = "x86",
target_arch = "arm",
target_arch = "mips",
target_arch = "mipsel"))]
fn size_of_32() {
assert_eq!(size_of::<uint>(), 4u);
assert_eq!(size_of::<*const uint>(), 4u);
}
#[test]
#[cfg(target_arch = "x86_64")]
fn size_of_64() {
assert_eq!(size_of::<uint>(), 8u);
assert_eq!(size_of::<*const uint>(), 8u);
}
#[test]
fn size_of_val_basic() {
assert_eq!(size_of_val(&1u8), 1);
assert_eq!(size_of_val(&1u16), 2);
assert_eq!(size_of_val(&1u32), 4);
assert_eq!(size_of_val(&1u64), 8);
}
#[test]
fn align_of_basic() {
assert_eq!(align_of::<u8>(), 1u);
assert_eq!(align_of::<u16>(), 2u);
assert_eq!(align_of::<u32>(), 4u);
}
#[test]
2014-10-11 11:32:40 -05:00
#[cfg(any(target_arch = "x86",
target_arch = "arm",
target_arch = "mips",
target_arch = "mipsel"))]
fn align_of_32() {
assert_eq!(align_of::<uint>(), 4u);
assert_eq!(align_of::<*const uint>(), 4u);
}
#[test]
#[cfg(target_arch = "x86_64")]
fn align_of_64() {
assert_eq!(align_of::<uint>(), 8u);
assert_eq!(align_of::<*const uint>(), 8u);
}
#[test]
fn align_of_val_basic() {
assert_eq!(align_of_val(&1u8), 1u);
assert_eq!(align_of_val(&1u16), 2u);
assert_eq!(align_of_val(&1u32), 4u);
}
#[test]
fn test_swap() {
let mut x = 31337i;
let mut y = 42i;
swap(&mut x, &mut y);
assert_eq!(x, 42);
assert_eq!(y, 31337);
}
#[test]
fn test_replace() {
let mut x = Some("test".to_string());
let y = replace(&mut x, None);
assert!(x.is_none());
assert!(y.is_some());
}
#[test]
fn test_transmute_copy() {
assert_eq!(1u, unsafe { transmute_copy(&1i) });
}
#[test]
fn test_transmute() {
trait Foo {}
impl Foo for int {}
let a = box 100i as Box<Foo>;
unsafe {
let x: ::core::raw::TraitObject = transmute(a);
assert!(*(x.data as *const int) == 100);
let _x: Box<Foo> = transmute(x);
}
unsafe {
Clean up rustc warnings. compiletest: compact "linux" "macos" etc.as "unix". liballoc: remove a superfluous "use". libcollections: remove invocations of deprecated methods in favor of their suggested replacements and use "_" for a loop counter. libcoretest: remove invocations of deprecated methods; also add "allow(deprecated)" for testing a deprecated method itself. libglob: use "cfg_attr". libgraphviz: add a test for one of data constructors. libgreen: remove a superfluous "use". libnum: "allow(type_overflow)" for type cast into u8 in a test code. librustc: names of static variables should be in upper case. libserialize: v[i] instead of get(). libstd/ascii: to_lowercase() instead of to_lower(). libstd/bitflags: modify AnotherSetOfFlags to use i8 as its backend. It will serve better for testing various aspects of bitflags!. libstd/collections: "allow(deprecated)" for testing a deprecated method itself. libstd/io: remove invocations of deprecated methods and superfluous "use". Also add #[test] where it was missing. libstd/num: introduce a helper function to effectively remove invocations of a deprecated method. libstd/path and rand: remove invocations of deprecated methods and superfluous "use". libstd/task and libsync/comm: "allow(deprecated)" for testing a deprecated method itself. libsync/deque: remove superfluous "unsafe". libsync/mutex and once: names of static variables should be in upper case. libterm: introduce a helper function to effectively remove invocations of a deprecated method. We still see a few warnings about using obsoleted native::task::spawn() in the test modules for libsync. I'm not sure how I should replace them with std::task::TaksBuilder and native::task::NativeTaskBuilder (dependency to libstd?) Signed-off-by: NODA, Kai <nodakai@gmail.com>
2014-10-05 05:11:17 -05:00
assert!(vec![76u8] == transmute("L".to_string()));
}
}
// FIXME #13642 (these benchmarks should be in another place)
/// Completely miscellaneous language-construct benchmarks.
// Static/dynamic method dispatch
struct Struct {
field: int
}
trait Trait {
fn method(&self) -> int;
}
impl Trait for Struct {
fn method(&self) -> int {
self.field
}
}
#[bench]
fn trait_vtable_method_call(b: &mut Bencher) {
let s = Struct { field: 10 };
let t = &s as &Trait;
b.iter(|| {
t.method()
});
}
#[bench]
fn trait_static_method_call(b: &mut Bencher) {
let s = Struct { field: 10 };
b.iter(|| {
s.method()
});
}
// Overhead of various match forms
#[bench]
fn match_option_some(b: &mut Bencher) {
let x = Some(10i);
b.iter(|| {
match x {
Some(y) => y,
None => 11
}
});
}
#[bench]
fn match_vec_pattern(b: &mut Bencher) {
let x = [1i,2,3,4,5,6];
b.iter(|| {
match x {
[1,2,3,..] => 10i,
_ => 11i,
}
});
}