#![cfg(feature = "derive")]
use byteable::{Byteable, ToByteArray, RawRepr, TryFromByteArray, TryFromRawRepr};
#[derive(Byteable, Debug, Clone, Copy, PartialEq)]
#[repr(u8)]
enum Status {
Idle = 0,
Running = 1,
Completed = 2,
Failed = 3,
}
#[derive(Byteable, Debug, Clone, Copy, PartialEq)]
struct Message {
#[byteable(try_transparent)]
status: Status,
#[byteable(little_endian)]
message: u64,
}
#[test]
fn test_enum_try_has_raw_type() {
let status = Status::Running;
let raw = status.to_raw();
let restored: Status = Status::try_from_raw(raw).unwrap();
assert_eq!(restored, Status::Running);
}
#[test]
fn test_enum_raw_roundtrip() {
let variants = [
Status::Idle,
Status::Running,
Status::Completed,
Status::Failed,
];
for &status in &variants {
let raw = status.to_raw();
let restored: Status = Status::try_from_raw(raw).unwrap();
assert_eq!(restored, status);
}
}
#[test]
fn test_enum_invalid_discriminant_via_raw() {
let bytes = [255u8];
let result = Status::try_from_byte_array(bytes);
assert!(result.is_err());
}
#[test]
fn test_enum_byte_conversion() {
assert_eq!(Status::Idle.to_byte_array(), [0]);
assert_eq!(Status::Running.to_byte_array(), [1]);
assert_eq!(Status::Completed.to_byte_array(), [2]);
assert_eq!(Status::Failed.to_byte_array(), [3]);
assert_eq!(Status::try_from_byte_array([0]).unwrap(), Status::Idle);
assert_eq!(Status::try_from_byte_array([1]).unwrap(), Status::Running);
assert_eq!(Status::try_from_byte_array([2]).unwrap(), Status::Completed);
assert_eq!(Status::try_from_byte_array([3]).unwrap(), Status::Failed);
}
#[test]
fn test_message_with_valid_status() {
let msg = Message {
status: Status::Running,
message: 0x123456789ABCDEF0,
};
let bytes = msg.to_byte_array();
assert_eq!(bytes.len(), 9);
assert_eq!(bytes[0], 1);
let restored = Message::try_from_byte_array(bytes).unwrap();
assert_eq!(restored, msg);
}
#[test]
fn test_message_with_all_status_variants() {
let test_cases = [
(Status::Idle, 0u64),
(Status::Running, 42u64),
(Status::Completed, 0xDEADBEEF),
(Status::Failed, u64::MAX),
];
for (status, message_val) in test_cases {
let msg = Message {
status,
message: message_val,
};
let bytes = msg.to_byte_array();
let restored = Message::try_from_byte_array(bytes).unwrap();
assert_eq!(restored, msg);
}
}
#[test]
fn test_message_with_invalid_status_discriminant() {
let mut bytes = [0u8; 9];
bytes[0] = 255; bytes[1..9].copy_from_slice(&0x123456789ABCDEF0u64.to_le_bytes());
let result = Message::try_from_byte_array(bytes);
assert!(result.is_err());
}
#[test]
fn test_message_byte_layout() {
let msg = Message {
status: Status::Completed,
message: 0x0102030405060708,
};
let bytes = msg.to_byte_array();
assert_eq!(bytes[0], 2);
assert_eq!(bytes[1], 0x08);
assert_eq!(bytes[2], 0x07);
assert_eq!(bytes[3], 0x06);
assert_eq!(bytes[4], 0x05);
assert_eq!(bytes[5], 0x04);
assert_eq!(bytes[6], 0x03);
assert_eq!(bytes[7], 0x02);
assert_eq!(bytes[8], 0x01);
}
#[test]
fn test_message_roundtrip_all_variants() {
let messages = [
Message {
status: Status::Idle,
message: 0,
},
Message {
status: Status::Running,
message: 1,
},
Message {
status: Status::Completed,
message: 0xFFFFFFFFFFFFFFFF,
},
Message {
status: Status::Failed,
message: 0x8000000000000000,
},
];
for msg in &messages {
let bytes = msg.to_byte_array();
let restored = Message::try_from_byte_array(bytes).unwrap();
assert_eq!(&restored, msg);
}
}
#[test]
fn test_message_invalid_discriminants() {
let invalid_discriminants = [4u8, 5, 10, 100, 200, 254, 255];
for invalid in invalid_discriminants {
let mut bytes = [0u8; 9];
bytes[0] = invalid;
let result = Message::try_from_byte_array(bytes);
assert!(
result.is_err(),
"Expected error for discriminant {}",
invalid
);
}
}
mod two_try_transparent_tests {
use byteable::{Byteable, ToByteArray, TryFromByteArray};
#[derive(Byteable, Debug, Clone, Copy, PartialEq)]
#[repr(u8)]
enum StatusA {
Ok = 0,
Err = 1,
}
#[derive(Byteable, Debug, Clone, Copy, PartialEq)]
#[repr(u16)]
#[byteable(little_endian)]
enum CodeB {
Alpha = 0x0100,
Beta = 0x0200,
}
#[derive(Byteable, Debug, Clone, Copy, PartialEq)]
struct Dual {
#[byteable(try_transparent)]
status: StatusA,
#[byteable(try_transparent)]
code: CodeB,
value: u8,
}
#[test]
fn test_dual_roundtrip() {
let d = Dual {
status: StatusA::Ok,
code: CodeB::Beta,
value: 99,
};
let bytes = d.to_byte_array();
assert_eq!(bytes.len(), 4);
let restored = Dual::try_from_byte_array(bytes).unwrap();
assert_eq!(restored, d);
}
#[test]
fn test_dual_all_combinations() {
let combos = [
(StatusA::Ok, CodeB::Alpha, 0u8),
(StatusA::Ok, CodeB::Beta, 255u8),
(StatusA::Err, CodeB::Alpha, 128u8),
(StatusA::Err, CodeB::Beta, 1u8),
];
for (status, code, value) in combos {
let d = Dual {
status,
code,
value,
};
let bytes = d.to_byte_array();
let restored = Dual::try_from_byte_array(bytes).unwrap();
assert_eq!(restored, d);
}
}
#[test]
fn test_dual_first_field_invalid() {
let bytes = [255u8, 0x00, 0x01, 0x00]; let result = Dual::try_from_byte_array(bytes);
assert!(result.is_err());
}
#[test]
fn test_dual_second_field_invalid() {
let bytes = [0u8, 0xFF, 0xFF, 0x00];
let result = Dual::try_from_byte_array(bytes);
assert!(result.is_err());
}
#[test]
fn test_dual_byte_layout() {
let d = Dual {
status: StatusA::Err,
code: CodeB::Alpha,
value: 42,
};
let bytes = d.to_byte_array();
assert_eq!(bytes[0], 1); assert_eq!(bytes[1], 0x00); assert_eq!(bytes[2], 0x01); assert_eq!(bytes[3], 42); }
}
mod command_packet_tests {
use byteable::{Byteable, ToByteArray, TryFromByteArray};
#[derive(Byteable, Debug, Clone, Copy, PartialEq)]
#[repr(u16)]
#[byteable(little_endian)]
enum Command {
Start = 0x1000,
Stop = 0x2000,
Pause = 0x3000,
}
#[derive(Byteable, Debug, Clone, Copy, PartialEq)]
struct CommandPacket {
#[byteable(try_transparent)]
command: Command,
#[byteable(little_endian)]
payload: u32,
}
#[test]
fn test_command_packet_with_try_transparent() {
let packet = CommandPacket {
command: Command::Start,
payload: 0xABCD1234,
};
let bytes = packet.to_byte_array();
assert_eq!(bytes.len(), 6);
assert_eq!(bytes[0], 0x00); assert_eq!(bytes[1], 0x10);
let restored = CommandPacket::try_from_byte_array(bytes).unwrap();
assert_eq!(restored, packet);
}
#[test]
fn test_command_packet_with_invalid_command() {
let mut bytes = [0u8; 6];
bytes[0] = 0xFF;
bytes[1] = 0xFF;
bytes[2..6].copy_from_slice(&0xABCD1234u32.to_le_bytes());
let result = CommandPacket::try_from_byte_array(bytes);
assert!(result.is_err());
}
#[test]
fn test_command_packet_all_variants() {
let packets = [
CommandPacket {
command: Command::Start,
payload: 0,
},
CommandPacket {
command: Command::Stop,
payload: 0xFFFFFFFF,
},
CommandPacket {
command: Command::Pause,
payload: 0x12345678,
},
];
for packet in &packets {
let bytes = packet.to_byte_array();
let restored = CommandPacket::try_from_byte_array(bytes).unwrap();
assert_eq!(&restored, packet);
}
}
}