field_of.rs 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. use std::io::Cursor;
  2. use binrw::{BinRead, BinWrite};
  3. use diff::Diff;
  4. use ezcad::field_of::FieldOf;
  5. #[test]
  6. fn field_of_be() {
  7. #[derive(BinRead, BinWrite, Debug, PartialEq, Default, Diff)]
  8. #[diff(attr(
  9. #[derive(Debug, PartialEq)]
  10. ))]
  11. struct Test {
  12. a: u32,
  13. b: u32,
  14. }
  15. let decoded: FieldOf<Test> = FieldOf {
  16. value: Test {
  17. a: 0x0A0B0C0D,
  18. b: 0x01020304,
  19. },
  20. };
  21. let encoded: Vec<u8> = vec![
  22. 0x00, 0x00, 0x00, 0x08, 0x0A, 0x0B, 0x0C, 0x0D, 0x01, 0x02, 0x03, 0x04,
  23. ];
  24. let mut stream: Cursor<Vec<u8>> = Cursor::new(encoded.clone());
  25. assert_eq!(FieldOf::<Test>::read_be(&mut stream).unwrap(), decoded);
  26. let mut buffer: Cursor<Vec<u8>> = Cursor::new(vec![]);
  27. decoded.write_be(&mut buffer).unwrap();
  28. assert_eq!(buffer.into_inner(), encoded);
  29. }
  30. #[test]
  31. fn field_of_le() {
  32. #[derive(BinRead, BinWrite, Debug, PartialEq, Default, Diff)]
  33. #[diff(attr(
  34. #[derive(Debug, PartialEq)]
  35. ))]
  36. struct Test {
  37. a: u32,
  38. b: u32,
  39. }
  40. let decoded: FieldOf<Test> = FieldOf {
  41. value: Test {
  42. a: 0x0A0B0C0D,
  43. b: 0x01020304,
  44. },
  45. };
  46. let encoded: Vec<u8> = vec![
  47. 0x08, 0x00, 0x00, 0x00, 0x0D, 0x0C, 0x0B, 0x0A, 0x04, 0x03, 0x02, 0x01,
  48. ];
  49. let mut stream: Cursor<Vec<u8>> = Cursor::new(encoded.clone());
  50. assert_eq!(FieldOf::<Test>::read_le(&mut stream).unwrap(), decoded);
  51. let mut buffer: Cursor<Vec<u8>> = Cursor::new(vec![]);
  52. decoded.write_le(&mut buffer).unwrap();
  53. assert_eq!(buffer.into_inner(), encoded);
  54. }