dependency_tests.rs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. use super::{from_vec, to_vec, Result};
  2. use serde::{Deserialize, Serialize};
  3. use serde_big_array::BigArray;
  4. #[test]
  5. fn test_empty_array_deserialization() -> Result<()> {
  6. #[derive(Deserialize, Debug, PartialEq)]
  7. struct Vacuous {
  8. // Of course you would never actually do this, but it should handle it properly regardless.
  9. #[serde(with = "BigArray")]
  10. nothing: [u8; 0],
  11. }
  12. let expected = Vacuous { nothing: [0u8; 0] };
  13. let vec = vec![];
  14. let result = from_vec(&vec);
  15. assert_eq!(expected, result?);
  16. Ok(())
  17. }
  18. #[derive(Serialize, Deserialize, Debug, PartialEq)]
  19. struct S {
  20. #[serde(with = "BigArray")]
  21. array: [u8; 0x40],
  22. }
  23. #[test]
  24. fn test_big_array_deserialization() -> Result<()> {
  25. let expected = S {
  26. array: [
  27. 0x81, 0xc0, 0xf, 0x3e, 0x2f, 0x11, 0xae, 0xc9, 0x48, 0x68, 0xc4, 0x66, 0x67, 0x1e,
  28. 0xda, 0x4e, 0xde, 0xf, 0x84, 0x23, 0x4d, 0x24, 0x0, 0x23, 0xe8, 0x84, 0xac, 0xc6, 0x7c,
  29. 0xb0, 0x34, 0x5, 0xa3, 0xc3, 0x4b, 0x59, 0xaa, 0x96, 0x9f, 0xc6, 0x2d, 0x31, 0x5a,
  30. 0xb5, 0x61, 0xfd, 0xad, 0x9b, 0x7a, 0xf8, 0xf3, 0xd7, 0xee, 0x4a, 0x86, 0xd7, 0x87,
  31. 0x20, 0x33, 0x32, 0x12, 0xc9, 0x55, 0x1e,
  32. ],
  33. };
  34. let mut vec = vec![0; 0x40];
  35. vec.copy_from_slice(expected.array.as_slice());
  36. let result = from_vec(&vec);
  37. assert_eq!(expected, result?);
  38. Ok(())
  39. }
  40. #[test]
  41. fn test_big_array_serialization() -> Result<()> {
  42. let value = S {
  43. array: [
  44. 0x81, 0xc0, 0xf, 0x3e, 0x2f, 0x11, 0xae, 0xc9, 0x48, 0x68, 0xc4, 0x66, 0x67, 0x1e,
  45. 0xda, 0x4e, 0xde, 0xf, 0x84, 0x23, 0x4d, 0x24, 0x0, 0x23, 0xe8, 0x84, 0xac, 0xc6, 0x7c,
  46. 0xb0, 0x34, 0x5, 0xa3, 0xc3, 0x4b, 0x59, 0xaa, 0x96, 0x9f, 0xc6, 0x2d, 0x31, 0x5a,
  47. 0xb5, 0x61, 0xfd, 0xad, 0x9b, 0x7a, 0xf8, 0xf3, 0xd7, 0xee, 0x4a, 0x86, 0xd7, 0x87,
  48. 0x20, 0x33, 0x32, 0x12, 0xc9, 0x55, 0x1e,
  49. ],
  50. };
  51. let mut expected = vec![0; 0x40];
  52. expected.copy_from_slice(value.array.as_slice());
  53. let result = to_vec(&value);
  54. assert_eq!(expected, result?);
  55. Ok(())
  56. }