blob: a2f4daf70fe8072e213e30a15db4d42ef0ff6f08 [file] [log] [blame]
Alex Crichtone8606192015-09-10 20:19:44 -07001#![allow(bad_style, unused_imports)]
Alex Crichton0df7c102015-09-10 16:35:37 -07002
Alex Crichton8e5f0cd2015-09-09 22:46:19 -07003extern crate libc;
4extern crate libc_test;
5
Alex Crichtonc8b895c2015-09-10 13:24:15 -07006use std::any::{Any, TypeId};
Alex Crichton16083062015-09-09 22:59:24 -07007use std::mem;
8
Alex Crichtona9adfbf2015-09-09 23:21:27 -07009use libc::*;
Alex Crichtona9adfbf2015-09-09 23:21:27 -070010
Alex Crichton0df7c102015-09-10 16:35:37 -070011trait Pretty {
12 fn pretty(&self) -> String;
13}
14
15impl<T> Pretty for *const T {
16 fn pretty(&self) -> String { format!("{:?}", self) }
17}
18impl<T> Pretty for *mut T {
19 fn pretty(&self) -> String { format!("{:?}", self) }
20}
21macro_rules! p {
22 ($($i:ident)*) => ($(
23 impl Pretty for $i {
24 fn pretty(&self) -> String { format!("{} ({:#x})", self, self) }
25 }
26 )*)
27}
28p! { i8 i16 i32 i64 u8 u16 u32 u64 usize isize }
29
Alex Crichton8fc95d22015-09-14 11:06:20 -070030static mut FAILED: bool = false;
31
Alex Crichton0df7c102015-09-10 16:35:37 -070032fn same<T: Eq + Pretty>(rust: T, c: T, attr: &str) {
Alex Crichton3e5155b2015-09-09 23:46:19 -070033 if rust != c {
Alex Crichton8fc95d22015-09-14 11:06:20 -070034 println!("bad {}: rust: {} != c {}", attr, rust.pretty(), c.pretty());
35 unsafe { FAILED = true; }
Alex Crichton3e5155b2015-09-09 23:46:19 -070036 }
37}
38
Alex Crichtonf81e3d32015-09-11 15:27:09 -070039#[allow(deprecated)]
Alex Crichtonc8b895c2015-09-10 13:24:15 -070040fn align<T: Any>() -> u64 {
41 // TODO: apparently these three types have less alignment in Rust on x86
42 // than they do in C this difference should.. probably be reconciled.
43 //
44 // Perhaps #27195?
45 if cfg!(target_pointer_width = "32") {
46 if TypeId::of::<T>() == TypeId::of::<f64>() ||
47 TypeId::of::<T>() == TypeId::of::<i64>() ||
48 TypeId::of::<T>() == TypeId::of::<u64>() {
49 return 8
50 }
51 }
Alex Crichtonf81e3d32015-09-11 15:27:09 -070052 mem::min_align_of::<T>() as u64
Alex Crichtonc8b895c2015-09-10 13:24:15 -070053}
54
Alex Crichton3e5155b2015-09-09 23:46:19 -070055macro_rules! offset_of {
56 ($ty:ident, $field:ident) => (
57 (&((*(0 as *const $ty)).$field)) as *const _ as u64
58 )
59}
60
Alex Crichton8e5f0cd2015-09-09 22:46:19 -070061include!(concat!(env!("OUT_DIR"), "/all.rs"));
Alex Crichton5a284332015-09-13 23:33:33 -070062
63fn main() {
64 println!("RUNNING ALL TESTS");
65 run_all();
Alex Crichton8fc95d22015-09-14 11:06:20 -070066 unsafe {
67 if FAILED {
68 panic!("some tests failed");
69 } else {
70 println!("PASSED");
71 }
72 }
Alex Crichton5a284332015-09-13 23:33:33 -070073}