diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 19:33:14 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 19:33:14 +0000 |
commit | 36d22d82aa202bb199967e9512281e9a53db42c9 (patch) | |
tree | 105e8c98ddea1c1e4784a60a5a6410fa416be2de /third_party/rust/serde_cbor/tests/bennofs.rs | |
parent | Initial commit. (diff) | |
download | firefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.tar.xz firefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.zip |
Adding upstream version 115.7.0esr.upstream/115.7.0esr
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'third_party/rust/serde_cbor/tests/bennofs.rs')
-rw-r--r-- | third_party/rust/serde_cbor/tests/bennofs.rs | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/third_party/rust/serde_cbor/tests/bennofs.rs b/third_party/rust/serde_cbor/tests/bennofs.rs new file mode 100644 index 0000000000..1b289f40d4 --- /dev/null +++ b/third_party/rust/serde_cbor/tests/bennofs.rs @@ -0,0 +1,60 @@ +#[macro_use] +extern crate serde_derive; + +use serde::Serialize; +use serde_cbor::ser::SliceWrite; +use serde_cbor::{self, Serializer}; + +#[derive(Debug, PartialEq, Serialize, Deserialize)] +struct Example { + foo: Foo, + payload: u8, +} + +#[derive(Debug, PartialEq, Serialize, Deserialize)] +struct Foo { + x: u8, + color: Color, +} + +#[derive(Debug, PartialEq, Serialize, Deserialize)] +enum Color { + Red, + Blue, + Yellow(u8), +} + +const EXAMPLE: Example = Example { + foo: Foo { + x: 0xAA, + color: Color::Yellow(40), + }, + payload: 0xCC, +}; + +#[cfg(feature = "std")] +mod std_tests { + use super::*; + + #[test] + fn test() { + let serialized = serde_cbor::ser::to_vec_packed(&EXAMPLE).expect("bennofs 1"); + println!("{:?}", serialized); + let deserialized: Example = serde_cbor::from_slice(&serialized).expect("bennofs 2"); + assert_eq!(EXAMPLE, deserialized); + } +} + +#[test] +fn test() { + let mut slice = [0u8; 64]; + let writer = SliceWrite::new(&mut slice); + let mut serializer = Serializer::new(writer).packed_format(); + EXAMPLE.serialize(&mut serializer).unwrap(); + let writer = serializer.into_inner(); + let end = writer.bytes_written(); + let slice = writer.into_inner(); + let deserialized: Example = + serde_cbor::de::from_slice_with_scratch(&slice[..end], &mut []).unwrap(); + assert_eq!(EXAMPLE, deserialized); +} |