basic_field.rs 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. use std::io::Cursor;
  2. use binrw::{BinRead, BinWrite};
  3. use ezcad::basic_field::BasicField;
  4. #[test]
  5. fn field_u32_be() {
  6. let encoded: Vec<u8> = vec![0x00, 0x00, 0x00, 0x04, 0x0A, 0x0B, 0x0C, 0x0D];
  7. let decoded: u32 = 0x0A0B0C0D;
  8. let mut stream: Cursor<Vec<u8>> = Cursor::new(encoded.clone());
  9. assert_eq!(
  10. BasicField::read_be(&mut stream).unwrap(),
  11. BasicField::U32(decoded.into())
  12. );
  13. let mut buffer: Cursor<Vec<u8>> = Cursor::new(vec![]);
  14. BasicField::U32(decoded.into())
  15. .write_be(&mut buffer)
  16. .unwrap();
  17. assert_eq!(buffer.into_inner(), encoded);
  18. }
  19. #[test]
  20. fn field_u32_le() {
  21. let encoded: Vec<u8> = vec![0x04, 0x00, 0x00, 0x00, 0x0D, 0x0C, 0x0B, 0x0A];
  22. let decoded: u32 = 0x0A0B0C0D;
  23. let mut stream: Cursor<Vec<u8>> = Cursor::new(encoded.clone());
  24. assert_eq!(
  25. BasicField::read_le(&mut stream).unwrap(),
  26. BasicField::U32(decoded.into())
  27. );
  28. let mut buffer: Cursor<Vec<u8>> = Cursor::new(vec![]);
  29. BasicField::U32(decoded.into())
  30. .write_le(&mut buffer)
  31. .unwrap();
  32. assert_eq!(buffer.into_inner(), encoded);
  33. }