array_of.rs 1.9 KB

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