array_of.rs 1.9 KB

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