basic_field.rs 967 B

123456789101112131415161718192021222324252627282930
  1. use std::io::Cursor;
  2. use binrw::{BinRead, BinWrite};
  3. use ezcad::types::U32;
  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!(U32::read_be(&mut stream).unwrap(), decoded.into());
  10. let mut buffer: Cursor<Vec<u8>> = Cursor::new(vec![]);
  11. U32::from(decoded).write_be(&mut buffer).unwrap();
  12. assert_eq!(buffer.into_inner(), encoded);
  13. }
  14. #[test]
  15. fn field_u32_le() {
  16. let encoded: Vec<u8> = vec![0x04, 0x00, 0x00, 0x00, 0x0D, 0x0C, 0x0B, 0x0A];
  17. let decoded: u32 = 0x0A0B0C0D;
  18. let mut stream: Cursor<Vec<u8>> = Cursor::new(encoded.clone());
  19. assert_eq!(U32::read_le(&mut stream).unwrap(), decoded.into());
  20. let mut buffer: Cursor<Vec<u8>> = Cursor::new(vec![]);
  21. U32::from(decoded).write_le(&mut buffer).unwrap();
  22. assert_eq!(buffer.into_inner(), encoded);
  23. }