#[macro_use] extern crate typeinfo; use std::mem; use typeinfo::Type::*; use typeinfo::{Type, TypeInfo, Field}; #[test] fn test_scalar_types() { fn check_scalar_type(ty: Type) { assert_eq!(::type_info(), ty); assert_eq!(ty.size(), mem::size_of::()); assert!(ty.is_scalar() && !ty.is_array() && !ty.is_compound()); } check_scalar_type::(Int8); check_scalar_type::(Int16); check_scalar_type::(Int32); check_scalar_type::(Int64); check_scalar_type::(UInt8); check_scalar_type::(UInt16); check_scalar_type::(UInt32); check_scalar_type::(UInt64); check_scalar_type::(Float32); check_scalar_type::(Float64); check_scalar_type::(Bool); check_scalar_type::(Char); if mem::size_of::() == 4 { check_scalar_type::(Int32); check_scalar_type::(UInt32); } else { check_scalar_type::(Int64); check_scalar_type::(UInt64); } } #[test] fn test_array_types() { let ty = <[u16; 42] as TypeInfo>::type_info(); assert_eq!(ty, Array(Box::new(UInt16), 42)); assert_eq!(ty.size(), 2 * 42); assert!(ty.is_array() && !ty.is_scalar() && !ty.is_compound()); let ty = <[[i8; 2]; 3] as TypeInfo>::type_info(); assert_eq!(ty, Array(Box::new(Array(Box::new(Int8), 2)), 3)); assert_eq!(ty.size(), 1 * 2 * 3); assert!(ty.is_array() && !ty.is_scalar() && !ty.is_compound()); } #[test] fn test_compound_types() { def![struct X { a: i32, }]; let ty = X::type_info(); assert_eq!(ty, Compound(vec![ Field::new(&Int32, "a", 0) ], mem::size_of::())); assert_eq!(ty.size(), mem::size_of::()); assert!(ty.is_compound() && !ty.is_scalar() && !ty.is_array()); def![struct Y { a: u64, x: [X; 2] }]; let ty = Y::type_info(); assert_eq!(ty, Compound(vec![ Field::new(&UInt64, "a", 0), Field::new(&Array(Box::new(X::type_info()), 2), "x", 8), ], mem::size_of::())); assert_eq!(ty.size(), mem::size_of::()); assert!(ty.is_compound() && !ty.is_scalar() && !ty.is_array()); } #[test] fn test_compound_copy_clone() { def![struct X { a: char }]; let x = X { a: '0' }; let y = x; assert_eq!(x.a, y.a); assert_eq!(x.clone().a, y.clone().a); } #[test] fn test_struct_attributes() { def![struct X { a: i8, b: u64 }]; def![#[repr(packed)] struct Y { a: i8, b: u64 }]; assert!(X::type_info().size() > Y::type_info().size()); } #[cfg(test)] mod module { def! { pub struct A { x: i32, y: i32 } } def! { pub struct B { pub x: i32, pub y: i32 } } pub mod multiple { def! { struct C { x: i32 } struct D { x: i32 } } def! { pub struct E { x: i32 } pub struct F { x: i32 } } def! { pub struct G { pub x: i32 } pub struct H { pub x: i32 } } } } #[test] #[allow(unused_variables, unused_imports)] fn test_pub_structs_fields() { use module::{A, B}; use module::multiple::{E, F, G, H}; let b = B { x: 1, y: 2 }; }