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.
This commit is contained in:
Jason Travis Smith
2016-01-07 05:10:50 -05:00
parent 8075e68607
commit aa4bb78f66
5 changed files with 274 additions and 8 deletions

View File

@ -1,5 +1,3 @@
#![feature(zero_one)]
extern crate rand;
extern crate alchemy;
@ -49,6 +47,7 @@ macro_rules! full_check_impl
($T: ident, $read_func: ident, $write_func: ident, $numBytes: expr) =>
{
{
use std::$T;
use rand::ThreadRng;
use rand::distributions::{IndependentSample, Range};
use alchemy::{Converter, BigEndian, LittleEndian};
@ -57,7 +56,7 @@ macro_rules! full_check_impl
let mut rng: ThreadRng;
rng = rand::thread_rng();
range = Range::new(std::$T::MIN, std::$T::MAX);
range = Range::new($T::MIN, $T::MAX);
for _ in 0..EXHAUSTIVE_RUNS
{
let val: $T;
@ -116,7 +115,7 @@ macro_rules! overflow_impl
$read_func: ident, $write_func: ident) =>
{
use alchemy::{Converter, BigEndian, LittleEndian};
use sigils::Zero;
#[test]
@ -171,7 +170,6 @@ macro_rules! test_buffer_overflow
mod $mod_name
{
use alchemy::$numBytes;
use std::num::Zero;
overflow_impl!($numBytes, $T, $read_func, $write_func);
}

50
tests/transmutable.rs Normal file
View File

@ -0,0 +1,50 @@
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]);