2017-08-14 18:07:06 +08:00
|
|
|
extern crate rand;
|
|
|
|
extern crate nalgebra;
|
|
|
|
extern crate abomonation;
|
|
|
|
|
|
|
|
use rand::random;
|
|
|
|
use abomonation::{Abomonation, encode, decode};
|
2017-08-14 20:32:02 +08:00
|
|
|
use nalgebra::{
|
|
|
|
DMatrix, Matrix3x4, Point3, Translation3, Rotation3, Isometry3, Quaternion,
|
|
|
|
IsometryMatrix3, Similarity3, SimilarityMatrix3
|
|
|
|
};
|
2017-08-14 18:07:06 +08:00
|
|
|
|
2017-08-14 18:30:50 +08:00
|
|
|
#[test]
|
|
|
|
fn abomonate_dmatrix() {
|
2017-08-15 03:11:24 +08:00
|
|
|
assert_encode_and_decode(DMatrix::<f32>::new_random(3, 5));
|
2017-08-14 18:30:50 +08:00
|
|
|
}
|
|
|
|
|
2017-08-14 18:46:25 +08:00
|
|
|
macro_rules! test_abomonation(
|
|
|
|
($($test: ident, $ty: ty);* $(;)*) => {$(
|
|
|
|
#[test]
|
|
|
|
fn $test() {
|
2017-08-15 03:11:24 +08:00
|
|
|
assert_encode_and_decode(random::<$ty>());
|
2017-08-14 18:46:25 +08:00
|
|
|
}
|
|
|
|
)*}
|
|
|
|
);
|
|
|
|
|
|
|
|
test_abomonation! {
|
|
|
|
abomonate_matrix3x4, Matrix3x4<f32>;
|
|
|
|
abomonate_point3, Point3<f32>;
|
|
|
|
abomonate_translation3, Translation3<f64>;
|
|
|
|
abomonate_rotation3, Rotation3<f64>;
|
2017-08-14 20:32:02 +08:00
|
|
|
abomonate_isometry3, Isometry3<f32>;
|
|
|
|
abomonate_isometry_matrix3, IsometryMatrix3<f64>;
|
|
|
|
abomonate_similarity3, Similarity3<f32>;
|
|
|
|
abomonate_similarity_matrix3, SimilarityMatrix3<f32>;
|
|
|
|
abomonate_quaternion, Quaternion<f32>;
|
2017-08-14 18:41:03 +08:00
|
|
|
}
|
|
|
|
|
2017-08-15 03:11:24 +08:00
|
|
|
fn assert_encode_and_decode<T: Abomonation + PartialEq + Clone>(original_data: T) {
|
|
|
|
use std::mem::drop;
|
|
|
|
|
|
|
|
// Hold on to a clone for later comparison
|
|
|
|
let data = original_data.clone();
|
|
|
|
|
|
|
|
// Encode
|
2017-08-14 18:07:06 +08:00
|
|
|
let mut bytes = Vec::new();
|
2017-08-15 03:11:24 +08:00
|
|
|
unsafe { encode(&original_data, &mut bytes); }
|
|
|
|
|
|
|
|
// Drop the original, so that dangling pointers are revealed by the test
|
|
|
|
drop(original_data);
|
2017-08-14 18:07:06 +08:00
|
|
|
|
|
|
|
if let Some((result, rest)) = unsafe { decode::<T>(&mut bytes) } {
|
2017-08-15 03:11:24 +08:00
|
|
|
assert!(result == &data);
|
2017-08-14 18:07:06 +08:00
|
|
|
assert!(rest.len() == 0, "binary data was not decoded completely");
|
|
|
|
}
|
|
|
|
}
|