use std::io::Cursor;

use binrw::{BinRead, BinWrite};
use diff::Diff;
use ezcad::field_of::FieldOf;

#[test]
fn field_of_be() {
    #[derive(BinRead, BinWrite, Debug, PartialEq, Default, Diff)]
    #[diff(attr(
        #[derive(Debug, PartialEq)]
    ))]
    struct Test {
        a: u32,
        b: u32,
    }

    let decoded: FieldOf<Test> = FieldOf {
        value: Test {
            a: 0x0A0B0C0D,
            b: 0x01020304,
        },
    };
    let encoded: Vec<u8> = vec![
        0x00, 0x00, 0x00, 0x08, 0x0A, 0x0B, 0x0C, 0x0D, 0x01, 0x02, 0x03, 0x04,
    ];

    let mut stream: Cursor<Vec<u8>> = Cursor::new(encoded.clone());
    assert_eq!(FieldOf::<Test>::read_be(&mut stream).unwrap(), decoded);

    let mut buffer: Cursor<Vec<u8>> = Cursor::new(vec![]);
    decoded.write_be(&mut buffer).unwrap();
    assert_eq!(buffer.into_inner(), encoded);
}

#[test]
fn field_of_le() {
    #[derive(BinRead, BinWrite, Debug, PartialEq, Default, Diff)]
    #[diff(attr(
        #[derive(Debug, PartialEq)]
    ))]
    struct Test {
        a: u32,
        b: u32,
    }

    let decoded: FieldOf<Test> = FieldOf {
        value: Test {
            a: 0x0A0B0C0D,
            b: 0x01020304,
        },
    };
    let encoded: Vec<u8> = vec![
        0x08, 0x00, 0x00, 0x00, 0x0D, 0x0C, 0x0B, 0x0A, 0x04, 0x03, 0x02, 0x01,
    ];

    let mut stream: Cursor<Vec<u8>> = Cursor::new(encoded.clone());
    assert_eq!(FieldOf::<Test>::read_le(&mut stream).unwrap(), decoded);

    let mut buffer: Cursor<Vec<u8>> = Cursor::new(vec![]);
    decoded.write_le(&mut buffer).unwrap();
    assert_eq!(buffer.into_inner(), encoded);
}
