Skip to content

Commit

Permalink
Added test case for a borrowed str
Browse files Browse the repository at this point in the history
  • Loading branch information
VictorKoenders committed Nov 30, 2021
1 parent f4b4a5d commit e26bd3a
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions tests/derive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub struct Test2<T: Decode> {
c: u32,
}

#[derive(bincode::BorrowDecode, PartialEq, Debug, Eq)]
#[derive(bincode::BorrowDecode, bincode::Encode, PartialEq, Debug, Eq)]
pub struct Test3<'a> {
a: &'a str,
b: u32,
Expand Down Expand Up @@ -55,7 +55,6 @@ fn test_encode() {
assert_eq!(&slice[..bytes_written], &[10, 10, 20]);
}

#[cfg(feature = "std")]
#[test]
fn test_decode() {
let start = Test2 {
Expand All @@ -64,11 +63,24 @@ fn test_decode() {
c: 1024u32,
};
let slice = [5, 10, 251, 0, 4];
let result: Test2<u32> =
bincode::decode_from_std_read(&mut slice.as_ref(), Configuration::standard()).unwrap();
let result: Test2<u32> = bincode::decode_from_slice(&slice, Configuration::standard()).unwrap();
assert_eq!(result, start);
}

#[test]
fn test_encode_decode_str() {
let start = Test3 {
a: "Foo bar",
b: 10u32,
c: 1024u32,
};
let mut slice = [0u8; 100];

let len = bincode::encode_into_slice(&start, &mut slice, Configuration::standard()).unwrap();
let end: Test3 = bincode::decode_from_slice(&slice[..len], Configuration::standard()).unwrap();
assert_eq!(end, start);
}

#[test]
fn test_encode_tuple() {
let start = TestTupleStruct(5, 10, 1024);
Expand Down

0 comments on commit e26bd3a

Please sign in to comment.