use pokeys_lib::encoders::{EncoderData, EncoderOptions, MAX_ENCODERS, ULTRA_FAST_ENCODER_INDEX};
#[test]
fn test_encoder_4x_sampling_configuration() {
let options = EncoderOptions::with_4x_sampling();
assert!(options.enabled, "4x sampling encoder should be enabled");
assert!(options.sampling_4x, "4x sampling should be enabled");
assert!(!options.sampling_2x, "2x sampling should be disabled");
let byte = options.to_byte();
assert_eq!(
byte & 0b00000011,
0b00000011,
"Bits 0 and 1 should be set for enabled + 4x"
);
let options_from_byte = EncoderOptions::from_byte(byte);
assert_eq!(
options, options_from_byte,
"Round-trip conversion should preserve options"
);
}
#[test]
fn test_encoder_2x_sampling_configuration() {
let options = EncoderOptions::with_2x_sampling();
assert!(options.enabled, "2x sampling encoder should be enabled");
assert!(!options.sampling_4x, "4x sampling should be disabled");
assert!(options.sampling_2x, "2x sampling should be enabled");
let byte = options.to_byte();
assert_eq!(
byte & 0b00000111,
0b00000101,
"Bits 0 and 2 should be set for enabled + 2x"
);
let options_from_byte = EncoderOptions::from_byte(byte);
assert_eq!(
options, options_from_byte,
"Round-trip conversion should preserve options"
);
}
#[test]
fn test_encoder_sampling_mutual_exclusion() {
let mut options = EncoderOptions::new();
options.enabled = true;
options.sampling_4x = true;
options.sampling_2x = true;
let byte = options.to_byte();
assert_eq!(
byte & 0b00000111,
0b00000111,
"Both sampling bits should be set in byte"
);
let decoded_options = EncoderOptions::from_byte(byte);
assert!(
decoded_options.sampling_4x,
"4x sampling flag should be preserved"
);
assert!(
decoded_options.sampling_2x,
"2x sampling flag should be preserved"
);
}
#[test]
fn test_encoder_data_sampling_detection() {
let mut encoder = EncoderData::new();
let options_4x = EncoderOptions::with_4x_sampling();
encoder.set_options(options_4x);
assert!(encoder.is_4x_sampling(), "Should detect 4x sampling");
assert!(!encoder.is_2x_sampling(), "Should not detect 2x sampling");
assert_eq!(
encoder.sampling_mode_str(),
"4x (both edges)",
"Should return correct mode string"
);
let options_2x = EncoderOptions::with_2x_sampling();
encoder.set_options(options_2x);
assert!(!encoder.is_4x_sampling(), "Should not detect 4x sampling");
assert!(encoder.is_2x_sampling(), "Should detect 2x sampling");
assert_eq!(
encoder.sampling_mode_str(),
"2x (A edges only)",
"Should return correct mode string"
);
let options_disabled = EncoderOptions::new();
encoder.set_options(options_disabled);
assert!(
!encoder.is_4x_sampling(),
"Should not detect 4x sampling when disabled"
);
assert!(
!encoder.is_2x_sampling(),
"Should not detect 2x sampling when disabled"
);
assert_eq!(
encoder.sampling_mode_str(),
"1x (disabled)",
"Should return correct mode string"
);
}
#[test]
fn test_encoder_options_bit_layout() {
let mut options = EncoderOptions::new();
options.enabled = true;
assert_eq!(options.to_byte() & 0b00000001, 0b00000001, "Bit 0: enable");
options.sampling_4x = true;
assert_eq!(
options.to_byte() & 0b00000011,
0b00000011,
"Bit 1: 4x sampling"
);
options.sampling_2x = true;
assert_eq!(
options.to_byte() & 0b00000111,
0b00000111,
"Bit 2: 2x sampling"
);
assert_eq!(
options.to_byte() & 0b00001000,
0b00000000,
"Bit 3: reserved (should be 0)"
);
options.direct_key_mapping_a = true;
assert_eq!(
options.to_byte() & 0b00010000,
0b00010000,
"Bit 4: direct key mapping A"
);
options.macro_mapping_a = true;
assert_eq!(
options.to_byte() & 0b00100000,
0b00100000,
"Bit 5: macro mapping A"
);
options.direct_key_mapping_b = true;
assert_eq!(
options.to_byte() & 0b01000000,
0b01000000,
"Bit 6: direct key mapping B"
);
options.macro_mapping_b = true;
assert_eq!(
options.to_byte() & 0b10000000,
0b10000000,
"Bit 7: macro mapping B"
);
}
#[test]
fn test_encoder_constants() {
assert_eq!(MAX_ENCODERS, 25, "Should support 25 normal encoders");
assert_eq!(
ULTRA_FAST_ENCODER_INDEX, 25,
"Ultra-fast encoder should be at index 25"
);
}
#[test]
fn test_encoder_sampling_mode_combinations() {
let disabled = EncoderOptions::new();
assert!(!disabled.enabled && !disabled.sampling_4x && !disabled.sampling_2x);
let mut enabled_1x = EncoderOptions::new();
enabled_1x.enabled = true;
assert!(enabled_1x.enabled && !enabled_1x.sampling_4x && !enabled_1x.sampling_2x);
let sampling_4x = EncoderOptions::with_4x_sampling();
assert!(sampling_4x.enabled && sampling_4x.sampling_4x && !sampling_4x.sampling_2x);
let sampling_2x = EncoderOptions::with_2x_sampling();
assert!(sampling_2x.enabled && !sampling_2x.sampling_4x && sampling_2x.sampling_2x);
}
#[test]
fn test_encoder_key_mapping_combinations() {
let mut options = EncoderOptions::with_4x_sampling();
options.direct_key_mapping_a = true;
options.direct_key_mapping_b = true;
let byte = options.to_byte();
assert_eq!(
byte & 0b01010011,
0b01010011,
"Should have enabled, 4x, and both key mappings"
);
let decoded = EncoderOptions::from_byte(byte);
assert!(decoded.enabled, "Should preserve enabled state");
assert!(decoded.sampling_4x, "Should preserve 4x sampling");
assert!(
decoded.direct_key_mapping_a,
"Should preserve key mapping A"
);
assert!(
decoded.direct_key_mapping_b,
"Should preserve key mapping B"
);
}
#[test]
fn test_encoder_data_initialization() {
let encoder = EncoderData::new();
assert_eq!(
encoder.encoder_value, 0,
"Initial encoder value should be 0"
);
assert_eq!(encoder.encoder_options, 0, "Initial options should be 0");
assert_eq!(
encoder.channel_a_pin, 0,
"Initial channel A pin should be 0"
);
assert_eq!(
encoder.channel_b_pin, 0,
"Initial channel B pin should be 0"
);
assert!(
!encoder.is_enabled(),
"Encoder should be disabled by default"
);
assert!(
!encoder.is_4x_sampling(),
"4x sampling should be disabled by default"
);
assert!(
!encoder.is_2x_sampling(),
"2x sampling should be disabled by default"
);
}
#[test]
fn test_encoder_protocol_compliance() {
let test_cases = [
(0b00000001, true, false, false, false, false, false, false), (0b00000011, true, true, false, false, false, false, false), (0b00000101, true, false, true, false, false, false, false), (0b00010001, true, false, false, true, false, false, false), (0b00100001, true, false, false, false, true, false, false), (0b01000001, true, false, false, false, false, true, false), (0b10000001, true, false, false, false, false, false, true), ];
for (byte, enabled, sampling_4x, sampling_2x, key_a, macro_a, key_b, macro_b) in test_cases {
let options = EncoderOptions::from_byte(byte);
assert_eq!(
options.enabled, enabled,
"Enabled bit mismatch for byte {byte:08b}"
);
assert_eq!(
options.sampling_4x, sampling_4x,
"4x sampling bit mismatch for byte {byte:08b}"
);
assert_eq!(
options.sampling_2x, sampling_2x,
"2x sampling bit mismatch for byte {byte:08b}"
);
assert_eq!(
options.direct_key_mapping_a, key_a,
"Key A bit mismatch for byte {byte:08b}"
);
assert_eq!(
options.macro_mapping_a, macro_a,
"Macro A bit mismatch for byte {byte:08b}"
);
assert_eq!(
options.direct_key_mapping_b, key_b,
"Key B bit mismatch for byte {byte:08b}"
);
assert_eq!(
options.macro_mapping_b, macro_b,
"Macro B bit mismatch for byte {byte:08b}"
);
assert_eq!(
options.to_byte(),
byte,
"Round-trip conversion failed for byte {byte:08b}"
);
}
}