Skip to content
This repository was archived by the owner on Aug 15, 2021. It is now read-only.

Add roundtrip example test case. #169

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -730,11 +730,11 @@ where
0xf8 => Err(self.error(ErrorCode::UnassignedCode)),
0xf9 => {
let value = self.parse_f16()?;
visitor.visit_f32(value)
visitor.visit_f64(value as f64)
}
0xfa => {
let value = self.parse_f32()?;
visitor.visit_f32(value)
visitor.visit_f64(value as f64)
}
0xfb => {
let value = self.parse_f64()?;
Expand Down
31 changes: 31 additions & 0 deletions tests/roundtrip.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#[macro_use]
extern crate serde_derive;

#[cfg(feature = "std")]
mod roundtrip {

use serde_cbor;

#[derive(Serialize, Deserialize, Debug, PartialEq)]
struct MyStuff {
#[serde(flatten)]
data: MyStuffType,
}

#[derive(Serialize, Deserialize, Debug, PartialEq)]
enum MyStuffType {
Ver1 { x: f64, y: f32 },
Ver2,
}

#[test]
/// Test roundtrip operation on a serde data structure.
fn test_roundtrip() {
let stuff1 = MyStuff {
data: MyStuffType::Ver1 { x: 2.5, y: 2.5 },
};
let data_bytes = serde_cbor::to_vec(&stuff1).unwrap();
let stuff2 = serde_cbor::from_slice(&data_bytes).unwrap();
assert_eq!(stuff1, stuff2);
}
}