use nexus_revo_io::NexusCmd; use nexus_revo_io::{SymReader, SymReaderFsm, SymReaderFsmPoll, SymWriter}; use std::io::ErrorKind; #[test] fn encode_decode() { let mut buf = Vec::with_capacity(88); let mut writer = SymWriter::new(&mut buf); writer .write_msg(0x6969, NexusCmd::VibrateMode) .expect("write_msg failed"); assert_eq!( buf, [ 0xe8, 0xe8, 0xe8, 0xe8, 0x81, 0x1d, 0xd1, 0xd1, 0x1d, 0x1d, 0xd1, 0xd1, 0x1d, 0x11, 0xdd, 0xd1, 0xdd, 0x2, 0x3b, 0xa3, 0xa2, 0x3a, 0x3b, 0xa3, 0xa2, 0x3a, 0x23, 0xbb, 0xa3, 0xba, 0x4, 0x77, 0x47, 0x44, 0x74, 0x77, 0x47, 0x44, 0x74, 0x47, 0x77, 0x47, 0x74, 0xf, 0xe0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, ] ); let mut reader = SymReader::new(buf.as_slice()); for _ in 0..3 { let msg = reader.read_msg().expect("read_msg failed"); assert_eq!(msg.0, 0x6969); assert_eq!(msg.1, NexusCmd::VibrateMode); } if let Err(e) = reader.read_msg() { assert_eq!(e.kind(), ErrorKind::UnexpectedEof); } else { unreachable!("no eof"); } } #[test] fn encode_decode_fsm() { let mut buf = Vec::with_capacity(88); let mut writer = SymWriter::new(&mut buf); writer .write_msg(0x6969, NexusCmd::VibrateMode) .expect("write_msg failed"); assert_eq!( buf, [ 0xe8, 0xe8, 0xe8, 0xe8, 0x81, 0x1d, 0xd1, 0xd1, 0x1d, 0x1d, 0xd1, 0xd1, 0x1d, 0x11, 0xdd, 0xd1, 0xdd, 0x2, 0x3b, 0xa3, 0xa2, 0x3a, 0x3b, 0xa3, 0xa2, 0x3a, 0x23, 0xbb, 0xa3, 0xba, 0x4, 0x77, 0x47, 0x44, 0x74, 0x77, 0x47, 0x44, 0x74, 0x47, 0x77, 0x47, 0x74, 0xf, 0xe0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, ] ); let mut reader = SymReaderFsm::new(buf.as_slice()); let mut msg_count = 0; const EXPECTED_I: [i32; 3] = [135, 238, 341]; for i in 0..705 { match reader.poll() { SymReaderFsmPoll::Msg(msg) => { assert_eq!(msg.0, 0x6969); assert_eq!(msg.1, NexusCmd::VibrateMode); assert!(msg_count <= 2); assert_eq!(EXPECTED_I[msg_count], i); msg_count += 1; } SymReaderFsmPoll::Err(e) => { assert_eq!(msg_count, 3); assert_eq!(e.kind(), ErrorKind::UnexpectedEof); return; } SymReaderFsmPoll::Pending => {} } } unreachable!("too many iterations"); }