Skip to content

Commit

Permalink
scroll_derive: add macro_rules test for 1.47 rustc breakage
Browse files Browse the repository at this point in the history
  • Loading branch information
m4b committed Oct 9, 2020
1 parent ffbe1f3 commit 348006f
Showing 1 changed file with 35 additions and 22 deletions.
57 changes: 35 additions & 22 deletions scroll_derive/tests/tests.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,31 @@
use scroll_derive::{Pread, Pwrite, SizeWith, IOread, IOwrite};
use scroll::{Pread, Pwrite, Cread, Cwrite, LE};
use scroll::{Cread, Cwrite, Pread, Pwrite, LE};
use scroll_derive::{IOread, IOwrite, Pread, Pwrite, SizeWith};

use scroll::ctx::SizeWith;

macro_rules! test {
( struct $name:ident { $( $field:ident: $t:ty, )* } ) => {
#[derive(Pread)]
pub struct $name {
$( pub $field: $t, )*
}
};
}

test! {
struct Test {
field: [u16; 40],
}
}

#[derive(Debug, PartialEq, Pread, Pwrite)]
struct Data {
id: u32,
timestamp: f64,
id: u32,
timestamp: f64,
}


#[test]
fn test_data (){
fn test_data() {
let bytes = [0xefu8, 0xbe, 0xad, 0xde, 0, 0, 0, 0, 0, 0, 224, 63];
let data: Data = bytes.pread_with(0, LE).unwrap();
println!("data: {:?}", &data);
Expand All @@ -26,38 +40,37 @@ fn test_data (){

#[derive(Debug, PartialEq, Pread, Pwrite)]
struct Data2 {
name: [u8; 32],
name: [u8; 32],
}

#[test]
fn test_array (){
fn test_array() {
let bytes = [0u8; 64];
let data: Data2 = bytes.pread_with(0, LE).unwrap();
println!("data: {:?}", &data);
}

#[derive(Debug, PartialEq, Pread, Pwrite, SizeWith)]
struct Data3 {
name: u32,
name: u32,
}

#[test]
fn test_sizewith (){
fn test_sizewith() {
let bytes = [0u8; 64];
let data: Data3 = bytes.gread_with(&mut 0, LE).unwrap();
println!("data: {:?}", &data);
}


#[derive(Debug, PartialEq, IOread, IOwrite, SizeWith)]
struct Data4 {
name: u32,
j: u16,
arr: [u8; 2]
arr: [u8; 2],
}

#[test]
fn test_ioread (){
fn test_ioread() {
let bytes = [0, 1, 2, 3, 0xde, 0xed, 0xbe, 0xaf];
let data: Data4 = bytes.cread_with(0, LE);
println!("data: {:?}", &data);
Expand All @@ -67,7 +80,7 @@ fn test_ioread (){
}

#[test]
fn test_iowrite (){
fn test_iowrite() {
let bytes = [0, 1, 2, 3, 0xde, 0xed, 0xbe, 0xaf];
let data: Data4 = bytes.cread_with(0, LE);
println!("data: {:?}", &data);
Expand All @@ -94,11 +107,11 @@ struct Data5 {
name: u32,
j: u16,
arr1: [u8; 2],
arr2: [u16; 2]
arr2: [u16; 2],
}

#[test]
fn test_pread_arrays (){
fn test_pread_arrays() {
let bytes = [0, 1, 2, 3, 0, 0, 0xde, 0xed, 0xad, 0xde, 0xef, 0xbe];
let data: Data5 = bytes.pread_with(0, LE).unwrap();
println!("data: {:?}", &data);
Expand All @@ -114,7 +127,6 @@ fn test_pread_arrays (){
assert_eq!(*offset, ::std::mem::size_of::<Data5>());
}


#[derive(Debug, PartialEq, Pread, SizeWith)]
#[repr(C)]
struct Data6 {
Expand All @@ -123,7 +135,7 @@ struct Data6 {
}

#[test]
fn test_array_copy (){
fn test_array_copy() {
let bytes = [0xde, 0xed, 0xef, 0xbe, 0x68, 0x65, 0x6c, 0x6c, 0x0];
let data: Data6 = bytes.pread_with(0, LE).unwrap();
let name: &str = data.name.pread(0).unwrap();
Expand All @@ -146,7 +158,9 @@ struct Data7B {

#[test]
fn test_nested_struct() {
let b = Data7B { a: Data7A { y: 1, x: 2 } };
let b = Data7B {
a: Data7A { y: 1, x: 2 },
};
let size = Data7B::size_with(&LE);
assert_eq!(size, 12);
let mut bytes = vec![0; size];
Expand All @@ -158,11 +172,11 @@ fn test_nested_struct() {
assert_eq!(b, b2);
}

#[derive(Debug, PartialEq, Eq, Pread, Pwrite, IOread, IOwrite, SizeWith)]
#[derive(Debug, PartialEq, Eq, Pread, Pwrite, IOread, IOwrite, SizeWith)]
#[repr(C)]
struct Data8<T, Y> {
ids: [T; 3],
xyz: Y
xyz: Y,
}

#[test]
Expand All @@ -179,7 +193,6 @@ fn test_generics() {
assert_eq!(written, size);
}


#[derive(Debug, PartialEq, Eq, Pread, Pwrite, IOread, IOwrite, SizeWith)]
struct Data9(u8, u16);

Expand Down

0 comments on commit 348006f

Please sign in to comment.