Skip to content

Commit

Permalink
Add a test which complains about unaligned field accesses
Browse files Browse the repository at this point in the history
  • Loading branch information
willglynn committed Nov 2, 2018
1 parent 29c9f68 commit 53748bc
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions scroll_derive/tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,3 +158,32 @@ fn test_nested_struct() {
assert_eq!(read, size);
assert_eq!(b, b2);
}

#[derive(Debug,Copy,Clone,Pwrite,Pread,IOwrite,IOread)]
#[repr(packed)]
struct PackedStruct {
a: u8,
b: u32,
}

#[test]
fn cwrite_packed_struct() {
use scroll::{Cwrite, Cread};
let mut bytes = [0u8; 5];
&bytes[..].cwrite(&PackedStruct{ a: 1, b: 2 }, 0);

let PackedStruct{ a, b } = bytes.cread(0);
assert_eq!(a, 1);
assert_eq!(b, 2);
}

#[test]
fn pwrite_packed_struct() {
use scroll::{Pwrite, Pread};
let mut bytes = [0u8; 5];
&bytes[..].pwrite(&PackedStruct{ a: 1, b: 2 }, 0).unwrap();

let PackedStruct{ a, b } = bytes.pread(0).unwrap();
assert_eq!(a, 1);
assert_eq!(b, 2);
}

0 comments on commit 53748bc

Please sign in to comment.