alchemy/tests/transmutable.rs
Jason Travis Smith aa4bb78f66 Vector types are now Transmutable.
A Vector can now be converted to and from bytes of data.
I'm not entirely sure that the structure bytes do not have to
be handled for endianess, but the underlying basic types are
correctly handled.

Tests and and example have also be created. The tests lib file was split
into a converter test file and a transmutable test file.
2016-01-07 05:10:50 -05:00

51 lines
1.2 KiB
Rust

extern crate rand;
extern crate alchemy;
extern crate sigils;
macro_rules! transmutation_test
{
($modName: ident, $varType: ident, $numBytes: expr, [$($var: ident)*]) =>
{
mod $modName
{
use rand::{thread_rng, Rng, ThreadRng};
use alchemy::{Endianess, Transmutable};
use sigils::Zero;
use sigils::vector::$varType;
#[test]
pub fn transmutation()
{
let mut vec: $varType<u64>;
let final_vec: $varType<u64>;
let endianess: Endianess;
let mut rng: ThreadRng;
let mut buffer: [u8; $numBytes];
// Initialize the variables.
rng = thread_rng();
vec = $varType::<u64>::zero();
$(vec.$var = rng.next_u64();)*
buffer = [0u8; $numBytes];
endianess = Endianess::PLATFORM;
vec.to_bytes(&mut buffer, endianess);
final_vec = $varType::from_bytes(&buffer, endianess);
$(assert_eq!(vec.$var, final_vec.$var);)*
}
}
}
}
transmutation_test!(vec2, Vector2, 16, [x y]);
transmutation_test!(vec3, Vector3, 24, [x y z]);
transmutation_test!(vec4, Vector4, 32, [x y z w]);