use std::io::Cursor;
use nalgebra::{Vector2, Vector4};
use super::ReadBytes;
#[test]
fn len() {
assert_eq!(ReadBytes::len(&mut Cursor::new([0, 0, 0, 0])).unwrap(), 4);
}
#[test]
fn read_slice() {
assert_eq!(ReadBytes::read_slice(&mut Cursor::new([1, 2, 3, 4]), 4, false).unwrap(), vec![1, 2, 3, 4]);
assert_eq!(ReadBytes::read_slice(&mut Cursor::new(vec![0u8; 0]), 0, false).unwrap(), vec![0u8; 0]);
assert!(ReadBytes::read_slice(&mut Cursor::new([]), 4, false).is_err());
}
#[test]
fn read_bool() {
assert!(!ReadBytes::read_bool(&mut Cursor::new([0])).unwrap());
assert!(ReadBytes::read_bool(&mut Cursor::new([1])).unwrap());
assert!(ReadBytes::read_bool(&mut Cursor::new([2])).is_err());
}
#[test]
fn read_u8() {
assert_eq!(ReadBytes::read_u8(&mut Cursor::new([10])).unwrap(), 10);
assert!(ReadBytes::read_u8(&mut Cursor::new([])).is_err());
}
#[test]
fn read_u16() {
assert_eq!(ReadBytes::read_u16(&mut Cursor::new([10, 0])).unwrap(), 10);
assert!(ReadBytes::read_u16(&mut Cursor::new([10])).is_err());
}
#[test]
fn read_u24() {
assert_eq!(ReadBytes::read_u24(&mut Cursor::new([152, 150, 129])).unwrap(), 8492696);
assert!(ReadBytes::read_u24(&mut Cursor::new([152, 150])).is_err());
}
#[test]
fn read_u32() {
assert_eq!(ReadBytes::read_u32(&mut Cursor::new([10, 0, 0, 0])).unwrap(), 10);
assert!(ReadBytes::read_u32(&mut Cursor::new([10, 0, 0])).is_err());
}
#[test]
fn read_u64() {
assert_eq!(ReadBytes::read_u64(&mut Cursor::new([10, 0, 0, 0, 0, 0, 0, 0])).unwrap(), 10);
assert!(ReadBytes::read_u64(&mut Cursor::new([10, 0, 0, 0, 0])).is_err());
}
#[test]
fn read_cauleb128() {
assert_eq!(ReadBytes::read_cauleb128(&mut Cursor::new([0x80, 10])).unwrap(), 10);
assert!(ReadBytes::read_cauleb128(&mut Cursor::new([])).is_err());
}
#[test]
fn read_i8() {
assert_eq!(ReadBytes::read_i8(&mut Cursor::new([254])).unwrap(), -2);
assert!(ReadBytes::read_i8(&mut Cursor::new([])).is_err());
}
#[test]
fn read_i16() {
assert_eq!(ReadBytes::read_i16(&mut Cursor::new([254, 254])).unwrap(), -258);
assert!(ReadBytes::read_i16(&mut Cursor::new([10])).is_err());
}
#[test]
fn read_i24() {
assert_eq!(ReadBytes::read_i24(&mut Cursor::new([152, 150, 129])).unwrap(), -8_284_520);
assert!(ReadBytes::read_i24(&mut Cursor::new([152, 150])).is_err());
}
#[test]
fn read_i32() {
assert_eq!(ReadBytes::read_i32(&mut Cursor::new([10, 0, 0, 0])).unwrap(), 10);
assert!(ReadBytes::read_i32(&mut Cursor::new([10, 0, 0])).is_err());
}
#[test]
fn read_i64() {
assert_eq!(ReadBytes::read_i64(&mut Cursor::new([10, 0, 0, 0, 0, 0, 0, 0])).unwrap(), 10);
assert!(ReadBytes::read_i64(&mut Cursor::new([10, 0, 0])).is_err());
}
#[test]
fn read_optional_i16() {
assert_eq!(ReadBytes::read_optional_i16(&mut Cursor::new([1, 254, 254])).unwrap(), -258);
assert!(ReadBytes::read_optional_i16(&mut Cursor::new([1, 10])).is_err());
assert!(ReadBytes::read_optional_i16(&mut Cursor::new([2, 10, 0])).is_err());
}
#[test]
fn read_optional_i32() {
assert_eq!(ReadBytes::read_optional_i32(&mut Cursor::new([1, 10, 0, 0, 0])).unwrap(), 10);
assert!(ReadBytes::read_optional_i32(&mut Cursor::new([1, 10, 0, 0])).is_err());
assert!(ReadBytes::read_optional_i32(&mut Cursor::new([2, 10, 0, 0, 0])).is_err());
}
#[test]
fn read_optional_i64() {
assert_eq!(ReadBytes::read_optional_i64(&mut Cursor::new([1, 10, 0, 0, 0, 0, 0, 0, 0])).unwrap(), 10);
assert!(ReadBytes::read_optional_i64(&mut Cursor::new([1, 10, 0, 0])).is_err());
assert!(ReadBytes::read_optional_i64(&mut Cursor::new([2, 10, 0, 0, 0])).is_err());
}
#[test]
fn read_f16() {
assert_eq!(ReadBytes::read_f16(&mut Cursor::new([32, 65])).unwrap(), half::f16::from_f32(2.5625));
assert!(ReadBytes::read_f16(&mut Cursor::new([65])).is_err());
}
#[test]
fn read_f32() {
assert_eq!(ReadBytes::read_f32(&mut Cursor::new([0, 0, 32, 65])).unwrap(), 10.0);
assert!(ReadBytes::read_f32(&mut Cursor::new([0, 32, 65])).is_err());
}
#[test]
fn read_f32_normal_from_u8() {
assert_eq!(ReadBytes::read_f32_normal_from_u8(&mut Cursor::new([253])).unwrap(), 0.9843137);
}
#[test]
fn read_f64() {
assert_eq!(ReadBytes::read_f64(&mut Cursor::new([0, 0, 0, 0, 0, 0, 36, 64])).unwrap(), 10.0);
assert!(ReadBytes::read_f64(&mut Cursor::new([0, 0, 0, 0, 36, 64])).is_err());
}
#[test]
fn read_string_u8() {
assert_eq!(ReadBytes::read_string_u8(&mut Cursor::new([87, 97, 104, 97, 104, 97, 104, 97, 104, 97]), 10).unwrap(), "Wahahahaha");
assert!(ReadBytes::read_string_u8(&mut Cursor::new([87, 97, 104, 97, 255, 104, 97, 104, 97, 104, 97]), 10).is_err());
}
#[test]
fn read_string_u8_iso_8859_15() {
assert_eq!(ReadBytes::read_string_u8_iso_8859_15(&mut Cursor::new([87, 97, 104, 97, 104, 97, 104, 97, 104, 97]), 10).unwrap(), "Wahahahaha");
assert_eq!(ReadBytes::read_string_u8_iso_8859_15(&mut Cursor::new([87, 97, 104, 97, 255, 104, 97, 104, 97, 104, 97]), 11).unwrap(), "Wahaÿhahaha");
}
#[test]
fn read_string_u8_0padded() {
assert_eq!(ReadBytes::read_string_u8_0padded(&mut Cursor::new([87, 97, 104, 97, 104, 97, 0, 0, 0, 0]), 10).unwrap(), "Wahaha");
assert_eq!(ReadBytes::read_string_u8_0padded(&mut Cursor::new([87, 97, 104, 97, 0, 104, 97, 0, 0, 0]), 10).unwrap(), "Waha");
assert!(ReadBytes::read_string_u8_0padded(&mut Cursor::new([87, 97, 104, 97, 255, 104, 97, 104, 97, 104, 97, 0, 0]), 12).is_err());
assert_eq!(ReadBytes::read_string_u8_0padded(&mut Cursor::new([87, 97, 104, 97, 104, 97, 104, 97, 104, 97]), 10).unwrap(), "Wahahahaha");
}
#[test]
fn read_string_u8_0terminated() {
assert_eq!(ReadBytes::read_string_u8_0terminated(&mut Cursor::new([87, 97, 104, 97, 104, 97, 104, 97, 0, 97])).unwrap(), "Wahahaha".to_owned());
assert!(ReadBytes::read_string_u8_0terminated(&mut Cursor::new([87, 97, 104, 97, 104, 97, 104, 97, 104, 97])).is_err());
assert!(ReadBytes::read_string_u8_0terminated(&mut Cursor::new([87, 97, 104, 97, 255, 104, 97, 104, 97, 104, 97])).is_err());
}
#[test]
fn read_sized_string_u8() {
assert_eq!(ReadBytes::read_sized_string_u8(&mut Cursor::new([10, 0, 87, 97, 104, 97, 104, 97, 104, 97, 104, 97])).unwrap(), "Wahahahaha".to_owned());
assert!(ReadBytes::read_sized_string_u8(&mut Cursor::new([5])).is_err());
assert!(ReadBytes::read_sized_string_u8(&mut Cursor::new([4, 0, 2])).is_err());
assert!(ReadBytes::read_sized_string_u8(&mut Cursor::new([11, 0, 87, 97, 104, 97, 255, 104, 97, 104, 97, 104, 97])).is_err());
}
#[test]
fn read_sized_string_u8_u32() {
assert_eq!(ReadBytes::read_sized_string_u8_u32(&mut Cursor::new([10, 0, 0, 0, 87, 97, 104, 97, 104, 97, 104, 97, 104, 97])).unwrap(), "Wahahahaha".to_owned());
assert!(ReadBytes::read_sized_string_u8_u32(&mut Cursor::new([5])).is_err());
assert!(ReadBytes::read_sized_string_u8_u32(&mut Cursor::new([4, 0, 0, 0, 2])).is_err());
assert!(ReadBytes::read_sized_string_u8_u32(&mut Cursor::new([11, 0, 0, 0, 87, 97, 104, 97, 255, 104, 97, 104, 97, 104, 97])).is_err());
}
#[test]
fn read_optional_string_u8() {
assert_eq!(ReadBytes::read_optional_string_u8(&mut Cursor::new([0])).unwrap(), "".to_owned());
assert_eq!(ReadBytes::read_optional_string_u8(&mut Cursor::new([1, 10, 0, 87, 97, 104, 97, 104, 97, 104, 97, 104, 97])).unwrap(), "Wahahahaha".to_owned());
assert!(ReadBytes::read_optional_string_u8(&mut Cursor::new([1])).is_err());
assert!(ReadBytes::read_optional_string_u8(&mut Cursor::new([2])).is_err());
assert!(ReadBytes::read_optional_string_u8(&mut Cursor::new([1, 5])).is_err());
assert!(ReadBytes::read_optional_string_u8(&mut Cursor::new([1, 4, 0, 2])).is_err());
assert!(ReadBytes::read_optional_string_u8(&mut Cursor::new([1, 11, 0, 87, 97, 104, 97, 255, 104, 97, 104, 97, 104, 97])).is_err());
}
#[test]
fn read_string_u16() {
assert_eq!(ReadBytes::read_string_u16(&mut Cursor::new([87, 0, 97, 0, 104, 0, 97, 0, 104, 0, 97, 0]), 12).unwrap(), "Wahaha");
assert!(ReadBytes::read_string_u16(&mut Cursor::new([87, 0, 0, 216, 104, 0, 97, 0, 104, 0, 97, 0, 1]), 13).is_err());
assert!(ReadBytes::read_string_u16(&mut Cursor::new([87, 0, 0, 216, 104, 0, 97, 0, 104, 0, 97, 0]), 14).is_err());
}
#[test]
fn read_string_u16_0padded() {
assert_eq!(ReadBytes::read_string_u16_0padded(&mut Cursor::new([87, 0, 97, 0, 104, 0, 97, 0, 104, 0, 97, 0, 0, 0, 0, 0, 0, 0, 0, 0]), 20).unwrap(), "Wahaha");
assert_eq!(ReadBytes::read_string_u16_0padded(&mut Cursor::new([87, 0, 97, 0, 104, 0, 97, 0, 0, 0, 104, 0, 97, 0, 0, 0, 0, 0, 0, 0]), 20).unwrap(), "Waha");
assert_eq!(ReadBytes::read_string_u16_0padded(&mut Cursor::new([87, 0, 97, 0, 104, 0, 97, 0, 104, 0, 97, 0, 104, 0, 97, 0, 104, 0, 97, 0]), 20).unwrap(), "Wahahahaha");
assert!(ReadBytes::read_string_u16_0padded(&mut Cursor::new([87, 0, 97, 0, 104, 0, 97, 0, 104, 0, 97, 0, 0, 0, 0, 0, 0, 0, 0, 0]), 40).is_err());
}
#[test]
fn read_string_u16_0terminated() {
assert_eq!(ReadBytes::read_string_u16_0terminated(&mut Cursor::new([87, 0, 97, 0, 104, 0, 97, 0, 104, 0, 97, 0, 104, 0, 97, 0, 0, 0])).unwrap(), "Wahahaha".to_owned());
assert!(ReadBytes::read_string_u16_0terminated(&mut Cursor::new([87, 0, 97, 0, 104, 0, 97, 0, 104, 0, 97, 0, 104, 0, 97, 0, 104, 0, 97, 0])).is_err());
}
#[test]
fn read_sized_string_u16() {
assert_eq!(ReadBytes::read_sized_string_u16(&mut Cursor::new([4, 0, 87, 0, 97, 0, 104, 0, 97, 0])).unwrap(), "Waha".to_owned());
assert!(ReadBytes::read_sized_string_u16(&mut Cursor::new([5])).is_err());
assert!(ReadBytes::read_sized_string_u16(&mut Cursor::new([4, 0, 2])).is_err());
}
#[test]
fn read_sized_string_u16_u32() {
assert_eq!(ReadBytes::read_sized_string_u16_u32(&mut Cursor::new([4, 0, 0, 0, 87, 0, 97, 0, 104, 0, 97, 0])).unwrap(), "Waha".to_owned());
assert!(ReadBytes::read_sized_string_u16_u32(&mut Cursor::new([5])).is_err());
assert!(ReadBytes::read_sized_string_u16_u32(&mut Cursor::new([4, 0, 0, 0, 2])).is_err());
}
#[test]
fn read_optional_string_u16() {
assert_eq!(ReadBytes::read_optional_string_u16(&mut Cursor::new([0])).unwrap(), "".to_owned());
assert_eq!(ReadBytes::read_optional_string_u16(&mut Cursor::new([1, 4, 0, 87, 0, 97, 0, 104, 0, 97, 0])).unwrap(), "Waha".to_owned());
assert!(ReadBytes::read_optional_string_u16(&mut Cursor::new([2, 5])).is_err());
assert!(ReadBytes::read_optional_string_u16(&mut Cursor::new([1, 5])).is_err());
assert!(ReadBytes::read_optional_string_u16(&mut Cursor::new([1, 4, 0, 2])).is_err());
}
#[test]
fn read_string_colour_rgb() {
assert_eq!(ReadBytes::read_string_colour_rgb(&mut Cursor::new([0xFF, 0x04, 0x05, 0x00])).unwrap(), "0504FF");
assert!(ReadBytes::read_string_colour_rgb(&mut Cursor::new([0x87, 0x97])).is_err());
}
#[test]
fn read_vector_2_u8() {
assert_eq!(ReadBytes::read_vector_2_u8(&mut Cursor::new([0x0A, 0x0A])).unwrap(), Vector2::new(10, 10));
assert!(ReadBytes::read_vector_2_u8(&mut Cursor::new([0x87])).is_err());
}
#[test]
fn read_vector_2_f32_pct_from_vector_2_u8() {
assert_eq!(ReadBytes::read_vector_2_f32_pct_from_vector_2_u8(&mut Cursor::new([0x0A, 0x0A])).unwrap(), Vector2::new(0.039215688, 0.039215688));
assert!(ReadBytes::read_vector_2_f32_pct_from_vector_2_u8(&mut Cursor::new([0x87])).is_err());
}
#[test]
fn read_vector_2_f32_from_vector_2_f16() {
assert_eq!(ReadBytes::read_vector_2_f32_from_vector_2_f16(&mut Cursor::new([
0x0A, 0x0A,
0x0A, 0x0A
])).unwrap(), Vector2::new(0.00018429756, 0.00018429756));
assert!(ReadBytes::read_vector_2_f32_from_vector_2_f16(&mut Cursor::new([0x87])).is_err());
}
#[test]
fn read_vector_4_u8() {
assert_eq!(ReadBytes::read_vector_4_u8(&mut Cursor::new([0x0A, 0x0A, 0x0A, 0x0A])).unwrap(), Vector4::new(10, 10, 10, 10));
assert!(ReadBytes::read_vector_4_u8(&mut Cursor::new([0x87])).is_err());
}
#[test]
fn read_vector_4_f32() {
assert_eq!(ReadBytes::read_vector_4_f32(&mut Cursor::new([
0x00, 0x00, 0xFF, 0x3F,
0x00, 0x00, 0xFF, 0x3F,
0x00, 0x00, 0xFF, 0x3F,
0x00, 0x00, 0xFF, 0x3F
])).unwrap(), Vector4::new(1.9921875, 1.9921875, 1.9921875, 1.9921875));
assert!(ReadBytes::read_vector_4_f32(&mut Cursor::new([0x87])).is_err());
}
#[test]
fn read_vector_4_f32_from_vec_3_f32() {
assert_eq!(ReadBytes::read_vector_4_f32_from_vec_3_f32(&mut Cursor::new([
0x00, 0x00, 0xFF, 0x3F,
0x00, 0x00, 0xFF, 0x3F,
0x00, 0x00, 0xFF, 0x3F
])).unwrap(), Vector4::new(1.9921875, 1.9921875, 1.9921875, 0.0));
assert!(ReadBytes::read_vector_4_f32_from_vec_3_f32(&mut Cursor::new([0x87])).is_err());
}
#[test]
fn read_vector_4_f32_normal_from_vector_4_u8() {
assert_eq!(ReadBytes::read_vector_4_f32_normal_from_vector_4_u8(&mut Cursor::new([
0x0A,
0x0A,
0x0A,
0x0A
])).unwrap(), Vector4::new(-0.92156863, -0.92156863, -0.92156863, -0.92156863));
assert!(ReadBytes::read_vector_4_f32_normal_from_vector_4_u8(&mut Cursor::new([0x87])).is_err());
}
#[test]
fn read_vector_4_f32_pct_from_vector_4_u8() {
assert_eq!(ReadBytes::read_vector_4_f32_pct_from_vector_4_u8(&mut Cursor::new([
0x0A,
0x0A,
0x0A,
0x0A
])).unwrap(), Vector4::new(0.039215688, 0.039215688, 0.039215688, 0.039215688));
assert!(ReadBytes::read_vector_4_f32_pct_from_vector_4_u8(&mut Cursor::new([0x87])).is_err());
}
#[test]
fn read_vector_4_f32_normal_from_vector_4_f16() {
assert_eq!(ReadBytes::read_vector_4_f32_normal_from_vector_4_f16(&mut Cursor::new([
0x0A, 0x3F,
0x0A, 0x3F,
0x0A, 0x3F,
0x0A, 0x3F
])).unwrap(), Vector4::new(3.096775, 3.096775, 3.096775, 1.7597656));
assert!(ReadBytes::read_vector_4_f32_normal_from_vector_4_f16(&mut Cursor::new([0x87])).is_err());
}