dependency_tests.rs 2.1 KB

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