2014-06-28 13:57:36 -07:00
|
|
|
use core::mem::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn size_of_basic() {
|
2015-02-03 13:50:52 +00:00
|
|
|
assert_eq!(size_of::<u8>(), 1);
|
|
|
|
assert_eq!(size_of::<u16>(), 2);
|
|
|
|
assert_eq!(size_of::<u32>(), 4);
|
|
|
|
assert_eq!(size_of::<u64>(), 8);
|
2014-06-28 13:57:36 -07:00
|
|
|
}
|
|
|
|
|
2016-05-06 09:31:11 -04:00
|
|
|
#[test]
|
|
|
|
#[cfg(target_pointer_width = "16")]
|
|
|
|
fn size_of_16() {
|
|
|
|
assert_eq!(size_of::<usize>(), 2);
|
|
|
|
assert_eq!(size_of::<*const usize>(), 2);
|
|
|
|
}
|
|
|
|
|
2014-06-28 13:57:36 -07:00
|
|
|
#[test]
|
2015-01-13 18:59:45 -08:00
|
|
|
#[cfg(target_pointer_width = "32")]
|
2014-06-28 13:57:36 -07:00
|
|
|
fn size_of_32() {
|
2015-03-25 17:06:52 -07:00
|
|
|
assert_eq!(size_of::<usize>(), 4);
|
|
|
|
assert_eq!(size_of::<*const usize>(), 4);
|
2014-06-28 13:57:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2015-01-13 18:59:45 -08:00
|
|
|
#[cfg(target_pointer_width = "64")]
|
2014-06-28 13:57:36 -07:00
|
|
|
fn size_of_64() {
|
2015-03-25 17:06:52 -07:00
|
|
|
assert_eq!(size_of::<usize>(), 8);
|
|
|
|
assert_eq!(size_of::<*const usize>(), 8);
|
2014-06-28 13:57:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[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() {
|
2015-02-03 13:50:52 +00:00
|
|
|
assert_eq!(align_of::<u8>(), 1);
|
|
|
|
assert_eq!(align_of::<u16>(), 2);
|
|
|
|
assert_eq!(align_of::<u32>(), 4);
|
2014-06-28 13:57:36 -07:00
|
|
|
}
|
|
|
|
|
2016-05-06 09:31:11 -04:00
|
|
|
#[test]
|
|
|
|
#[cfg(target_pointer_width = "16")]
|
|
|
|
fn align_of_16() {
|
|
|
|
assert_eq!(align_of::<usize>(), 2);
|
|
|
|
assert_eq!(align_of::<*const usize>(), 2);
|
|
|
|
}
|
|
|
|
|
2014-06-28 13:57:36 -07:00
|
|
|
#[test]
|
2015-01-13 18:59:45 -08:00
|
|
|
#[cfg(target_pointer_width = "32")]
|
2014-06-28 13:57:36 -07:00
|
|
|
fn align_of_32() {
|
2015-03-25 17:06:52 -07:00
|
|
|
assert_eq!(align_of::<usize>(), 4);
|
|
|
|
assert_eq!(align_of::<*const usize>(), 4);
|
2014-06-28 13:57:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2015-01-13 18:59:45 -08:00
|
|
|
#[cfg(target_pointer_width = "64")]
|
2014-06-28 13:57:36 -07:00
|
|
|
fn align_of_64() {
|
2015-03-25 17:06:52 -07:00
|
|
|
assert_eq!(align_of::<usize>(), 8);
|
|
|
|
assert_eq!(align_of::<*const usize>(), 8);
|
2014-06-28 13:57:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn align_of_val_basic() {
|
2015-02-03 13:50:52 +00:00
|
|
|
assert_eq!(align_of_val(&1u8), 1);
|
|
|
|
assert_eq!(align_of_val(&1u16), 2);
|
|
|
|
assert_eq!(align_of_val(&1u32), 4);
|
2014-06-28 13:57:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_swap() {
|
2015-01-25 22:05:03 +01:00
|
|
|
let mut x = 31337;
|
|
|
|
let mut y = 42;
|
2014-06-28 13:57:36 -07:00
|
|
|
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() {
|
2015-02-03 13:50:52 +00:00
|
|
|
assert_eq!(1, unsafe { transmute_copy(&1) });
|
2014-06-28 13:57:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_transmute() {
|
2019-12-06 20:18:12 -08:00
|
|
|
trait Foo {
|
|
|
|
fn dummy(&self) {}
|
|
|
|
}
|
2015-03-25 17:06:52 -07:00
|
|
|
impl Foo for isize {}
|
2014-06-28 13:57:36 -07:00
|
|
|
|
2018-07-13 14:25:22 +09:00
|
|
|
let a = box 100isize as Box<dyn Foo>;
|
2014-06-28 13:57:36 -07:00
|
|
|
unsafe {
|
|
|
|
let x: ::core::raw::TraitObject = transmute(a);
|
2015-03-25 17:06:52 -07:00
|
|
|
assert!(*(x.data as *const isize) == 100);
|
2018-07-13 14:25:22 +09:00
|
|
|
let _x: Box<dyn Foo> = transmute(x);
|
2014-06-28 13:57:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
unsafe {
|
2015-03-10 17:59:23 -07:00
|
|
|
assert_eq!(transmute::<_, Vec<u8>>("L".to_string()), [76]);
|
2014-06-28 13:57:36 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-07 21:33:36 +02:00
|
|
|
#[test]
|
|
|
|
#[allow(dead_code)]
|
|
|
|
fn test_discriminant_send_sync() {
|
|
|
|
enum Regular {
|
|
|
|
A,
|
2019-12-06 20:18:12 -08:00
|
|
|
B(i32),
|
2017-10-07 21:33:36 +02:00
|
|
|
}
|
|
|
|
enum NotSendSync {
|
2019-12-06 20:18:12 -08:00
|
|
|
A(*const i32),
|
2017-10-07 21:33:36 +02:00
|
|
|
}
|
|
|
|
|
2019-12-06 20:18:12 -08:00
|
|
|
fn is_send_sync<T: Send + Sync>() {}
|
2017-10-07 21:33:36 +02:00
|
|
|
|
|
|
|
is_send_sync::<Discriminant<Regular>>();
|
|
|
|
is_send_sync::<Discriminant<NotSendSync>>();
|
|
|
|
}
|